src/HOL/Analysis/Cartesian_Euclidean_Space.thy
author paulson <lp15@cam.ac.uk>
Sun Feb 25 12:54:55 2018 +0000 (17 months ago)
changeset 67719 bffb7482faaa
parent 67686 2c58505bf151
child 67728 d97a28a006f9
permissions -rw-r--r--
new material on matrices, etc., and consolidating duplicate results about of_nat
     1 section \<open>Instantiates the finite Cartesian product of Euclidean spaces as a Euclidean space.\<close>
     2 
     3 theory Cartesian_Euclidean_Space
     4 imports Finite_Cartesian_Product Derivative
     5 begin
     6 
     7 lemma subspace_special_hyperplane: "subspace {x. x $ k = 0}"
     8   by (simp add: subspace_def)
     9 
    10 lemma sum_mult_product:
    11   "sum h {..<A * B :: nat} = (\<Sum>i\<in>{..<A}. \<Sum>j\<in>{..<B}. h (j + i * B))"
    12   unfolding sum_nat_group[of h B A, unfolded atLeast0LessThan, symmetric]
    13 proof (rule sum.cong, simp, rule sum.reindex_cong)
    14   fix i
    15   show "inj_on (\<lambda>j. j + i * B) {..<B}" by (auto intro!: inj_onI)
    16   show "{i * B..<i * B + B} = (\<lambda>j. j + i * B) ` {..<B}"
    17   proof safe
    18     fix j assume "j \<in> {i * B..<i * B + B}"
    19     then show "j \<in> (\<lambda>j. j + i * B) ` {..<B}"
    20       by (auto intro!: image_eqI[of _ _ "j - i * B"])
    21   qed simp
    22 qed simp
    23 
    24 subsection\<open>Basic componentwise operations on vectors.\<close>
    25 
    26 instantiation vec :: (times, finite) times
    27 begin
    28 
    29 definition "( * ) \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
    30 instance ..
    31 
    32 end
    33 
    34 instantiation vec :: (one, finite) one
    35 begin
    36 
    37 definition "1 \<equiv> (\<chi> i. 1)"
    38 instance ..
    39 
    40 end
    41 
    42 instantiation vec :: (ord, finite) ord
    43 begin
    44 
    45 definition "x \<le> y \<longleftrightarrow> (\<forall>i. x$i \<le> y$i)"
    46 definition "x < (y::'a^'b) \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
    47 instance ..
    48 
    49 end
    50 
    51 text\<open>The ordering on one-dimensional vectors is linear.\<close>
    52 
    53 class cart_one =
    54   assumes UNIV_one: "card (UNIV :: 'a set) = Suc 0"
    55 begin
    56 
    57 subclass finite
    58 proof
    59   from UNIV_one show "finite (UNIV :: 'a set)"
    60     by (auto intro!: card_ge_0_finite)
    61 qed
    62 
    63 end
    64 
    65 instance vec:: (order, finite) order
    66   by standard (auto simp: less_eq_vec_def less_vec_def vec_eq_iff
    67       intro: order.trans order.antisym order.strict_implies_order)
    68 
    69 instance vec :: (linorder, cart_one) linorder
    70 proof
    71   obtain a :: 'b where all: "\<And>P. (\<forall>i. P i) \<longleftrightarrow> P a"
    72   proof -
    73     have "card (UNIV :: 'b set) = Suc 0" by (rule UNIV_one)
    74     then obtain b :: 'b where "UNIV = {b}" by (auto iff: card_Suc_eq)
    75     then have "\<And>P. (\<forall>i\<in>UNIV. P i) \<longleftrightarrow> P b" by auto
    76     then show thesis by (auto intro: that)
    77   qed
    78   fix x y :: "'a^'b::cart_one"
    79   note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps
    80   show "x \<le> y \<or> y \<le> x" by auto
    81 qed
    82 
    83 text\<open>Constant Vectors\<close>
    84 
    85 definition "vec x = (\<chi> i. x)"
    86 
    87 lemma interval_cbox_cart: "{a::real^'n..b} = cbox a b"
    88   by (auto simp add: less_eq_vec_def mem_box Basis_vec_def inner_axis)
    89 
    90 text\<open>Also the scalar-vector multiplication.\<close>
    91 
    92 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
    93   where "c *s x = (\<chi> i. c * (x$i))"
    94 
    95 
    96 subsection \<open>A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space.\<close>
    97 
    98 lemma sum_cong_aux:
    99   "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> sum f A = sum g A"
   100   by (auto intro: sum.cong)
   101 
   102 hide_fact (open) sum_cong_aux
   103 
   104 method_setup vector = \<open>
   105 let
   106   val ss1 =
   107     simpset_of (put_simpset HOL_basic_ss @{context}
   108       addsimps [@{thm sum.distrib} RS sym,
   109       @{thm sum_subtractf} RS sym, @{thm sum_distrib_left},
   110       @{thm sum_distrib_right}, @{thm sum_negf} RS sym])
   111   val ss2 =
   112     simpset_of (@{context} addsimps
   113              [@{thm plus_vec_def}, @{thm times_vec_def},
   114               @{thm minus_vec_def}, @{thm uminus_vec_def},
   115               @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def},
   116               @{thm scaleR_vec_def},
   117               @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}])
   118   fun vector_arith_tac ctxt ths =
   119     simp_tac (put_simpset ss1 ctxt)
   120     THEN' (fn i => resolve_tac ctxt @{thms Cartesian_Euclidean_Space.sum_cong_aux} i
   121          ORELSE resolve_tac ctxt @{thms sum.neutral} i
   122          ORELSE simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm vec_eq_iff}]) i)
   123     (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
   124     THEN' asm_full_simp_tac (put_simpset ss2 ctxt addsimps ths)
   125 in
   126   Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (vector_arith_tac ctxt ths))
   127 end
   128 \<close> "lift trivial vector statements to real arith statements"
   129 
   130 lemma vec_0[simp]: "vec 0 = 0" by vector
   131 lemma vec_1[simp]: "vec 1 = 1" by vector
   132 
   133 lemma vec_inj[simp]: "vec x = vec y \<longleftrightarrow> x = y" by vector
   134 
   135 lemma vec_in_image_vec: "vec x \<in> (vec ` S) \<longleftrightarrow> x \<in> S" by auto
   136 
   137 lemma vec_add: "vec(x + y) = vec x + vec y"  by vector
   138 lemma vec_sub: "vec(x - y) = vec x - vec y" by vector
   139 lemma vec_cmul: "vec(c * x) = c *s vec x " by vector
   140 lemma vec_neg: "vec(- x) = - vec x " by vector
   141 
   142 lemma vec_sum:
   143   assumes "finite S"
   144   shows "vec(sum f S) = sum (vec \<circ> f) S"
   145   using assms
   146 proof induct
   147   case empty
   148   then show ?case by simp
   149 next
   150   case insert
   151   then show ?case by (auto simp add: vec_add)
   152 qed
   153 
   154 text\<open>Obvious "component-pushing".\<close>
   155 
   156 lemma vec_component [simp]: "vec x $ i = x"
   157   by vector
   158 
   159 lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i"
   160   by vector
   161 
   162 lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)"
   163   by vector
   164 
   165 lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   166 
   167 lemmas vector_component =
   168   vec_component vector_add_component vector_mult_component
   169   vector_smult_component vector_minus_component vector_uminus_component
   170   vector_scaleR_component cond_component
   171 
   172 
   173 subsection \<open>Some frequently useful arithmetic lemmas over vectors.\<close>
   174 
   175 instance vec :: (semigroup_mult, finite) semigroup_mult
   176   by standard (vector mult.assoc)
   177 
   178 instance vec :: (monoid_mult, finite) monoid_mult
   179   by standard vector+
   180 
   181 instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
   182   by standard (vector mult.commute)
   183 
   184 instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
   185   by standard vector
   186 
   187 instance vec :: (semiring, finite) semiring
   188   by standard (vector field_simps)+
   189 
   190 instance vec :: (semiring_0, finite) semiring_0
   191   by standard (vector field_simps)+
   192 instance vec :: (semiring_1, finite) semiring_1
   193   by standard vector
   194 instance vec :: (comm_semiring, finite) comm_semiring
   195   by standard (vector field_simps)+
   196 
   197 instance vec :: (comm_semiring_0, finite) comm_semiring_0 ..
   198 instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   199 instance vec :: (semiring_0_cancel, finite) semiring_0_cancel ..
   200 instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel ..
   201 instance vec :: (ring, finite) ring ..
   202 instance vec :: (semiring_1_cancel, finite) semiring_1_cancel ..
   203 instance vec :: (comm_semiring_1, finite) comm_semiring_1 ..
   204 
   205 instance vec :: (ring_1, finite) ring_1 ..
   206 
   207 instance vec :: (real_algebra, finite) real_algebra
   208   by standard (simp_all add: vec_eq_iff)
   209 
   210 instance vec :: (real_algebra_1, finite) real_algebra_1 ..
   211 
   212 lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   213 proof (induct n)
   214   case 0
   215   then show ?case by vector
   216 next
   217   case Suc
   218   then show ?case by vector
   219 qed
   220 
   221 lemma one_index [simp]: "(1 :: 'a :: one ^ 'n) $ i = 1"
   222   by vector
   223 
   224 lemma neg_one_index [simp]: "(- 1 :: 'a :: {one, uminus} ^ 'n) $ i = - 1"
   225   by vector
   226 
   227 instance vec :: (semiring_char_0, finite) semiring_char_0
   228 proof
   229   fix m n :: nat
   230   show "inj (of_nat :: nat \<Rightarrow> 'a ^ 'b)"
   231     by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
   232 qed
   233 
   234 instance vec :: (numeral, finite) numeral ..
   235 instance vec :: (semiring_numeral, finite) semiring_numeral ..
   236 
   237 lemma numeral_index [simp]: "numeral w $ i = numeral w"
   238   by (induct w) (simp_all only: numeral.simps vector_add_component one_index)
   239 
   240 lemma neg_numeral_index [simp]: "- numeral w $ i = - numeral w"
   241   by (simp only: vector_uminus_component numeral_index)
   242 
   243 instance vec :: (comm_ring_1, finite) comm_ring_1 ..
   244 instance vec :: (ring_char_0, finite) ring_char_0 ..
   245 
   246 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
   247   by (vector mult.assoc)
   248 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
   249   by (vector field_simps)
   250 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
   251   by (vector field_simps)
   252 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
   253 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
   254 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
   255   by (vector field_simps)
   256 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   257 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   258 lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
   259 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   260 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
   261   by (vector field_simps)
   262 
   263 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
   264   by (simp add: vec_eq_iff)
   265 
   266 lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n)" by (metis norm_eq_zero)
   267 
   268 lemma norm_axis_1 [simp]: "norm (axis m (1::real)) = 1"
   269   by (simp add: inner_axis' norm_eq_1)
   270 
   271 lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   272   by vector
   273 
   274 lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
   275   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
   276 
   277 lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
   278   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
   279 
   280 lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
   281   by (metis vector_mul_lcancel)
   282 
   283 lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
   284   by (metis vector_mul_rcancel)
   285 
   286 lemma component_le_norm_cart: "\<bar>x$i\<bar> <= norm x"
   287   apply (simp add: norm_vec_def)
   288   apply (rule member_le_L2_set, simp_all)
   289   done
   290 
   291 lemma norm_bound_component_le_cart: "norm x <= e ==> \<bar>x$i\<bar> <= e"
   292   by (metis component_le_norm_cart order_trans)
   293 
   294 lemma norm_bound_component_lt_cart: "norm x < e ==> \<bar>x$i\<bar> < e"
   295   by (metis component_le_norm_cart le_less_trans)
   296 
   297 lemma norm_le_l1_cart: "norm x <= sum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
   298   by (simp add: norm_vec_def L2_set_le_sum)
   299 
   300 lemma scalar_mult_eq_scaleR: "c *s x = c *\<^sub>R x"
   301   unfolding scaleR_vec_def vector_scalar_mult_def by simp
   302 
   303 lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
   304   unfolding dist_norm scalar_mult_eq_scaleR
   305   unfolding scaleR_right_diff_distrib[symmetric] by simp
   306 
   307 lemma sum_component [simp]:
   308   fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
   309   shows "(sum f S)$i = sum (\<lambda>x. (f x)$i) S"
   310 proof (cases "finite S")
   311   case True
   312   then show ?thesis by induct simp_all
   313 next
   314   case False
   315   then show ?thesis by simp
   316 qed
   317 
   318 lemma sum_eq: "sum f S = (\<chi> i. sum (\<lambda>x. (f x)$i ) S)"
   319   by (simp add: vec_eq_iff)
   320 
   321 lemma sum_cmul:
   322   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
   323   shows "sum (\<lambda>x. c *s f x) S = c *s sum f S"
   324   by (simp add: vec_eq_iff sum_distrib_left)
   325 
   326 lemma sum_norm_allsubsets_bound_cart:
   327   fixes f:: "'a \<Rightarrow> real ^'n"
   328   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (sum f Q) \<le> e"
   329   shows "sum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
   330   using sum_norm_allsubsets_bound[OF assms]
   331   by simp
   332 
   333 subsection\<open>Closures and interiors of halfspaces\<close>
   334 
   335 lemma interior_halfspace_le [simp]:
   336   assumes "a \<noteq> 0"
   337     shows "interior {x. a \<bullet> x \<le> b} = {x. a \<bullet> x < b}"
   338 proof -
   339   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
   340   proof -
   341     obtain e where "e>0" and e: "cball x e \<subseteq> S"
   342       using \<open>open S\<close> open_contains_cball x by blast
   343     then have "x + (e / norm a) *\<^sub>R a \<in> cball x e"
   344       by (simp add: dist_norm)
   345     then have "x + (e / norm a) *\<^sub>R a \<in> S"
   346       using e by blast
   347     then have "x + (e / norm a) *\<^sub>R a \<in> {x. a \<bullet> x \<le> b}"
   348       using S by blast
   349     moreover have "e * (a \<bullet> a) / norm a > 0"
   350       by (simp add: \<open>0 < e\<close> assms)
   351     ultimately show ?thesis
   352       by (simp add: algebra_simps)
   353   qed
   354   show ?thesis
   355     by (rule interior_unique) (auto simp: open_halfspace_lt *)
   356 qed
   357 
   358 lemma interior_halfspace_ge [simp]:
   359    "a \<noteq> 0 \<Longrightarrow> interior {x. a \<bullet> x \<ge> b} = {x. a \<bullet> x > b}"
   360 using interior_halfspace_le [of "-a" "-b"] by simp
   361 
   362 lemma interior_halfspace_component_le [simp]:
   363      "interior {x. x$k \<le> a} = {x :: (real,'n::finite) vec. x$k < a}" (is "?LE")
   364   and interior_halfspace_component_ge [simp]:
   365      "interior {x. x$k \<ge> a} = {x :: (real,'n::finite) vec. x$k > a}" (is "?GE")
   366 proof -
   367   have "axis k (1::real) \<noteq> 0"
   368     by (simp add: axis_def vec_eq_iff)
   369   moreover have "axis k (1::real) \<bullet> x = x$k" for x
   370     by (simp add: cart_eq_inner_axis inner_commute)
   371   ultimately show ?LE ?GE
   372     using interior_halfspace_le [of "axis k (1::real)" a]
   373           interior_halfspace_ge [of "axis k (1::real)" a] by auto
   374 qed
   375 
   376 lemma closure_halfspace_lt [simp]:
   377   assumes "a \<noteq> 0"
   378     shows "closure {x. a \<bullet> x < b} = {x. a \<bullet> x \<le> b}"
   379 proof -
   380   have [simp]: "-{x. a \<bullet> x < b} = {x. a \<bullet> x \<ge> b}"
   381     by (force simp:)
   382   then show ?thesis
   383     using interior_halfspace_ge [of a b] assms
   384     by (force simp: closure_interior)
   385 qed
   386 
   387 lemma closure_halfspace_gt [simp]:
   388    "a \<noteq> 0 \<Longrightarrow> closure {x. a \<bullet> x > b} = {x. a \<bullet> x \<ge> b}"
   389 using closure_halfspace_lt [of "-a" "-b"] by simp
   390 
   391 lemma closure_halfspace_component_lt [simp]:
   392      "closure {x. x$k < a} = {x :: (real,'n::finite) vec. x$k \<le> a}" (is "?LE")
   393   and closure_halfspace_component_gt [simp]:
   394      "closure {x. x$k > a} = {x :: (real,'n::finite) vec. x$k \<ge> 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 closure_halfspace_lt [of "axis k (1::real)" a]
   402           closure_halfspace_gt [of "axis k (1::real)" a] by auto
   403 qed
   404 
   405 lemma interior_hyperplane [simp]:
   406   assumes "a \<noteq> 0"
   407     shows "interior {x. a \<bullet> x = b} = {}"
   408 proof -
   409   have [simp]: "{x. a \<bullet> x = b} = {x. a \<bullet> x \<le> b} \<inter> {x. a \<bullet> x \<ge> b}"
   410     by (force simp:)
   411   then show ?thesis
   412     by (auto simp: assms)
   413 qed
   414 
   415 lemma frontier_halfspace_le:
   416   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   417     shows "frontier {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
   418 proof (cases "a = 0")
   419   case True with assms show ?thesis by simp
   420 next
   421   case False then show ?thesis
   422     by (force simp: frontier_def closed_halfspace_le)
   423 qed
   424 
   425 lemma frontier_halfspace_ge:
   426   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   427     shows "frontier {x. a \<bullet> x \<ge> b} = {x. a \<bullet> x = b}"
   428 proof (cases "a = 0")
   429   case True with assms show ?thesis by simp
   430 next
   431   case False then show ?thesis
   432     by (force simp: frontier_def closed_halfspace_ge)
   433 qed
   434 
   435 lemma frontier_halfspace_lt:
   436   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   437     shows "frontier {x. a \<bullet> x < b} = {x. a \<bullet> x = b}"
   438 proof (cases "a = 0")
   439   case True with assms show ?thesis by simp
   440 next
   441   case False then show ?thesis
   442     by (force simp: frontier_def interior_open open_halfspace_lt)
   443 qed
   444 
   445 lemma frontier_halfspace_gt:
   446   assumes "a \<noteq> 0 \<or> b \<noteq> 0"
   447     shows "frontier {x. a \<bullet> x > b} = {x. a \<bullet> x = b}"
   448 proof (cases "a = 0")
   449   case True with assms show ?thesis by simp
   450 next
   451   case False then show ?thesis
   452     by (force simp: frontier_def interior_open open_halfspace_gt)
   453 qed
   454 
   455 lemma interior_standard_hyperplane:
   456    "interior {x :: (real,'n::finite) vec. x$k = a} = {}"
   457 proof -
   458   have "axis k (1::real) \<noteq> 0"
   459     by (simp add: axis_def vec_eq_iff)
   460   moreover have "axis k (1::real) \<bullet> x = x$k" for x
   461     by (simp add: cart_eq_inner_axis inner_commute)
   462   ultimately show ?thesis
   463     using interior_hyperplane [of "axis k (1::real)" a]
   464     by force
   465 qed
   466 
   467 subsection \<open>Matrix operations\<close>
   468 
   469 text\<open>Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"}\<close>
   470 
   471 definition matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"
   472     (infixl "**" 70)
   473   where "m ** m' == (\<chi> i j. sum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
   474 
   475 definition matrix_vector_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"
   476     (infixl "*v" 70)
   477   where "m *v x \<equiv> (\<chi> i. sum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
   478 
   479 definition vector_matrix_mult :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "
   480     (infixl "v*" 70)
   481   where "v v* m == (\<chi> j. sum (\<lambda>i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n"
   482 
   483 definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
   484 definition transpose where
   485   "(transpose::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
   486 definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
   487 definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
   488 definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
   489 definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
   490 
   491 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
   492 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)"
   493   by (vector matrix_matrix_mult_def sum.distrib[symmetric] field_simps)
   494 
   495 lemma matrix_mul_lid [simp]:
   496   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   497   shows "mat 1 ** A = A"
   498   apply (simp add: matrix_matrix_mult_def mat_def)
   499   apply vector
   500   apply (auto simp only: if_distrib cond_application_beta sum.delta'[OF finite]
   501     mult_1_left mult_zero_left if_True UNIV_I)
   502   done
   503 
   504 
   505 lemma matrix_mul_rid [simp]:
   506   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   507   shows "A ** mat 1 = A"
   508   apply (simp add: matrix_matrix_mult_def mat_def)
   509   apply vector
   510   apply (auto simp only: if_distrib cond_application_beta sum.delta[OF finite]
   511     mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
   512   done
   513 
   514 lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
   515   apply (vector matrix_matrix_mult_def sum_distrib_left sum_distrib_right mult.assoc)
   516   apply (subst sum.swap)
   517   apply simp
   518   done
   519 
   520 lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
   521   apply (vector matrix_matrix_mult_def matrix_vector_mult_def
   522     sum_distrib_left sum_distrib_right mult.assoc)
   523   apply (subst sum.swap)
   524   apply simp
   525   done
   526 
   527 lemma matrix_vector_mul_lid [simp]: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)"
   528   apply (vector matrix_vector_mult_def mat_def)
   529   apply (simp add: if_distrib cond_application_beta sum.delta' cong del: if_weak_cong)
   530   done
   531 
   532 lemma matrix_transpose_mul:
   533     "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)"
   534   by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult.commute)
   535 
   536 lemma matrix_eq:
   537   fixes A B :: "'a::semiring_1 ^ 'n ^ 'm"
   538   shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
   539   apply auto
   540   apply (subst vec_eq_iff)
   541   apply clarify
   542   apply (clarsimp simp add: matrix_vector_mult_def if_distrib cond_application_beta vec_eq_iff cong del: if_weak_cong)
   543   apply (erule_tac x="axis ia 1" in allE)
   544   apply (erule_tac x="i" in allE)
   545   apply (auto simp add: if_distrib cond_application_beta axis_def
   546     sum.delta[OF finite] cong del: if_weak_cong)
   547   done
   548 
   549 lemma matrix_vector_mul_component: "((A::real^_^_) *v x)$k = (A$k) \<bullet> x"
   550   by (simp add: matrix_vector_mult_def inner_vec_def)
   551 
   552 lemma dot_lmul_matrix: "((x::real ^_) v* A) \<bullet> y = x \<bullet> (A *v y)"
   553   apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def sum_distrib_right sum_distrib_left ac_simps)
   554   apply (subst sum.swap)
   555   apply simp
   556   done
   557 
   558 lemma transpose_mat [simp]: "transpose (mat n) = mat n"
   559   by (vector transpose_def mat_def)
   560 
   561 lemma transpose_transpose [simp]: "transpose(transpose A) = A"
   562   by (vector transpose_def)
   563 
   564 lemma row_transpose [simp]:
   565   fixes A:: "'a::semiring_1^_^_"
   566   shows "row i (transpose A) = column i A"
   567   by (simp add: row_def column_def transpose_def vec_eq_iff)
   568 
   569 lemma column_transpose [simp]:
   570   fixes A:: "'a::semiring_1^_^_"
   571   shows "column i (transpose A) = row i A"
   572   by (simp add: row_def column_def transpose_def vec_eq_iff)
   573 
   574 lemma rows_transpose [simp]: "rows(transpose (A::'a::semiring_1^_^_)) = columns A"
   575   by (auto simp add: rows_def columns_def row_transpose intro: set_eqI)
   576 
   577 lemma columns_transpose [simp]: "columns(transpose (A::'a::semiring_1^_^_)) = rows A"
   578   by (metis transpose_transpose rows_transpose)
   579 
   580 lemma matrix_mult_transpose_dot_column:
   581   fixes A :: "real^'n^'n"
   582   shows "transpose A ** A = (\<chi> i j. (column i A) \<bullet> (column j A))"
   583   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def column_def inner_vec_def)
   584 
   585 lemma matrix_mult_transpose_dot_row:
   586   fixes A :: "real^'n^'n"
   587   shows "A ** transpose A = (\<chi> i j. (row i A) \<bullet> (row j A))"
   588   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def row_def inner_vec_def)
   589 
   590 text\<open>Two sometimes fruitful ways of looking at matrix-vector multiplication.\<close>
   591 
   592 lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
   593   by (simp add: matrix_vector_mult_def inner_vec_def)
   594 
   595 lemma matrix_mult_sum:
   596   "(A::'a::comm_semiring_1^'n^'m) *v x = sum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
   597   by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult.commute)
   598 
   599 lemma vector_componentwise:
   600   "(x::'a::ring_1^'n) = (\<chi> j. \<Sum>i\<in>UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)"
   601   by (simp add: axis_def if_distrib sum.If_cases vec_eq_iff)
   602 
   603 lemma basis_expansion: "sum (\<lambda>i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)"
   604   by (auto simp add: axis_def vec_eq_iff if_distrib sum.If_cases cong del: if_weak_cong)
   605 
   606 lemma linear_componentwise_expansion:
   607   fixes f:: "real ^'m \<Rightarrow> real ^ _"
   608   assumes lf: "linear f"
   609   shows "(f x)$j = sum (\<lambda>i. (x$i) * (f (axis i 1)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
   610 proof -
   611   let ?M = "(UNIV :: 'm set)"
   612   let ?N = "(UNIV :: 'n set)"
   613   have "?rhs = (sum (\<lambda>i.(x$i) *\<^sub>R f (axis i 1) ) ?M)$j"
   614     unfolding sum_component by simp
   615   then show ?thesis
   616     unfolding linear_sum_mul[OF lf, symmetric]
   617     unfolding scalar_mult_eq_scaleR[symmetric]
   618     unfolding basis_expansion
   619     by simp
   620 qed
   621 
   622 subsection\<open>Inverse matrices  (not necessarily square)\<close>
   623 
   624 definition
   625   "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   626 
   627 definition
   628   "matrix_inv(A:: 'a::semiring_1^'n^'m) =
   629     (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   630 
   631 text\<open>Correspondence between matrices and linear operators.\<close>
   632 
   633 definition matrix :: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
   634   where "matrix f = (\<chi> i j. (f(axis j 1))$i)"
   635 
   636 lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::real ^ _))"
   637   by (simp add: linear_iff matrix_vector_mult_def vec_eq_iff
   638       field_simps sum_distrib_left sum.distrib)
   639 
   640 lemma
   641   fixes A :: "real^'n^'m"
   642   shows matrix_vector_mult_linear_continuous_at [continuous_intros]: "isCont (( *v) A) z"
   643     and matrix_vector_mult_linear_continuous_on [continuous_intros]: "continuous_on S (( *v) A)"
   644   by (simp_all add: linear_linear linear_continuous_at linear_continuous_on matrix_vector_mul_linear)
   645 
   646 lemma matrix_vector_mult_add_distrib [algebra_simps]:
   647   fixes A :: "real^'n^'m"
   648   shows "A *v (x + y) = A *v x + A *v y"
   649   using matrix_vector_mul_linear [of A]  by (simp add: linear_add)
   650 
   651 lemma matrix_vector_mult_diff_distrib [algebra_simps]:
   652   fixes A :: "real^'n^'m"
   653   shows "A *v (x - y) = A *v x - A *v y"
   654   using matrix_vector_mul_linear [of A]  by (simp add: linear_diff)
   655 
   656 lemma matrix_vector_mult_scaleR[algebra_simps]:
   657   fixes A :: "real^'n^'m"
   658   shows "A *v (c *\<^sub>R x) = c *\<^sub>R (A *v x)"
   659   using linear_iff matrix_vector_mul_linear by blast
   660 
   661 lemma matrix_vector_mult_0_right [simp]: "A *v 0 = 0"
   662   by (simp add: matrix_vector_mult_def vec_eq_iff)
   663 
   664 lemma matrix_vector_mult_0 [simp]: "0 *v w = 0"
   665   by (simp add: matrix_vector_mult_def vec_eq_iff)
   666 
   667 lemma matrix_vector_mult_add_rdistrib [algebra_simps]:
   668   fixes A :: "real^'n^'m"
   669   shows "(A + B) *v x = (A *v x) + (B *v x)"
   670   by (simp add: vec_eq_iff inner_add_left matrix_vector_mul_component)
   671 
   672 lemma matrix_vector_mult_diff_rdistrib [algebra_simps]:
   673   fixes A :: "real^'n^'m"
   674   shows "(A - B) *v x = (A *v x) - (B *v x)"
   675   by (simp add: vec_eq_iff inner_diff_left matrix_vector_mul_component)
   676 
   677 lemma matrix_works:
   678   assumes lf: "linear f"
   679   shows "matrix f *v x = f (x::real ^ 'n)"
   680   apply (simp add: matrix_def matrix_vector_mult_def vec_eq_iff mult.commute)
   681   by (simp add: linear_componentwise_expansion lf)
   682 
   683 lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::real ^ 'n))"
   684   by (simp add: ext matrix_works)
   685 
   686 declare matrix_vector_mul [symmetric, simp]
   687 
   688 lemma matrix_of_matrix_vector_mul [simp]: "matrix(\<lambda>x. A *v (x :: real ^ 'n)) = A"
   689   by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
   690 
   691 lemma matrix_compose:
   692   assumes lf: "linear (f::real^'n \<Rightarrow> real^'m)"
   693     and lg: "linear (g::real^'m \<Rightarrow> real^_)"
   694   shows "matrix (g \<circ> f) = matrix g ** matrix f"
   695   using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
   696   by (simp add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
   697 
   698 lemma matrix_vector_column:
   699   "(A::'a::comm_semiring_1^'n^_) *v x = sum (\<lambda>i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)"
   700   by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult.commute)
   701 
   702 lemma adjoint_matrix: "adjoint(\<lambda>x. (A::real^'n^'m) *v x) = (\<lambda>x. transpose A *v x)"
   703   apply (rule adjoint_unique)
   704   apply (simp add: transpose_def inner_vec_def matrix_vector_mult_def
   705     sum_distrib_right sum_distrib_left)
   706   apply (subst sum.swap)
   707   apply (auto simp add: ac_simps)
   708   done
   709 
   710 lemma matrix_adjoint: assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'m)"
   711   shows "matrix(adjoint f) = transpose(matrix f)"
   712   apply (subst matrix_vector_mul[OF lf])
   713   unfolding adjoint_matrix matrix_of_matrix_vector_mul
   714   apply rule
   715   done
   716 
   717 
   718 subsection\<open>Some bounds on components etc. relative to operator norm.\<close>
   719 
   720 lemma norm_column_le_onorm:
   721   fixes A :: "real^'n^'m"
   722   shows "norm(column i A) \<le> onorm(( *v) A)"
   723 proof -
   724   have bl: "bounded_linear (( *v) A)"
   725     by (simp add: linear_linear matrix_vector_mul_linear)
   726   have "norm (\<chi> j. A $ j $ i) \<le> norm (A *v axis i 1)"
   727     by (simp add: matrix_mult_dot cart_eq_inner_axis)
   728   also have "\<dots> \<le> onorm (( *v) A)"
   729     using onorm [OF bl, of "axis i 1"] by (auto simp: axis_in_Basis)
   730   finally have "norm (\<chi> j. A $ j $ i) \<le> onorm (( *v) A)" .
   731   then show ?thesis
   732     unfolding column_def .
   733 qed
   734 
   735 lemma matrix_component_le_onorm:
   736   fixes A :: "real^'n^'m"
   737   shows "\<bar>A $ i $ j\<bar> \<le> onorm(( *v) A)"
   738 proof -
   739   have "\<bar>A $ i $ j\<bar> \<le> norm (\<chi> n. (A $ n $ j))"
   740     by (metis (full_types, lifting) component_le_norm_cart vec_lambda_beta)
   741   also have "\<dots> \<le> onorm (( *v) A)"
   742     by (metis (no_types) column_def norm_column_le_onorm)
   743   finally show ?thesis .
   744 qed
   745 
   746 lemma component_le_onorm:
   747   fixes f :: "real^'m \<Rightarrow> real^'n"
   748   shows "linear f \<Longrightarrow> \<bar>matrix f $ i $ j\<bar> \<le> onorm f"
   749   by (metis matrix_component_le_onorm matrix_vector_mul)
   750 
   751 lemma onorm_le_matrix_component_sum:
   752   fixes A :: "real^'n^'m"
   753   shows "onorm(( *v) A) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>)"
   754 proof (rule onorm_le)
   755   fix x
   756   have "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<bar>(A *v x) $ i\<bar>)"
   757     by (rule norm_le_l1_cart)
   758   also have "\<dots> \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)"
   759   proof (rule sum_mono)
   760     fix i
   761     have "\<bar>(A *v x) $ i\<bar> \<le> \<bar>\<Sum>j\<in>UNIV. A $ i $ j * x $ j\<bar>"
   762       by (simp add: matrix_vector_mult_def)
   763     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j * x $ j\<bar>)"
   764       by (rule sum_abs)
   765     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)"
   766       by (rule sum_mono) (simp add: abs_mult component_le_norm_cart mult_left_mono)
   767     finally show "\<bar>(A *v x) $ i\<bar> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar> * norm x)" .
   768   qed
   769   finally show "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>) * norm x"
   770     by (simp add: sum_distrib_right)
   771 qed
   772 
   773 lemma onorm_le_matrix_component:
   774   fixes A :: "real^'n^'m"
   775   assumes "\<And>i j. abs(A$i$j) \<le> B"
   776   shows "onorm(( *v) A) \<le> real (CARD('m)) * real (CARD('n)) * B"
   777 proof (rule onorm_le)
   778   fix x :: "(real, 'n) vec"
   779   have "norm (A *v x) \<le> (\<Sum>i\<in>UNIV. \<bar>(A *v x) $ i\<bar>)"
   780     by (rule norm_le_l1_cart)
   781   also have "\<dots> \<le> (\<Sum>i::'m \<in>UNIV. real (CARD('n)) * B * norm x)"
   782   proof (rule sum_mono)
   783     fix i
   784     have "\<bar>(A *v x) $ i\<bar> \<le> norm(A $ i) * norm x"
   785       by (simp add: matrix_mult_dot Cauchy_Schwarz_ineq2)
   786     also have "\<dots> \<le> (\<Sum>j\<in>UNIV. \<bar>A $ i $ j\<bar>) * norm x"
   787       by (simp add: mult_right_mono norm_le_l1_cart)
   788     also have "\<dots> \<le> real (CARD('n)) * B * norm x"
   789       by (simp add: assms sum_bounded_above mult_right_mono)
   790     finally show "\<bar>(A *v x) $ i\<bar> \<le> real (CARD('n)) * B * norm x" .
   791   qed
   792   also have "\<dots> \<le> CARD('m) * real (CARD('n)) * B * norm x"
   793     by simp
   794   finally show "norm (A *v x) \<le> CARD('m) * real (CARD('n)) * B * norm x" .
   795 qed
   796 
   797 subsection \<open>lambda skolemization on cartesian products\<close>
   798 
   799 lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
   800    (\<exists>x::'a ^ 'n. \<forall>i. P i (x $ i))" (is "?lhs \<longleftrightarrow> ?rhs")
   801 proof -
   802   let ?S = "(UNIV :: 'n set)"
   803   { assume H: "?rhs"
   804     then have ?lhs by auto }
   805   moreover
   806   { assume H: "?lhs"
   807     then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
   808     let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
   809     { fix i
   810       from f have "P i (f i)" by metis
   811       then have "P i (?x $ i)" by auto
   812     }
   813     hence "\<forall>i. P i (?x$i)" by metis
   814     hence ?rhs by metis }
   815   ultimately show ?thesis by metis
   816 qed
   817 
   818 lemma rational_approximation:
   819   assumes "e > 0"
   820   obtains r::real where "r \<in> \<rat>" "\<bar>r - x\<bar> < e"
   821   using Rats_dense_in_real [of "x - e/2" "x + e/2"] assms by auto
   822 
   823 lemma matrix_rational_approximation:
   824   fixes A :: "real^'n^'m"
   825   assumes "e > 0"
   826   obtains B where "\<And>i j. B$i$j \<in> \<rat>" "onorm(\<lambda>x. (A - B) *v x) < e"
   827 proof -
   828   have "\<forall>i j. \<exists>q \<in> \<rat>. \<bar>q - A $ i $ j\<bar> < e / (2 * CARD('m) * CARD('n))"
   829     using assms by (force intro: rational_approximation [of "e / (2 * CARD('m) * CARD('n))"])
   830   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))"
   831     by (auto simp: lambda_skolem Bex_def)
   832   show ?thesis
   833   proof
   834     have "onorm (( *v) (A - B)) \<le> real CARD('m) * real CARD('n) *
   835     (e / (2 * real CARD('m) * real CARD('n)))"
   836       apply (rule onorm_le_matrix_component)
   837       using Bclo by (simp add: abs_minus_commute less_imp_le)
   838     also have "\<dots> < e"
   839       using \<open>0 < e\<close> by (simp add: divide_simps)
   840     finally show "onorm (( *v) (A - B)) < e" .
   841   qed (use B in auto)
   842 qed
   843 
   844 lemma vector_sub_project_orthogonal_cart: "(b::real^'n) \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *s b) = 0"
   845   unfolding inner_simps scalar_mult_eq_scaleR by auto
   846 
   847 lemma left_invertible_transpose:
   848   "(\<exists>(B). B ** transpose (A) = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). A ** B = mat 1)"
   849   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   850 
   851 lemma right_invertible_transpose:
   852   "(\<exists>(B). transpose (A) ** B = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). B ** A = mat 1)"
   853   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   854 
   855 lemma matrix_left_invertible_injective:
   856   "(\<exists>B. (B::real^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
   857 proof -
   858   { fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
   859     from xy have "B*v (A *v x) = B *v (A*v y)" by simp
   860     hence "x = y"
   861       unfolding matrix_vector_mul_assoc B matrix_vector_mul_lid . }
   862   moreover
   863   { assume A: "\<forall>x y. A *v x = A *v y \<longrightarrow> x = y"
   864     hence i: "inj (( *v) A)" unfolding inj_on_def by auto
   865     from linear_injective_left_inverse[OF matrix_vector_mul_linear i]
   866     obtain g where g: "linear g" "g \<circ> ( *v) A = id" by blast
   867     have "matrix g ** A = mat 1"
   868       unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
   869       using g(2) by (simp add: fun_eq_iff)
   870     then have "\<exists>B. (B::real ^'m^'n) ** A = mat 1" by blast }
   871   ultimately show ?thesis by blast
   872 qed
   873 
   874 lemma matrix_left_invertible_ker:
   875   "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
   876   unfolding matrix_left_invertible_injective
   877   using linear_injective_0[OF matrix_vector_mul_linear, of A]
   878   by (simp add: inj_on_def)
   879 
   880 lemma matrix_right_invertible_surjective:
   881   "(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
   882 proof -
   883   { fix B :: "real ^'m^'n"
   884     assume AB: "A ** B = mat 1"
   885     { fix x :: "real ^ 'm"
   886       have "A *v (B *v x) = x"
   887         by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB) }
   888     hence "surj (( *v) A)" unfolding surj_def by metis }
   889   moreover
   890   { assume sf: "surj (( *v) A)"
   891     from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
   892     obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "( *v) A \<circ> g = id"
   893       by blast
   894 
   895     have "A ** (matrix g) = mat 1"
   896       unfolding matrix_eq  matrix_vector_mul_lid
   897         matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
   898       using g(2) unfolding o_def fun_eq_iff id_def
   899       .
   900     hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
   901   }
   902   ultimately show ?thesis unfolding surj_def by blast
   903 qed
   904 
   905 lemma matrix_left_invertible_independent_columns:
   906   fixes A :: "real^'n^'m"
   907   shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow>
   908       (\<forall>c. sum (\<lambda>i. c i *s column i A) (UNIV :: 'n set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
   909     (is "?lhs \<longleftrightarrow> ?rhs")
   910 proof -
   911   let ?U = "UNIV :: 'n set"
   912   { assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
   913     { fix c i
   914       assume c: "sum (\<lambda>i. c i *s column i A) ?U = 0" and i: "i \<in> ?U"
   915       let ?x = "\<chi> i. c i"
   916       have th0:"A *v ?x = 0"
   917         using c
   918         unfolding matrix_mult_sum vec_eq_iff
   919         by auto
   920       from k[rule_format, OF th0] i
   921       have "c i = 0" by (vector vec_eq_iff)}
   922     hence ?rhs by blast }
   923   moreover
   924   { assume H: ?rhs
   925     { fix x assume x: "A *v x = 0"
   926       let ?c = "\<lambda>i. ((x$i ):: real)"
   927       from H[rule_format, of ?c, unfolded matrix_mult_sum[symmetric], OF x]
   928       have "x = 0" by vector }
   929   }
   930   ultimately show ?thesis unfolding matrix_left_invertible_ker by blast
   931 qed
   932 
   933 lemma matrix_right_invertible_independent_rows:
   934   fixes A :: "real^'n^'m"
   935   shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow>
   936     (\<forall>c. sum (\<lambda>i. c i *s row i A) (UNIV :: 'm set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
   937   unfolding left_invertible_transpose[symmetric]
   938     matrix_left_invertible_independent_columns
   939   by (simp add: column_transpose)
   940 
   941 lemma matrix_right_invertible_span_columns:
   942   "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow>
   943     span (columns A) = UNIV" (is "?lhs = ?rhs")
   944 proof -
   945   let ?U = "UNIV :: 'm set"
   946   have fU: "finite ?U" by simp
   947   have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). sum (\<lambda>i. (x$i) *s column i A) ?U = y)"
   948     unfolding matrix_right_invertible_surjective matrix_mult_sum surj_def
   949     apply (subst eq_commute)
   950     apply rule
   951     done
   952   have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
   953   { assume h: ?lhs
   954     { fix x:: "real ^'n"
   955       from h[unfolded lhseq, rule_format, of x] obtain y :: "real ^'m"
   956         where y: "sum (\<lambda>i. (y$i) *s column i A) ?U = x" by blast
   957       have "x \<in> span (columns A)"
   958         unfolding y[symmetric]
   959         apply (rule span_sum)
   960         unfolding scalar_mult_eq_scaleR
   961         apply (rule span_mul)
   962         apply (rule span_superset)
   963         unfolding columns_def
   964         apply blast
   965         done
   966     }
   967     then have ?rhs unfolding rhseq by blast }
   968   moreover
   969   { assume h:?rhs
   970     let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). sum (\<lambda>i. (x$i) *s column i A) ?U = y"
   971     { fix y
   972       have "?P y"
   973       proof (rule span_induct_alt[of ?P "columns A", folded scalar_mult_eq_scaleR])
   974         show "\<exists>x::real ^ 'm. sum (\<lambda>i. (x$i) *s column i A) ?U = 0"
   975           by (rule exI[where x=0], simp)
   976       next
   977         fix c y1 y2
   978         assume y1: "y1 \<in> columns A" and y2: "?P y2"
   979         from y1 obtain i where i: "i \<in> ?U" "y1 = column i A"
   980           unfolding columns_def by blast
   981         from y2 obtain x:: "real ^'m" where
   982           x: "sum (\<lambda>i. (x$i) *s column i A) ?U = y2" by blast
   983         let ?x = "(\<chi> j. if j = i then c + (x$i) else (x$j))::real^'m"
   984         show "?P (c*s y1 + y2)"
   985         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)
   986           fix j
   987           have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j)
   988               else (x$xa) * ((column xa A$j))) = (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))"
   989             using i(1) by (simp add: field_simps)
   990           have "sum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
   991               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"
   992             apply (rule sum.cong[OF refl])
   993             using th apply blast
   994             done
   995           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"
   996             by (simp add: sum.distrib)
   997           also have "\<dots> = c * ((column i A)$j) + sum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
   998             unfolding sum.delta[OF fU]
   999             using i(1) by simp
  1000           finally show "sum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
  1001             else (x$xa) * ((column xa A$j))) ?U = c * ((column i A)$j) + sum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U" .
  1002         qed
  1003       next
  1004         show "y \<in> span (columns A)"
  1005           unfolding h by blast
  1006       qed
  1007     }
  1008     then have ?lhs unfolding lhseq ..
  1009   }
  1010   ultimately show ?thesis by blast
  1011 qed
  1012 
  1013 lemma matrix_left_invertible_span_rows:
  1014   "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
  1015   unfolding right_invertible_transpose[symmetric]
  1016   unfolding columns_transpose[symmetric]
  1017   unfolding matrix_right_invertible_span_columns
  1018   ..
  1019 
  1020 text \<open>The same result in terms of square matrices.\<close>
  1021 
  1022 lemma matrix_left_right_inverse:
  1023   fixes A A' :: "real ^'n^'n"
  1024   shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
  1025 proof -
  1026   { fix A A' :: "real ^'n^'n"
  1027     assume AA': "A ** A' = mat 1"
  1028     have sA: "surj (( *v) A)"
  1029       unfolding surj_def
  1030       apply clarify
  1031       apply (rule_tac x="(A' *v y)" in exI)
  1032       apply (simp add: matrix_vector_mul_assoc AA' matrix_vector_mul_lid)
  1033       done
  1034     from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
  1035     obtain f' :: "real ^'n \<Rightarrow> real ^'n"
  1036       where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
  1037     have th: "matrix f' ** A = mat 1"
  1038       by (simp add: matrix_eq matrix_works[OF f'(1)]
  1039           matrix_vector_mul_assoc[symmetric] matrix_vector_mul_lid f'(2)[rule_format])
  1040     hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
  1041     hence "matrix f' = A'"
  1042       by (simp add: matrix_mul_assoc[symmetric] AA' matrix_mul_rid matrix_mul_lid)
  1043     hence "matrix f' ** A = A' ** A" by simp
  1044     hence "A' ** A = mat 1" by (simp add: th)
  1045   }
  1046   then show ?thesis by blast
  1047 qed
  1048 
  1049 text \<open>Considering an n-element vector as an n-by-1 or 1-by-n matrix.\<close>
  1050 
  1051 definition "rowvector v = (\<chi> i j. (v$j))"
  1052 
  1053 definition "columnvector v = (\<chi> i j. (v$i))"
  1054 
  1055 lemma transpose_columnvector: "transpose(columnvector v) = rowvector v"
  1056   by (simp add: transpose_def rowvector_def columnvector_def vec_eq_iff)
  1057 
  1058 lemma transpose_rowvector: "transpose(rowvector v) = columnvector v"
  1059   by (simp add: transpose_def columnvector_def rowvector_def vec_eq_iff)
  1060 
  1061 lemma dot_rowvector_columnvector: "columnvector (A *v v) = A ** columnvector v"
  1062   by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
  1063 
  1064 lemma dot_matrix_product:
  1065   "(x::real^'n) \<bullet> y = (((rowvector x ::real^'n^1) ** (columnvector y :: real^1^'n))$1)$1"
  1066   by (vector matrix_matrix_mult_def rowvector_def columnvector_def inner_vec_def)
  1067 
  1068 lemma dot_matrix_vector_mul:
  1069   fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
  1070   shows "(A *v x) \<bullet> (B *v y) =
  1071       (((rowvector x :: real^'n^1) ** ((transpose A ** B) ** (columnvector y :: real ^1^'n)))$1)$1"
  1072   unfolding dot_matrix_product transpose_columnvector[symmetric]
  1073     dot_rowvector_columnvector matrix_transpose_mul matrix_mul_assoc ..
  1074 
  1075 lemma infnorm_cart:"infnorm (x::real^'n) = Sup {\<bar>x$i\<bar> |i. i\<in>UNIV}"
  1076   by (simp add: infnorm_def inner_axis Basis_vec_def) (metis (lifting) inner_axis real_inner_1_right)
  1077 
  1078 lemma component_le_infnorm_cart: "\<bar>x$i\<bar> \<le> infnorm (x::real^'n)"
  1079   using Basis_le_infnorm[of "axis i 1" x]
  1080   by (simp add: Basis_vec_def axis_eq_axis inner_axis)
  1081 
  1082 lemma continuous_component[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. f x $ i)"
  1083   unfolding continuous_def by (rule tendsto_vec_nth)
  1084 
  1085 lemma continuous_on_component[continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. f x $ i)"
  1086   unfolding continuous_on_def by (fast intro: tendsto_vec_nth)
  1087 
  1088 lemma continuous_on_vec_lambda[continuous_intros]:
  1089   "(\<And>i. continuous_on S (f i)) \<Longrightarrow> continuous_on S (\<lambda>x. \<chi> i. f i x)"
  1090   unfolding continuous_on_def by (auto intro: tendsto_vec_lambda)
  1091 
  1092 lemma closed_positive_orthant: "closed {x::real^'n. \<forall>i. 0 \<le>x$i}"
  1093   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1094 
  1095 lemma bounded_component_cart: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $ i) ` s)"
  1096   unfolding bounded_def
  1097   apply clarify
  1098   apply (rule_tac x="x $ i" in exI)
  1099   apply (rule_tac x="e" in exI)
  1100   apply clarify
  1101   apply (rule order_trans [OF dist_vec_nth_le], simp)
  1102   done
  1103 
  1104 lemma compact_lemma_cart:
  1105   fixes f :: "nat \<Rightarrow> 'a::heine_borel ^ 'n"
  1106   assumes f: "bounded (range f)"
  1107   shows "\<exists>l r. strict_mono r \<and>
  1108         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
  1109     (is "?th d")
  1110 proof -
  1111   have "\<forall>d' \<subseteq> d. ?th d'"
  1112     by (rule compact_lemma_general[where unproj=vec_lambda])
  1113       (auto intro!: f bounded_component_cart simp: vec_lambda_eta)
  1114   then show "?th d" by simp
  1115 qed
  1116 
  1117 instance vec :: (heine_borel, finite) heine_borel
  1118 proof
  1119   fix f :: "nat \<Rightarrow> 'a ^ 'b"
  1120   assume f: "bounded (range f)"
  1121   then obtain l r where r: "strict_mono r"
  1122       and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $ i) (l $ i) < e) sequentially"
  1123     using compact_lemma_cart [OF f] by blast
  1124   let ?d = "UNIV::'b set"
  1125   { fix e::real assume "e>0"
  1126     hence "0 < e / (real_of_nat (card ?d))"
  1127       using zero_less_card_finite divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
  1128     with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))) sequentially"
  1129       by simp
  1130     moreover
  1131     { fix n
  1132       assume n: "\<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))"
  1133       have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $ i) (l $ i))"
  1134         unfolding dist_vec_def using zero_le_dist by (rule L2_set_le_sum)
  1135       also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
  1136         by (rule sum_strict_mono) (simp_all add: n)
  1137       finally have "dist (f (r n)) l < e" by simp
  1138     }
  1139     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  1140       by (rule eventually_mono)
  1141   }
  1142   hence "((f \<circ> r) \<longlongrightarrow> l) sequentially" unfolding o_def tendsto_iff by simp
  1143   with r show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" by auto
  1144 qed
  1145 
  1146 lemma interval_cart:
  1147   fixes a :: "real^'n"
  1148   shows "box a b = {x::real^'n. \<forall>i. a$i < x$i \<and> x$i < b$i}"
  1149     and "cbox a b = {x::real^'n. \<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i}"
  1150   by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def mem_box Basis_vec_def inner_axis)
  1151 
  1152 lemma mem_box_cart:
  1153   fixes a :: "real^'n"
  1154   shows "x \<in> box a b \<longleftrightarrow> (\<forall>i. a$i < x$i \<and> x$i < b$i)"
  1155     and "x \<in> cbox a b \<longleftrightarrow> (\<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i)"
  1156   using interval_cart[of a b] by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def)
  1157 
  1158 lemma interval_eq_empty_cart:
  1159   fixes a :: "real^'n"
  1160   shows "(box a b = {} \<longleftrightarrow> (\<exists>i. b$i \<le> a$i))" (is ?th1)
  1161     and "(cbox a b = {} \<longleftrightarrow> (\<exists>i. b$i < a$i))" (is ?th2)
  1162 proof -
  1163   { fix i x assume as:"b$i \<le> a$i" and x:"x\<in>box a b"
  1164     hence "a $ i < x $ i \<and> x $ i < b $ i" unfolding mem_box_cart by auto
  1165     hence "a$i < b$i" by auto
  1166     hence False using as by auto }
  1167   moreover
  1168   { assume as:"\<forall>i. \<not> (b$i \<le> a$i)"
  1169     let ?x = "(1/2) *\<^sub>R (a + b)"
  1170     { fix i
  1171       have "a$i < b$i" using as[THEN spec[where x=i]] by auto
  1172       hence "a$i < ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i < b$i"
  1173         unfolding vector_smult_component and vector_add_component
  1174         by auto }
  1175     hence "box a b \<noteq> {}" using mem_box_cart(1)[of "?x" a b] by auto }
  1176   ultimately show ?th1 by blast
  1177 
  1178   { fix i x assume as:"b$i < a$i" and x:"x\<in>cbox a b"
  1179     hence "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" unfolding mem_box_cart by auto
  1180     hence "a$i \<le> b$i" by auto
  1181     hence False using as by auto }
  1182   moreover
  1183   { assume as:"\<forall>i. \<not> (b$i < a$i)"
  1184     let ?x = "(1/2) *\<^sub>R (a + b)"
  1185     { fix i
  1186       have "a$i \<le> b$i" using as[THEN spec[where x=i]] by auto
  1187       hence "a$i \<le> ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i \<le> b$i"
  1188         unfolding vector_smult_component and vector_add_component
  1189         by auto }
  1190     hence "cbox a b \<noteq> {}" using mem_box_cart(2)[of "?x" a b] by auto  }
  1191   ultimately show ?th2 by blast
  1192 qed
  1193 
  1194 lemma interval_ne_empty_cart:
  1195   fixes a :: "real^'n"
  1196   shows "cbox a b \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i \<le> b$i)"
  1197     and "box a b \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
  1198   unfolding interval_eq_empty_cart[of a b] by (auto simp add: not_less not_le)
  1199     (* BH: Why doesn't just "auto" work here? *)
  1200 
  1201 lemma subset_interval_imp_cart:
  1202   fixes a :: "real^'n"
  1203   shows "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> cbox c d \<subseteq> cbox a b"
  1204     and "(\<forall>i. a$i < c$i \<and> d$i < b$i) \<Longrightarrow> cbox c d \<subseteq> box a b"
  1205     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> box c d \<subseteq> cbox a b"
  1206     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> box c d \<subseteq> box a b"
  1207   unfolding subset_eq[unfolded Ball_def] unfolding mem_box_cart
  1208   by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
  1209 
  1210 lemma interval_sing:
  1211   fixes a :: "'a::linorder^'n"
  1212   shows "{a .. a} = {a} \<and> {a<..<a} = {}"
  1213   apply (auto simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
  1214   done
  1215 
  1216 lemma subset_interval_cart:
  1217   fixes a :: "real^'n"
  1218   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)
  1219     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)
  1220     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)
  1221     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)
  1222   using subset_box[of c d a b] by (simp_all add: Basis_vec_def inner_axis)
  1223 
  1224 lemma disjoint_interval_cart:
  1225   fixes a::"real^'n"
  1226   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)
  1227     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)
  1228     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)
  1229     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)
  1230   using disjoint_interval[of a b c d] by (simp_all add: Basis_vec_def inner_axis)
  1231 
  1232 lemma Int_interval_cart:
  1233   fixes a :: "real^'n"
  1234   shows "cbox a b \<inter> cbox c d =  {(\<chi> i. max (a$i) (c$i)) .. (\<chi> i. min (b$i) (d$i))}"
  1235   unfolding Int_interval
  1236   by (auto simp: mem_box less_eq_vec_def)
  1237     (auto simp: Basis_vec_def inner_axis)
  1238 
  1239 lemma closed_interval_left_cart:
  1240   fixes b :: "real^'n"
  1241   shows "closed {x::real^'n. \<forall>i. x$i \<le> b$i}"
  1242   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1243 
  1244 lemma closed_interval_right_cart:
  1245   fixes a::"real^'n"
  1246   shows "closed {x::real^'n. \<forall>i. a$i \<le> x$i}"
  1247   by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1248 
  1249 lemma is_interval_cart:
  1250   "is_interval (s::(real^'n) set) \<longleftrightarrow>
  1251     (\<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)"
  1252   by (simp add: is_interval_def Ball_def Basis_vec_def inner_axis imp_ex)
  1253 
  1254 lemma closed_halfspace_component_le_cart: "closed {x::real^'n. x$i \<le> a}"
  1255   by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1256 
  1257 lemma closed_halfspace_component_ge_cart: "closed {x::real^'n. x$i \<ge> a}"
  1258   by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)
  1259 
  1260 lemma open_halfspace_component_lt_cart: "open {x::real^'n. x$i < a}"
  1261   by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)
  1262 
  1263 lemma open_halfspace_component_gt_cart: "open {x::real^'n. x$i  > a}"
  1264   by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)
  1265 
  1266 lemma Lim_component_le_cart:
  1267   fixes f :: "'a \<Rightarrow> real^'n"
  1268   assumes "(f \<longlongrightarrow> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f x $i \<le> b) net"
  1269   shows "l$i \<le> b"
  1270   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_vec_nth, OF assms(1, 3)])
  1271 
  1272 lemma Lim_component_ge_cart:
  1273   fixes f :: "'a \<Rightarrow> real^'n"
  1274   assumes "(f \<longlongrightarrow> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$i) net"
  1275   shows "b \<le> l$i"
  1276   by (rule tendsto_le[OF assms(2) tendsto_vec_nth tendsto_const, OF assms(1, 3)])
  1277 
  1278 lemma Lim_component_eq_cart:
  1279   fixes f :: "'a \<Rightarrow> real^'n"
  1280   assumes net: "(f \<longlongrightarrow> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
  1281   shows "l$i = b"
  1282   using ev[unfolded order_eq_iff eventually_conj_iff] and
  1283     Lim_component_ge_cart[OF net, of b i] and
  1284     Lim_component_le_cart[OF net, of i b] by auto
  1285 
  1286 lemma connected_ivt_component_cart:
  1287   fixes x :: "real^'n"
  1288   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)"
  1289   using connected_ivt_hyperplane[of s x y "axis k 1" a]
  1290   by (auto simp add: inner_axis inner_commute)
  1291 
  1292 lemma subspace_substandard_cart: "subspace {x::real^_. (\<forall>i. P i \<longrightarrow> x$i = 0)}"
  1293   unfolding subspace_def by auto
  1294 
  1295 lemma closed_substandard_cart:
  1296   "closed {x::'a::real_normed_vector ^ 'n. \<forall>i. P i \<longrightarrow> x$i = 0}"
  1297 proof -
  1298   { fix i::'n
  1299     have "closed {x::'a ^ 'n. P i \<longrightarrow> x$i = 0}"
  1300       by (cases "P i") (simp_all add: closed_Collect_eq continuous_on_const continuous_on_id continuous_on_component) }
  1301   thus ?thesis
  1302     unfolding Collect_all_eq by (simp add: closed_INT)
  1303 qed
  1304 
  1305 lemma dim_substandard_cart: "dim {x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0} = card d"
  1306   (is "dim ?A = _")
  1307 proof -
  1308   let ?a = "\<lambda>x. axis x 1 :: real^'n"
  1309   have *: "{x. \<forall>i\<in>Basis. i \<notin> ?a ` d \<longrightarrow> x \<bullet> i = 0} = ?A"
  1310     by (auto simp: image_iff Basis_vec_def axis_eq_axis inner_axis)
  1311   have "?a ` d \<subseteq> Basis"
  1312     by (auto simp: Basis_vec_def)
  1313   thus ?thesis
  1314     using dim_substandard[of "?a ` d"] card_image[of ?a d]
  1315     by (auto simp: axis_eq_axis inj_on_def *)
  1316 qed
  1317 
  1318 lemma dim_subset_UNIV_cart:
  1319   fixes S :: "(real^'n) set"
  1320   shows "dim S \<le> CARD('n)"
  1321   by (metis dim_subset_UNIV DIM_cart DIM_real mult.right_neutral)
  1322 
  1323 lemma affinity_inverses:
  1324   assumes m0: "m \<noteq> (0::'a::field)"
  1325   shows "(\<lambda>x. m *s x + c) \<circ> (\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) = id"
  1326   "(\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) \<circ> (\<lambda>x. m *s x + c) = id"
  1327   using m0
  1328   apply (auto simp add: fun_eq_iff vector_add_ldistrib diff_conv_add_uminus simp del: add_uminus_conv_diff)
  1329   apply (simp_all add: vector_smult_lneg[symmetric] vector_smult_assoc vector_sneg_minus1 [symmetric])
  1330   done
  1331 
  1332 lemma vector_affinity_eq:
  1333   assumes m0: "(m::'a::field) \<noteq> 0"
  1334   shows "m *s x + c = y \<longleftrightarrow> x = inverse m *s y + -(inverse m *s c)"
  1335 proof
  1336   assume h: "m *s x + c = y"
  1337   hence "m *s x = y - c" by (simp add: field_simps)
  1338   hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp
  1339   then show "x = inverse m *s y + - (inverse m *s c)"
  1340     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1341 next
  1342   assume h: "x = inverse m *s y + - (inverse m *s c)"
  1343   show "m *s x + c = y" unfolding h
  1344     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1345 qed
  1346 
  1347 lemma vector_eq_affinity:
  1348     "(m::'a::field) \<noteq> 0 ==> (y = m *s x + c \<longleftrightarrow> inverse(m) *s y + -(inverse(m) *s c) = x)"
  1349   using vector_affinity_eq[where m=m and x=x and y=y and c=c]
  1350   by metis
  1351 
  1352 lemma vector_cart:
  1353   fixes f :: "real^'n \<Rightarrow> real"
  1354   shows "(\<chi> i. f (axis i 1)) = (\<Sum>i\<in>Basis. f i *\<^sub>R i)"
  1355   unfolding euclidean_eq_iff[where 'a="real^'n"]
  1356   by simp (simp add: Basis_vec_def inner_axis)
  1357 
  1358 lemma const_vector_cart:"((\<chi> i. d)::real^'n) = (\<Sum>i\<in>Basis. d *\<^sub>R i)"
  1359   by (rule vector_cart)
  1360 
  1361 subsection "Convex Euclidean Space"
  1362 
  1363 lemma Cart_1:"(1::real^'n) = \<Sum>Basis"
  1364   using const_vector_cart[of 1] by (simp add: one_vec_def)
  1365 
  1366 declare vector_add_ldistrib[simp] vector_ssub_ldistrib[simp] vector_smult_assoc[simp] vector_smult_rneg[simp]
  1367 declare vector_sadd_rdistrib[simp] vector_sub_rdistrib[simp]
  1368 
  1369 lemmas vector_component_simps = vector_minus_component vector_smult_component vector_add_component less_eq_vec_def vec_lambda_beta vector_uminus_component
  1370 
  1371 lemma convex_box_cart:
  1372   assumes "\<And>i. convex {x. P i x}"
  1373   shows "convex {x. \<forall>i. P i (x$i)}"
  1374   using assms unfolding convex_def by auto
  1375 
  1376 lemma convex_positive_orthant_cart: "convex {x::real^'n. (\<forall>i. 0 \<le> x$i)}"
  1377   by (rule convex_box_cart) (simp add: atLeast_def[symmetric])
  1378 
  1379 lemma unit_interval_convex_hull_cart:
  1380   "cbox (0::real^'n) 1 = convex hull {x. \<forall>i. (x$i = 0) \<or> (x$i = 1)}"
  1381   unfolding Cart_1 unit_interval_convex_hull[where 'a="real^'n"] box_real[symmetric]
  1382   by (rule arg_cong[where f="\<lambda>x. convex hull x"]) (simp add: Basis_vec_def inner_axis)
  1383 
  1384 lemma cube_convex_hull_cart:
  1385   assumes "0 < d"
  1386   obtains s::"(real^'n) set"
  1387     where "finite s" "cbox (x - (\<chi> i. d)) (x + (\<chi> i. d)) = convex hull s"
  1388 proof -
  1389   from assms obtain s where "finite s"
  1390     and "cbox (x - sum (( *\<^sub>R) d) Basis) (x + sum (( *\<^sub>R) d) Basis) = convex hull s"
  1391     by (rule cube_convex_hull)
  1392   with that[of s] show thesis
  1393     by (simp add: const_vector_cart)
  1394 qed
  1395 
  1396 
  1397 subsection "Derivative"
  1398 
  1399 definition "jacobian f net = matrix(frechet_derivative f net)"
  1400 
  1401 lemma jacobian_works:
  1402   "(f::(real^'a) \<Rightarrow> (real^'b)) differentiable net \<longleftrightarrow>
  1403     (f has_derivative (\<lambda>h. (jacobian f net) *v h)) net"
  1404   apply rule
  1405   unfolding jacobian_def
  1406   apply (simp only: matrix_works[OF linear_frechet_derivative]) defer
  1407   apply (rule differentiableI)
  1408   apply assumption
  1409   unfolding frechet_derivative_works
  1410   apply assumption
  1411   done
  1412 
  1413 
  1414 subsection \<open>Component of the differential must be zero if it exists at a local
  1415   maximum or minimum for that corresponding component.\<close>
  1416 
  1417 lemma differential_zero_maxmin_cart:
  1418   fixes f::"real^'a \<Rightarrow> real^'b"
  1419   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))"
  1420     "f differentiable (at x)"
  1421   shows "jacobian f (at x) $ k = 0"
  1422   using differential_zero_maxmin_component[of "axis k 1" e x f] assms
  1423     vector_cart[of "\<lambda>j. frechet_derivative f (at x) j $ k"]
  1424   by (simp add: Basis_vec_def axis_eq_axis inner_axis jacobian_def matrix_def)
  1425 
  1426 subsection \<open>Lemmas for working on @{typ "real^1"}\<close>
  1427 
  1428 lemma forall_1[simp]: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
  1429   by (metis (full_types) num1_eq_iff)
  1430 
  1431 lemma ex_1[simp]: "(\<exists>x::1. P x) \<longleftrightarrow> P 1"
  1432   by auto (metis (full_types) num1_eq_iff)
  1433 
  1434 lemma exhaust_2:
  1435   fixes x :: 2
  1436   shows "x = 1 \<or> x = 2"
  1437 proof (induct x)
  1438   case (of_int z)
  1439   then have "0 <= z" and "z < 2" by simp_all
  1440   then have "z = 0 | z = 1" by arith
  1441   then show ?case by auto
  1442 qed
  1443 
  1444 lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
  1445   by (metis exhaust_2)
  1446 
  1447 lemma exhaust_3:
  1448   fixes x :: 3
  1449   shows "x = 1 \<or> x = 2 \<or> x = 3"
  1450 proof (induct x)
  1451   case (of_int z)
  1452   then have "0 <= z" and "z < 3" by simp_all
  1453   then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
  1454   then show ?case by auto
  1455 qed
  1456 
  1457 lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
  1458   by (metis exhaust_3)
  1459 
  1460 lemma UNIV_1 [simp]: "UNIV = {1::1}"
  1461   by (auto simp add: num1_eq_iff)
  1462 
  1463 lemma UNIV_2: "UNIV = {1::2, 2::2}"
  1464   using exhaust_2 by auto
  1465 
  1466 lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
  1467   using exhaust_3 by auto
  1468 
  1469 lemma sum_1: "sum f (UNIV::1 set) = f 1"
  1470   unfolding UNIV_1 by simp
  1471 
  1472 lemma sum_2: "sum f (UNIV::2 set) = f 1 + f 2"
  1473   unfolding UNIV_2 by simp
  1474 
  1475 lemma sum_3: "sum f (UNIV::3 set) = f 1 + f 2 + f 3"
  1476   unfolding UNIV_3 by (simp add: ac_simps)
  1477 
  1478 instantiation num1 :: cart_one
  1479 begin
  1480 
  1481 instance
  1482 proof
  1483   show "CARD(1) = Suc 0" by auto
  1484 qed
  1485 
  1486 end
  1487 
  1488 subsection\<open>The collapse of the general concepts to dimension one.\<close>
  1489 
  1490 lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
  1491   by (simp add: vec_eq_iff)
  1492 
  1493 lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
  1494   apply auto
  1495   apply (erule_tac x= "x$1" in allE)
  1496   apply (simp only: vector_one[symmetric])
  1497   done
  1498 
  1499 lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
  1500   by (simp add: norm_vec_def)
  1501 
  1502 lemma norm_real: "norm(x::real ^ 1) = \<bar>x$1\<bar>"
  1503   by (simp add: norm_vector_1)
  1504 
  1505 lemma dist_real: "dist(x::real ^ 1) y = \<bar>(x$1) - (y$1)\<bar>"
  1506   by (auto simp add: norm_real dist_norm)
  1507 
  1508 
  1509 subsection\<open>Explicit vector construction from lists.\<close>
  1510 
  1511 definition "vector l = (\<chi> i. foldr (\<lambda>x f n. fun_upd (f (n+1)) n x) l (\<lambda>n x. 0) 1 i)"
  1512 
  1513 lemma vector_1: "(vector[x]) $1 = x"
  1514   unfolding vector_def by simp
  1515 
  1516 lemma vector_2:
  1517  "(vector[x,y]) $1 = x"
  1518  "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
  1519   unfolding vector_def by simp_all
  1520 
  1521 lemma vector_3:
  1522  "(vector [x,y,z] ::('a::zero)^3)$1 = x"
  1523  "(vector [x,y,z] ::('a::zero)^3)$2 = y"
  1524  "(vector [x,y,z] ::('a::zero)^3)$3 = z"
  1525   unfolding vector_def by simp_all
  1526 
  1527 lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
  1528   by (metis vector_1 vector_one)
  1529 
  1530 lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
  1531   apply auto
  1532   apply (erule_tac x="v$1" in allE)
  1533   apply (erule_tac x="v$2" in allE)
  1534   apply (subgoal_tac "vector [v$1, v$2] = v")
  1535   apply simp
  1536   apply (vector vector_def)
  1537   apply (simp add: forall_2)
  1538   done
  1539 
  1540 lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
  1541   apply auto
  1542   apply (erule_tac x="v$1" in allE)
  1543   apply (erule_tac x="v$2" in allE)
  1544   apply (erule_tac x="v$3" in allE)
  1545   apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
  1546   apply simp
  1547   apply (vector vector_def)
  1548   apply (simp add: forall_3)
  1549   done
  1550 
  1551 lemma bounded_linear_component_cart[intro]: "bounded_linear (\<lambda>x::real^'n. x $ k)"
  1552   apply (rule bounded_linearI[where K=1])
  1553   using component_le_norm_cart[of _ k] unfolding real_norm_def by auto
  1554 
  1555 lemma interval_split_cart:
  1556   "{a..b::real^'n} \<inter> {x. x$k \<le> c} = {a .. (\<chi> i. if i = k then min (b$k) c else b$i)}"
  1557   "cbox a b \<inter> {x. x$k \<ge> c} = {(\<chi> i. if i = k then max (a$k) c else a$i) .. b}"
  1558   apply (rule_tac[!] set_eqI)
  1559   unfolding Int_iff mem_box_cart mem_Collect_eq interval_cbox_cart
  1560   unfolding vec_lambda_beta
  1561   by auto
  1562 
  1563 lemmas cartesian_euclidean_space_uniform_limit_intros[uniform_limit_intros] =
  1564   bounded_linear.uniform_limit[OF blinfun.bounded_linear_right]
  1565   bounded_linear.uniform_limit[OF bounded_linear_vec_nth]
  1566   bounded_linear.uniform_limit[OF bounded_linear_component_cart]
  1567 
  1568 end