src/HOL/Analysis/Cartesian_Euclidean_Space.thy
author paulson <lp15@cam.ac.uk>
Sat Apr 28 21:37:45 2018 +0100 (16 months ago)
changeset 68054 ebd179b82e20
parent 68050 7eacc812ad1c
child 68062 ee88c0fccbae
permissions -rw-r--r--
getting rid of more "defer", etc.
     1 (* Title:      HOL/Analysis/Cartesian_Euclidean_Space.thy
     2    Some material by Jose Divasón, Tim Makarios and L C Paulson
     3 *)
     4 
     5 section \<open>Instantiates the finite Cartesian product of Euclidean spaces as a Euclidean space\<close>
     6 
     7 theory Cartesian_Euclidean_Space
     8 imports Finite_Cartesian_Product Derivative
     9 begin
    10 
    11 lemma norm_le_componentwise:
    12    "(\<And>b. b \<in> Basis \<Longrightarrow> abs(x \<bullet> b) \<le> abs(y \<bullet> b)) \<Longrightarrow> norm x \<le> norm y"
    13   by (auto simp: norm_le euclidean_inner [of x x] euclidean_inner [of y y] abs_le_square_iff power2_eq_square intro!: sum_mono)
    14 
    15 lemma norm_le_componentwise_cart:
    16   fixes x :: "real^'n"
    17   shows "(\<And>i. abs(x$i) \<le> abs(y$i)) \<Longrightarrow> norm x \<le> norm y"
    18   unfolding cart_eq_inner_axis
    19   by (rule norm_le_componentwise) (metis axis_index)
    20   
    21 lemma subspace_special_hyperplane: "subspace {x. x $ k = 0}"
    22   by (simp add: subspace_def)
    23 
    24 lemma sum_mult_product:
    25   "sum h {..<A * B :: nat} = (\<Sum>i\<in>{..<A}. \<Sum>j\<in>{..<B}. h (j + i * B))"
    26   unfolding sum_nat_group[of h B A, unfolded atLeast0LessThan, symmetric]
    27 proof (rule sum.cong, simp, rule sum.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 subsection\<open>Basic componentwise operations on vectors\<close>
    39 
    40 instantiation vec :: (times, finite) times
    41 begin
    42 
    43 definition "( * ) \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
    44 instance ..
    45 
    46 end
    47 
    48 instantiation vec :: (one, finite) one
    49 begin
    50 
    51 definition "1 \<equiv> (\<chi> i. 1)"
    52 instance ..
    53 
    54 end
    55 
    56 instantiation vec :: (ord, finite) ord
    57 begin
    58 
    59 definition "x \<le> y \<longleftrightarrow> (\<forall>i. x$i \<le> y$i)"
    60 definition "x < (y::'a^'b) \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
    61 instance ..
    62 
    63 end
    64 
    65 text\<open>The ordering on one-dimensional vectors is linear.\<close>
    66 
    67 class cart_one =
    68   assumes UNIV_one: "card (UNIV :: 'a set) = Suc 0"
    69 begin
    70 
    71 subclass finite
    72 proof
    73   from UNIV_one show "finite (UNIV :: 'a set)"
    74     by (auto intro!: card_ge_0_finite)
    75 qed
    76 
    77 end
    78 
    79 instance vec:: (order, finite) order
    80   by standard (auto simp: less_eq_vec_def less_vec_def vec_eq_iff
    81       intro: order.trans order.antisym order.strict_implies_order)
    82 
    83 instance vec :: (linorder, cart_one) linorder
    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   fix x y :: "'a^'b::cart_one"
    93   note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps
    94   show "x \<le> y \<or> y \<le> x" by auto
    95 qed
    96 
    97 text\<open>Constant Vectors\<close>
    98 
    99 definition "vec x = (\<chi> i. x)"
   100 
   101 lemma interval_cbox_cart: "{a::real^'n..b} = cbox a b"
   102   by (auto simp add: less_eq_vec_def mem_box Basis_vec_def inner_axis)
   103 
   104 text\<open>Also the scalar-vector multiplication.\<close>
   105 
   106 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
   107   where "c *s x = (\<chi> i. c * (x$i))"
   108 
   109 
   110 subsection \<open>A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space\<close>
   111 
   112 lemma sum_cong_aux:
   113   "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> sum f A = sum g A"
   114   by (auto intro: sum.cong)
   115 
   116 hide_fact (open) sum_cong_aux
   117 
   118 method_setup vector = \<open>
   119 let
   120   val ss1 =
   121     simpset_of (put_simpset HOL_basic_ss @{context}
   122       addsimps [@{thm sum.distrib} RS sym,
   123       @{thm sum_subtractf} RS sym, @{thm sum_distrib_left},
   124       @{thm sum_distrib_right}, @{thm sum_negf} RS sym])
   125   val ss2 =
   126     simpset_of (@{context} addsimps
   127              [@{thm plus_vec_def}, @{thm times_vec_def},
   128               @{thm minus_vec_def}, @{thm uminus_vec_def},
   129               @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def},
   130               @{thm scaleR_vec_def},
   131               @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}])
   132   fun vector_arith_tac ctxt ths =
   133     simp_tac (put_simpset ss1 ctxt)
   134     THEN' (fn i => resolve_tac ctxt @{thms Cartesian_Euclidean_Space.sum_cong_aux} i
   135          ORELSE resolve_tac ctxt @{thms sum.neutral} i
   136          ORELSE simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm vec_eq_iff}]) i)
   137     (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
   138     THEN' asm_full_simp_tac (put_simpset ss2 ctxt addsimps ths)
   139 in
   140   Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (vector_arith_tac ctxt ths))
   141 end
   142 \<close> "lift trivial vector statements to real arith statements"
   143 
   144 lemma vec_0[simp]: "vec 0 = 0" by vector
   145 lemma vec_1[simp]: "vec 1 = 1" by vector
   146 
   147 lemma vec_inj[simp]: "vec x = vec y \<longleftrightarrow> x = y" by vector
   148 
   149 lemma vec_in_image_vec: "vec x \<in> (vec ` S) \<longleftrightarrow> x \<in> S" by auto
   150 
   151 lemma vec_add: "vec(x + y) = vec x + vec y"  by vector
   152 lemma vec_sub: "vec(x - y) = vec x - vec y" by vector
   153 lemma vec_cmul: "vec(c * x) = c *s vec x " by vector
   154 lemma vec_neg: "vec(- x) = - vec x " by vector
   155 
   156 lemma vec_scaleR: "vec(c * x) = c *\<^sub>R vec x"
   157   by vector
   158 
   159 lemma vec_sum:
   160   assumes "finite S"
   161   shows "vec(sum f S) = sum (vec \<circ> f) S"
   162   using assms
   163 proof induct
   164   case empty
   165   then show ?case by simp
   166 next
   167   case insert
   168   then show ?case by (auto simp add: vec_add)
   169 qed
   170 
   171 text\<open>Obvious "component-pushing".\<close>
   172 
   173 lemma vec_component [simp]: "vec x $ i = x"
   174   by vector
   175 
   176 lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i"
   177   by vector
   178 
   179 lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)"
   180   by vector
   181 
   182 lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   183 
   184 lemmas vector_component =
   185   vec_component vector_add_component vector_mult_component
   186   vector_smult_component vector_minus_component vector_uminus_component
   187   vector_scaleR_component cond_component
   188 
   189 
   190 subsection \<open>Some frequently useful arithmetic lemmas over vectors\<close>
   191 
   192 instance vec :: (semigroup_mult, finite) semigroup_mult
   193   by standard (vector mult.assoc)
   194 
   195 instance vec :: (monoid_mult, finite) monoid_mult
   196   by standard vector+
   197 
   198 instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
   199   by standard (vector mult.commute)
   200 
   201 instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
   202   by standard vector
   203 
   204 instance vec :: (semiring, finite) semiring
   205   by standard (vector field_simps)+
   206 
   207 instance vec :: (semiring_0, finite) semiring_0
   208   by standard (vector field_simps)+
   209 instance vec :: (semiring_1, finite) semiring_1
   210   by standard vector
   211 instance vec :: (comm_semiring, finite) comm_semiring
   212   by standard (vector field_simps)+
   213 
   214 instance vec :: (comm_semiring_0, finite) comm_semiring_0 ..
   215 instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   216 instance vec :: (semiring_0_cancel, finite) semiring_0_cancel ..
   217 instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel ..
   218 instance vec :: (ring, finite) ring ..
   219 instance vec :: (semiring_1_cancel, finite) semiring_1_cancel ..
   220 instance vec :: (comm_semiring_1, finite) comm_semiring_1 ..
   221 
   222 instance vec :: (ring_1, finite) ring_1 ..
   223 
   224 instance vec :: (real_algebra, finite) real_algebra
   225   by standard (simp_all add: vec_eq_iff)
   226 
   227 instance vec :: (real_algebra_1, finite) real_algebra_1 ..
   228 
   229 lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   230 proof (induct n)
   231   case 0
   232   then show ?case by vector
   233 next
   234   case Suc
   235   then show ?case by vector
   236 qed
   237 
   238 lemma one_index [simp]: "(1 :: 'a :: one ^ 'n) $ i = 1"
   239   by vector
   240 
   241 lemma neg_one_index [simp]: "(- 1 :: 'a :: {one, uminus} ^ 'n) $ i = - 1"
   242   by vector
   243 
   244 instance vec :: (semiring_char_0, finite) semiring_char_0
   245 proof
   246   fix m n :: nat
   247   show "inj (of_nat :: nat \<Rightarrow> 'a ^ 'b)"
   248     by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
   249 qed
   250 
   251 instance vec :: (numeral, finite) numeral ..
   252 instance vec :: (semiring_numeral, finite) semiring_numeral ..
   253 
   254 lemma numeral_index [simp]: "numeral w $ i = numeral w"
   255   by (induct w) (simp_all only: numeral.simps vector_add_component one_index)
   256 
   257 lemma neg_numeral_index [simp]: "- numeral w $ i = - numeral w"
   258   by (simp only: vector_uminus_component numeral_index)
   259 
   260 instance vec :: (comm_ring_1, finite) comm_ring_1 ..
   261 instance vec :: (ring_char_0, finite) ring_char_0 ..
   262 
   263 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
   264   by (vector mult.assoc)
   265 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
   266   by (vector field_simps)
   267 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
   268   by (vector field_simps)
   269 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
   270 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
   271 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
   272   by (vector field_simps)
   273 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   274 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   275 lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
   276 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   277 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
   278   by (vector field_simps)
   279 
   280 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
   281   by (simp add: vec_eq_iff)
   282 
   283 lemma linear_vec [simp]: "linear vec"
   284   by (simp add: linearI vec_add vec_eq_iff)
   285 
   286 lemma differentiable_vec:
   287   fixes S :: "'a::euclidean_space set"
   288   shows "vec differentiable_on S"
   289   by (simp add: linear_linear bounded_linear_imp_differentiable_on)
   290 
   291 lemma continuous_vec [continuous_intros]:
   292   fixes x :: "'a::euclidean_space"
   293   shows "isCont vec x"
   294   apply (clarsimp simp add: continuous_def LIM_def dist_vec_def L2_set_def)
   295   apply (rule_tac x="r / sqrt (real CARD('b))" in exI)
   296   by (simp add: mult.commute pos_less_divide_eq real_sqrt_mult)
   297 
   298 lemma box_vec_eq_empty [simp]:
   299   shows "cbox (vec a) (vec b) = {} \<longleftrightarrow> cbox a b = {}"
   300         "box (vec a) (vec b) = {} \<longleftrightarrow> box a b = {}"
   301   by (auto simp: Basis_vec_def mem_box box_eq_empty inner_axis)
   302 
   303 lemma norm_axis_1 [simp]: "norm (axis m (1::real)) = 1"
   304   by (simp add: inner_axis' norm_eq_1)
   305 
   306 lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   307   by vector
   308 
   309 lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
   310   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
   311 
   312 lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
   313   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
   314 
   315 lemma component_le_norm_cart: "\<bar>x$i\<bar> \<le> norm x"
   316   apply (simp add: norm_vec_def)
   317   apply (rule member_le_L2_set, simp_all)
   318   done
   319 
   320 lemma norm_bound_component_le_cart: "norm x \<le> e ==> \<bar>x$i\<bar> \<le> e"
   321   by (metis component_le_norm_cart order_trans)
   322 
   323 lemma norm_bound_component_lt_cart: "norm x < e ==> \<bar>x$i\<bar> < e"
   324   by (metis component_le_norm_cart le_less_trans)
   325 
   326 lemma norm_le_l1_cart: "norm x \<le> sum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
   327   by (simp add: norm_vec_def L2_set_le_sum)
   328 
   329 lemma scalar_mult_eq_scaleR [simp]: "c *s x = c *\<^sub>R x"
   330   unfolding scaleR_vec_def vector_scalar_mult_def by simp
   331 
   332 lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
   333   unfolding dist_norm scalar_mult_eq_scaleR
   334   unfolding scaleR_right_diff_distrib[symmetric] by simp
   335 
   336 lemma sum_component [simp]:
   337   fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
   338   shows "(sum f S)$i = sum (\<lambda>x. (f x)$i) S"
   339 proof (cases "finite S")
   340   case True
   341   then show ?thesis by induct simp_all
   342 next
   343   case False
   344   then show ?thesis by simp
   345 qed
   346 
   347 lemma sum_eq: "sum f S = (\<chi> i. sum (\<lambda>x. (f x)$i ) S)"
   348   by (simp add: vec_eq_iff)
   349 
   350 lemma sum_cmul:
   351   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
   352   shows "sum (\<lambda>x. c *s f x) S = c *s sum f S"
   353   by (simp add: vec_eq_iff sum_distrib_left)
   354 
   355 lemma sum_norm_allsubsets_bound_cart:
   356   fixes f:: "'a \<Rightarrow> real ^'n"
   357   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (sum f Q) \<le> e"
   358   shows "sum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
   359   using sum_norm_allsubsets_bound[OF assms]
   360   by simp
   361 
   362 subsection\<open>Closures and interiors of halfspaces\<close>
   363 
   364 lemma interior_halfspace_le [simp]:
   365   assumes "a \<noteq> 0"
   366     shows "interior {x. a \<bullet> x \<le> b} = {x. a \<bullet> x < b}"
   367 proof -
   368   have *: "a \<bullet> x < b" if x: "x \<in> S" and S: "S \<subseteq> {x. a \<bullet> x \<le> b}" and "open S" for S x
   369   proof -
   370     obtain e where "e>0" and e: "cball x e \<subseteq> S"
   371       using \<open>open S\<close> open_contains_cball x by blast
   372     then have "x + (e / norm a) *\<^sub>R a \<in> cball x e"
   373       by (simp add: dist_norm)
   374     then have "x + (e / norm a) *\<^sub>R a \<in> S"
   375       using e by blast
   376     then have "x + (e / norm a) *\<^sub>R a \<in> {x. a \<bullet> x \<le> b}"
   377       using S by blast
   378     moreover have "e * (a \<bullet> a) / norm a > 0"
   379       by (simp add: \<open>0 < e\<close> assms)
   380     ultimately show ?thesis
   381       by (simp add: algebra_simps)
   382   qed
   383   show ?thesis
   384     by (rule interior_unique) (auto simp: open_halfspace_lt *)
   385 qed
   386 
   387 lemma interior_halfspace_ge [simp]:
   388    "a \<noteq> 0 \<Longrightarrow> interior {x. a \<bullet> x \<ge> b} = {x. a \<bullet> x > b}"
   389 using interior_halfspace_le [of "-a" "-b"] by simp
   390 
   391 lemma interior_halfspace_component_le [simp]:
   392      "interior {x. x$k \<le> a} = {x :: (real^'n). x$k < a}" (is "?LE")
   393   and interior_halfspace_component_ge [simp]:
   394      "interior {x. x$k \<ge> a} = {x :: (real^'n). x$k > a}" (is "?GE")
   395 proof -
   396   have "axis k (1::real) \<noteq> 0"
   397     by (simp add: axis_def vec_eq_iff)
   398   moreover have "axis k (1::real) \<bullet> x = x$k" for x
   399     by (simp add: cart_eq_inner_axis inner_commute)
   400   ultimately show ?LE ?GE
   401     using interior_halfspace_le [of "axis k (1::real)" a]
   402           interior_halfspace_ge [of "axis k (1::real)" a] by auto
   403 qed
   404 
   405 lemma closure_halfspace_lt [simp]:
   406   assumes "a \<noteq> 0"
   407     shows "closure {x. a \<bullet> x < b} = {x. a \<bullet> x \<le> b}"
   408 proof -
   409   have [simp]: "-{x. a \<bullet> x < b} = {x. a \<bullet> x \<ge> b}"
   410     by (force simp:)
   411   then show ?thesis
   412     using interior_halfspace_ge [of a b] assms
   413     by (force simp: closure_interior)
   414 qed
   415 
   416 lemma closure_halfspace_gt [simp]:
   417    "a \<noteq> 0 \<Longrightarrow> closure {x. a \<bullet> x > b} = {x. a \<bullet> x \<ge> b}"
   418 using closure_halfspace_lt [of "-a" "-b"] by simp
   419 
   420 lemma closure_halfspace_component_lt [simp]:
   421      "closure {x. x$k < a} = {x :: (real^'n). x$k \<le> a}" (is "?LE")
   422   and closure_halfspace_component_gt [simp]:
   423      "closure {x. x$k > a} = {x :: (real^'n). x$k \<ge> a}" (is "?GE")
   424 proof -
   425   have "axis k (1::real) \<noteq> 0"
   426     by (simp add: axis_def vec_eq_iff)
   427   moreover have "axis k (1::real) \<bullet> x = x$k" for x
   428     by (simp add: cart_eq_inner_axis inner_commute)
   429   ultimately show ?LE ?GE
   430     using closure_halfspace_lt [of "axis k (1::real)" a]
   431           closure_halfspace_gt [of "axis k (1::real)" a] by auto
   432 qed
   433 
   434 lemma interior_hyperplane [simp]:
   435   assumes "a \<noteq> 0"
   436     shows "interior {x. a \<bullet> x = b} = {}"
   437 proof -
   438   have [simp]: "{x. a \<bullet> x = b} = {x. a \<bullet> x \<le> b} \<inter> {x. a \<bullet> x \<ge> b}"
   439     by (force simp:)
   440   then show ?thesis
   441     by (auto simp: assms)
   442 qed
   443 
   444 lemma frontier_halfspace_le:
   445   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   446     shows "frontier {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
   447 proof (cases "a = 0")
   448   case True with assms show ?thesis by simp
   449 next
   450   case False then show ?thesis
   451     by (force simp: frontier_def closed_halfspace_le)
   452 qed
   453 
   454 lemma frontier_halfspace_ge:
   455   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   456     shows "frontier {x. a \<bullet> x \<ge> b} = {x. a \<bullet> x = b}"
   457 proof (cases "a = 0")
   458   case True with assms show ?thesis by simp
   459 next
   460   case False then show ?thesis
   461     by (force simp: frontier_def closed_halfspace_ge)
   462 qed
   463 
   464 lemma frontier_halfspace_lt:
   465   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   466     shows "frontier {x. a \<bullet> x < b} = {x. a \<bullet> x = b}"
   467 proof (cases "a = 0")
   468   case True with assms show ?thesis by simp
   469 next
   470   case False then show ?thesis
   471     by (force simp: frontier_def interior_open open_halfspace_lt)
   472 qed
   473 
   474 lemma frontier_halfspace_gt:
   475   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   476     shows "frontier {x. a \<bullet> x > b} = {x. a \<bullet> x = b}"
   477 proof (cases "a = 0")
   478   case True with assms show ?thesis by simp
   479 next
   480   case False then show ?thesis
   481     by (force simp: frontier_def interior_open open_halfspace_gt)
   482 qed
   483 
   484 lemma interior_standard_hyperplane:
   485    "interior {x :: (real^'n). x$k = a} = {}"
   486 proof -
   487   have "axis k (1::real) \<noteq> 0"
   488     by (simp add: axis_def vec_eq_iff)
   489   moreover have "axis k (1::real) \<bullet> x = x$k" for x
   490     by (simp add: cart_eq_inner_axis inner_commute)
   491   ultimately show ?thesis
   492     using interior_hyperplane [of "axis k (1::real)" a]
   493     by force
   494 qed
   495 
   496 subsection \<open>Matrix operations\<close>
   497 
   498 text\<open>Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"}\<close>
   499 
   500 definition map_matrix::"('a \<Rightarrow> 'b) \<Rightarrow> (('a, 'i::finite)vec, 'j::finite) vec \<Rightarrow> (('b, 'i)vec, 'j) vec" where
   501   "map_matrix f x = (\<chi> i j. f (x $ i $ j))"
   502 
   503 lemma nth_map_matrix[simp]: "map_matrix f x $ i $ j = f (x $ i $ j)"
   504   by (simp add: map_matrix_def)
   505 
   506 definition matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"
   507     (infixl "**" 70)
   508   where "m ** m' == (\<chi> i j. sum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
   509 
   510 definition matrix_vector_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"
   511     (infixl "*v" 70)
   512   where "m *v x \<equiv> (\<chi> i. sum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
   513 
   514 definition vector_matrix_mult :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "
   515     (infixl "v*" 70)
   516   where "v v* m == (\<chi> j. sum (\<lambda>i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n"
   517 
   518 definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
   519 definition transpose where
   520   "(transpose::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
   521 definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
   522 definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
   523 definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
   524 definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
   525    
   526 lemma times0_left [simp]: "(0::'a::semiring_1^'n^'m) ** (A::'a ^'p^'n) = 0" 
   527   by (simp add: matrix_matrix_mult_def zero_vec_def)
   528 
   529 lemma times0_right [simp]: "(A::'a::semiring_1^'n^'m) ** (0::'a ^'p^'n) = 0" 
   530   by (simp add: matrix_matrix_mult_def zero_vec_def)
   531 
   532 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
   533 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)"
   534   by (vector matrix_matrix_mult_def sum.distrib[symmetric] field_simps)
   535 
   536 lemma matrix_mul_lid [simp]:
   537   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   538   shows "mat 1 ** A = A"
   539   apply (simp add: matrix_matrix_mult_def mat_def)
   540   apply vector
   541   apply (auto simp only: if_distrib cond_application_beta sum.delta'[OF finite]
   542     mult_1_left mult_zero_left if_True UNIV_I)
   543   done
   544 
   545 lemma matrix_mul_rid [simp]:
   546   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   547   shows "A ** mat 1 = A"
   548   apply (simp add: matrix_matrix_mult_def mat_def)
   549   apply vector
   550   apply (auto simp only: if_distrib cond_application_beta sum.delta[OF finite]
   551     mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
   552   done
   553 
   554 lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
   555   apply (vector matrix_matrix_mult_def sum_distrib_left sum_distrib_right mult.assoc)
   556   apply (subst sum.swap)
   557   apply simp
   558   done
   559 
   560 lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
   561   apply (vector matrix_matrix_mult_def matrix_vector_mult_def
   562     sum_distrib_left sum_distrib_right mult.assoc)
   563   apply (subst sum.swap)
   564   apply simp
   565   done
   566 
   567 lemma scalar_matrix_assoc:
   568   fixes A :: "('a::real_algebra_1)^'m^'n"
   569   shows "k *\<^sub>R (A ** B) = (k *\<^sub>R A) ** B"
   570   by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff scaleR_sum_right)
   571 
   572 lemma matrix_scalar_ac:
   573   fixes A :: "('a::real_algebra_1)^'m^'n"
   574   shows "A ** (k *\<^sub>R B) = k *\<^sub>R A ** B"
   575   by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff)
   576 
   577 lemma matrix_vector_mul_lid [simp]: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)"
   578   apply (vector matrix_vector_mult_def mat_def)
   579   apply (simp add: if_distrib cond_application_beta sum.delta' cong del: if_weak_cong)
   580   done
   581 
   582 lemma matrix_transpose_mul:
   583     "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)"
   584   by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult.commute)
   585 
   586 lemma matrix_eq:
   587   fixes A B :: "'a::semiring_1 ^ 'n ^ 'm"
   588   shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
   589   apply auto
   590   apply (subst vec_eq_iff)
   591   apply clarify
   592   apply (clarsimp simp add: matrix_vector_mult_def if_distrib cond_application_beta vec_eq_iff cong del: if_weak_cong)
   593   apply (erule_tac x="axis ia 1" in allE)
   594   apply (erule_tac x="i" in allE)
   595   apply (auto simp add: if_distrib cond_application_beta axis_def
   596     sum.delta[OF finite] cong del: if_weak_cong)
   597   done
   598 
   599 lemma matrix_vector_mul_component: "(A *v x)$k = (A$k) \<bullet> x"
   600   by (simp add: matrix_vector_mult_def inner_vec_def)
   601 
   602 lemma dot_lmul_matrix: "((x::real ^_) v* A) \<bullet> y = x \<bullet> (A *v y)"
   603   apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def sum_distrib_right sum_distrib_left ac_simps)
   604   apply (subst sum.swap)
   605   apply simp
   606   done
   607 
   608 lemma transpose_mat [simp]: "transpose (mat n) = mat n"
   609   by (vector transpose_def mat_def)
   610 
   611 lemma transpose_transpose [simp]: "transpose(transpose A) = A"
   612   by (vector transpose_def)
   613 
   614 lemma row_transpose [simp]:
   615   fixes A:: "'a::semiring_1^_^_"
   616   shows "row i (transpose A) = column i A"
   617   by (simp add: row_def column_def transpose_def vec_eq_iff)
   618 
   619 lemma column_transpose [simp]:
   620   fixes A:: "'a::semiring_1^_^_"
   621   shows "column i (transpose A) = row i A"
   622   by (simp add: row_def column_def transpose_def vec_eq_iff)
   623 
   624 lemma rows_transpose [simp]: "rows(transpose (A::'a::semiring_1^_^_)) = columns A"
   625   by (auto simp add: rows_def columns_def row_transpose intro: set_eqI)
   626 
   627 lemma columns_transpose [simp]: "columns(transpose (A::'a::semiring_1^_^_)) = rows A"
   628   by (metis transpose_transpose rows_transpose)
   629 
   630 lemma transpose_scalar: "transpose (k *\<^sub>R A) = k *\<^sub>R transpose A"
   631   unfolding transpose_def
   632   by (simp add: vec_eq_iff)
   633 
   634 lemma transpose_iff [iff]: "transpose A = transpose B \<longleftrightarrow> A = B"
   635   by (metis transpose_transpose)
   636 
   637 lemma matrix_mult_transpose_dot_column:
   638   shows "transpose A ** A = (\<chi> i j. (column i A) \<bullet> (column j A))"
   639   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def column_def inner_vec_def)
   640 
   641 lemma matrix_mult_transpose_dot_row:
   642   shows "A ** transpose A = (\<chi> i j. (row i A) \<bullet> (row j A))"
   643   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def row_def inner_vec_def)
   644 
   645 text\<open>Two sometimes fruitful ways of looking at matrix-vector multiplication.\<close>
   646 
   647 lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
   648   by (simp add: matrix_vector_mult_def inner_vec_def)
   649 
   650 lemma matrix_mult_sum:
   651   "(A::'a::comm_semiring_1^'n^'m) *v x = sum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
   652   by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult.commute)
   653 
   654 lemma vector_componentwise:
   655   "(x::'a::ring_1^'n) = (\<chi> j. \<Sum>i\<in>UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)"
   656   by (simp add: axis_def if_distrib sum.If_cases vec_eq_iff)
   657 
   658 lemma basis_expansion: "sum (\<lambda>i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)"
   659   by (auto simp add: axis_def vec_eq_iff if_distrib sum.If_cases cong del: if_weak_cong)
   660 
   661 lemma linear_componentwise_expansion:
   662   fixes f:: "real ^'m \<Rightarrow> real ^ _"
   663   assumes lf: "linear f"
   664   shows "(f x)$j = sum (\<lambda>i. (x$i) * (f (axis i 1)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
   665 proof -
   666   let ?M = "(UNIV :: 'm set)"
   667   let ?N = "(UNIV :: 'n set)"
   668   have "?rhs = (sum (\<lambda>i.(x$i) *\<^sub>R f (axis i 1) ) ?M)$j"
   669     unfolding sum_component by simp
   670   then show ?thesis
   671     unfolding linear_sum_mul[OF lf, symmetric]
   672     unfolding scalar_mult_eq_scaleR[symmetric]
   673     unfolding basis_expansion
   674     by simp
   675 qed
   676 
   677 subsection\<open>Inverse matrices  (not necessarily square)\<close>
   678 
   679 definition
   680   "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   681 
   682 definition
   683   "matrix_inv(A:: 'a::semiring_1^'n^'m) =
   684     (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   685 
   686 text\<open>Correspondence between matrices and linear operators.\<close>
   687 
   688 definition matrix :: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
   689   where "matrix f = (\<chi> i j. (f(axis j 1))$i)"
   690 
   691 lemma matrix_id_mat_1: "matrix id = mat 1"
   692   by (simp add: mat_def matrix_def axis_def)
   693 
   694 lemma matrix_scaleR: "(matrix (( *\<^sub>R) r)) = mat r"
   695   by (simp add: mat_def matrix_def axis_def if_distrib cong: if_cong)
   696 
   697 lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::('a::real_algebra_1) ^ _))"
   698   by (simp add: linear_iff matrix_vector_mult_def vec_eq_iff field_simps sum.distrib scaleR_right.sum)
   699 
   700 lemma
   701   fixes A :: "'a::{euclidean_space,real_algebra_1}^'n^'m"
   702   shows matrix_vector_mult_linear_continuous_at [continuous_intros]: "isCont (( *v) A) z"
   703     and matrix_vector_mult_linear_continuous_on [continuous_intros]: "continuous_on S (( *v) A)"
   704   by (simp_all add: linear_linear linear_continuous_at linear_continuous_on matrix_vector_mul_linear)
   705 
   706 lemma vector_matrix_left_distrib [algebra_simps]:
   707   shows "(x + y) v* A = x v* A + y v* A"
   708   unfolding vector_matrix_mult_def
   709   by (simp add: algebra_simps sum.distrib vec_eq_iff)
   710 
   711 lemma matrix_vector_right_distrib [algebra_simps]:
   712   "A *v (x + y) = A *v x + A *v y"
   713   by (vector matrix_vector_mult_def sum.distrib distrib_left)
   714 
   715 lemma matrix_vector_mult_diff_distrib [algebra_simps]:
   716   fixes A :: "'a::ring_1^'n^'m"
   717   shows "A *v (x - y) = A *v x - A *v y"
   718   by (vector matrix_vector_mult_def sum_subtractf right_diff_distrib)
   719 
   720 lemma matrix_vector_mult_scaleR[algebra_simps]:
   721   fixes A :: "real^'n^'m"
   722   shows "A *v (c *\<^sub>R x) = c *\<^sub>R (A *v x)"
   723   using linear_iff matrix_vector_mul_linear by blast
   724 
   725 lemma matrix_vector_mult_0_right [simp]: "A *v 0 = 0"
   726   by (simp add: matrix_vector_mult_def vec_eq_iff)
   727 
   728 lemma matrix_vector_mult_0 [simp]: "0 *v w = 0"
   729   by (simp add: matrix_vector_mult_def vec_eq_iff)
   730 
   731 lemma matrix_vector_mult_add_rdistrib [algebra_simps]:
   732   "(A + B) *v x = (A *v x) + (B *v x)"
   733   by (vector matrix_vector_mult_def sum.distrib distrib_right)
   734 
   735 lemma matrix_vector_mult_diff_rdistrib [algebra_simps]:
   736   fixes A :: "'a :: ring_1^'n^'m"
   737   shows "(A - B) *v x = (A *v x) - (B *v x)"
   738   by (vector matrix_vector_mult_def sum_subtractf left_diff_distrib)
   739 
   740 lemma matrix_works:
   741   assumes lf: "linear f"
   742   shows "matrix f *v x = f (x::real ^ 'n)"
   743   apply (simp add: matrix_def matrix_vector_mult_def vec_eq_iff mult.commute)
   744   by (simp add: linear_componentwise_expansion lf)
   745 
   746 lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::real ^ 'n))"
   747   by (simp add: ext matrix_works)
   748 
   749 declare matrix_vector_mul [symmetric, simp]
   750 
   751 lemma matrix_of_matrix_vector_mul [simp]: "matrix(\<lambda>x. A *v (x :: real ^ 'n)) = A"
   752   by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
   753 
   754 lemma matrix_compose:
   755   assumes lf: "linear (f::real^'n \<Rightarrow> real^'m)"
   756     and lg: "linear (g::real^'m \<Rightarrow> real^_)"
   757   shows "matrix (g \<circ> f) = matrix g ** matrix f"
   758   using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
   759   by (simp add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
   760 
   761 lemma matrix_vector_column:
   762   "(A::'a::comm_semiring_1^'n^_) *v x = sum (\<lambda>i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)"
   763   by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult.commute)
   764 
   765 lemma adjoint_matrix: "adjoint(\<lambda>x. (A::real^'n^'m) *v x) = (\<lambda>x. transpose A *v x)"
   766   apply (rule adjoint_unique)
   767   apply (simp add: transpose_def inner_vec_def matrix_vector_mult_def
   768     sum_distrib_right sum_distrib_left)
   769   apply (subst sum.swap)
   770   apply (auto simp add: ac_simps)
   771   done
   772 
   773 lemma matrix_adjoint: assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'m)"
   774   shows "matrix(adjoint f) = transpose(matrix f)"
   775   apply (subst matrix_vector_mul[OF lf])
   776   unfolding adjoint_matrix matrix_of_matrix_vector_mul
   777   apply rule
   778   done
   779 
   780 lemma inj_matrix_vector_mult:
   781   fixes A::"'a::field^'n^'m"
   782   assumes "invertible A"
   783   shows "inj (( *v) A)"
   784   by (metis assms inj_on_inverseI invertible_def matrix_vector_mul_assoc matrix_vector_mul_lid)
   785 
   786 lemma scalar_invertible:
   787   fixes A :: "('a::real_algebra_1)^'m^'n"
   788   assumes "k \<noteq> 0" and "invertible A"
   789   shows "invertible (k *\<^sub>R A)"
   790 proof -
   791   obtain A' where "A ** A' = mat 1" and "A' ** A = mat 1"
   792     using assms unfolding invertible_def by auto
   793   with `k \<noteq> 0`
   794   have "(k *\<^sub>R A) ** ((1/k) *\<^sub>R A') = mat 1" "((1/k) *\<^sub>R A') ** (k *\<^sub>R A) = mat 1"
   795     by (simp_all add: assms matrix_scalar_ac)
   796   thus "invertible (k *\<^sub>R A)"
   797     unfolding invertible_def by auto
   798 qed
   799 
   800 lemma scalar_invertible_iff:
   801   fixes A :: "('a::real_algebra_1)^'m^'n"
   802   assumes "k \<noteq> 0" and "invertible A"
   803   shows "invertible (k *\<^sub>R A) \<longleftrightarrow> k \<noteq> 0 \<and> invertible A"
   804   by (simp add: assms scalar_invertible)
   805 
   806 lemma vector_transpose_matrix [simp]: "x v* transpose A = A *v x"
   807   unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
   808   by simp
   809 
   810 lemma transpose_matrix_vector [simp]: "transpose A *v x = x v* A"
   811   unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
   812   by simp
   813 
   814 lemma vector_scalar_commute:
   815   fixes A :: "'a::{field}^'m^'n"
   816   shows "A *v (c *s x) = c *s (A *v x)"
   817   by (simp add: vector_scalar_mult_def matrix_vector_mult_def mult_ac sum_distrib_left)
   818 
   819 lemma scalar_vector_matrix_assoc:
   820   fixes k :: "'a::{field}" and x :: "'a::{field}^'n" and A :: "'a^'m^'n"
   821   shows "(k *s x) v* A = k *s (x v* A)"
   822   by (metis transpose_matrix_vector vector_scalar_commute)
   823  
   824 lemma vector_matrix_mult_0 [simp]: "0 v* A = 0"
   825   unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
   826 
   827 lemma vector_matrix_mult_0_right [simp]: "x v* 0 = 0"
   828   unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
   829 
   830 lemma vector_matrix_mul_rid [simp]:
   831   fixes v :: "('a::semiring_1)^'n"
   832   shows "v v* mat 1 = v"
   833   by (metis matrix_vector_mul_lid transpose_mat vector_transpose_matrix)
   834 
   835 lemma scaleR_vector_matrix_assoc:
   836   fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
   837   shows "(k *\<^sub>R x) v* A = k *\<^sub>R (x v* A)"
   838   by (metis matrix_vector_mult_scaleR transpose_matrix_vector)
   839 
   840 lemma vector_scaleR_matrix_ac:
   841   fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
   842   shows "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
   843 proof -
   844   have "x v* (k *\<^sub>R A) = (k *\<^sub>R x) v* A"
   845     unfolding vector_matrix_mult_def
   846     by (simp add: algebra_simps)
   847   with scaleR_vector_matrix_assoc
   848   show "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
   849     by auto
   850 qed
   851 
   852 
   853 subsection\<open>Some bounds on components etc. relative to operator norm\<close>
   854 
   855 lemma norm_column_le_onorm:
   856   fixes A :: "real^'n^'m"
   857   shows "norm(column i A) \<le> onorm(( *v) A)"
   858 proof -
   859   have bl: "bounded_linear (( *v) A)"
   860     by (simp add: linear_linear matrix_vector_mul_linear)
   861   have "norm (\<chi> j. A $ j $ i) \<le> norm (A *v axis i 1)"
   862     by (simp add: matrix_mult_dot cart_eq_inner_axis)
   863   also have "\<dots> \<le> onorm (( *v) A)"
   864     using onorm [OF bl, of "axis i 1"] by auto
   865   finally have "norm (\<chi> j. A $ j $ i) \<le> onorm (( *v) A)" .
   866   then show ?thesis
   867     unfolding column_def .
   868 qed
   869 
   870 lemma matrix_component_le_onorm:
   871   fixes A :: "real^'n^'m"
   872   shows "\<bar>A $ i $ j\<bar> \<le> onorm(( *v) A)"
   873 proof -
   874   have "\<bar>A $ i $ j\<bar> \<le> norm (\<chi> n. (A $ n $ j))"
   875     by (metis (full_types, lifting) component_le_norm_cart vec_lambda_beta)
   876   also have "\<dots> \<le> onorm (( *v) A)"
   877     by (metis (no_types) column_def norm_column_le_onorm)
   878   finally show ?thesis .
   879 qed
   880 
   881 lemma component_le_onorm:
   882   fixes f :: "real^'m \<Rightarrow> real^'n"
   883   shows "linear f \<Longrightarrow> \<bar>matrix f $ i $ j\<bar> \<le> onorm f"
   884   by (metis matrix_component_le_onorm matrix_vector_mul)
   885 
   886 lemma onorm_le_matrix_component_sum:
   887   fixes A :: "real^'n^'m"
   888   shows "onorm(( *v) A) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>)"
   889 proof (rule onorm_le)
   890   fix x
   891   have "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<bar>(A *v x) $ i\<bar>)"
   892     by (rule norm_le_l1_cart)
   893   also have "\<dots> \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)"
   894   proof (rule sum_mono)
   895     fix i
   896     have "\<bar>(A *v x) $ i\<bar> \<le> \<bar>\<Sum>j\<in>UNIV. A $ i $ j * x $ j\<bar>"
   897       by (simp add: matrix_vector_mult_def)
   898     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j * x $ j\<bar>)"
   899       by (rule sum_abs)
   900     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)"
   901       by (rule sum_mono) (simp add: abs_mult component_le_norm_cart mult_left_mono)
   902     finally show "\<bar>(A *v x) $ i\<bar> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)" .
   903   qed
   904   finally show "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>) * norm x"
   905     by (simp add: sum_distrib_right)
   906 qed
   907 
   908 lemma onorm_le_matrix_component:
   909   fixes A :: "real^'n^'m"
   910   assumes "\<And>i j. abs(A$i$j) \<le> B"
   911   shows "onorm(( *v) A) \<le> real (CARD('m)) * real (CARD('n)) * B"
   912 proof (rule onorm_le)
   913   fix x :: "real^'n::_"
   914   have "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<bar>(A *v x) $ i\<bar>)"
   915     by (rule norm_le_l1_cart)
   916   also have "\<dots> \<le> (\<Sum>i::'m \<in>UNIV. real (CARD('n)) * B * norm x)"
   917   proof (rule sum_mono)
   918     fix i
   919     have "\<bar>(A *v x) $ i\<bar> \<le> norm(A $ i) * norm x"
   920       by (simp add: matrix_mult_dot Cauchy_Schwarz_ineq2)
   921     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>) * norm x"
   922       by (simp add: mult_right_mono norm_le_l1_cart)
   923     also have "\<dots> \<le> real (CARD('n)) * B * norm x"
   924       by (simp add: assms sum_bounded_above mult_right_mono)
   925     finally show "\<bar>(A *v x) $ i\<bar> \<le> real (CARD('n)) * B * norm x" .
   926   qed
   927   also have "\<dots> \<le> CARD('m) * real (CARD('n)) * B * norm x"
   928     by simp
   929   finally show "norm (A *v x) \<le> CARD('m) * real (CARD('n)) * B * norm x" .
   930 qed
   931 
   932 subsection \<open>lambda skolemization on cartesian products\<close>
   933 
   934 lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
   935    (\<exists>x::'a ^ 'n. \<forall>i. P i (x $ i))" (is "?lhs \<longleftrightarrow> ?rhs")
   936 proof -
   937   let ?S = "(UNIV :: 'n set)"
   938   { assume H: "?rhs"
   939     then have ?lhs by auto }
   940   moreover
   941   { assume H: "?lhs"
   942     then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
   943     let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
   944     { fix i
   945       from f have "P i (f i)" by metis
   946       then have "P i (?x $ i)" by auto
   947     }
   948     hence "\<forall>i. P i (?x$i)" by metis
   949     hence ?rhs by metis }
   950   ultimately show ?thesis by metis
   951 qed
   952 
   953 lemma rational_approximation:
   954   assumes "e > 0"
   955   obtains r::real where "r \<in> \<rat>" "\<bar>r - x\<bar> < e"
   956   using Rats_dense_in_real [of "x - e/2" "x + e/2"] assms by auto
   957 
   958 lemma matrix_rational_approximation:
   959   fixes A :: "real^'n^'m"
   960   assumes "e > 0"
   961   obtains B where "\<And>i j. B$i$j \<in> \<rat>" "onorm(\<lambda>x. (A - B) *v x) < e"
   962 proof -
   963   have "\<forall>i j. \<exists>q \<in> \<rat>. \<bar>q - A $ i $ j\<bar> < e / (2 * CARD('m) * CARD('n))"
   964     using assms by (force intro: rational_approximation [of "e / (2 * CARD('m) * CARD('n))"])
   965   then obtain B where B: "\<And>i j. B$i$j \<in> \<rat>" and Bclo: "\<And>i j. \<bar>B$i$j - A $ i $ j\<bar> < e / (2 * CARD('m) * CARD('n))"
   966     by (auto simp: lambda_skolem Bex_def)
   967   show ?thesis
   968   proof
   969     have "onorm (( *v) (A - B)) \<le> real CARD('m) * real CARD('n) *
   970     (e / (2 * real CARD('m) * real CARD('n)))"
   971       apply (rule onorm_le_matrix_component)
   972       using Bclo by (simp add: abs_minus_commute less_imp_le)
   973     also have "\<dots> < e"
   974       using \<open>0 < e\<close> by (simp add: divide_simps)
   975     finally show "onorm (( *v) (A - B)) < e" .
   976   qed (use B in auto)
   977 qed
   978 
   979 lemma vector_sub_project_orthogonal_cart: "(b::real^'n) \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *s b) = 0"
   980   unfolding inner_simps scalar_mult_eq_scaleR by auto
   981 
   982 lemma left_invertible_transpose:
   983   "(\<exists>(B). B ** transpose (A) = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). A ** B = mat 1)"
   984   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   985 
   986 lemma right_invertible_transpose:
   987   "(\<exists>(B). transpose (A) ** B = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). B ** A = mat 1)"
   988   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   989 
   990 lemma matrix_left_invertible_injective:
   991   fixes A :: "real^'n^'m"
   992   shows "(\<exists>B. B ** A = mat 1) \<longleftrightarrow> inj (( *v) A)"
   993 proof safe
   994   fix B
   995   assume B: "B ** A = mat 1"
   996   show "inj (( *v) A)"
   997     unfolding inj_on_def
   998       by (metis B matrix_vector_mul_assoc matrix_vector_mul_lid)
   999 next
  1000   assume "inj (( *v) A)"
  1001   with linear_injective_left_inverse[OF matrix_vector_mul_linear]
  1002   obtain g where "linear g" and g: "g \<circ> ( *v) A = id"
  1003     by blast
  1004   have "matrix g ** A = mat 1"
  1005     by (metis \<open>linear g\<close> g matrix_compose matrix_id_mat_1 matrix_of_matrix_vector_mul matrix_vector_mul_linear)
  1006   then show "\<exists>B. B ** A = mat 1"
  1007     by metis
  1008 qed
  1009 
  1010 lemma matrix_left_invertible_ker:
  1011   "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
  1012   unfolding matrix_left_invertible_injective
  1013   using linear_injective_0[OF matrix_vector_mul_linear, of A]
  1014   by (simp add: inj_on_def)
  1015 
  1016 lemma matrix_right_invertible_surjective:
  1017   "(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
  1018 proof -
  1019   { fix B :: "real ^'m^'n"
  1020     assume AB: "A ** B = mat 1"
  1021     { fix x :: "real ^ 'm"
  1022       have "A *v (B *v x) = x"
  1023         by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB) }
  1024     hence "surj (( *v) A)" unfolding surj_def by metis }
  1025   moreover
  1026   { assume sf: "surj (( *v) A)"
  1027     from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
  1028     obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "( *v) A \<circ> g = id"
  1029       by blast
  1030 
  1031     have "A ** (matrix g) = mat 1"
  1032       unfolding matrix_eq  matrix_vector_mul_lid
  1033         matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
  1034       using g(2) unfolding o_def fun_eq_iff id_def
  1035       .
  1036     hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
  1037   }
  1038   ultimately show ?thesis unfolding surj_def by blast
  1039 qed
  1040 
  1041 lemma matrix_left_invertible_independent_columns:
  1042   fixes A :: "real^'n^'m"
  1043   shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow>
  1044       (\<forall>c. sum (\<lambda>i. c i *s column i A) (UNIV :: 'n set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
  1045     (is "?lhs \<longleftrightarrow> ?rhs")
  1046 proof -
  1047   let ?U = "UNIV :: 'n set"
  1048   { assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
  1049     { fix c i
  1050       assume c: "sum (\<lambda>i. c i *s column i A) ?U = 0" and i: "i \<in> ?U"
  1051       let ?x = "\<chi> i. c i"
  1052       have th0:"A *v ?x = 0"
  1053         using c
  1054         unfolding matrix_mult_sum vec_eq_iff
  1055         by auto
  1056       from k[rule_format, OF th0] i
  1057       have "c i = 0" by (vector vec_eq_iff)}
  1058     hence ?rhs by blast }
  1059   moreover
  1060   { assume H: ?rhs
  1061     { fix x assume x: "A *v x = 0"
  1062       let ?c = "\<lambda>i. ((x$i ):: real)"
  1063       from H[rule_format, of ?c, unfolded matrix_mult_sum[symmetric], OF x]
  1064       have "x = 0" by vector }
  1065   }
  1066   ultimately show ?thesis unfolding matrix_left_invertible_ker by blast
  1067 qed
  1068 
  1069 lemma matrix_right_invertible_independent_rows:
  1070   fixes A :: "real^'n^'m"
  1071   shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow>
  1072     (\<forall>c. sum (\<lambda>i. c i *s row i A) (UNIV :: 'm set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
  1073   unfolding left_invertible_transpose[symmetric]
  1074     matrix_left_invertible_independent_columns
  1075   by (simp add: column_transpose)
  1076 
  1077 lemma matrix_right_invertible_span_columns:
  1078   "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow>
  1079     span (columns A) = UNIV" (is "?lhs = ?rhs")
  1080 proof -
  1081   let ?U = "UNIV :: 'm set"
  1082   have fU: "finite ?U" by simp
  1083   have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). sum (\<lambda>i. (x$i) *s column i A) ?U = y)"
  1084     unfolding matrix_right_invertible_surjective matrix_mult_sum surj_def
  1085     by (simp add: eq_commute)
  1086   have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
  1087   { assume h: ?lhs
  1088     { fix x:: "real ^'n"
  1089       from h[unfolded lhseq, rule_format, of x] obtain y :: "real ^'m"
  1090         where y: "sum (\<lambda>i. (y$i) *s column i A) ?U = x" by blast
  1091       have "x \<in> span (columns A)"
  1092         unfolding y[symmetric] scalar_mult_eq_scaleR
  1093       proof (rule span_sum [OF span_mul])
  1094         show "column i A \<in> span (columns A)" for i
  1095           using columns_def span_inc by auto
  1096       qed
  1097     }
  1098     then have ?rhs unfolding rhseq by blast }
  1099   moreover
  1100   { assume h:?rhs
  1101     let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). sum (\<lambda>i. (x$i) *s column i A) ?U = y"
  1102     { fix y
  1103       have "?P y"
  1104       proof (rule span_induct_alt[of ?P "columns A", folded scalar_mult_eq_scaleR])
  1105         show "\<exists>x::real ^ 'm. sum (\<lambda>i. (x$i) *s column i A) ?U = 0"
  1106           by (rule exI[where x=0], simp)
  1107       next
  1108         fix c y1 y2
  1109         assume y1: "y1 \<in> columns A" and y2: "?P y2"
  1110         from y1 obtain i where i: "i \<in> ?U" "y1 = column i A"
  1111           unfolding columns_def by blast
  1112         from y2 obtain x:: "real ^'m" where
  1113           x: "sum (\<lambda>i. (x$i) *s column i A) ?U = y2" by blast
  1114         let ?x = "(\<chi> j. if j = i then c + (x$i) else (x$j))::real^'m"
  1115         show "?P (c*s y1 + y2)"
  1116         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)
  1117           fix j
  1118           have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j)
  1119               else (x$xa) * ((column xa A$j))) = (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))"
  1120             using i(1) by (simp add: field_simps)
  1121           have "sum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
  1122               else (x$xa) * ((column xa A$j))) ?U = sum (\<lambda>xa. (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))) ?U"
  1123             by (rule sum.cong[OF refl]) (use th in blast)
  1124           also have "\<dots> = sum (\<lambda>xa. if xa = i then c * ((column i A)$j) else 0) ?U + sum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
  1125             by (simp add: sum.distrib)
  1126           also have "\<dots> = c * ((column i A)$j) + sum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
  1127             unfolding sum.delta[OF fU]
  1128             using i(1) by simp
  1129           finally show "sum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
  1130             else (x$xa) * ((column xa A$j))) ?U = c * ((column i A)$j) + sum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U" .
  1131         qed
  1132       next
  1133         show "y \<in> span (columns A)"
  1134           unfolding h by blast
  1135       qed
  1136     }
  1137     then have ?lhs unfolding lhseq ..
  1138   }
  1139   ultimately show ?thesis by blast
  1140 qed
  1141 
  1142 lemma matrix_left_invertible_span_rows:
  1143   "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
  1144   unfolding right_invertible_transpose[symmetric]
  1145   unfolding columns_transpose[symmetric]
  1146   unfolding matrix_right_invertible_span_columns
  1147   ..
  1148 
  1149 text \<open>The same result in terms of square matrices.\<close>
  1150 
  1151 lemma matrix_left_right_inverse:
  1152   fixes A A' :: "real ^'n^'n"
  1153   shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
  1154 proof -
  1155   { fix A A' :: "real ^'n^'n"
  1156     assume AA': "A ** A' = mat 1"
  1157     have sA: "surj (( *v) A)"
  1158       using AA' matrix_right_invertible_surjective by auto
  1159     from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
  1160     obtain f' :: "real ^'n \<Rightarrow> real ^'n"
  1161       where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
  1162     have th: "matrix f' ** A = mat 1"
  1163       by (simp add: matrix_eq matrix_works[OF f'(1)]
  1164           matrix_vector_mul_assoc[symmetric] f'(2)[rule_format])
  1165     hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
  1166     hence "matrix f' = A'"
  1167       by (simp add: matrix_mul_assoc[symmetric] AA')
  1168     hence "matrix f' ** A = A' ** A" by simp
  1169     hence "A' ** A = mat 1" by (simp add: th)
  1170   }
  1171   then show ?thesis by blast
  1172 qed
  1173 
  1174 lemma invertible_mult:
  1175   assumes inv_A: "invertible A"
  1176   and inv_B: "invertible B"
  1177   shows "invertible (A**B)"
  1178 proof -
  1179   obtain A' where AA': "A ** A' = mat 1" and A'A: "A' ** A = mat 1" 
  1180     using inv_A unfolding invertible_def by blast
  1181   obtain B' where BB': "B ** B' = mat 1" and B'B: "B' ** B = mat 1" 
  1182     using inv_B unfolding invertible_def by blast
  1183   show ?thesis
  1184   proof (unfold invertible_def, rule exI[of _ "B'**A'"], rule conjI)
  1185     have "A ** B ** (B' ** A') = A ** (B ** (B' ** A'))" 
  1186       using matrix_mul_assoc[of A B "(B' ** A')", symmetric] .
  1187     also have "... = A ** (B ** B' ** A')" unfolding matrix_mul_assoc[of B "B'" "A'"] ..
  1188     also have "... = A ** (mat 1 ** A')" unfolding BB' ..
  1189     also have "... = A ** A'" unfolding matrix_mul_lid ..
  1190     also have "... = mat 1" unfolding AA' ..
  1191     finally show "A ** B ** (B' ** A') = mat (1::'a)" .    
  1192     have "B' ** A' ** (A ** B) = B' ** (A' ** (A ** B))" using matrix_mul_assoc[of B' A' "(A ** B)", symmetric] .
  1193     also have "... =  B' ** (A' ** A ** B)" unfolding matrix_mul_assoc[of A' A B] ..
  1194     also have "... =  B' ** (mat 1 ** B)" unfolding A'A ..
  1195     also have "... = B' ** B"  unfolding matrix_mul_lid ..
  1196     also have "... = mat 1" unfolding B'B ..
  1197     finally show "B' ** A' ** (A ** B) = mat 1" .
  1198   qed
  1199 qed
  1200 
  1201 lemma transpose_invertible:
  1202   fixes A :: "real^'n^'n"
  1203   assumes "invertible A"
  1204   shows "invertible (transpose A)"
  1205   by (meson assms invertible_def matrix_left_right_inverse right_invertible_transpose)
  1206 
  1207 lemma vector_matrix_mul_assoc:
  1208   fixes v :: "('a::comm_semiring_1)^'n"
  1209   shows "(v v* M) v* N = v v* (M ** N)"
  1210 proof -
  1211   from matrix_vector_mul_assoc
  1212   have "transpose N *v (transpose M *v v) = (transpose N ** transpose M) *v v" by fast
  1213   thus "(v v* M) v* N = v v* (M ** N)"
  1214     by (simp add: matrix_transpose_mul [symmetric])
  1215 qed
  1216 
  1217 lemma matrix_scaleR_vector_ac:
  1218   fixes A :: "real^('m::finite)^'n"
  1219   shows "A *v (k *\<^sub>R v) = k *\<^sub>R A *v v"
  1220   by (metis matrix_vector_mult_scaleR transpose_scalar vector_scaleR_matrix_ac vector_transpose_matrix)
  1221 
  1222 lemma scaleR_matrix_vector_assoc:
  1223   fixes A :: "real^('m::finite)^'n"
  1224   shows "k *\<^sub>R (A *v v) = k *\<^sub>R A *v v"
  1225   by (metis matrix_scaleR_vector_ac matrix_vector_mult_scaleR)
  1226 
  1227 text \<open>Considering an n-element vector as an n-by-1 or 1-by-n matrix.\<close>
  1228 
  1229 definition "rowvector v = (\<chi> i j. (v$j))"
  1230 
  1231 definition "columnvector v = (\<chi> i j. (v$i))"
  1232 
  1233 lemma transpose_columnvector: "transpose(columnvector v) = rowvector v"
  1234   by (simp add: transpose_def rowvector_def columnvector_def vec_eq_iff)
  1235 
  1236 lemma transpose_rowvector: "transpose(rowvector v) = columnvector v"
  1237   by (simp add: transpose_def columnvector_def rowvector_def vec_eq_iff)
  1238 
  1239 lemma dot_rowvector_columnvector: "columnvector (A *v v) = A ** columnvector v"
  1240   by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
  1241 
  1242 lemma dot_matrix_product:
  1243   "(x::real^'n) \<bullet> y = (((rowvector x ::real^'n^1) ** (columnvector y :: real^1^'n))$1)$1"
  1244   by (vector matrix_matrix_mult_def rowvector_def columnvector_def inner_vec_def)
  1245 
  1246 lemma dot_matrix_vector_mul:
  1247   fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
  1248   shows "(A *v x) \<bullet> (B *v y) =
  1249       (((rowvector x :: real^'n^1) ** ((transpose A ** B) ** (columnvector y :: real ^1^'n)))$1)$1"
  1250   unfolding dot_matrix_product transpose_columnvector[symmetric]
  1251     dot_rowvector_columnvector matrix_transpose_mul matrix_mul_assoc ..
  1252 
  1253 lemma infnorm_cart:"infnorm (x::real^'n) = Sup {\<bar>x$i\<bar> |i. i\<in>UNIV}"
  1254   by (simp add: infnorm_def inner_axis Basis_vec_def) (metis (lifting) inner_axis real_inner_1_right)
  1255 
  1256 lemma component_le_infnorm_cart: "\<bar>x$i\<bar> \<le> infnorm (x::real^'n)"
  1257   using Basis_le_infnorm[of "axis i 1" x]
  1258   by (simp add: Basis_vec_def axis_eq_axis inner_axis)
  1259 
  1260 lemma continuous_component[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. f x $ i)"
  1261   unfolding continuous_def by (rule tendsto_vec_nth)
  1262 
  1263 lemma continuous_on_component[continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. f x $ i)"
  1264   unfolding continuous_on_def by (fast intro: tendsto_vec_nth)
  1265 
  1266 lemma continuous_on_vec_lambda[continuous_intros]:
  1267   "(\<And>i. continuous_on S (f i)) \<Longrightarrow> continuous_on S (\<lambda>x. \<chi> i. f i x)"
  1268   unfolding continuous_on_def by (auto intro: tendsto_vec_lambda)
  1269 
  1270 lemma closed_positive_orthant: "closed {x::real^'n. \<forall>i. 0 \<le>x$i}"
  1271   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1272 
  1273 lemma bounded_component_cart: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $ i) ` s)"
  1274   unfolding bounded_def
  1275   apply clarify
  1276   apply (rule_tac x="x $ i" in exI)
  1277   apply (rule_tac x="e" in exI)
  1278   apply clarify
  1279   apply (rule order_trans [OF dist_vec_nth_le], simp)
  1280   done
  1281 
  1282 lemma compact_lemma_cart:
  1283   fixes f :: "nat \<Rightarrow> 'a::heine_borel ^ 'n"
  1284   assumes f: "bounded (range f)"
  1285   shows "\<exists>l r. strict_mono r \<and>
  1286         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
  1287     (is "?th d")
  1288 proof -
  1289   have "\<forall>d' \<subseteq> d. ?th d'"
  1290     by (rule compact_lemma_general[where unproj=vec_lambda])
  1291       (auto intro!: f bounded_component_cart simp: vec_lambda_eta)
  1292   then show "?th d" by simp
  1293 qed
  1294 
  1295 instance vec :: (heine_borel, finite) heine_borel
  1296 proof
  1297   fix f :: "nat \<Rightarrow> 'a ^ 'b"
  1298   assume f: "bounded (range f)"
  1299   then obtain l r where r: "strict_mono r"
  1300       and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $ i) (l $ i) < e) sequentially"
  1301     using compact_lemma_cart [OF f] by blast
  1302   let ?d = "UNIV::'b set"
  1303   { fix e::real assume "e>0"
  1304     hence "0 < e / (real_of_nat (card ?d))"
  1305       using zero_less_card_finite divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
  1306     with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))) sequentially"
  1307       by simp
  1308     moreover
  1309     { fix n
  1310       assume n: "\<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))"
  1311       have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $ i) (l $ i))"
  1312         unfolding dist_vec_def using zero_le_dist by (rule L2_set_le_sum)
  1313       also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
  1314         by (rule sum_strict_mono) (simp_all add: n)
  1315       finally have "dist (f (r n)) l < e" by simp
  1316     }
  1317     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  1318       by (rule eventually_mono)
  1319   }
  1320   hence "((f \<circ> r) \<longlongrightarrow> l) sequentially" unfolding o_def tendsto_iff by simp
  1321   with r show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" by auto
  1322 qed
  1323 
  1324 lemma interval_cart:
  1325   fixes a :: "real^'n"
  1326   shows "box a b = {x::real^'n. \<forall>i. a$i < x$i \<and> x$i < b$i}"
  1327     and "cbox a b = {x::real^'n. \<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i}"
  1328   by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def mem_box Basis_vec_def inner_axis)
  1329 
  1330 lemma mem_box_cart:
  1331   fixes a :: "real^'n"
  1332   shows "x \<in> box a b \<longleftrightarrow> (\<forall>i. a$i < x$i \<and> x$i < b$i)"
  1333     and "x \<in> cbox a b \<longleftrightarrow> (\<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i)"
  1334   using interval_cart[of a b] by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def)
  1335 
  1336 lemma interval_eq_empty_cart:
  1337   fixes a :: "real^'n"
  1338   shows "(box a b = {} \<longleftrightarrow> (\<exists>i. b$i \<le> a$i))" (is ?th1)
  1339     and "(cbox a b = {} \<longleftrightarrow> (\<exists>i. b$i < a$i))" (is ?th2)
  1340 proof -
  1341   { fix i x assume as:"b$i \<le> a$i" and x:"x\<in>box a b"
  1342     hence "a $ i < x $ i \<and> x $ i < b $ i" unfolding mem_box_cart by auto
  1343     hence "a$i < b$i" by auto
  1344     hence False using as by auto }
  1345   moreover
  1346   { assume as:"\<forall>i. \<not> (b$i \<le> a$i)"
  1347     let ?x = "(1/2) *\<^sub>R (a + b)"
  1348     { fix i
  1349       have "a$i < b$i" using as[THEN spec[where x=i]] by auto
  1350       hence "a$i < ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i < b$i"
  1351         unfolding vector_smult_component and vector_add_component
  1352         by auto }
  1353     hence "box a b \<noteq> {}" using mem_box_cart(1)[of "?x" a b] by auto }
  1354   ultimately show ?th1 by blast
  1355 
  1356   { fix i x assume as:"b$i < a$i" and x:"x\<in>cbox a b"
  1357     hence "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" unfolding mem_box_cart by auto
  1358     hence "a$i \<le> b$i" by auto
  1359     hence False using as by auto }
  1360   moreover
  1361   { assume as:"\<forall>i. \<not> (b$i < a$i)"
  1362     let ?x = "(1/2) *\<^sub>R (a + b)"
  1363     { fix i
  1364       have "a$i \<le> b$i" using as[THEN spec[where x=i]] by auto
  1365       hence "a$i \<le> ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i \<le> b$i"
  1366         unfolding vector_smult_component and vector_add_component
  1367         by auto }
  1368     hence "cbox a b \<noteq> {}" using mem_box_cart(2)[of "?x" a b] by auto  }
  1369   ultimately show ?th2 by blast
  1370 qed
  1371 
  1372 lemma interval_ne_empty_cart:
  1373   fixes a :: "real^'n"
  1374   shows "cbox a b \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i \<le> b$i)"
  1375     and "box a b \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
  1376   unfolding interval_eq_empty_cart[of a b] by (auto simp add: not_less not_le)
  1377     (* BH: Why doesn't just "auto" work here? *)
  1378 
  1379 lemma subset_interval_imp_cart:
  1380   fixes a :: "real^'n"
  1381   shows "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> cbox c d \<subseteq> cbox a b"
  1382     and "(\<forall>i. a$i < c$i \<and> d$i < b$i) \<Longrightarrow> cbox c d \<subseteq> box a b"
  1383     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> box c d \<subseteq> cbox a b"
  1384     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> box c d \<subseteq> box a b"
  1385   unfolding subset_eq[unfolded Ball_def] unfolding mem_box_cart
  1386   by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
  1387 
  1388 lemma interval_sing:
  1389   fixes a :: "'a::linorder^'n"
  1390   shows "{a .. a} = {a} \<and> {a<..<a} = {}"
  1391   apply (auto simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
  1392   done
  1393 
  1394 lemma subset_interval_cart:
  1395   fixes a :: "real^'n"
  1396   shows "cbox c d \<subseteq> cbox 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)
  1397     and "cbox c d \<subseteq> box a b \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i < c$i \<and> d$i < b$i)" (is ?th2)
  1398     and "box c d \<subseteq> cbox a b \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th3)
  1399     and "box c d \<subseteq> box a b \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th4)
  1400   using subset_box[of c d a b] by (simp_all add: Basis_vec_def inner_axis)
  1401 
  1402 lemma disjoint_interval_cart:
  1403   fixes a::"real^'n"
  1404   shows "cbox a b \<inter> cbox 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)
  1405     and "cbox a b \<inter> box 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)
  1406     and "box a b \<inter> cbox 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)
  1407     and "box a b \<inter> box 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)
  1408   using disjoint_interval[of a b c d] by (simp_all add: Basis_vec_def inner_axis)
  1409 
  1410 lemma Int_interval_cart:
  1411   fixes a :: "real^'n"
  1412   shows "cbox a b \<inter> cbox c d =  {(\<chi> i. max (a$i) (c$i)) .. (\<chi> i. min (b$i) (d$i))}"
  1413   unfolding Int_interval
  1414   by (auto simp: mem_box less_eq_vec_def)
  1415     (auto simp: Basis_vec_def inner_axis)
  1416 
  1417 lemma closed_interval_left_cart:
  1418   fixes b :: "real^'n"
  1419   shows "closed {x::real^'n. \<forall>i. x$i \<le> b$i}"
  1420   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1421 
  1422 lemma closed_interval_right_cart:
  1423   fixes a::"real^'n"
  1424   shows "closed {x::real^'n. \<forall>i. a$i \<le> x$i}"
  1425   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1426 
  1427 lemma is_interval_cart:
  1428   "is_interval (s::(real^'n) set) \<longleftrightarrow>
  1429     (\<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)"
  1430   by (simp add: is_interval_def Ball_def Basis_vec_def inner_axis imp_ex)
  1431 
  1432 lemma closed_halfspace_component_le_cart: "closed {x::real^'n. x$i \<le> a}"
  1433   by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1434 
  1435 lemma closed_halfspace_component_ge_cart: "closed {x::real^'n. x$i \<ge> a}"
  1436   by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1437 
  1438 lemma open_halfspace_component_lt_cart: "open {x::real^'n. x$i < a}"
  1439   by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)
  1440 
  1441 lemma open_halfspace_component_gt_cart: "open {x::real^'n. x$i  > a}"
  1442   by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)
  1443 
  1444 lemma Lim_component_le_cart:
  1445   fixes f :: "'a \<Rightarrow> real^'n"
  1446   assumes "(f \<longlongrightarrow> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f x $i \<le> b) net"
  1447   shows "l$i \<le> b"
  1448   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_vec_nth, OF assms(1, 3)])
  1449 
  1450 lemma Lim_component_ge_cart:
  1451   fixes f :: "'a \<Rightarrow> real^'n"
  1452   assumes "(f \<longlongrightarrow> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$i) net"
  1453   shows "b \<le> l$i"
  1454   by (rule tendsto_le[OF assms(2) tendsto_vec_nth tendsto_const, OF assms(1, 3)])
  1455 
  1456 lemma Lim_component_eq_cart:
  1457   fixes f :: "'a \<Rightarrow> real^'n"
  1458   assumes net: "(f \<longlongrightarrow> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
  1459   shows "l$i = b"
  1460   using ev[unfolded order_eq_iff eventually_conj_iff] and
  1461     Lim_component_ge_cart[OF net, of b i] and
  1462     Lim_component_le_cart[OF net, of i b] by auto
  1463 
  1464 lemma connected_ivt_component_cart:
  1465   fixes x :: "real^'n"
  1466   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)"
  1467   using connected_ivt_hyperplane[of s x y "axis k 1" a]
  1468   by (auto simp add: inner_axis inner_commute)
  1469 
  1470 lemma subspace_substandard_cart: "subspace {x::real^_. (\<forall>i. P i \<longrightarrow> x$i = 0)}"
  1471   unfolding subspace_def by auto
  1472 
  1473 lemma closed_substandard_cart:
  1474   "closed {x::'a::real_normed_vector ^ 'n. \<forall>i. P i \<longrightarrow> x$i = 0}"
  1475 proof -
  1476   { fix i::'n
  1477     have "closed {x::'a ^ 'n. P i \<longrightarrow> x$i = 0}"
  1478       by (cases "P i") (simp_all add: closed_Collect_eq continuous_on_const continuous_on_id continuous_on_component) }
  1479   thus ?thesis
  1480     unfolding Collect_all_eq by (simp add: closed_INT)
  1481 qed
  1482 
  1483 lemma dim_substandard_cart: "dim {x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0} = card d"
  1484   (is "dim ?A = _")
  1485 proof -
  1486   let ?a = "\<lambda>x. axis x 1 :: real^'n"
  1487   have *: "{x. \<forall>i\<in>Basis. i \<notin> ?a ` d \<longrightarrow> x \<bullet> i = 0} = ?A"
  1488     by (auto simp: image_iff Basis_vec_def axis_eq_axis inner_axis)
  1489   have "?a ` d \<subseteq> Basis"
  1490     by (auto simp: Basis_vec_def)
  1491   thus ?thesis
  1492     using dim_substandard[of "?a ` d"] card_image[of ?a d]
  1493     by (auto simp: axis_eq_axis inj_on_def *)
  1494 qed
  1495 
  1496 lemma dim_subset_UNIV_cart:
  1497   fixes S :: "(real^'n) set"
  1498   shows "dim S \<le> CARD('n)"
  1499   by (metis dim_subset_UNIV DIM_cart DIM_real mult.right_neutral)
  1500 
  1501 lemma affinity_inverses:
  1502   assumes m0: "m \<noteq> (0::'a::field)"
  1503   shows "(\<lambda>x. m *s x + c) \<circ> (\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) = id"
  1504   "(\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) \<circ> (\<lambda>x. m *s x + c) = id"
  1505   using m0
  1506   apply (auto simp add: fun_eq_iff vector_add_ldistrib diff_conv_add_uminus simp del: add_uminus_conv_diff)
  1507   apply (simp_all add: vector_smult_lneg[symmetric] vector_smult_assoc vector_sneg_minus1 [symmetric])
  1508   done
  1509 
  1510 lemma vector_affinity_eq:
  1511   assumes m0: "(m::'a::field) \<noteq> 0"
  1512   shows "m *s x + c = y \<longleftrightarrow> x = inverse m *s y + -(inverse m *s c)"
  1513 proof
  1514   assume h: "m *s x + c = y"
  1515   hence "m *s x = y - c" by (simp add: field_simps)
  1516   hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp
  1517   then show "x = inverse m *s y + - (inverse m *s c)"
  1518     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1519 next
  1520   assume h: "x = inverse m *s y + - (inverse m *s c)"
  1521   show "m *s x + c = y" unfolding h
  1522     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1523 qed
  1524 
  1525 lemma vector_eq_affinity:
  1526     "(m::'a::field) \<noteq> 0 ==> (y = m *s x + c \<longleftrightarrow> inverse(m) *s y + -(inverse(m) *s c) = x)"
  1527   using vector_affinity_eq[where m=m and x=x and y=y and c=c]
  1528   by metis
  1529 
  1530 lemma vector_cart:
  1531   fixes f :: "real^'n \<Rightarrow> real"
  1532   shows "(\<chi> i. f (axis i 1)) = (\<Sum>i\<in>Basis. f i *\<^sub>R i)"
  1533   unfolding euclidean_eq_iff[where 'a="real^'n"]
  1534   by simp (simp add: Basis_vec_def inner_axis)
  1535 
  1536 lemma const_vector_cart:"((\<chi> i. d)::real^'n) = (\<Sum>i\<in>Basis. d *\<^sub>R i)"
  1537   by (rule vector_cart)
  1538 
  1539 subsection "Convex Euclidean Space"
  1540 
  1541 lemma Cart_1:"(1::real^'n) = \<Sum>Basis"
  1542   using const_vector_cart[of 1] by (simp add: one_vec_def)
  1543 
  1544 declare vector_add_ldistrib[simp] vector_ssub_ldistrib[simp] vector_smult_assoc[simp] vector_smult_rneg[simp]
  1545 declare vector_sadd_rdistrib[simp] vector_sub_rdistrib[simp]
  1546 
  1547 lemmas vector_component_simps = vector_minus_component vector_smult_component vector_add_component less_eq_vec_def vec_lambda_beta vector_uminus_component
  1548 
  1549 lemma convex_box_cart:
  1550   assumes "\<And>i. convex {x. P i x}"
  1551   shows "convex {x. \<forall>i. P i (x$i)}"
  1552   using assms unfolding convex_def by auto
  1553 
  1554 lemma convex_positive_orthant_cart: "convex {x::real^'n. (\<forall>i. 0 \<le> x$i)}"
  1555   by (rule convex_box_cart) (simp add: atLeast_def[symmetric])
  1556 
  1557 lemma unit_interval_convex_hull_cart:
  1558   "cbox (0::real^'n) 1 = convex hull {x. \<forall>i. (x$i = 0) \<or> (x$i = 1)}"
  1559   unfolding Cart_1 unit_interval_convex_hull[where 'a="real^'n"] box_real[symmetric]
  1560   by (rule arg_cong[where f="\<lambda>x. convex hull x"]) (simp add: Basis_vec_def inner_axis)
  1561 
  1562 lemma cube_convex_hull_cart:
  1563   assumes "0 < d"
  1564   obtains s::"(real^'n) set"
  1565     where "finite s" "cbox (x - (\<chi> i. d)) (x + (\<chi> i. d)) = convex hull s"
  1566 proof -
  1567   from assms obtain s where "finite s"
  1568     and "cbox (x - sum (( *\<^sub>R) d) Basis) (x + sum (( *\<^sub>R) d) Basis) = convex hull s"
  1569     by (rule cube_convex_hull)
  1570   with that[of s] show thesis
  1571     by (simp add: const_vector_cart)
  1572 qed
  1573 
  1574 
  1575 subsection "Derivative"
  1576 
  1577 definition "jacobian f net = matrix(frechet_derivative f net)"
  1578 
  1579 lemma jacobian_works:
  1580   "(f::(real^'a) \<Rightarrow> (real^'b)) differentiable net \<longleftrightarrow>
  1581     (f has_derivative (\<lambda>h. (jacobian f net) *v h)) net" (is "?lhs = ?rhs")
  1582 proof
  1583   assume ?lhs then show ?rhs
  1584     by (simp add: frechet_derivative_works has_derivative_linear jacobian_def)
  1585 next
  1586   assume ?rhs then show ?lhs
  1587     by (rule differentiableI)
  1588 qed
  1589 
  1590 
  1591 subsection \<open>Component of the differential must be zero if it exists at a local
  1592   maximum or minimum for that corresponding component\<close>
  1593 
  1594 lemma differential_zero_maxmin_cart:
  1595   fixes f::"real^'a \<Rightarrow> real^'b"
  1596   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))"
  1597     "f differentiable (at x)"
  1598   shows "jacobian f (at x) $ k = 0"
  1599   using differential_zero_maxmin_component[of "axis k 1" e x f] assms
  1600     vector_cart[of "\<lambda>j. frechet_derivative f (at x) j $ k"]
  1601   by (simp add: Basis_vec_def axis_eq_axis inner_axis jacobian_def matrix_def)
  1602 
  1603 subsection \<open>Lemmas for working on @{typ "real^1"}\<close>
  1604 
  1605 lemma forall_1[simp]: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
  1606   by (metis (full_types) num1_eq_iff)
  1607 
  1608 lemma ex_1[simp]: "(\<exists>x::1. P x) \<longleftrightarrow> P 1"
  1609   by auto (metis (full_types) num1_eq_iff)
  1610 
  1611 lemma exhaust_2:
  1612   fixes x :: 2
  1613   shows "x = 1 \<or> x = 2"
  1614 proof (induct x)
  1615   case (of_int z)
  1616   then have "0 \<le> z" and "z < 2" by simp_all
  1617   then have "z = 0 | z = 1" by arith
  1618   then show ?case by auto
  1619 qed
  1620 
  1621 lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
  1622   by (metis exhaust_2)
  1623 
  1624 lemma exhaust_3:
  1625   fixes x :: 3
  1626   shows "x = 1 \<or> x = 2 \<or> x = 3"
  1627 proof (induct x)
  1628   case (of_int z)
  1629   then have "0 \<le> z" and "z < 3" by simp_all
  1630   then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
  1631   then show ?case by auto
  1632 qed
  1633 
  1634 lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
  1635   by (metis exhaust_3)
  1636 
  1637 lemma UNIV_1 [simp]: "UNIV = {1::1}"
  1638   by (auto simp add: num1_eq_iff)
  1639 
  1640 lemma UNIV_2: "UNIV = {1::2, 2::2}"
  1641   using exhaust_2 by auto
  1642 
  1643 lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
  1644   using exhaust_3 by auto
  1645 
  1646 lemma sum_1: "sum f (UNIV::1 set) = f 1"
  1647   unfolding UNIV_1 by simp
  1648 
  1649 lemma sum_2: "sum f (UNIV::2 set) = f 1 + f 2"
  1650   unfolding UNIV_2 by simp
  1651 
  1652 lemma sum_3: "sum f (UNIV::3 set) = f 1 + f 2 + f 3"
  1653   unfolding UNIV_3 by (simp add: ac_simps)
  1654 
  1655 lemma num1_eqI:
  1656   fixes a::num1 shows "a = b"
  1657   by (metis (full_types) UNIV_1 UNIV_I empty_iff insert_iff)
  1658 
  1659 lemma num1_eq1 [simp]:
  1660   fixes a::num1 shows "a = 1"
  1661   by (rule num1_eqI)
  1662 
  1663 instantiation num1 :: cart_one
  1664 begin
  1665 
  1666 instance
  1667 proof
  1668   show "CARD(1) = Suc 0" by auto
  1669 qed
  1670 
  1671 end
  1672 
  1673 instantiation num1 :: linorder begin
  1674 definition "a < b \<longleftrightarrow> Rep_num1 a < Rep_num1 b"
  1675 definition "a \<le> b \<longleftrightarrow> Rep_num1 a \<le> Rep_num1 b"
  1676 instance
  1677   by intro_classes (auto simp: less_eq_num1_def less_num1_def intro: num1_eqI)
  1678 end
  1679 
  1680 instance num1 :: wellorder
  1681   by intro_classes (auto simp: less_eq_num1_def less_num1_def)
  1682 
  1683 subsection\<open>The collapse of the general concepts to dimension one\<close>
  1684 
  1685 lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
  1686   by (simp add: vec_eq_iff)
  1687 
  1688 lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
  1689   apply auto
  1690   apply (erule_tac x= "x$1" in allE)
  1691   apply (simp only: vector_one[symmetric])
  1692   done
  1693 
  1694 lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
  1695   by (simp add: norm_vec_def)
  1696 
  1697 lemma dist_vector_1:
  1698   fixes x :: "'a::real_normed_vector^1"
  1699   shows "dist x y = dist (x$1) (y$1)"
  1700   by (simp add: dist_norm norm_vector_1)
  1701 
  1702 lemma norm_real: "norm(x::real ^ 1) = \<bar>x$1\<bar>"
  1703   by (simp add: norm_vector_1)
  1704 
  1705 lemma dist_real: "dist(x::real ^ 1) y = \<bar>(x$1) - (y$1)\<bar>"
  1706   by (auto simp add: norm_real dist_norm)
  1707 
  1708 subsection\<open> Rank of a matrix\<close>
  1709 
  1710 text\<open>Equivalence of row and column rank is taken from George Mackiw's paper, Mathematics Magazine 1995, p. 285.\<close>
  1711 
  1712 lemma matrix_vector_mult_in_columnspace:
  1713   fixes A :: "real^'n^'m"
  1714   shows "(A *v x) \<in> span(columns A)"
  1715   apply (simp add: matrix_vector_column columns_def transpose_def column_def)
  1716   apply (intro span_sum span_mul)
  1717   apply (force intro: span_superset)
  1718   done
  1719 
  1720 lemma orthogonal_nullspace_rowspace:
  1721   fixes A :: "real^'n^'m"
  1722   assumes 0: "A *v x = 0" and y: "y \<in> span(rows A)"
  1723   shows "orthogonal x y"
  1724 proof (rule span_induct [OF y])
  1725   show "subspace {a. orthogonal x a}"
  1726     by (simp add: subspace_orthogonal_to_vector)
  1727 next
  1728   fix v
  1729   assume "v \<in> rows A"
  1730   then obtain i where "v = row i A"
  1731     by (auto simp: rows_def)
  1732   with 0 show "orthogonal x v"
  1733     unfolding orthogonal_def inner_vec_def matrix_vector_mult_def row_def
  1734     by (simp add: mult.commute) (metis (no_types) vec_lambda_beta zero_index)
  1735 qed
  1736 
  1737 lemma nullspace_inter_rowspace:
  1738   fixes A :: "real^'n^'m"
  1739   shows "A *v x = 0 \<and> x \<in> span(rows A) \<longleftrightarrow> x = 0"
  1740   using orthogonal_nullspace_rowspace orthogonal_self by auto
  1741 
  1742 lemma matrix_vector_mul_injective_on_rowspace:
  1743   fixes A :: "real^'n^'m"
  1744   shows "\<lbrakk>A *v x = A *v y; x \<in> span(rows A); y \<in> span(rows A)\<rbrakk> \<Longrightarrow> x = y"
  1745   using nullspace_inter_rowspace [of A "x-y"]
  1746   by (metis eq_iff_diff_eq_0 matrix_vector_mult_diff_distrib span_diff)
  1747 
  1748 definition rank :: "real^'n^'m=>nat"
  1749   where "rank A \<equiv> dim(columns A)"
  1750 
  1751 lemma dim_rows_le_dim_columns:
  1752   fixes A :: "real^'n^'m"
  1753   shows "dim(rows A) \<le> dim(columns A)"
  1754 proof -
  1755   have "dim (span (rows A)) \<le> dim (span (columns A))"
  1756   proof -
  1757     obtain B where "independent B" "span(rows A) \<subseteq> span B"
  1758               and B: "B \<subseteq> span(rows A)""card B = dim (span(rows A))"
  1759       using basis_exists [of "span(rows A)"] by blast
  1760     with span_subspace have eq: "span B = span(rows A)"
  1761       by auto
  1762     then have inj: "inj_on (( *v) A) (span B)"
  1763       using inj_on_def matrix_vector_mul_injective_on_rowspace by blast
  1764     then have ind: "independent (( *v) A ` B)"
  1765       by (rule independent_inj_on_image [OF \<open>independent B\<close> matrix_vector_mul_linear])
  1766     then have "finite (( *v) A ` B) \<and> card (( *v) A ` B) \<le> dim (( *v) A ` B)"
  1767       by (rule independent_bound_general)
  1768     then show ?thesis
  1769       by (metis (no_types, lifting) B ind inj eq card_image image_subset_iff independent_card_le_dim inj_on_subset matrix_vector_mult_in_columnspace)
  1770   qed
  1771   then show ?thesis
  1772     by simp
  1773 qed
  1774 
  1775 lemma rank_row:
  1776   fixes A :: "real^'n^'m"
  1777   shows "rank A = dim(rows A)"
  1778   unfolding rank_def
  1779   by (metis dim_rows_le_dim_columns columns_transpose dual_order.antisym rows_transpose)
  1780 
  1781 lemma rank_transpose:
  1782   fixes A :: "real^'n^'m"
  1783   shows "rank(transpose A) = rank A"
  1784   by (metis rank_def rank_row rows_transpose)
  1785 
  1786 lemma matrix_vector_mult_basis:
  1787   fixes A :: "real^'n^'m"
  1788   shows "A *v (axis k 1) = column k A"
  1789   by (simp add: cart_eq_inner_axis column_def matrix_mult_dot)
  1790 
  1791 lemma columns_image_basis:
  1792   fixes A :: "real^'n^'m"
  1793   shows "columns A = ( *v) A ` (range (\<lambda>i. axis i 1))"
  1794   by (force simp: columns_def matrix_vector_mult_basis [symmetric])
  1795 
  1796 lemma rank_dim_range:
  1797   fixes A :: "real^'n^'m"
  1798   shows "rank A = dim(range (\<lambda>x. A *v x))"
  1799   unfolding rank_def
  1800 proof (rule span_eq_dim)
  1801   show "span (columns A) = span (range (( *v) A))"
  1802     apply (auto simp: columns_image_basis span_linear_image matrix_vector_mul_linear)
  1803     by (metis columns_image_basis matrix_vector_mul_linear matrix_vector_mult_in_columnspace span_linear_image)
  1804 qed
  1805 
  1806 lemma rank_bound:
  1807   fixes A :: "real^'n^'m"
  1808   shows "rank A \<le> min CARD('m) (CARD('n))"
  1809   by (metis (mono_tags, hide_lams) min.bounded_iff DIM_cart DIM_real dim_subset_UNIV mult.right_neutral rank_def rank_transpose)
  1810 
  1811 lemma full_rank_injective:
  1812   fixes A :: "real^'n^'m"
  1813   shows "rank A = CARD('n) \<longleftrightarrow> inj (( *v) A)"
  1814   by (simp add: matrix_left_invertible_injective [symmetric] matrix_left_invertible_span_rows rank_row dim_eq_full [symmetric])
  1815 
  1816 lemma full_rank_surjective:
  1817   fixes A :: "real^'n^'m"
  1818   shows "rank A = CARD('m) \<longleftrightarrow> surj (( *v) A)"
  1819   by (simp add: matrix_right_invertible_surjective [symmetric] left_invertible_transpose [symmetric]
  1820                 matrix_left_invertible_injective full_rank_injective [symmetric] rank_transpose)
  1821 
  1822 lemma rank_I: "rank(mat 1::real^'n^'n) = CARD('n)"
  1823   by (simp add: full_rank_injective inj_on_def)
  1824 
  1825 lemma less_rank_noninjective:
  1826   fixes A :: "real^'n^'m"
  1827   shows "rank A < CARD('n) \<longleftrightarrow> \<not> inj (( *v) A)"
  1828 using less_le rank_bound by (auto simp: full_rank_injective [symmetric])
  1829 
  1830 lemma matrix_nonfull_linear_equations_eq:
  1831   fixes A :: "real^'n^'m"
  1832   shows "(\<exists>x. (x \<noteq> 0) \<and> A *v x = 0) \<longleftrightarrow> ~(rank A = CARD('n))"
  1833   by (meson matrix_left_invertible_injective full_rank_injective matrix_left_invertible_ker)
  1834 
  1835 lemma rank_eq_0: "rank A = 0 \<longleftrightarrow> A = 0" and rank_0 [simp]: "rank 0 = 0"
  1836   by (auto simp: rank_dim_range matrix_eq)
  1837 
  1838 
  1839 lemma rank_mul_le_right:
  1840   fixes A :: "real^'n^'m" and B :: "real^'p^'n"
  1841   shows "rank(A ** B) \<le> rank B"
  1842 proof -
  1843   have "rank(A ** B) \<le> dim (( *v) A ` range (( *v) B))"
  1844     by (auto simp: rank_dim_range image_comp o_def matrix_vector_mul_assoc)
  1845   also have "\<dots> \<le> rank B"
  1846     by (simp add: rank_dim_range matrix_vector_mul_linear dim_image_le)
  1847   finally show ?thesis .
  1848 qed
  1849 
  1850 lemma rank_mul_le_left:
  1851   fixes A :: "real^'n^'m" and B :: "real^'p^'n"
  1852   shows "rank(A ** B) \<le> rank A"
  1853   by (metis matrix_transpose_mul rank_mul_le_right rank_transpose)
  1854 
  1855 subsection\<open>Routine results connecting the types @{typ "real^1"} and @{typ real}\<close>
  1856 
  1857 lemma vector_one_nth [simp]:
  1858   fixes x :: "'a^1" shows "vec (x $ 1) = x"
  1859   by (metis vec_def vector_one)
  1860 
  1861 lemma vec_cbox_1_eq [simp]:
  1862   shows "vec ` cbox u v = cbox (vec u) (vec v ::real^1)"
  1863   by (force simp: Basis_vec_def cart_eq_inner_axis [symmetric] mem_box)
  1864 
  1865 lemma vec_nth_cbox_1_eq [simp]:
  1866   fixes u v :: "'a::euclidean_space^1"
  1867   shows "(\<lambda>x. x $ 1) ` cbox u v = cbox (u$1) (v$1)"
  1868     by (auto simp: Basis_vec_def cart_eq_inner_axis [symmetric] mem_box image_iff Bex_def inner_axis) (metis vec_component)
  1869 
  1870 lemma vec_nth_1_iff_cbox [simp]:
  1871   fixes a b :: "'a::euclidean_space"
  1872   shows "(\<lambda>x::'a^1. x $ 1) ` S = cbox a b \<longleftrightarrow> S = cbox (vec a) (vec b)"
  1873     (is "?lhs = ?rhs")
  1874 proof
  1875   assume L: ?lhs show ?rhs
  1876   proof (intro equalityI subsetI)
  1877     fix x 
  1878     assume "x \<in> S"
  1879     then have "x $ 1 \<in> (\<lambda>v. v $ (1::1)) ` cbox (vec a) (vec b)"
  1880       using L by auto
  1881     then show "x \<in> cbox (vec a) (vec b)"
  1882       by (metis (no_types, lifting) imageE vector_one_nth)
  1883   next
  1884     fix x :: "'a^1"
  1885     assume "x \<in> cbox (vec a) (vec b)"
  1886     then show "x \<in> S"
  1887       by (metis (no_types, lifting) L imageE imageI vec_component vec_nth_cbox_1_eq vector_one_nth)
  1888   qed
  1889 qed simp
  1890 
  1891 lemma tendsto_at_within_vector_1:
  1892   fixes S :: "'a :: metric_space set"
  1893   assumes "(f \<longlongrightarrow> fx) (at x within S)"
  1894   shows "((\<lambda>y::'a^1. \<chi> i. f (y $ 1)) \<longlongrightarrow> (vec fx::'a^1)) (at (vec x) within vec ` S)"
  1895 proof (rule topological_tendstoI)
  1896   fix T :: "('a^1) set"
  1897   assume "open T" "vec fx \<in> T"
  1898   have "\<forall>\<^sub>F x in at x within S. f x \<in> (\<lambda>x. x $ 1) ` T"
  1899     using \<open>open T\<close> \<open>vec fx \<in> T\<close> assms open_image_vec_nth tendsto_def by fastforce
  1900   then show "\<forall>\<^sub>F x::'a^1 in at (vec x) within vec ` S. (\<chi> i. f (x $ 1)) \<in> T"
  1901     unfolding eventually_at dist_norm [symmetric]
  1902     by (rule ex_forward)
  1903        (use \<open>open T\<close> in 
  1904          \<open>fastforce simp: dist_norm dist_vec_def L2_set_def image_iff vector_one open_vec_def\<close>)
  1905 qed
  1906 
  1907 lemma has_derivative_vector_1:
  1908   assumes der_g: "(g has_derivative (\<lambda>x. x * g' a)) (at a within S)"
  1909   shows "((\<lambda>x. vec (g (x $ 1))) has_derivative ( *\<^sub>R) (g' a))
  1910          (at ((vec a)::real^1) within vec ` S)"
  1911     using der_g
  1912     apply (auto simp: Deriv.has_derivative_within bounded_linear_scaleR_right norm_vector_1)
  1913     apply (drule tendsto_at_within_vector_1, vector)
  1914     apply (auto simp: algebra_simps eventually_at tendsto_def)
  1915     done
  1916 
  1917 
  1918 subsection\<open>Explicit vector construction from lists\<close>
  1919 
  1920 definition "vector l = (\<chi> i. foldr (\<lambda>x f n. fun_upd (f (n+1)) n x) l (\<lambda>n x. 0) 1 i)"
  1921 
  1922 lemma vector_1 [simp]: "(vector[x]) $1 = x"
  1923   unfolding vector_def by simp
  1924 
  1925 lemma vector_2 [simp]: "(vector[x,y]) $1 = x" "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
  1926   unfolding vector_def by simp_all
  1927 
  1928 lemma vector_3 [simp]:
  1929  "(vector [x,y,z] ::('a::zero)^3)$1 = x"
  1930  "(vector [x,y,z] ::('a::zero)^3)$2 = y"
  1931  "(vector [x,y,z] ::('a::zero)^3)$3 = z"
  1932   unfolding vector_def by simp_all
  1933 
  1934 lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
  1935   by (metis vector_1 vector_one)
  1936 
  1937 lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
  1938   apply auto
  1939   apply (erule_tac x="v$1" in allE)
  1940   apply (erule_tac x="v$2" in allE)
  1941   apply (subgoal_tac "vector [v$1, v$2] = v")
  1942   apply simp
  1943   apply (vector vector_def)
  1944   apply (simp add: forall_2)
  1945   done
  1946 
  1947 lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
  1948   apply auto
  1949   apply (erule_tac x="v$1" in allE)
  1950   apply (erule_tac x="v$2" in allE)
  1951   apply (erule_tac x="v$3" in allE)
  1952   apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
  1953   apply simp
  1954   apply (vector vector_def)
  1955   apply (simp add: forall_3)
  1956   done
  1957 
  1958 lemma bounded_linear_component_cart[intro]: "bounded_linear (\<lambda>x::real^'n. x $ k)"
  1959   apply (rule bounded_linearI[where K=1])
  1960   using component_le_norm_cart[of _ k] unfolding real_norm_def by auto
  1961 
  1962 lemma interval_split_cart:
  1963   "{a..b::real^'n} \<inter> {x. x$k \<le> c} = {a .. (\<chi> i. if i = k then min (b$k) c else b$i)}"
  1964   "cbox a b \<inter> {x. x$k \<ge> c} = {(\<chi> i. if i = k then max (a$k) c else a$i) .. b}"
  1965   apply (rule_tac[!] set_eqI)
  1966   unfolding Int_iff mem_box_cart mem_Collect_eq interval_cbox_cart
  1967   unfolding vec_lambda_beta
  1968   by auto
  1969 
  1970 lemmas cartesian_euclidean_space_uniform_limit_intros[uniform_limit_intros] =
  1971   bounded_linear.uniform_limit[OF blinfun.bounded_linear_right]
  1972   bounded_linear.uniform_limit[OF bounded_linear_vec_nth]
  1973   bounded_linear.uniform_limit[OF bounded_linear_component_cart]
  1974 
  1975 end