src/HOL/Analysis/Convex_Euclidean_Space.thy
author paulson <lp15@cam.ac.uk>
Mon Oct 30 16:02:59 2017 +0000 (19 months ago)
changeset 66939 04678058308f
parent 66884 c2128ab11f61
child 67135 1a94352812f4
permissions -rw-r--r--
New results in topology, mostly from HOL Light's moretop.ml
     1 (* Title:      HOL/Analysis/Convex_Euclidean_Space.thy
     2    Author:     L C Paulson, University of Cambridge
     3    Author:     Robert Himmelmann, TU Muenchen
     4    Author:     Bogdan Grechuk, University of Edinburgh
     5    Author:     Armin Heller, TU Muenchen
     6    Author:     Johannes Hoelzl, TU Muenchen
     7 *)
     8 
     9 section \<open>Convex sets, functions and related things\<close>
    10 
    11 theory Convex_Euclidean_Space
    12 imports
    13   Connected
    14   "HOL-Library.Set_Algebras"
    15 begin
    16 
    17 lemma swap_continuous: (*move to Topological_Spaces?*)
    18   assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
    19     shows "continuous_on (cbox (c,a) (d,b)) (\<lambda>(x, y). f y x)"
    20 proof -
    21   have "(\<lambda>(x, y). f y x) = (\<lambda>(x, y). f x y) \<circ> prod.swap"
    22     by auto
    23   then show ?thesis
    24     apply (rule ssubst)
    25     apply (rule continuous_on_compose)
    26     apply (simp add: split_def)
    27     apply (rule continuous_intros | simp add: assms)+
    28     done
    29 qed
    30 
    31 lemma dim_image_eq:
    32   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
    33   assumes lf: "linear f"
    34     and fi: "inj_on f (span S)"
    35   shows "dim (f ` S) = dim (S::'n::euclidean_space set)"
    36 proof -
    37   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S"
    38     using basis_exists[of S] by auto
    39   then have "span S = span B"
    40     using span_mono[of B S] span_mono[of S "span B"] span_span[of B] by auto
    41   then have "independent (f ` B)"
    42     using independent_inj_on_image[of B f] B assms by auto
    43   moreover have "card (f ` B) = card B"
    44     using assms card_image[of f B] subset_inj_on[of f "span S" B] B span_inc by auto
    45   moreover have "(f ` B) \<subseteq> (f ` S)"
    46     using B by auto
    47   ultimately have "dim (f ` S) \<ge> dim S"
    48     using independent_card_le_dim[of "f ` B" "f ` S"] B by auto
    49   then show ?thesis
    50     using dim_image_le[of f S] assms by auto
    51 qed
    52 
    53 lemma linear_injective_on_subspace_0:
    54   assumes lf: "linear f"
    55     and "subspace S"
    56   shows "inj_on f S \<longleftrightarrow> (\<forall>x \<in> S. f x = 0 \<longrightarrow> x = 0)"
    57 proof -
    58   have "inj_on f S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f x = f y \<longrightarrow> x = y)"
    59     by (simp add: inj_on_def)
    60   also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f x - f y = 0 \<longrightarrow> x - y = 0)"
    61     by simp
    62   also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f (x - y) = 0 \<longrightarrow> x - y = 0)"
    63     by (simp add: linear_diff[OF lf])
    64   also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. f x = 0 \<longrightarrow> x = 0)"
    65     using \<open>subspace S\<close> subspace_def[of S] subspace_diff[of S] by auto
    66   finally show ?thesis .
    67 qed
    68 
    69 lemma subspace_Inter: "\<forall>s \<in> f. subspace s \<Longrightarrow> subspace (\<Inter>f)"
    70   unfolding subspace_def by auto
    71 
    72 lemma span_eq[simp]: "span s = s \<longleftrightarrow> subspace s"
    73   unfolding span_def by (rule hull_eq) (rule subspace_Inter)
    74 
    75 lemma substdbasis_expansion_unique:
    76   assumes d: "d \<subseteq> Basis"
    77   shows "(\<Sum>i\<in>d. f i *\<^sub>R i) = (x::'a::euclidean_space) \<longleftrightarrow>
    78     (\<forall>i\<in>Basis. (i \<in> d \<longrightarrow> f i = x \<bullet> i) \<and> (i \<notin> d \<longrightarrow> x \<bullet> i = 0))"
    79 proof -
    80   have *: "\<And>x a b P. x * (if P then a else b) = (if P then x * a else x * b)"
    81     by auto
    82   have **: "finite d"
    83     by (auto intro: finite_subset[OF assms])
    84   have ***: "\<And>i. i \<in> Basis \<Longrightarrow> (\<Sum>i\<in>d. f i *\<^sub>R i) \<bullet> i = (\<Sum>x\<in>d. if x = i then f x else 0)"
    85     using d
    86     by (auto intro!: sum.cong simp: inner_Basis inner_sum_left)
    87   show ?thesis
    88     unfolding euclidean_eq_iff[where 'a='a] by (auto simp: sum.delta[OF **] ***)
    89 qed
    90 
    91 lemma independent_substdbasis: "d \<subseteq> Basis \<Longrightarrow> independent d"
    92   by (rule independent_mono[OF independent_Basis])
    93 
    94 lemma dim_cball:
    95   assumes "e > 0"
    96   shows "dim (cball (0 :: 'n::euclidean_space) e) = DIM('n)"
    97 proof -
    98   {
    99     fix x :: "'n::euclidean_space"
   100     define y where "y = (e / norm x) *\<^sub>R x"
   101     then have "y \<in> cball 0 e"
   102       using assms by auto
   103     moreover have *: "x = (norm x / e) *\<^sub>R y"
   104       using y_def assms by simp
   105     moreover from * have "x = (norm x/e) *\<^sub>R y"
   106       by auto
   107     ultimately have "x \<in> span (cball 0 e)"
   108       using span_mul[of y "cball 0 e" "norm x/e"] span_inc[of "cball 0 e"]
   109       by (simp add: span_superset)
   110   }
   111   then have "span (cball 0 e) = (UNIV :: 'n::euclidean_space set)"
   112     by auto
   113   then show ?thesis
   114     using dim_span[of "cball (0 :: 'n::euclidean_space) e"] by (auto simp add: dim_UNIV)
   115 qed
   116 
   117 lemma indep_card_eq_dim_span:
   118   fixes B :: "'n::euclidean_space set"
   119   assumes "independent B"
   120   shows "finite B \<and> card B = dim (span B)"
   121   using assms basis_card_eq_dim[of B "span B"] span_inc by auto
   122 
   123 lemma sum_not_0: "sum f A \<noteq> 0 \<Longrightarrow> \<exists>a \<in> A. f a \<noteq> 0"
   124   by (rule ccontr) auto
   125 
   126 lemma subset_translation_eq [simp]:
   127     fixes a :: "'a::real_vector" shows "op + a ` s \<subseteq> op + a ` t \<longleftrightarrow> s \<subseteq> t"
   128   by auto
   129 
   130 lemma translate_inj_on:
   131   fixes A :: "'a::ab_group_add set"
   132   shows "inj_on (\<lambda>x. a + x) A"
   133   unfolding inj_on_def by auto
   134 
   135 lemma translation_assoc:
   136   fixes a b :: "'a::ab_group_add"
   137   shows "(\<lambda>x. b + x) ` ((\<lambda>x. a + x) ` S) = (\<lambda>x. (a + b) + x) ` S"
   138   by auto
   139 
   140 lemma translation_invert:
   141   fixes a :: "'a::ab_group_add"
   142   assumes "(\<lambda>x. a + x) ` A = (\<lambda>x. a + x) ` B"
   143   shows "A = B"
   144 proof -
   145   have "(\<lambda>x. -a + x) ` ((\<lambda>x. a + x) ` A) = (\<lambda>x. - a + x) ` ((\<lambda>x. a + x) ` B)"
   146     using assms by auto
   147   then show ?thesis
   148     using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto
   149 qed
   150 
   151 lemma translation_galois:
   152   fixes a :: "'a::ab_group_add"
   153   shows "T = ((\<lambda>x. a + x) ` S) \<longleftrightarrow> S = ((\<lambda>x. (- a) + x) ` T)"
   154   using translation_assoc[of "-a" a S]
   155   apply auto
   156   using translation_assoc[of a "-a" T]
   157   apply auto
   158   done
   159 
   160 lemma translation_inverse_subset:
   161   assumes "((\<lambda>x. - a + x) ` V) \<le> (S :: 'n::ab_group_add set)"
   162   shows "V \<le> ((\<lambda>x. a + x) ` S)"
   163 proof -
   164   {
   165     fix x
   166     assume "x \<in> V"
   167     then have "x-a \<in> S" using assms by auto
   168     then have "x \<in> {a + v |v. v \<in> S}"
   169       apply auto
   170       apply (rule exI[of _ "x-a"])
   171       apply simp
   172       done
   173     then have "x \<in> ((\<lambda>x. a+x) ` S)" by auto
   174   }
   175   then show ?thesis by auto
   176 qed
   177 
   178 subsection \<open>Convexity\<close>
   179 
   180 definition convex :: "'a::real_vector set \<Rightarrow> bool"
   181   where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
   182 
   183 lemma convexI:
   184   assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   185   shows "convex s"
   186   using assms unfolding convex_def by fast
   187 
   188 lemma convexD:
   189   assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
   190   shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   191   using assms unfolding convex_def by fast
   192 
   193 lemma convex_alt: "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
   194   (is "_ \<longleftrightarrow> ?alt")
   195 proof
   196   show "convex s" if alt: ?alt
   197   proof -
   198     {
   199       fix x y and u v :: real
   200       assume mem: "x \<in> s" "y \<in> s"
   201       assume "0 \<le> u" "0 \<le> v"
   202       moreover
   203       assume "u + v = 1"
   204       then have "u = 1 - v" by auto
   205       ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   206         using alt [rule_format, OF mem] by auto
   207     }
   208     then show ?thesis
   209       unfolding convex_def by auto
   210   qed
   211   show ?alt if "convex s"
   212     using that by (auto simp: convex_def)
   213 qed
   214 
   215 lemma convexD_alt:
   216   assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
   217   shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
   218   using assms unfolding convex_alt by auto
   219 
   220 lemma mem_convex_alt:
   221   assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
   222   shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
   223   apply (rule convexD)
   224   using assms
   225        apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
   226   done
   227 
   228 lemma convex_empty[intro,simp]: "convex {}"
   229   unfolding convex_def by simp
   230 
   231 lemma convex_singleton[intro,simp]: "convex {a}"
   232   unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
   233 
   234 lemma convex_UNIV[intro,simp]: "convex UNIV"
   235   unfolding convex_def by auto
   236 
   237 lemma convex_Inter: "(\<And>s. s\<in>f \<Longrightarrow> convex s) \<Longrightarrow> convex(\<Inter>f)"
   238   unfolding convex_def by auto
   239 
   240 lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
   241   unfolding convex_def by auto
   242 
   243 lemma convex_INT: "(\<And>i. i \<in> A \<Longrightarrow> convex (B i)) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
   244   unfolding convex_def by auto
   245 
   246 lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
   247   unfolding convex_def by auto
   248 
   249 lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
   250   unfolding convex_def
   251   by (auto simp: inner_add intro!: convex_bound_le)
   252 
   253 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
   254 proof -
   255   have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
   256     by auto
   257   show ?thesis
   258     unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
   259 qed
   260 
   261 lemma convex_halfspace_abs_le: "convex {x. \<bar>inner a x\<bar> \<le> b}"
   262 proof -
   263   have *: "{x. \<bar>inner a x\<bar> \<le> b} = {x. inner a x \<le> b} \<inter> {x. -b \<le> inner a x}"
   264     by auto
   265   show ?thesis
   266     unfolding * by (simp add: convex_Int convex_halfspace_ge convex_halfspace_le)
   267 qed
   268 
   269 lemma convex_hyperplane: "convex {x. inner a x = b}"
   270 proof -
   271   have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
   272     by auto
   273   show ?thesis using convex_halfspace_le convex_halfspace_ge
   274     by (auto intro!: convex_Int simp: *)
   275 qed
   276 
   277 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
   278   unfolding convex_def
   279   by (auto simp: convex_bound_lt inner_add)
   280 
   281 lemma convex_halfspace_gt: "convex {x. inner a x > b}"
   282    using convex_halfspace_lt[of "-a" "-b"] by auto
   283 
   284 lemma convex_real_interval [iff]:
   285   fixes a b :: "real"
   286   shows "convex {a..}" and "convex {..b}"
   287     and "convex {a<..}" and "convex {..<b}"
   288     and "convex {a..b}" and "convex {a<..b}"
   289     and "convex {a..<b}" and "convex {a<..<b}"
   290 proof -
   291   have "{a..} = {x. a \<le> inner 1 x}"
   292     by auto
   293   then show 1: "convex {a..}"
   294     by (simp only: convex_halfspace_ge)
   295   have "{..b} = {x. inner 1 x \<le> b}"
   296     by auto
   297   then show 2: "convex {..b}"
   298     by (simp only: convex_halfspace_le)
   299   have "{a<..} = {x. a < inner 1 x}"
   300     by auto
   301   then show 3: "convex {a<..}"
   302     by (simp only: convex_halfspace_gt)
   303   have "{..<b} = {x. inner 1 x < b}"
   304     by auto
   305   then show 4: "convex {..<b}"
   306     by (simp only: convex_halfspace_lt)
   307   have "{a..b} = {a..} \<inter> {..b}"
   308     by auto
   309   then show "convex {a..b}"
   310     by (simp only: convex_Int 1 2)
   311   have "{a<..b} = {a<..} \<inter> {..b}"
   312     by auto
   313   then show "convex {a<..b}"
   314     by (simp only: convex_Int 3 2)
   315   have "{a..<b} = {a..} \<inter> {..<b}"
   316     by auto
   317   then show "convex {a..<b}"
   318     by (simp only: convex_Int 1 4)
   319   have "{a<..<b} = {a<..} \<inter> {..<b}"
   320     by auto
   321   then show "convex {a<..<b}"
   322     by (simp only: convex_Int 3 4)
   323 qed
   324 
   325 lemma convex_Reals: "convex \<real>"
   326   by (simp add: convex_def scaleR_conv_of_real)
   327 
   328 
   329 subsection \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
   330 
   331 lemma convex_sum:
   332   fixes C :: "'a::real_vector set"
   333   assumes "finite s"
   334     and "convex C"
   335     and "(\<Sum> i \<in> s. a i) = 1"
   336   assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   337     and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   338   shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   339   using assms(1,3,4,5)
   340 proof (induct arbitrary: a set: finite)
   341   case empty
   342   then show ?case by simp
   343 next
   344   case (insert i s) note IH = this(3)
   345   have "a i + sum a s = 1"
   346     and "0 \<le> a i"
   347     and "\<forall>j\<in>s. 0 \<le> a j"
   348     and "y i \<in> C"
   349     and "\<forall>j\<in>s. y j \<in> C"
   350     using insert.hyps(1,2) insert.prems by simp_all
   351   then have "0 \<le> sum a s"
   352     by (simp add: sum_nonneg)
   353   have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
   354   proof (cases "sum a s = 0")
   355     case True
   356     with \<open>a i + sum a s = 1\<close> have "a i = 1"
   357       by simp
   358     from sum_nonneg_0 [OF \<open>finite s\<close> _ True] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
   359       by simp
   360     show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
   361       by simp
   362   next
   363     case False
   364     with \<open>0 \<le> sum a s\<close> have "0 < sum a s"
   365       by simp
   366     then have "(\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   367       using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
   368       by (simp add: IH sum_divide_distrib [symmetric])
   369     from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
   370       and \<open>0 \<le> sum a s\<close> and \<open>a i + sum a s = 1\<close>
   371     have "a i *\<^sub>R y i + sum a s *\<^sub>R (\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   372       by (rule convexD)
   373     then show ?thesis
   374       by (simp add: scaleR_sum_right False)
   375   qed
   376   then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
   377     by simp
   378 qed
   379 
   380 lemma convex:
   381   "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (sum u {1..k} = 1)
   382       \<longrightarrow> sum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   383 proof safe
   384   fix k :: nat
   385   fix u :: "nat \<Rightarrow> real"
   386   fix x
   387   assume "convex s"
   388     "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   389     "sum u {1..k} = 1"
   390   with convex_sum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
   391     by auto
   392 next
   393   assume *: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> sum u {1..k} = 1
   394     \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   395   {
   396     fix \<mu> :: real
   397     fix x y :: 'a
   398     assume xy: "x \<in> s" "y \<in> s"
   399     assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   400     let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   401     let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   402     have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
   403       by auto
   404     then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
   405       by simp
   406     then have "sum ?u {1 .. 2} = 1"
   407       using sum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   408       by auto
   409     with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   410       using mu xy by auto
   411     have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   412       using sum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   413     from sum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   414     have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   415       by auto
   416     then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
   417       using s by (auto simp: add.commute)
   418   }
   419   then show "convex s"
   420     unfolding convex_alt by auto
   421 qed
   422 
   423 
   424 lemma convex_explicit:
   425   fixes s :: "'a::real_vector set"
   426   shows "convex s \<longleftrightarrow>
   427     (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> sum u t = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   428 proof safe
   429   fix t
   430   fix u :: "'a \<Rightarrow> real"
   431   assume "convex s"
   432     and "finite t"
   433     and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   434   then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   435     using convex_sum[of t s u "\<lambda> x. x"] by auto
   436 next
   437   assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
   438     sum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   439   show "convex s"
   440     unfolding convex_alt
   441   proof safe
   442     fix x y
   443     fix \<mu> :: real
   444     assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   445     show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   446     proof (cases "x = y")
   447       case False
   448       then show ?thesis
   449         using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
   450         by auto
   451     next
   452       case True
   453       then show ?thesis
   454         using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
   455         by (auto simp: field_simps real_vector.scale_left_diff_distrib)
   456     qed
   457   qed
   458 qed
   459 
   460 lemma convex_finite:
   461   assumes "finite s"
   462   shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   463   unfolding convex_explicit
   464   apply safe
   465   subgoal for u by (erule allE [where x=s], erule allE [where x=u]) auto
   466   subgoal for t u
   467   proof -
   468     have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   469       by simp
   470     assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   471     assume *: "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   472     assume "t \<subseteq> s"
   473     then have "s \<inter> t = t" by auto
   474     with sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] * show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   475       by (auto simp: assms sum.If_cases if_distrib if_distrib_arg)
   476   qed
   477   done
   478 
   479 
   480 subsection \<open>Functions that are convex on a set\<close>
   481 
   482 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   483   where "convex_on s f \<longleftrightarrow>
   484     (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   485 
   486 lemma convex_onI [intro?]:
   487   assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   488     f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   489   shows "convex_on A f"
   490   unfolding convex_on_def
   491 proof clarify
   492   fix x y
   493   fix u v :: real
   494   assume A: "x \<in> A" "y \<in> A" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   495   from A(5) have [simp]: "v = 1 - u"
   496     by (simp add: algebra_simps)
   497   from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   498     using assms[of u y x]
   499     by (cases "u = 0 \<or> u = 1") (auto simp: algebra_simps)
   500 qed
   501 
   502 lemma convex_on_linorderI [intro?]:
   503   fixes A :: "('a::{linorder,real_vector}) set"
   504   assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x < y \<Longrightarrow>
   505     f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   506   shows "convex_on A f"
   507 proof
   508   fix x y
   509   fix t :: real
   510   assume A: "x \<in> A" "y \<in> A" "t > 0" "t < 1"
   511   with assms [of t x y] assms [of "1 - t" y x]
   512   show "f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   513     by (cases x y rule: linorder_cases) (auto simp: algebra_simps)
   514 qed
   515 
   516 lemma convex_onD:
   517   assumes "convex_on A f"
   518   shows "\<And>t x y. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   519     f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   520   using assms by (auto simp: convex_on_def)
   521 
   522 lemma convex_onD_Icc:
   523   assumes "convex_on {x..y} f" "x \<le> (y :: _ :: {real_vector,preorder})"
   524   shows "\<And>t. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow>
   525     f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   526   using assms(2) by (intro convex_onD [OF assms(1)]) simp_all
   527 
   528 lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   529   unfolding convex_on_def by auto
   530 
   531 lemma convex_on_add [intro]:
   532   assumes "convex_on s f"
   533     and "convex_on s g"
   534   shows "convex_on s (\<lambda>x. f x + g x)"
   535 proof -
   536   {
   537     fix x y
   538     assume "x \<in> s" "y \<in> s"
   539     moreover
   540     fix u v :: real
   541     assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   542     ultimately
   543     have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   544       using assms unfolding convex_on_def by (auto simp: add_mono)
   545     then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   546       by (simp add: field_simps)
   547   }
   548   then show ?thesis
   549     unfolding convex_on_def by auto
   550 qed
   551 
   552 lemma convex_on_cmul [intro]:
   553   fixes c :: real
   554   assumes "0 \<le> c"
   555     and "convex_on s f"
   556   shows "convex_on s (\<lambda>x. c * f x)"
   557 proof -
   558   have *: "u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   559     for u c fx v fy :: real
   560     by (simp add: field_simps)
   561   show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   562     unfolding convex_on_def and * by auto
   563 qed
   564 
   565 lemma convex_lower:
   566   assumes "convex_on s f"
   567     and "x \<in> s"
   568     and "y \<in> s"
   569     and "0 \<le> u"
   570     and "0 \<le> v"
   571     and "u + v = 1"
   572   shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   573 proof -
   574   let ?m = "max (f x) (f y)"
   575   have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   576     using assms(4,5) by (auto simp: mult_left_mono add_mono)
   577   also have "\<dots> = max (f x) (f y)"
   578     using assms(6) by (simp add: distrib_right [symmetric])
   579   finally show ?thesis
   580     using assms unfolding convex_on_def by fastforce
   581 qed
   582 
   583 lemma convex_on_dist [intro]:
   584   fixes s :: "'a::real_normed_vector set"
   585   shows "convex_on s (\<lambda>x. dist a x)"
   586 proof (auto simp: convex_on_def dist_norm)
   587   fix x y
   588   assume "x \<in> s" "y \<in> s"
   589   fix u v :: real
   590   assume "0 \<le> u"
   591   assume "0 \<le> v"
   592   assume "u + v = 1"
   593   have "a = u *\<^sub>R a + v *\<^sub>R a"
   594     unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
   595   then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   596     by (auto simp: algebra_simps)
   597   show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   598     unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   599     using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
   600 qed
   601 
   602 
   603 subsection \<open>Arithmetic operations on sets preserve convexity\<close>
   604 
   605 lemma convex_linear_image:
   606   assumes "linear f"
   607     and "convex s"
   608   shows "convex (f ` s)"
   609 proof -
   610   interpret f: linear f by fact
   611   from \<open>convex s\<close> show "convex (f ` s)"
   612     by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
   613 qed
   614 
   615 lemma convex_linear_vimage:
   616   assumes "linear f"
   617     and "convex s"
   618   shows "convex (f -` s)"
   619 proof -
   620   interpret f: linear f by fact
   621   from \<open>convex s\<close> show "convex (f -` s)"
   622     by (simp add: convex_def f.add f.scaleR)
   623 qed
   624 
   625 lemma convex_scaling:
   626   assumes "convex s"
   627   shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   628 proof -
   629   have "linear (\<lambda>x. c *\<^sub>R x)"
   630     by (simp add: linearI scaleR_add_right)
   631   then show ?thesis
   632     using \<open>convex s\<close> by (rule convex_linear_image)
   633 qed
   634 
   635 lemma convex_scaled:
   636   assumes "convex S"
   637   shows "convex ((\<lambda>x. x *\<^sub>R c) ` S)"
   638 proof -
   639   have "linear (\<lambda>x. x *\<^sub>R c)"
   640     by (simp add: linearI scaleR_add_left)
   641   then show ?thesis
   642     using \<open>convex S\<close> by (rule convex_linear_image)
   643 qed
   644 
   645 lemma convex_negations:
   646   assumes "convex S"
   647   shows "convex ((\<lambda>x. - x) ` S)"
   648 proof -
   649   have "linear (\<lambda>x. - x)"
   650     by (simp add: linearI)
   651   then show ?thesis
   652     using \<open>convex S\<close> by (rule convex_linear_image)
   653 qed
   654 
   655 lemma convex_sums:
   656   assumes "convex S"
   657     and "convex T"
   658   shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   659 proof -
   660   have "linear (\<lambda>(x, y). x + y)"
   661     by (auto intro: linearI simp: scaleR_add_right)
   662   with assms have "convex ((\<lambda>(x, y). x + y) ` (S \<times> T))"
   663     by (intro convex_linear_image convex_Times)
   664   also have "((\<lambda>(x, y). x + y) ` (S \<times> T)) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   665     by auto
   666   finally show ?thesis .
   667 qed
   668 
   669 lemma convex_differences:
   670   assumes "convex S" "convex T"
   671   shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
   672 proof -
   673   have "{x - y| x y. x \<in> S \<and> y \<in> T} = {x + y |x y. x \<in> S \<and> y \<in> uminus ` T}"
   674     by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
   675   then show ?thesis
   676     using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   677 qed
   678 
   679 lemma convex_translation:
   680   assumes "convex S"
   681   shows "convex ((\<lambda>x. a + x) ` S)"
   682 proof -
   683   have "(\<Union> x\<in> {a}. \<Union>y \<in> S. {x + y}) = (\<lambda>x. a + x) ` S"
   684     by auto
   685   then show ?thesis
   686     using convex_sums[OF convex_singleton[of a] assms] by auto
   687 qed
   688 
   689 lemma convex_affinity:
   690   assumes "convex S"
   691   shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` S)"
   692 proof -
   693   have "(\<lambda>x. a + c *\<^sub>R x) ` S = op + a ` op *\<^sub>R c ` S"
   694     by auto
   695   then show ?thesis
   696     using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   697 qed
   698 
   699 lemma pos_is_convex: "convex {0 :: real <..}"
   700   unfolding convex_alt
   701 proof safe
   702   fix y x \<mu> :: real
   703   assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   704   {
   705     assume "\<mu> = 0"
   706     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y"
   707       by simp
   708     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   709       using * by simp
   710   }
   711   moreover
   712   {
   713     assume "\<mu> = 1"
   714     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   715       using * by simp
   716   }
   717   moreover
   718   {
   719     assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   720     then have "\<mu> > 0" "(1 - \<mu>) > 0"
   721       using * by auto
   722     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   723       using * by (auto simp: add_pos_pos)
   724   }
   725   ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
   726     by fastforce
   727 qed
   728 
   729 lemma convex_on_sum:
   730   fixes a :: "'a \<Rightarrow> real"
   731     and y :: "'a \<Rightarrow> 'b::real_vector"
   732     and f :: "'b \<Rightarrow> real"
   733   assumes "finite s" "s \<noteq> {}"
   734     and "convex_on C f"
   735     and "convex C"
   736     and "(\<Sum> i \<in> s. a i) = 1"
   737     and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   738     and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   739   shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   740   using assms
   741 proof (induct s arbitrary: a rule: finite_ne_induct)
   742   case (singleton i)
   743   then have ai: "a i = 1"
   744     by auto
   745   then show ?case
   746     by auto
   747 next
   748   case (insert i s)
   749   then have "convex_on C f"
   750     by simp
   751   from this[unfolded convex_on_def, rule_format]
   752   have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
   753       f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   754     by simp
   755   show ?case
   756   proof (cases "a i = 1")
   757     case True
   758     then have "(\<Sum> j \<in> s. a j) = 0"
   759       using insert by auto
   760     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   761       using insert by (fastforce simp: sum_nonneg_eq_0_iff)
   762     then show ?thesis
   763       using insert by auto
   764   next
   765     case False
   766     from insert have yai: "y i \<in> C" "a i \<ge> 0"
   767       by auto
   768     have fis: "finite (insert i s)"
   769       using insert by auto
   770     then have ai1: "a i \<le> 1"
   771       using sum_nonneg_leq_bound[of "insert i s" a] insert by simp
   772     then have "a i < 1"
   773       using False by auto
   774     then have i0: "1 - a i > 0"
   775       by auto
   776     let ?a = "\<lambda>j. a j / (1 - a i)"
   777     have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
   778       using i0 insert that by fastforce
   779     have "(\<Sum> j \<in> insert i s. a j) = 1"
   780       using insert by auto
   781     then have "(\<Sum> j \<in> s. a j) = 1 - a i"
   782       using sum.insert insert by fastforce
   783     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
   784       using i0 by auto
   785     then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
   786       unfolding sum_divide_distrib by simp
   787     have "convex C" using insert by auto
   788     then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   789       using insert convex_sum [OF \<open>finite s\<close> \<open>convex C\<close> a1 a_nonneg] by auto
   790     have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   791       using a_nonneg a1 insert by blast
   792     have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) = f ((\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   793       using sum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
   794       by (auto simp only: add.commute)
   795     also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   796       using i0 by auto
   797     also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   798       using scaleR_right.sum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   799       by (auto simp: algebra_simps)
   800     also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   801       by (auto simp: divide_inverse)
   802     also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   803       using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   804       by (auto simp: add.commute)
   805     also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   806       using add_right_mono [OF mult_left_mono [of _ _ "1 - a i",
   807             OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"]
   808       by simp
   809     also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   810       unfolding sum_distrib_left[of "1 - a i" "\<lambda> j. ?a j * f (y j)"]
   811       using i0 by auto
   812     also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
   813       using i0 by auto
   814     also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
   815       using insert by auto
   816     finally show ?thesis
   817       by simp
   818   qed
   819 qed
   820 
   821 lemma convex_on_alt:
   822   fixes C :: "'a::real_vector set"
   823   assumes "convex C"
   824   shows "convex_on C f \<longleftrightarrow>
   825     (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
   826       f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   827 proof safe
   828   fix x y
   829   fix \<mu> :: real
   830   assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   831   from this[unfolded convex_on_def, rule_format]
   832   have "0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" for u v
   833     by auto
   834   from this [of "\<mu>" "1 - \<mu>", simplified] *
   835   show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   836     by auto
   837 next
   838   assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
   839     f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   840   {
   841     fix x y
   842     fix u v :: real
   843     assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   844     then have[simp]: "1 - u = v" by auto
   845     from *[rule_format, of x y u]
   846     have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   847       using ** by auto
   848   }
   849   then show "convex_on C f"
   850     unfolding convex_on_def by auto
   851 qed
   852 
   853 lemma convex_on_diff:
   854   fixes f :: "real \<Rightarrow> real"
   855   assumes f: "convex_on I f"
   856     and I: "x \<in> I" "y \<in> I"
   857     and t: "x < t" "t < y"
   858   shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   859     and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   860 proof -
   861   define a where "a \<equiv> (t - y) / (x - y)"
   862   with t have "0 \<le> a" "0 \<le> 1 - a"
   863     by (auto simp: field_simps)
   864   with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   865     by (auto simp: convex_on_def)
   866   have "a * x + (1 - a) * y = a * (x - y) + y"
   867     by (simp add: field_simps)
   868   also have "\<dots> = t"
   869     unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
   870   finally have "f t \<le> a * f x + (1 - a) * f y"
   871     using cvx by simp
   872   also have "\<dots> = a * (f x - f y) + f y"
   873     by (simp add: field_simps)
   874   finally have "f t - f y \<le> a * (f x - f y)"
   875     by simp
   876   with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   877     by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   878   with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   879     by (simp add: le_divide_eq divide_le_eq field_simps)
   880 qed
   881 
   882 lemma pos_convex_function:
   883   fixes f :: "real \<Rightarrow> real"
   884   assumes "convex C"
   885     and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   886   shows "convex_on C f"
   887   unfolding convex_on_alt[OF assms(1)]
   888   using assms
   889 proof safe
   890   fix x y \<mu> :: real
   891   let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   892   assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   893   then have "1 - \<mu> \<ge> 0" by auto
   894   then have xpos: "?x \<in> C"
   895     using * unfolding convex_alt by fastforce
   896   have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
   897       \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   898     using add_mono [OF mult_left_mono [OF leq [OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
   899         mult_left_mono [OF leq [OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
   900     by auto
   901   then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   902     by (auto simp: field_simps)
   903   then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   904     using convex_on_alt by auto
   905 qed
   906 
   907 lemma atMostAtLeast_subset_convex:
   908   fixes C :: "real set"
   909   assumes "convex C"
   910     and "x \<in> C" "y \<in> C" "x < y"
   911   shows "{x .. y} \<subseteq> C"
   912 proof safe
   913   fix z assume z: "z \<in> {x .. y}"
   914   have less: "z \<in> C" if *: "x < z" "z < y"
   915   proof -
   916     let ?\<mu> = "(y - z) / (y - x)"
   917     have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
   918       using assms * by (auto simp: field_simps)
   919     then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   920       using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   921       by (simp add: algebra_simps)
   922     have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   923       by (auto simp: field_simps)
   924     also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   925       using assms by (simp only: add_divide_distrib) (auto simp: field_simps)
   926     also have "\<dots> = z"
   927       using assms by (auto simp: field_simps)
   928     finally show ?thesis
   929       using comb by auto
   930   qed
   931   show "z \<in> C"
   932     using z less assms by (auto simp: le_less)
   933 qed
   934 
   935 lemma f''_imp_f':
   936   fixes f :: "real \<Rightarrow> real"
   937   assumes "convex C"
   938     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   939     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   940     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   941     and x: "x \<in> C"
   942     and y: "y \<in> C"
   943   shows "f' x * (y - x) \<le> f y - f x"
   944   using assms
   945 proof -
   946   have less_imp: "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   947     if *: "x \<in> C" "y \<in> C" "y > x" for x y :: real
   948   proof -
   949     from * have ge: "y - x > 0" "y - x \<ge> 0"
   950       by auto
   951     from * have le: "x - y < 0" "x - y \<le> 0"
   952       by auto
   953     then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   954       using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
   955           THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   956       by auto
   957     then have "z1 \<in> C"
   958       using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
   959       by fastforce
   960     from z1 have z1': "f x - f y = (x - y) * f' z1"
   961       by (simp add: field_simps)
   962     obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   963       using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
   964           THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   965       by auto
   966     obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   967       using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
   968           THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   969       by auto
   970     have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   971       using * z1' by auto
   972     also have "\<dots> = (y - z1) * f'' z3"
   973       using z3 by auto
   974     finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
   975       by simp
   976     have A': "y - z1 \<ge> 0"
   977       using z1 by auto
   978     have "z3 \<in> C"
   979       using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
   980       by fastforce
   981     then have B': "f'' z3 \<ge> 0"
   982       using assms by auto
   983     from A' B' have "(y - z1) * f'' z3 \<ge> 0"
   984       by auto
   985     from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
   986       by auto
   987     from mult_right_mono_neg[OF this le(2)]
   988     have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   989       by (simp add: algebra_simps)
   990     then have "f' y * (x - y) - (f x - f y) \<le> 0"
   991       using le by auto
   992     then have res: "f' y * (x - y) \<le> f x - f y"
   993       by auto
   994     have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   995       using * z1 by auto
   996     also have "\<dots> = (z1 - x) * f'' z2"
   997       using z2 by auto
   998     finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
   999       by simp
  1000     have A: "z1 - x \<ge> 0"
  1001       using z1 by auto
  1002     have "z2 \<in> C"
  1003       using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
  1004       by fastforce
  1005     then have B: "f'' z2 \<ge> 0"
  1006       using assms by auto
  1007     from A B have "(z1 - x) * f'' z2 \<ge> 0"
  1008       by auto
  1009     with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
  1010       by auto
  1011     from mult_right_mono[OF this ge(2)]
  1012     have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
  1013       by (simp add: algebra_simps)
  1014     then have "f y - f x - f' x * (y - x) \<ge> 0"
  1015       using ge by auto
  1016     then show "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
  1017       using res by auto
  1018   qed
  1019   show ?thesis
  1020   proof (cases "x = y")
  1021     case True
  1022     with x y show ?thesis by auto
  1023   next
  1024     case False
  1025     with less_imp x y show ?thesis
  1026       by (auto simp: neq_iff)
  1027   qed
  1028 qed
  1029 
  1030 lemma f''_ge0_imp_convex:
  1031   fixes f :: "real \<Rightarrow> real"
  1032   assumes conv: "convex C"
  1033     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
  1034     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
  1035     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
  1036   shows "convex_on C f"
  1037   using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
  1038   by fastforce
  1039 
  1040 lemma minus_log_convex:
  1041   fixes b :: real
  1042   assumes "b > 1"
  1043   shows "convex_on {0 <..} (\<lambda> x. - log b x)"
  1044 proof -
  1045   have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
  1046     using DERIV_log by auto
  1047   then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
  1048     by (auto simp: DERIV_minus)
  1049   have "\<And>z::real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
  1050     using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
  1051   from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
  1052   have "\<And>z::real. z > 0 \<Longrightarrow>
  1053     DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
  1054     by auto
  1055   then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
  1056     DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
  1057     unfolding inverse_eq_divide by (auto simp: mult.assoc)
  1058   have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
  1059     using \<open>b > 1\<close> by (auto intro!: less_imp_le)
  1060   from f''_ge0_imp_convex[OF pos_is_convex, unfolded greaterThan_iff, OF f' f''0 f''_ge0]
  1061   show ?thesis
  1062     by auto
  1063 qed
  1064 
  1065 
  1066 subsection \<open>Convexity of real functions\<close>
  1067 
  1068 lemma convex_on_realI:
  1069   assumes "connected A"
  1070     and "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
  1071     and "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f' x \<le> f' y"
  1072   shows "convex_on A f"
  1073 proof (rule convex_on_linorderI)
  1074   fix t x y :: real
  1075   assume t: "t > 0" "t < 1"
  1076   assume xy: "x \<in> A" "y \<in> A" "x < y"
  1077   define z where "z = (1 - t) * x + t * y"
  1078   with \<open>connected A\<close> and xy have ivl: "{x..y} \<subseteq> A"
  1079     using connected_contains_Icc by blast
  1080 
  1081   from xy t have xz: "z > x"
  1082     by (simp add: z_def algebra_simps)
  1083   have "y - z = (1 - t) * (y - x)"
  1084     by (simp add: z_def algebra_simps)
  1085   also from xy t have "\<dots> > 0"
  1086     by (intro mult_pos_pos) simp_all
  1087   finally have yz: "z < y"
  1088     by simp
  1089 
  1090   from assms xz yz ivl t have "\<exists>\<xi>. \<xi> > x \<and> \<xi> < z \<and> f z - f x = (z - x) * f' \<xi>"
  1091     by (intro MVT2) (auto intro!: assms(2))
  1092   then obtain \<xi> where \<xi>: "\<xi> > x" "\<xi> < z" "f' \<xi> = (f z - f x) / (z - x)"
  1093     by auto
  1094   from assms xz yz ivl t have "\<exists>\<eta>. \<eta> > z \<and> \<eta> < y \<and> f y - f z = (y - z) * f' \<eta>"
  1095     by (intro MVT2) (auto intro!: assms(2))
  1096   then obtain \<eta> where \<eta>: "\<eta> > z" "\<eta> < y" "f' \<eta> = (f y - f z) / (y - z)"
  1097     by auto
  1098 
  1099   from \<eta>(3) have "(f y - f z) / (y - z) = f' \<eta>" ..
  1100   also from \<xi> \<eta> ivl have "\<xi> \<in> A" "\<eta> \<in> A"
  1101     by auto
  1102   with \<xi> \<eta> have "f' \<eta> \<ge> f' \<xi>"
  1103     by (intro assms(3)) auto
  1104   also from \<xi>(3) have "f' \<xi> = (f z - f x) / (z - x)" .
  1105   finally have "(f y - f z) * (z - x) \<ge> (f z - f x) * (y - z)"
  1106     using xz yz by (simp add: field_simps)
  1107   also have "z - x = t * (y - x)"
  1108     by (simp add: z_def algebra_simps)
  1109   also have "y - z = (1 - t) * (y - x)"
  1110     by (simp add: z_def algebra_simps)
  1111   finally have "(f y - f z) * t \<ge> (f z - f x) * (1 - t)"
  1112     using xy by simp
  1113   then show "(1 - t) * f x + t * f y \<ge> f ((1 - t) *\<^sub>R x + t *\<^sub>R y)"
  1114     by (simp add: z_def algebra_simps)
  1115 qed
  1116 
  1117 lemma convex_on_inverse:
  1118   assumes "A \<subseteq> {0<..}"
  1119   shows "convex_on A (inverse :: real \<Rightarrow> real)"
  1120 proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\<lambda>x. -inverse (x^2)"])
  1121   fix u v :: real
  1122   assume "u \<in> {0<..}" "v \<in> {0<..}" "u \<le> v"
  1123   with assms show "-inverse (u^2) \<le> -inverse (v^2)"
  1124     by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all)
  1125 qed (insert assms, auto intro!: derivative_eq_intros simp: divide_simps power2_eq_square)
  1126 
  1127 lemma convex_onD_Icc':
  1128   assumes "convex_on {x..y} f" "c \<in> {x..y}"
  1129   defines "d \<equiv> y - x"
  1130   shows "f c \<le> (f y - f x) / d * (c - x) + f x"
  1131 proof (cases x y rule: linorder_cases)
  1132   case less
  1133   then have d: "d > 0"
  1134     by (simp add: d_def)
  1135   from assms(2) less have A: "0 \<le> (c - x) / d" "(c - x) / d \<le> 1"
  1136     by (simp_all add: d_def divide_simps)
  1137   have "f c = f (x + (c - x) * 1)"
  1138     by simp
  1139   also from less have "1 = ((y - x) / d)"
  1140     by (simp add: d_def)
  1141   also from d have "x + (c - x) * \<dots> = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y"
  1142     by (simp add: field_simps)
  1143   also have "f \<dots> \<le> (1 - (c - x) / d) * f x + (c - x) / d * f y"
  1144     using assms less by (intro convex_onD_Icc) simp_all
  1145   also from d have "\<dots> = (f y - f x) / d * (c - x) + f x"
  1146     by (simp add: field_simps)
  1147   finally show ?thesis .
  1148 qed (insert assms(2), simp_all)
  1149 
  1150 lemma convex_onD_Icc'':
  1151   assumes "convex_on {x..y} f" "c \<in> {x..y}"
  1152   defines "d \<equiv> y - x"
  1153   shows "f c \<le> (f x - f y) / d * (y - c) + f y"
  1154 proof (cases x y rule: linorder_cases)
  1155   case less
  1156   then have d: "d > 0"
  1157     by (simp add: d_def)
  1158   from assms(2) less have A: "0 \<le> (y - c) / d" "(y - c) / d \<le> 1"
  1159     by (simp_all add: d_def divide_simps)
  1160   have "f c = f (y - (y - c) * 1)"
  1161     by simp
  1162   also from less have "1 = ((y - x) / d)"
  1163     by (simp add: d_def)
  1164   also from d have "y - (y - c) * \<dots> = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y"
  1165     by (simp add: field_simps)
  1166   also have "f \<dots> \<le> (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y"
  1167     using assms less by (intro convex_onD_Icc) (simp_all add: field_simps)
  1168   also from d have "\<dots> = (f x - f y) / d * (y - c) + f y"
  1169     by (simp add: field_simps)
  1170   finally show ?thesis .
  1171 qed (insert assms(2), simp_all)
  1172 
  1173 lemma convex_supp_sum:
  1174   assumes "convex S" and 1: "supp_sum u I = 1"
  1175       and "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> u i \<and> (u i = 0 \<or> f i \<in> S)"
  1176     shows "supp_sum (\<lambda>i. u i *\<^sub>R f i) I \<in> S"
  1177 proof -
  1178   have fin: "finite {i \<in> I. u i \<noteq> 0}"
  1179     using 1 sum.infinite by (force simp: supp_sum_def support_on_def)
  1180   then have eq: "supp_sum (\<lambda>i. u i *\<^sub>R f i) I = sum (\<lambda>i. u i *\<^sub>R f i) {i \<in> I. u i \<noteq> 0}"
  1181     by (force intro: sum.mono_neutral_left simp: supp_sum_def support_on_def)
  1182   show ?thesis
  1183     apply (simp add: eq)
  1184     apply (rule convex_sum [OF fin \<open>convex S\<close>])
  1185     using 1 assms apply (auto simp: supp_sum_def support_on_def)
  1186     done
  1187 qed
  1188 
  1189 lemma convex_translation_eq [simp]: "convex ((\<lambda>x. a + x) ` s) \<longleftrightarrow> convex s"
  1190   by (metis convex_translation translation_galois)
  1191 
  1192 lemma convex_linear_image_eq [simp]:
  1193     fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
  1194     shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> convex (f ` s) \<longleftrightarrow> convex s"
  1195     by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq)
  1196 
  1197 lemma basis_to_basis_subspace_isomorphism:
  1198   assumes s: "subspace (S:: ('n::euclidean_space) set)"
  1199     and t: "subspace (T :: ('m::euclidean_space) set)"
  1200     and d: "dim S = dim T"
  1201     and B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S"
  1202     and C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "card C = dim T"
  1203   shows "\<exists>f. linear f \<and> f ` B = C \<and> f ` S = T \<and> inj_on f S"
  1204 proof -
  1205   from B independent_bound have fB: "finite B"
  1206     by blast
  1207   from C independent_bound have fC: "finite C"
  1208     by blast
  1209   from B(4) C(4) card_le_inj[of B C] d obtain f where
  1210     f: "f ` B \<subseteq> C" "inj_on f B" using \<open>finite B\<close> \<open>finite C\<close> by auto
  1211   from linear_independent_extend[OF B(2)] obtain g where
  1212     g: "linear g" "\<forall>x \<in> B. g x = f x" by blast
  1213   from inj_on_iff_eq_card[OF fB, of f] f(2)
  1214   have "card (f ` B) = card B" by simp
  1215   with B(4) C(4) have ceq: "card (f ` B) = card C" using d
  1216     by simp
  1217   have "g ` B = f ` B" using g(2)
  1218     by (auto simp add: image_iff)
  1219   also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
  1220   finally have gBC: "g ` B = C" .
  1221   have gi: "inj_on g B" using f(2) g(2)
  1222     by (auto simp add: inj_on_def)
  1223   note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
  1224   {
  1225     fix x y
  1226     assume x: "x \<in> S" and y: "y \<in> S" and gxy: "g x = g y"
  1227     from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B"
  1228       by blast+
  1229     from gxy have th0: "g (x - y) = 0"
  1230       by (simp add: linear_diff[OF g(1)])
  1231     have th1: "x - y \<in> span B" using x' y'
  1232       by (metis span_diff)
  1233     have "x = y" using g0[OF th1 th0] by simp
  1234   }
  1235   then have giS: "inj_on g S" unfolding inj_on_def by blast
  1236   from span_subspace[OF B(1,3) s]
  1237   have "g ` S = span (g ` B)"
  1238     by (simp add: span_linear_image[OF g(1)])
  1239   also have "\<dots> = span C"
  1240     unfolding gBC ..
  1241   also have "\<dots> = T"
  1242     using span_subspace[OF C(1,3) t] .
  1243   finally have gS: "g ` S = T" .
  1244   from g(1) gS giS gBC show ?thesis
  1245     by blast
  1246 qed
  1247 
  1248 lemma closure_bounded_linear_image_subset:
  1249   assumes f: "bounded_linear f"
  1250   shows "f ` closure S \<subseteq> closure (f ` S)"
  1251   using linear_continuous_on [OF f] closed_closure closure_subset
  1252   by (rule image_closure_subset)
  1253 
  1254 lemma closure_linear_image_subset:
  1255   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::real_normed_vector"
  1256   assumes "linear f"
  1257   shows "f ` (closure S) \<subseteq> closure (f ` S)"
  1258   using assms unfolding linear_conv_bounded_linear
  1259   by (rule closure_bounded_linear_image_subset)
  1260 
  1261 lemma closed_injective_linear_image:
  1262     fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1263     assumes S: "closed S" and f: "linear f" "inj f"
  1264     shows "closed (f ` S)"
  1265 proof -
  1266   obtain g where g: "linear g" "g \<circ> f = id"
  1267     using linear_injective_left_inverse [OF f] by blast
  1268   then have confg: "continuous_on (range f) g"
  1269     using linear_continuous_on linear_conv_bounded_linear by blast
  1270   have [simp]: "g ` f ` S = S"
  1271     using g by (simp add: image_comp)
  1272   have cgf: "closed (g ` f ` S)"
  1273     by (simp add: \<open>g \<circ> f = id\<close> S image_comp)
  1274   have [simp]: "(range f \<inter> g -` S) = f ` S"
  1275     using g unfolding o_def id_def image_def by auto metis+
  1276   show ?thesis
  1277   proof (rule closedin_closed_trans [of "range f"])
  1278     show "closedin (subtopology euclidean (range f)) (f ` S)"
  1279       using continuous_closedin_preimage [OF confg cgf] by simp
  1280     show "closed (range f)"
  1281       apply (rule closed_injective_image_subspace)
  1282       using f apply (auto simp: linear_linear linear_injective_0)
  1283       done
  1284   qed
  1285 qed
  1286 
  1287 lemma closed_injective_linear_image_eq:
  1288     fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1289     assumes f: "linear f" "inj f"
  1290       shows "(closed(image f s) \<longleftrightarrow> closed s)"
  1291   by (metis closed_injective_linear_image closure_eq closure_linear_image_subset closure_subset_eq f(1) f(2) inj_image_subset_iff)
  1292 
  1293 lemma closure_injective_linear_image:
  1294     fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1295     shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
  1296   apply (rule subset_antisym)
  1297   apply (simp add: closure_linear_image_subset)
  1298   by (simp add: closure_minimal closed_injective_linear_image closure_subset image_mono)
  1299 
  1300 lemma closure_bounded_linear_image:
  1301     fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1302     shows "\<lbrakk>linear f; bounded S\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
  1303   apply (rule subset_antisym, simp add: closure_linear_image_subset)
  1304   apply (rule closure_minimal, simp add: closure_subset image_mono)
  1305   by (meson bounded_closure closed_closure compact_continuous_image compact_eq_bounded_closed linear_continuous_on linear_conv_bounded_linear)
  1306 
  1307 lemma closure_scaleR:
  1308   fixes S :: "'a::real_normed_vector set"
  1309   shows "(op *\<^sub>R c) ` (closure S) = closure ((op *\<^sub>R c) ` S)"
  1310 proof
  1311   show "(op *\<^sub>R c) ` (closure S) \<subseteq> closure ((op *\<^sub>R c) ` S)"
  1312     using bounded_linear_scaleR_right
  1313     by (rule closure_bounded_linear_image_subset)
  1314   show "closure ((op *\<^sub>R c) ` S) \<subseteq> (op *\<^sub>R c) ` (closure S)"
  1315     by (intro closure_minimal image_mono closure_subset closed_scaling closed_closure)
  1316 qed
  1317 
  1318 lemma fst_linear: "linear fst"
  1319   unfolding linear_iff by (simp add: algebra_simps)
  1320 
  1321 lemma snd_linear: "linear snd"
  1322   unfolding linear_iff by (simp add: algebra_simps)
  1323 
  1324 lemma fst_snd_linear: "linear (\<lambda>(x,y). x + y)"
  1325   unfolding linear_iff by (simp add: algebra_simps)
  1326 
  1327 lemma vector_choose_size:
  1328   assumes "0 \<le> c"
  1329   obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c"
  1330 proof -
  1331   obtain a::'a where "a \<noteq> 0"
  1332     using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce
  1333   then show ?thesis
  1334     by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms)
  1335 qed
  1336 
  1337 lemma vector_choose_dist:
  1338   assumes "0 \<le> c"
  1339   obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c"
  1340 by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size)
  1341 
  1342 lemma sphere_eq_empty [simp]:
  1343   fixes a :: "'a::{real_normed_vector, perfect_space}"
  1344   shows "sphere a r = {} \<longleftrightarrow> r < 0"
  1345 by (auto simp: sphere_def dist_norm) (metis dist_norm le_less_linear vector_choose_dist)
  1346 
  1347 lemma sum_delta_notmem:
  1348   assumes "x \<notin> s"
  1349   shows "sum (\<lambda>y. if (y = x) then P x else Q y) s = sum Q s"
  1350     and "sum (\<lambda>y. if (x = y) then P x else Q y) s = sum Q s"
  1351     and "sum (\<lambda>y. if (y = x) then P y else Q y) s = sum Q s"
  1352     and "sum (\<lambda>y. if (x = y) then P y else Q y) s = sum Q s"
  1353   apply (rule_tac [!] sum.cong)
  1354   using assms
  1355   apply auto
  1356   done
  1357 
  1358 lemma sum_delta'':
  1359   fixes s::"'a::real_vector set"
  1360   assumes "finite s"
  1361   shows "(\<Sum>x\<in>s. (if y = x then f x else 0) *\<^sub>R x) = (if y\<in>s then (f y) *\<^sub>R y else 0)"
  1362 proof -
  1363   have *: "\<And>x y. (if y = x then f x else (0::real)) *\<^sub>R x = (if x=y then (f x) *\<^sub>R x else 0)"
  1364     by auto
  1365   show ?thesis
  1366     unfolding * using sum.delta[OF assms, of y "\<lambda>x. f x *\<^sub>R x"] by auto
  1367 qed
  1368 
  1369 lemma if_smult: "(if P then x else (y::real)) *\<^sub>R v = (if P then x *\<^sub>R v else y *\<^sub>R v)"
  1370   by (fact if_distrib)
  1371 
  1372 lemma dist_triangle_eq:
  1373   fixes x y z :: "'a::real_inner"
  1374   shows "dist x z = dist x y + dist y z \<longleftrightarrow>
  1375     norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)"
  1376 proof -
  1377   have *: "x - y + (y - z) = x - z" by auto
  1378   show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *]
  1379     by (auto simp add:norm_minus_commute)
  1380 qed
  1381 
  1382 
  1383 subsection \<open>Affine set and affine hull\<close>
  1384 
  1385 definition affine :: "'a::real_vector set \<Rightarrow> bool"
  1386   where "affine s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u v. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
  1387 
  1388 lemma affine_alt: "affine s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u::real. (1 - u) *\<^sub>R x + u *\<^sub>R y \<in> s)"
  1389   unfolding affine_def by (metis eq_diff_eq')
  1390 
  1391 lemma affine_empty [iff]: "affine {}"
  1392   unfolding affine_def by auto
  1393 
  1394 lemma affine_sing [iff]: "affine {x}"
  1395   unfolding affine_alt by (auto simp add: scaleR_left_distrib [symmetric])
  1396 
  1397 lemma affine_UNIV [iff]: "affine UNIV"
  1398   unfolding affine_def by auto
  1399 
  1400 lemma affine_Inter [intro]: "(\<And>s. s\<in>f \<Longrightarrow> affine s) \<Longrightarrow> affine (\<Inter>f)"
  1401   unfolding affine_def by auto
  1402 
  1403 lemma affine_Int[intro]: "affine s \<Longrightarrow> affine t \<Longrightarrow> affine (s \<inter> t)"
  1404   unfolding affine_def by auto
  1405 
  1406 lemma affine_scaling: "affine s \<Longrightarrow> affine (image (\<lambda>x. c *\<^sub>R x) s)"
  1407   apply (clarsimp simp add: affine_def)
  1408   apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI)
  1409   apply (auto simp: algebra_simps)
  1410   done
  1411 
  1412 lemma affine_affine_hull [simp]: "affine(affine hull s)"
  1413   unfolding hull_def
  1414   using affine_Inter[of "{t. affine t \<and> s \<subseteq> t}"] by auto
  1415 
  1416 lemma affine_hull_eq[simp]: "(affine hull s = s) \<longleftrightarrow> affine s"
  1417   by (metis affine_affine_hull hull_same)
  1418 
  1419 lemma affine_hyperplane: "affine {x. a \<bullet> x = b}"
  1420   by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
  1421 
  1422 
  1423 subsubsection \<open>Some explicit formulations (from Lars Schewe)\<close>
  1424 
  1425 lemma affine:
  1426   fixes V::"'a::real_vector set"
  1427   shows "affine V \<longleftrightarrow>
  1428     (\<forall>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> V \<and> sum u s = 1 \<longrightarrow> (sum (\<lambda>x. (u x) *\<^sub>R x)) s \<in> V)"
  1429   unfolding affine_def
  1430   apply rule
  1431   apply(rule, rule, rule)
  1432   apply(erule conjE)+
  1433   defer
  1434   apply (rule, rule, rule, rule, rule)
  1435 proof -
  1436   fix x y u v
  1437   assume as: "x \<in> V" "y \<in> V" "u + v = (1::real)"
  1438     "\<forall>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> V \<and> sum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
  1439   then show "u *\<^sub>R x + v *\<^sub>R y \<in> V"
  1440     apply (cases "x = y")
  1441     using as(4)[THEN spec[where x="{x,y}"], THEN spec[where x="\<lambda>w. if w = x then u else v"]]
  1442       and as(1-3)
  1443     apply (auto simp add: scaleR_left_distrib[symmetric])
  1444     done
  1445 next
  1446   fix s u
  1447   assume as: "\<forall>x\<in>V. \<forall>y\<in>V. \<forall>u v. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V"
  1448     "finite s" "s \<noteq> {}" "s \<subseteq> V" "sum u s = (1::real)"
  1449   define n where "n = card s"
  1450   have "card s = 0 \<or> card s = 1 \<or> card s = 2 \<or> card s > 2" by auto
  1451   then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
  1452   proof (auto simp only: disjE)
  1453     assume "card s = 2"
  1454     then have "card s = Suc (Suc 0)"
  1455       by auto
  1456     then obtain a b where "s = {a, b}"
  1457       unfolding card_Suc_eq by auto
  1458     then show ?thesis
  1459       using as(1)[THEN bspec[where x=a], THEN bspec[where x=b]] using as(4,5)
  1460       by (auto simp add: sum_clauses(2))
  1461   next
  1462     assume "card s > 2"
  1463     then show ?thesis using as and n_def
  1464     proof (induct n arbitrary: u s)
  1465       case 0
  1466       then show ?case by auto
  1467     next
  1468       case (Suc n)
  1469       fix s :: "'a set" and u :: "'a \<Rightarrow> real"
  1470       assume IA:
  1471         "\<And>u s.  \<lbrakk>2 < card s; \<forall>x\<in>V. \<forall>y\<in>V. \<forall>u v. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V; finite s;
  1472           s \<noteq> {}; s \<subseteq> V; sum u s = 1; n = card s \<rbrakk> \<Longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
  1473         and as:
  1474           "Suc n = card s" "2 < card s" "\<forall>x\<in>V. \<forall>y\<in>V. \<forall>u v. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V"
  1475            "finite s" "s \<noteq> {}" "s \<subseteq> V" "sum u s = 1"
  1476       have "\<exists>x\<in>s. u x \<noteq> 1"
  1477       proof (rule ccontr)
  1478         assume "\<not> ?thesis"
  1479         then have "sum u s = real_of_nat (card s)"
  1480           unfolding card_eq_sum by auto
  1481         then show False
  1482           using as(7) and \<open>card s > 2\<close>
  1483           by (metis One_nat_def less_Suc0 Zero_not_Suc of_nat_1 of_nat_eq_iff numeral_2_eq_2)
  1484       qed
  1485       then obtain x where x:"x \<in> s" "u x \<noteq> 1" by auto
  1486 
  1487       have c: "card (s - {x}) = card s - 1"
  1488         apply (rule card_Diff_singleton)
  1489         using \<open>x\<in>s\<close> as(4)
  1490         apply auto
  1491         done
  1492       have *: "s = insert x (s - {x})" "finite (s - {x})"
  1493         using \<open>x\<in>s\<close> and as(4) by auto
  1494       have **: "sum u (s - {x}) = 1 - u x"
  1495         using sum_clauses(2)[OF *(2), of u x, unfolded *(1)[symmetric] as(7)] by auto
  1496       have ***: "inverse (1 - u x) * sum u (s - {x}) = 1"
  1497         unfolding ** using \<open>u x \<noteq> 1\<close> by auto
  1498       have "(\<Sum>xa\<in>s - {x}. (inverse (1 - u x) * u xa) *\<^sub>R xa) \<in> V"
  1499       proof (cases "card (s - {x}) > 2")
  1500         case True
  1501         then have "s - {x} \<noteq> {}" "card (s - {x}) = n"
  1502           unfolding c and as(1)[symmetric]
  1503         proof (rule_tac ccontr)
  1504           assume "\<not> s - {x} \<noteq> {}"
  1505           then have "card (s - {x}) = 0" unfolding card_0_eq[OF *(2)] by simp
  1506           then show False using True by auto
  1507         qed auto
  1508         then show ?thesis
  1509           apply (rule_tac IA[of "s - {x}" "\<lambda>y. (inverse (1 - u x) * u y)"])
  1510           unfolding sum_distrib_left[symmetric]
  1511           using as and *** and True
  1512           apply auto
  1513           done
  1514       next
  1515         case False
  1516         then have "card (s - {x}) = Suc (Suc 0)"
  1517           using as(2) and c by auto
  1518         then obtain a b where "(s - {x}) = {a, b}" "a\<noteq>b"
  1519           unfolding card_Suc_eq by auto
  1520         then show ?thesis
  1521           using as(3)[THEN bspec[where x=a], THEN bspec[where x=b]]
  1522           using *** *(2) and \<open>s \<subseteq> V\<close>
  1523           unfolding sum_distrib_left
  1524           by (auto simp add: sum_clauses(2))
  1525       qed
  1526       then have "u x + (1 - u x) = 1 \<Longrightarrow>
  1527           u x *\<^sub>R x + (1 - u x) *\<^sub>R ((\<Sum>xa\<in>s - {x}. u xa *\<^sub>R xa) /\<^sub>R (1 - u x)) \<in> V"
  1528         apply -
  1529         apply (rule as(3)[rule_format])
  1530         unfolding  Real_Vector_Spaces.scaleR_right.sum
  1531         using x(1) as(6)
  1532         apply auto
  1533         done
  1534       then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
  1535         unfolding scaleR_scaleR[symmetric] and scaleR_right.sum [symmetric]
  1536         apply (subst *)
  1537         unfolding sum_clauses(2)[OF *(2)]
  1538         using \<open>u x \<noteq> 1\<close>
  1539         apply auto
  1540         done
  1541     qed
  1542   next
  1543     assume "card s = 1"
  1544     then obtain a where "s={a}"
  1545       by (auto simp add: card_Suc_eq)
  1546     then show ?thesis
  1547       using as(4,5) by simp
  1548   qed (insert \<open>s\<noteq>{}\<close> \<open>finite s\<close>, auto)
  1549 qed
  1550 
  1551 lemma affine_hull_explicit:
  1552   "affine hull p =
  1553     {y. \<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and> sum u s = 1 \<and> sum (\<lambda>v. (u v) *\<^sub>R v) s = y}"
  1554   apply (rule hull_unique)
  1555   apply (subst subset_eq)
  1556   prefer 3
  1557   apply rule
  1558   unfolding mem_Collect_eq
  1559   apply (erule exE)+
  1560   apply (erule conjE)+
  1561   prefer 2
  1562   apply rule
  1563 proof -
  1564   fix x
  1565   assume "x\<in>p"
  1566   then show "\<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1567     apply (rule_tac x="{x}" in exI)
  1568     apply (rule_tac x="\<lambda>x. 1" in exI)
  1569     apply auto
  1570     done
  1571 next
  1572   fix t x s u
  1573   assume as: "p \<subseteq> t" "affine t" "finite s" "s \<noteq> {}"
  1574     "s \<subseteq> p" "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1575   then show "x \<in> t"
  1576     using as(2)[unfolded affine, THEN spec[where x=s], THEN spec[where x=u]]
  1577     by auto
  1578 next
  1579   show "affine {y. \<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y}"
  1580     unfolding affine_def
  1581     apply (rule, rule, rule, rule, rule)
  1582     unfolding mem_Collect_eq
  1583   proof -
  1584     fix u v :: real
  1585     assume uv: "u + v = 1"
  1586     fix x
  1587     assume "\<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1588     then obtain sx ux where
  1589       x: "finite sx" "sx \<noteq> {}" "sx \<subseteq> p" "sum ux sx = 1" "(\<Sum>v\<in>sx. ux v *\<^sub>R v) = x"
  1590       by auto
  1591     fix y
  1592     assume "\<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1593     then obtain sy uy where
  1594       y: "finite sy" "sy \<noteq> {}" "sy \<subseteq> p" "sum uy sy = 1" "(\<Sum>v\<in>sy. uy v *\<^sub>R v) = y" by auto
  1595     have xy: "finite (sx \<union> sy)"
  1596       using x(1) y(1) by auto
  1597     have **: "(sx \<union> sy) \<inter> sx = sx" "(sx \<union> sy) \<inter> sy = sy"
  1598       by auto
  1599     show "\<exists>s ua. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and>
  1600         sum ua s = 1 \<and> (\<Sum>v\<in>s. ua v *\<^sub>R v) = u *\<^sub>R x + v *\<^sub>R y"
  1601       apply (rule_tac x="sx \<union> sy" in exI)
  1602       apply (rule_tac x="\<lambda>a. (if a\<in>sx then u * ux a else 0) + (if a\<in>sy then v * uy a else 0)" in exI)
  1603       unfolding scaleR_left_distrib sum.distrib if_smult scaleR_zero_left
  1604         ** sum.inter_restrict[OF xy, symmetric]
  1605       unfolding scaleR_scaleR[symmetric] Real_Vector_Spaces.scaleR_right.sum [symmetric]
  1606         and sum_distrib_left[symmetric]
  1607       unfolding x y
  1608       using x(1-3) y(1-3) uv
  1609       apply simp
  1610       done
  1611   qed
  1612 qed
  1613 
  1614 lemma affine_hull_finite:
  1615   assumes "finite s"
  1616   shows "affine hull s = {y. \<exists>u. sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  1617   unfolding affine_hull_explicit and set_eq_iff and mem_Collect_eq
  1618   apply (rule, rule)
  1619   apply (erule exE)+
  1620   apply (erule conjE)+
  1621   defer
  1622   apply (erule exE)
  1623   apply (erule conjE)
  1624 proof -
  1625   fix x u
  1626   assume "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1627   then show "\<exists>sa u. finite sa \<and>
  1628       \<not> (\<forall>x. (x \<in> sa) = (x \<in> {})) \<and> sa \<subseteq> s \<and> sum u sa = 1 \<and> (\<Sum>v\<in>sa. u v *\<^sub>R v) = x"
  1629     apply (rule_tac x=s in exI, rule_tac x=u in exI)
  1630     using assms
  1631     apply auto
  1632     done
  1633 next
  1634   fix x t u
  1635   assume "t \<subseteq> s"
  1636   then have *: "s \<inter> t = t"
  1637     by auto
  1638   assume "finite t" "\<not> (\<forall>x. (x \<in> t) = (x \<in> {}))" "sum u t = 1" "(\<Sum>v\<in>t. u v *\<^sub>R v) = x"
  1639   then show "\<exists>u. sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1640     apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
  1641     unfolding if_smult scaleR_zero_left and sum.inter_restrict[OF assms, symmetric] and *
  1642     apply auto
  1643     done
  1644 qed
  1645 
  1646 
  1647 subsubsection \<open>Stepping theorems and hence small special cases\<close>
  1648 
  1649 lemma affine_hull_empty[simp]: "affine hull {} = {}"
  1650   by (rule hull_unique) auto
  1651 
  1652 (*could delete: it simply rewrites sum expressions, but it's used twice*)
  1653 lemma affine_hull_finite_step:
  1654   fixes y :: "'a::real_vector"
  1655   shows
  1656     "(\<exists>u. sum u {} = w \<and> sum (\<lambda>x. u x *\<^sub>R x) {} = y) \<longleftrightarrow> w = 0 \<and> y = 0" (is ?th1)
  1657     and
  1658     "finite s \<Longrightarrow>
  1659       (\<exists>u. sum u (insert a s) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a s) = y) \<longleftrightarrow>
  1660       (\<exists>v u. sum u s = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y - v *\<^sub>R a)" (is "_ \<Longrightarrow> ?lhs = ?rhs")
  1661 proof -
  1662   show ?th1 by simp
  1663   assume fin: "finite s"
  1664   show "?lhs = ?rhs"
  1665   proof
  1666     assume ?lhs
  1667     then obtain u where u: "sum u (insert a s) = w \<and> (\<Sum>x\<in>insert a s. u x *\<^sub>R x) = y"
  1668       by auto
  1669     show ?rhs
  1670     proof (cases "a \<in> s")
  1671       case True
  1672       then have *: "insert a s = s" by auto
  1673       show ?thesis
  1674         using u[unfolded *]
  1675         apply(rule_tac x=0 in exI)
  1676         apply auto
  1677         done
  1678     next
  1679       case False
  1680       then show ?thesis
  1681         apply (rule_tac x="u a" in exI)
  1682         using u and fin
  1683         apply auto
  1684         done
  1685     qed
  1686   next
  1687     assume ?rhs
  1688     then obtain v u where vu: "sum u s = w - v"  "(\<Sum>x\<in>s. u x *\<^sub>R x) = y - v *\<^sub>R a"
  1689       by auto
  1690     have *: "\<And>x M. (if x = a then v else M) *\<^sub>R x = (if x = a then v *\<^sub>R x else M *\<^sub>R x)"
  1691       by auto
  1692     show ?lhs
  1693     proof (cases "a \<in> s")
  1694       case True
  1695       then show ?thesis
  1696         apply (rule_tac x="\<lambda>x. (if x=a then v else 0) + u x" in exI)
  1697         unfolding sum_clauses(2)[OF fin]
  1698         apply simp
  1699         unfolding scaleR_left_distrib and sum.distrib
  1700         unfolding vu and * and scaleR_zero_left
  1701         apply (auto simp add: sum.delta[OF fin])
  1702         done
  1703     next
  1704       case False
  1705       then have **:
  1706         "\<And>x. x \<in> s \<Longrightarrow> u x = (if x = a then v else u x)"
  1707         "\<And>x. x \<in> s \<Longrightarrow> u x *\<^sub>R x = (if x = a then v *\<^sub>R x else u x *\<^sub>R x)" by auto
  1708       from False show ?thesis
  1709         apply (rule_tac x="\<lambda>x. if x=a then v else u x" in exI)
  1710         unfolding sum_clauses(2)[OF fin] and * using vu
  1711         using sum.cong [of s _ "\<lambda>x. u x *\<^sub>R x" "\<lambda>x. if x = a then v *\<^sub>R x else u x *\<^sub>R x", OF _ **(2)]
  1712         using sum.cong [of s _ u "\<lambda>x. if x = a then v else u x", OF _ **(1)]
  1713         apply auto
  1714         done
  1715     qed
  1716   qed
  1717 qed
  1718 
  1719 lemma affine_hull_2:
  1720   fixes a b :: "'a::real_vector"
  1721   shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}"
  1722   (is "?lhs = ?rhs")
  1723 proof -
  1724   have *:
  1725     "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  1726     "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  1727   have "?lhs = {y. \<exists>u. sum u {a, b} = 1 \<and> (\<Sum>v\<in>{a, b}. u v *\<^sub>R v) = y}"
  1728     using affine_hull_finite[of "{a,b}"] by auto
  1729   also have "\<dots> = {y. \<exists>v u. u b = 1 - v \<and> u b *\<^sub>R b = y - v *\<^sub>R a}"
  1730     by (simp add: affine_hull_finite_step(2)[of "{b}" a])
  1731   also have "\<dots> = ?rhs" unfolding * by auto
  1732   finally show ?thesis by auto
  1733 qed
  1734 
  1735 lemma affine_hull_3:
  1736   fixes a b c :: "'a::real_vector"
  1737   shows "affine hull {a,b,c} = { u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c| u v w. u + v + w = 1}"
  1738 proof -
  1739   have *:
  1740     "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  1741     "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  1742   show ?thesis
  1743     apply (simp add: affine_hull_finite affine_hull_finite_step)
  1744     unfolding *
  1745     apply auto
  1746     apply (rule_tac x=v in exI)
  1747     apply (rule_tac x=va in exI)
  1748     apply auto
  1749     apply (rule_tac x=u in exI)
  1750     apply force
  1751     done
  1752 qed
  1753 
  1754 lemma mem_affine:
  1755   assumes "affine S" "x \<in> S" "y \<in> S" "u + v = 1"
  1756   shows "u *\<^sub>R x + v *\<^sub>R y \<in> S"
  1757   using assms affine_def[of S] by auto
  1758 
  1759 lemma mem_affine_3:
  1760   assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S" "u + v + w = 1"
  1761   shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> S"
  1762 proof -
  1763   have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> affine hull {x, y, z}"
  1764     using affine_hull_3[of x y z] assms by auto
  1765   moreover
  1766   have "affine hull {x, y, z} \<subseteq> affine hull S"
  1767     using hull_mono[of "{x, y, z}" "S"] assms by auto
  1768   moreover
  1769   have "affine hull S = S"
  1770     using assms affine_hull_eq[of S] by auto
  1771   ultimately show ?thesis by auto
  1772 qed
  1773 
  1774 lemma mem_affine_3_minus:
  1775   assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S"
  1776   shows "x + v *\<^sub>R (y-z) \<in> S"
  1777   using mem_affine_3[of S x y z 1 v "-v"] assms
  1778   by (simp add: algebra_simps)
  1779 
  1780 corollary mem_affine_3_minus2:
  1781     "\<lbrakk>affine S; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> x - v *\<^sub>R (y-z) \<in> S"
  1782   by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left)
  1783 
  1784 
  1785 subsubsection \<open>Some relations between affine hull and subspaces\<close>
  1786 
  1787 lemma affine_hull_insert_subset_span:
  1788   "affine hull (insert a s) \<subseteq> {a + v| v . v \<in> span {x - a | x . x \<in> s}}"
  1789   unfolding subset_eq Ball_def
  1790   unfolding affine_hull_explicit span_explicit mem_Collect_eq
  1791   apply (rule, rule)
  1792   apply (erule exE)+
  1793   apply (erule conjE)+
  1794 proof -
  1795   fix x t u
  1796   assume as: "finite t" "t \<noteq> {}" "t \<subseteq> insert a s" "sum u t = 1" "(\<Sum>v\<in>t. u v *\<^sub>R v) = x"
  1797   have "(\<lambda>x. x - a) ` (t - {a}) \<subseteq> {x - a |x. x \<in> s}"
  1798     using as(3) by auto
  1799   then show "\<exists>v. x = a + v \<and> (\<exists>S u. finite S \<and> S \<subseteq> {x - a |x. x \<in> s} \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = v)"
  1800     apply (rule_tac x="x - a" in exI)
  1801     apply (rule conjI, simp)
  1802     apply (rule_tac x="(\<lambda>x. x - a) ` (t - {a})" in exI)
  1803     apply (rule_tac x="\<lambda>x. u (x + a)" in exI)
  1804     apply (rule conjI) using as(1) apply simp
  1805     apply (erule conjI)
  1806     using as(1)
  1807     apply (simp add: sum.reindex[unfolded inj_on_def] scaleR_right_diff_distrib
  1808       sum_subtractf scaleR_left.sum[symmetric] sum_diff1 scaleR_left_diff_distrib)
  1809     unfolding as
  1810     apply simp
  1811     done
  1812 qed
  1813 
  1814 lemma affine_hull_insert_span:
  1815   assumes "a \<notin> s"
  1816   shows "affine hull (insert a s) = {a + v | v . v \<in> span {x - a | x.  x \<in> s}}"
  1817   apply (rule, rule affine_hull_insert_subset_span)
  1818   unfolding subset_eq Ball_def
  1819   unfolding affine_hull_explicit and mem_Collect_eq
  1820 proof (rule, rule, erule exE, erule conjE)
  1821   fix y v
  1822   assume "y = a + v" "v \<in> span {x - a |x. x \<in> s}"
  1823   then obtain t u where obt: "finite t" "t \<subseteq> {x - a |x. x \<in> s}" "a + (\<Sum>v\<in>t. u v *\<^sub>R v) = y"
  1824     unfolding span_explicit by auto
  1825   define f where "f = (\<lambda>x. x + a) ` t"
  1826   have f: "finite f" "f \<subseteq> s" "(\<Sum>v\<in>f. u (v - a) *\<^sub>R (v - a)) = y - a"
  1827     unfolding f_def using obt by (auto simp add: sum.reindex[unfolded inj_on_def])
  1828   have *: "f \<inter> {a} = {}" "f \<inter> - {a} = f"
  1829     using f(2) assms by auto
  1830   show "\<exists>sa u. finite sa \<and> sa \<noteq> {} \<and> sa \<subseteq> insert a s \<and> sum u sa = 1 \<and> (\<Sum>v\<in>sa. u v *\<^sub>R v) = y"
  1831     apply (rule_tac x = "insert a f" in exI)
  1832     apply (rule_tac x = "\<lambda>x. if x=a then 1 - sum (\<lambda>x. u (x - a)) f else u (x - a)" in exI)
  1833     using assms and f
  1834     unfolding sum_clauses(2)[OF f(1)] and if_smult
  1835     unfolding sum.If_cases[OF f(1), of "\<lambda>x. x = a"]
  1836     apply (auto simp add: sum_subtractf scaleR_left.sum algebra_simps *)
  1837     done
  1838 qed
  1839 
  1840 lemma affine_hull_span:
  1841   assumes "a \<in> s"
  1842   shows "affine hull s = {a + v | v. v \<in> span {x - a | x. x \<in> s - {a}}}"
  1843   using affine_hull_insert_span[of a "s - {a}", unfolded insert_Diff[OF assms]] by auto
  1844 
  1845 
  1846 subsubsection \<open>Parallel affine sets\<close>
  1847 
  1848 definition affine_parallel :: "'a::real_vector set \<Rightarrow> 'a::real_vector set \<Rightarrow> bool"
  1849   where "affine_parallel S T \<longleftrightarrow> (\<exists>a. T = (\<lambda>x. a + x) ` S)"
  1850 
  1851 lemma affine_parallel_expl_aux:
  1852   fixes S T :: "'a::real_vector set"
  1853   assumes "\<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T"
  1854   shows "T = (\<lambda>x. a + x) ` S"
  1855 proof -
  1856   {
  1857     fix x
  1858     assume "x \<in> T"
  1859     then have "( - a) + x \<in> S"
  1860       using assms by auto
  1861     then have "x \<in> ((\<lambda>x. a + x) ` S)"
  1862       using imageI[of "-a+x" S "(\<lambda>x. a+x)"] by auto
  1863   }
  1864   moreover have "T \<ge> (\<lambda>x. a + x) ` S"
  1865     using assms by auto
  1866   ultimately show ?thesis by auto
  1867 qed
  1868 
  1869 lemma affine_parallel_expl: "affine_parallel S T \<longleftrightarrow> (\<exists>a. \<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T)"
  1870   unfolding affine_parallel_def
  1871   using affine_parallel_expl_aux[of S _ T] by auto
  1872 
  1873 lemma affine_parallel_reflex: "affine_parallel S S"
  1874   unfolding affine_parallel_def
  1875   apply (rule exI[of _ "0"])
  1876   apply auto
  1877   done
  1878 
  1879 lemma affine_parallel_commut:
  1880   assumes "affine_parallel A B"
  1881   shows "affine_parallel B A"
  1882 proof -
  1883   from assms obtain a where B: "B = (\<lambda>x. a + x) ` A"
  1884     unfolding affine_parallel_def by auto
  1885   have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  1886   from B show ?thesis
  1887     using translation_galois [of B a A]
  1888     unfolding affine_parallel_def by auto
  1889 qed
  1890 
  1891 lemma affine_parallel_assoc:
  1892   assumes "affine_parallel A B"
  1893     and "affine_parallel B C"
  1894   shows "affine_parallel A C"
  1895 proof -
  1896   from assms obtain ab where "B = (\<lambda>x. ab + x) ` A"
  1897     unfolding affine_parallel_def by auto
  1898   moreover
  1899   from assms obtain bc where "C = (\<lambda>x. bc + x) ` B"
  1900     unfolding affine_parallel_def by auto
  1901   ultimately show ?thesis
  1902     using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto
  1903 qed
  1904 
  1905 lemma affine_translation_aux:
  1906   fixes a :: "'a::real_vector"
  1907   assumes "affine ((\<lambda>x. a + x) ` S)"
  1908   shows "affine S"
  1909 proof -
  1910   {
  1911     fix x y u v
  1912     assume xy: "x \<in> S" "y \<in> S" "(u :: real) + v = 1"
  1913     then have "(a + x) \<in> ((\<lambda>x. a + x) ` S)" "(a + y) \<in> ((\<lambda>x. a + x) ` S)"
  1914       by auto
  1915     then have h1: "u *\<^sub>R  (a + x) + v *\<^sub>R (a + y) \<in> (\<lambda>x. a + x) ` S"
  1916       using xy assms unfolding affine_def by auto
  1917     have "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) = (u + v) *\<^sub>R a + (u *\<^sub>R x + v *\<^sub>R y)"
  1918       by (simp add: algebra_simps)
  1919     also have "\<dots> = a + (u *\<^sub>R x + v *\<^sub>R y)"
  1920       using \<open>u + v = 1\<close> by auto
  1921     ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \<in> (\<lambda>x. a + x) ` S"
  1922       using h1 by auto
  1923     then have "u *\<^sub>R x + v *\<^sub>R y : S" by auto
  1924   }
  1925   then show ?thesis unfolding affine_def by auto
  1926 qed
  1927 
  1928 lemma affine_translation:
  1929   fixes a :: "'a::real_vector"
  1930   shows "affine S \<longleftrightarrow> affine ((\<lambda>x. a + x) ` S)"
  1931 proof -
  1932   have "affine S \<Longrightarrow> affine ((\<lambda>x. a + x) ` S)"
  1933     using affine_translation_aux[of "-a" "((\<lambda>x. a + x) ` S)"]
  1934     using translation_assoc[of "-a" a S] by auto
  1935   then show ?thesis using affine_translation_aux by auto
  1936 qed
  1937 
  1938 lemma parallel_is_affine:
  1939   fixes S T :: "'a::real_vector set"
  1940   assumes "affine S" "affine_parallel S T"
  1941   shows "affine T"
  1942 proof -
  1943   from assms obtain a where "T = (\<lambda>x. a + x) ` S"
  1944     unfolding affine_parallel_def by auto
  1945   then show ?thesis
  1946     using affine_translation assms by auto
  1947 qed
  1948 
  1949 lemma subspace_imp_affine: "subspace s \<Longrightarrow> affine s"
  1950   unfolding subspace_def affine_def by auto
  1951 
  1952 
  1953 subsubsection \<open>Subspace parallel to an affine set\<close>
  1954 
  1955 lemma subspace_affine: "subspace S \<longleftrightarrow> affine S \<and> 0 \<in> S"
  1956 proof -
  1957   have h0: "subspace S \<Longrightarrow> affine S \<and> 0 \<in> S"
  1958     using subspace_imp_affine[of S] subspace_0 by auto
  1959   {
  1960     assume assm: "affine S \<and> 0 \<in> S"
  1961     {
  1962       fix c :: real
  1963       fix x
  1964       assume x: "x \<in> S"
  1965       have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto
  1966       moreover
  1967       have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \<in> S"
  1968         using affine_alt[of S] assm x by auto
  1969       ultimately have "c *\<^sub>R x \<in> S" by auto
  1970     }
  1971     then have h1: "\<forall>c. \<forall>x \<in> S. c *\<^sub>R x \<in> S" by auto
  1972 
  1973     {
  1974       fix x y
  1975       assume xy: "x \<in> S" "y \<in> S"
  1976       define u where "u = (1 :: real)/2"
  1977       have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)"
  1978         by auto
  1979       moreover
  1980       have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y"
  1981         by (simp add: algebra_simps)
  1982       moreover
  1983       have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> S"
  1984         using affine_alt[of S] assm xy by auto
  1985       ultimately
  1986       have "(1/2) *\<^sub>R (x+y) \<in> S"
  1987         using u_def by auto
  1988       moreover
  1989       have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))"
  1990         by auto
  1991       ultimately
  1992       have "x + y \<in> S"
  1993         using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto
  1994     }
  1995     then have "\<forall>x \<in> S. \<forall>y \<in> S. x + y \<in> S"
  1996       by auto
  1997     then have "subspace S"
  1998       using h1 assm unfolding subspace_def by auto
  1999   }
  2000   then show ?thesis using h0 by metis
  2001 qed
  2002 
  2003 lemma affine_diffs_subspace:
  2004   assumes "affine S" "a \<in> S"
  2005   shows "subspace ((\<lambda>x. (-a)+x) ` S)"
  2006 proof -
  2007   have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  2008   have "affine ((\<lambda>x. (-a)+x) ` S)"
  2009     using  affine_translation assms by auto
  2010   moreover have "0 : ((\<lambda>x. (-a)+x) ` S)"
  2011     using assms exI[of "(\<lambda>x. x\<in>S \<and> -a+x = 0)" a] by auto
  2012   ultimately show ?thesis using subspace_affine by auto
  2013 qed
  2014 
  2015 lemma parallel_subspace_explicit:
  2016   assumes "affine S"
  2017     and "a \<in> S"
  2018   assumes "L \<equiv> {y. \<exists>x \<in> S. (-a) + x = y}"
  2019   shows "subspace L \<and> affine_parallel S L"
  2020 proof -
  2021   from assms have "L = plus (- a) ` S" by auto
  2022   then have par: "affine_parallel S L"
  2023     unfolding affine_parallel_def ..
  2024   then have "affine L" using assms parallel_is_affine by auto
  2025   moreover have "0 \<in> L"
  2026     using assms by auto
  2027   ultimately show ?thesis
  2028     using subspace_affine par by auto
  2029 qed
  2030 
  2031 lemma parallel_subspace_aux:
  2032   assumes "subspace A"
  2033     and "subspace B"
  2034     and "affine_parallel A B"
  2035   shows "A \<supseteq> B"
  2036 proof -
  2037   from assms obtain a where a: "\<forall>x. x \<in> A \<longleftrightarrow> a + x \<in> B"
  2038     using affine_parallel_expl[of A B] by auto
  2039   then have "-a \<in> A"
  2040     using assms subspace_0[of B] by auto
  2041   then have "a \<in> A"
  2042     using assms subspace_neg[of A "-a"] by auto
  2043   then show ?thesis
  2044     using assms a unfolding subspace_def by auto
  2045 qed
  2046 
  2047 lemma parallel_subspace:
  2048   assumes "subspace A"
  2049     and "subspace B"
  2050     and "affine_parallel A B"
  2051   shows "A = B"
  2052 proof
  2053   show "A \<supseteq> B"
  2054     using assms parallel_subspace_aux by auto
  2055   show "A \<subseteq> B"
  2056     using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto
  2057 qed
  2058 
  2059 lemma affine_parallel_subspace:
  2060   assumes "affine S" "S \<noteq> {}"
  2061   shows "\<exists>!L. subspace L \<and> affine_parallel S L"
  2062 proof -
  2063   have ex: "\<exists>L. subspace L \<and> affine_parallel S L"
  2064     using assms parallel_subspace_explicit by auto
  2065   {
  2066     fix L1 L2
  2067     assume ass: "subspace L1 \<and> affine_parallel S L1" "subspace L2 \<and> affine_parallel S L2"
  2068     then have "affine_parallel L1 L2"
  2069       using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto
  2070     then have "L1 = L2"
  2071       using ass parallel_subspace by auto
  2072   }
  2073   then show ?thesis using ex by auto
  2074 qed
  2075 
  2076 
  2077 subsection \<open>Cones\<close>
  2078 
  2079 definition cone :: "'a::real_vector set \<Rightarrow> bool"
  2080   where "cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>c\<ge>0. c *\<^sub>R x \<in> s)"
  2081 
  2082 lemma cone_empty[intro, simp]: "cone {}"
  2083   unfolding cone_def by auto
  2084 
  2085 lemma cone_univ[intro, simp]: "cone UNIV"
  2086   unfolding cone_def by auto
  2087 
  2088 lemma cone_Inter[intro]: "\<forall>s\<in>f. cone s \<Longrightarrow> cone (\<Inter>f)"
  2089   unfolding cone_def by auto
  2090 
  2091 lemma subspace_imp_cone: "subspace S \<Longrightarrow> cone S"
  2092   by (simp add: cone_def subspace_mul)
  2093 
  2094 
  2095 subsubsection \<open>Conic hull\<close>
  2096 
  2097 lemma cone_cone_hull: "cone (cone hull s)"
  2098   unfolding hull_def by auto
  2099 
  2100 lemma cone_hull_eq: "cone hull s = s \<longleftrightarrow> cone s"
  2101   apply (rule hull_eq)
  2102   using cone_Inter
  2103   unfolding subset_eq
  2104   apply auto
  2105   done
  2106 
  2107 lemma mem_cone:
  2108   assumes "cone S" "x \<in> S" "c \<ge> 0"
  2109   shows "c *\<^sub>R x : S"
  2110   using assms cone_def[of S] by auto
  2111 
  2112 lemma cone_contains_0:
  2113   assumes "cone S"
  2114   shows "S \<noteq> {} \<longleftrightarrow> 0 \<in> S"
  2115 proof -
  2116   {
  2117     assume "S \<noteq> {}"
  2118     then obtain a where "a \<in> S" by auto
  2119     then have "0 \<in> S"
  2120       using assms mem_cone[of S a 0] by auto
  2121   }
  2122   then show ?thesis by auto
  2123 qed
  2124 
  2125 lemma cone_0: "cone {0}"
  2126   unfolding cone_def by auto
  2127 
  2128 lemma cone_Union[intro]: "(\<forall>s\<in>f. cone s) \<longrightarrow> cone (\<Union>f)"
  2129   unfolding cone_def by blast
  2130 
  2131 lemma cone_iff:
  2132   assumes "S \<noteq> {}"
  2133   shows "cone S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  2134 proof -
  2135   {
  2136     assume "cone S"
  2137     {
  2138       fix c :: real
  2139       assume "c > 0"
  2140       {
  2141         fix x
  2142         assume "x \<in> S"
  2143         then have "x \<in> (op *\<^sub>R c) ` S"
  2144           unfolding image_def
  2145           using \<open>cone S\<close> \<open>c>0\<close> mem_cone[of S x "1/c"]
  2146             exI[of "(\<lambda>t. t \<in> S \<and> x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"]
  2147           by auto
  2148       }
  2149       moreover
  2150       {
  2151         fix x
  2152         assume "x \<in> (op *\<^sub>R c) ` S"
  2153         then have "x \<in> S"
  2154           using \<open>cone S\<close> \<open>c > 0\<close>
  2155           unfolding cone_def image_def \<open>c > 0\<close> by auto
  2156       }
  2157       ultimately have "(op *\<^sub>R c) ` S = S" by auto
  2158     }
  2159     then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  2160       using \<open>cone S\<close> cone_contains_0[of S] assms by auto
  2161   }
  2162   moreover
  2163   {
  2164     assume a: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  2165     {
  2166       fix x
  2167       assume "x \<in> S"
  2168       fix c1 :: real
  2169       assume "c1 \<ge> 0"
  2170       then have "c1 = 0 \<or> c1 > 0" by auto
  2171       then have "c1 *\<^sub>R x \<in> S" using a \<open>x \<in> S\<close> by auto
  2172     }
  2173     then have "cone S" unfolding cone_def by auto
  2174   }
  2175   ultimately show ?thesis by blast
  2176 qed
  2177 
  2178 lemma cone_hull_empty: "cone hull {} = {}"
  2179   by (metis cone_empty cone_hull_eq)
  2180 
  2181 lemma cone_hull_empty_iff: "S = {} \<longleftrightarrow> cone hull S = {}"
  2182   by (metis bot_least cone_hull_empty hull_subset xtrans(5))
  2183 
  2184 lemma cone_hull_contains_0: "S \<noteq> {} \<longleftrightarrow> 0 \<in> cone hull S"
  2185   using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S]
  2186   by auto
  2187 
  2188 lemma mem_cone_hull:
  2189   assumes "x \<in> S" "c \<ge> 0"
  2190   shows "c *\<^sub>R x \<in> cone hull S"
  2191   by (metis assms cone_cone_hull hull_inc mem_cone)
  2192 
  2193 lemma cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \<ge> 0 \<and> x \<in> S}"
  2194   (is "?lhs = ?rhs")
  2195 proof -
  2196   {
  2197     fix x
  2198     assume "x \<in> ?rhs"
  2199     then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  2200       by auto
  2201     fix c :: real
  2202     assume c: "c \<ge> 0"
  2203     then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx"
  2204       using x by (simp add: algebra_simps)
  2205     moreover
  2206     have "c * cx \<ge> 0" using c x by auto
  2207     ultimately
  2208     have "c *\<^sub>R x \<in> ?rhs" using x by auto
  2209   }
  2210   then have "cone ?rhs"
  2211     unfolding cone_def by auto
  2212   then have "?rhs \<in> Collect cone"
  2213     unfolding mem_Collect_eq by auto
  2214   {
  2215     fix x
  2216     assume "x \<in> S"
  2217     then have "1 *\<^sub>R x \<in> ?rhs"
  2218       apply auto
  2219       apply (rule_tac x = 1 in exI)
  2220       apply auto
  2221       done
  2222     then have "x \<in> ?rhs" by auto
  2223   }
  2224   then have "S \<subseteq> ?rhs" by auto
  2225   then have "?lhs \<subseteq> ?rhs"
  2226     using \<open>?rhs \<in> Collect cone\<close> hull_minimal[of S "?rhs" "cone"] by auto
  2227   moreover
  2228   {
  2229     fix x
  2230     assume "x \<in> ?rhs"
  2231     then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  2232       by auto
  2233     then have "xx \<in> cone hull S"
  2234       using hull_subset[of S] by auto
  2235     then have "x \<in> ?lhs"
  2236       using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto
  2237   }
  2238   ultimately show ?thesis by auto
  2239 qed
  2240 
  2241 lemma cone_closure:
  2242   fixes S :: "'a::real_normed_vector set"
  2243   assumes "cone S"
  2244   shows "cone (closure S)"
  2245 proof (cases "S = {}")
  2246   case True
  2247   then show ?thesis by auto
  2248 next
  2249   case False
  2250   then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> op *\<^sub>R c ` S = S)"
  2251     using cone_iff[of S] assms by auto
  2252   then have "0 \<in> closure S \<and> (\<forall>c. c > 0 \<longrightarrow> op *\<^sub>R c ` closure S = closure S)"
  2253     using closure_subset by (auto simp add: closure_scaleR)
  2254   then show ?thesis
  2255     using False cone_iff[of "closure S"] by auto
  2256 qed
  2257 
  2258 
  2259 subsection \<open>Affine dependence and consequential theorems (from Lars Schewe)\<close>
  2260 
  2261 definition affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
  2262   where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
  2263 
  2264 lemma affine_dependent_subset:
  2265    "\<lbrakk>affine_dependent s; s \<subseteq> t\<rbrakk> \<Longrightarrow> affine_dependent t"
  2266 apply (simp add: affine_dependent_def Bex_def)
  2267 apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]])
  2268 done
  2269 
  2270 lemma affine_independent_subset:
  2271   shows "\<lbrakk>~ affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> ~ affine_dependent s"
  2272 by (metis affine_dependent_subset)
  2273 
  2274 lemma affine_independent_Diff:
  2275    "~ affine_dependent s \<Longrightarrow> ~ affine_dependent(s - t)"
  2276 by (meson Diff_subset affine_dependent_subset)
  2277 
  2278 lemma affine_dependent_explicit:
  2279   "affine_dependent p \<longleftrightarrow>
  2280     (\<exists>s u. finite s \<and> s \<subseteq> p \<and> sum u s = 0 \<and>
  2281       (\<exists>v\<in>s. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) s = 0)"
  2282   unfolding affine_dependent_def affine_hull_explicit mem_Collect_eq
  2283   apply rule
  2284   apply (erule bexE, erule exE, erule exE)
  2285   apply (erule conjE)+
  2286   defer
  2287   apply (erule exE, erule exE)
  2288   apply (erule conjE)+
  2289   apply (erule bexE)
  2290 proof -
  2291   fix x s u
  2292   assume as: "x \<in> p" "finite s" "s \<noteq> {}" "s \<subseteq> p - {x}" "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  2293   have "x \<notin> s" using as(1,4) by auto
  2294   show "\<exists>s u. finite s \<and> s \<subseteq> p \<and> sum u s = 0 \<and> (\<exists>v\<in>s. u v \<noteq> 0) \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  2295     apply (rule_tac x="insert x s" in exI, rule_tac x="\<lambda>v. if v = x then - 1 else u v" in exI)
  2296     unfolding if_smult and sum_clauses(2)[OF as(2)] and sum_delta_notmem[OF \<open>x\<notin>s\<close>] and as
  2297     using as
  2298     apply auto
  2299     done
  2300 next
  2301   fix s u v
  2302   assume as: "finite s" "s \<subseteq> p" "sum u s = 0" "(\<Sum>v\<in>s. u v *\<^sub>R v) = 0" "v \<in> s" "u v \<noteq> 0"
  2303   have "s \<noteq> {v}"
  2304     using as(3,6) by auto
  2305   then show "\<exists>x\<in>p. \<exists>s u. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p - {x} \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  2306     apply (rule_tac x=v in bexI)
  2307     apply (rule_tac x="s - {v}" in exI)
  2308     apply (rule_tac x="\<lambda>x. - (1 / u v) * u x" in exI)
  2309     unfolding scaleR_scaleR[symmetric] and scaleR_right.sum [symmetric]
  2310     unfolding sum_distrib_left[symmetric] and sum_diff1[OF as(1)]
  2311     using as
  2312     apply auto
  2313     done
  2314 qed
  2315 
  2316 lemma affine_dependent_explicit_finite:
  2317   fixes s :: "'a::real_vector set"
  2318   assumes "finite s"
  2319   shows "affine_dependent s \<longleftrightarrow>
  2320     (\<exists>u. sum u s = 0 \<and> (\<exists>v\<in>s. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) s = 0)"
  2321   (is "?lhs = ?rhs")
  2322 proof
  2323   have *: "\<And>vt u v. (if vt then u v else 0) *\<^sub>R v = (if vt then (u v) *\<^sub>R v else 0::'a)"
  2324     by auto
  2325   assume ?lhs
  2326   then obtain t u v where
  2327     "finite t" "t \<subseteq> s" "sum u t = 0" "v\<in>t" "u v \<noteq> 0"  "(\<Sum>v\<in>t. u v *\<^sub>R v) = 0"
  2328     unfolding affine_dependent_explicit by auto
  2329   then show ?rhs
  2330     apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
  2331     apply auto unfolding * and sum.inter_restrict[OF assms, symmetric]
  2332     unfolding Int_absorb1[OF \<open>t\<subseteq>s\<close>]
  2333     apply auto
  2334     done
  2335 next
  2336   assume ?rhs
  2337   then obtain u v where "sum u s = 0"  "v\<in>s" "u v \<noteq> 0" "(\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  2338     by auto
  2339   then show ?lhs unfolding affine_dependent_explicit
  2340     using assms by auto
  2341 qed
  2342 
  2343 
  2344 subsection \<open>Connectedness of convex sets\<close>
  2345 
  2346 lemma connectedD:
  2347   "connected S \<Longrightarrow> open A \<Longrightarrow> open B \<Longrightarrow> S \<subseteq> A \<union> B \<Longrightarrow> A \<inter> B \<inter> S = {} \<Longrightarrow> A \<inter> S = {} \<or> B \<inter> S = {}"
  2348   by (rule Topological_Spaces.topological_space_class.connectedD)
  2349 
  2350 lemma convex_connected:
  2351   fixes s :: "'a::real_normed_vector set"
  2352   assumes "convex s"
  2353   shows "connected s"
  2354 proof (rule connectedI)
  2355   fix A B
  2356   assume "open A" "open B" "A \<inter> B \<inter> s = {}" "s \<subseteq> A \<union> B"
  2357   moreover
  2358   assume "A \<inter> s \<noteq> {}" "B \<inter> s \<noteq> {}"
  2359   then obtain a b where a: "a \<in> A" "a \<in> s" and b: "b \<in> B" "b \<in> s" by auto
  2360   define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u
  2361   then have "continuous_on {0 .. 1} f"
  2362     by (auto intro!: continuous_intros)
  2363   then have "connected (f ` {0 .. 1})"
  2364     by (auto intro!: connected_continuous_image)
  2365   note connectedD[OF this, of A B]
  2366   moreover have "a \<in> A \<inter> f ` {0 .. 1}"
  2367     using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def)
  2368   moreover have "b \<in> B \<inter> f ` {0 .. 1}"
  2369     using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def)
  2370   moreover have "f ` {0 .. 1} \<subseteq> s"
  2371     using \<open>convex s\<close> a b unfolding convex_def f_def by auto
  2372   ultimately show False by auto
  2373 qed
  2374 
  2375 corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
  2376   by (simp add: convex_connected)
  2377 
  2378 corollary component_complement_connected:
  2379   fixes S :: "'a::real_normed_vector set"
  2380   assumes "connected S" "C \<in> components (-S)"
  2381   shows "connected(-C)"
  2382   using component_diff_connected [of S UNIV] assms
  2383   by (auto simp: Compl_eq_Diff_UNIV)
  2384 
  2385 proposition clopen:
  2386   fixes S :: "'a :: real_normed_vector set"
  2387   shows "closed S \<and> open S \<longleftrightarrow> S = {} \<or> S = UNIV"
  2388     by (force intro!: connected_UNIV [unfolded connected_clopen, rule_format])
  2389 
  2390 corollary compact_open:
  2391   fixes S :: "'a :: euclidean_space set"
  2392   shows "compact S \<and> open S \<longleftrightarrow> S = {}"
  2393   by (auto simp: compact_eq_bounded_closed clopen)
  2394 
  2395 corollary finite_imp_not_open:
  2396     fixes S :: "'a::{real_normed_vector, perfect_space} set"
  2397     shows "\<lbrakk>finite S; open S\<rbrakk> \<Longrightarrow> S={}"
  2398   using clopen [of S] finite_imp_closed not_bounded_UNIV by blast
  2399 
  2400 corollary empty_interior_finite:
  2401     fixes S :: "'a::{real_normed_vector, perfect_space} set"
  2402     shows "finite S \<Longrightarrow> interior S = {}"
  2403   by (metis interior_subset finite_subset open_interior [of S] finite_imp_not_open)
  2404 
  2405 text \<open>Balls, being convex, are connected.\<close>
  2406 
  2407 lemma convex_prod:
  2408   assumes "\<And>i. i \<in> Basis \<Longrightarrow> convex {x. P i x}"
  2409   shows "convex {x. \<forall>i\<in>Basis. P i (x\<bullet>i)}"
  2410   using assms unfolding convex_def
  2411   by (auto simp: inner_add_left)
  2412 
  2413 lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i)}"
  2414   by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval)
  2415 
  2416 lemma convex_local_global_minimum:
  2417   fixes s :: "'a::real_normed_vector set"
  2418   assumes "e > 0"
  2419     and "convex_on s f"
  2420     and "ball x e \<subseteq> s"
  2421     and "\<forall>y\<in>ball x e. f x \<le> f y"
  2422   shows "\<forall>y\<in>s. f x \<le> f y"
  2423 proof (rule ccontr)
  2424   have "x \<in> s" using assms(1,3) by auto
  2425   assume "\<not> ?thesis"
  2426   then obtain y where "y\<in>s" and y: "f x > f y" by auto
  2427   then have xy: "0 < dist x y"  by auto
  2428   then obtain u where "0 < u" "u \<le> 1" and u: "u < e / dist x y"
  2429     using real_lbound_gt_zero[of 1 "e / dist x y"] xy \<open>e>0\<close> by auto
  2430   then have "f ((1-u) *\<^sub>R x + u *\<^sub>R y) \<le> (1-u) * f x + u * f y"
  2431     using \<open>x\<in>s\<close> \<open>y\<in>s\<close>
  2432     using assms(2)[unfolded convex_on_def,
  2433       THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x="1-u"]]
  2434     by auto
  2435   moreover
  2436   have *: "x - ((1 - u) *\<^sub>R x + u *\<^sub>R y) = u *\<^sub>R (x - y)"
  2437     by (simp add: algebra_simps)
  2438   have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> ball x e"
  2439     unfolding mem_ball dist_norm
  2440     unfolding * and norm_scaleR and abs_of_pos[OF \<open>0<u\<close>]
  2441     unfolding dist_norm[symmetric]
  2442     using u
  2443     unfolding pos_less_divide_eq[OF xy]
  2444     by auto
  2445   then have "f x \<le> f ((1 - u) *\<^sub>R x + u *\<^sub>R y)"
  2446     using assms(4) by auto
  2447   ultimately show False
  2448     using mult_strict_left_mono[OF y \<open>u>0\<close>]
  2449     unfolding left_diff_distrib
  2450     by auto
  2451 qed
  2452 
  2453 lemma convex_ball [iff]:
  2454   fixes x :: "'a::real_normed_vector"
  2455   shows "convex (ball x e)"
  2456 proof (auto simp add: convex_def)
  2457   fix y z
  2458   assume yz: "dist x y < e" "dist x z < e"
  2459   fix u v :: real
  2460   assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  2461   have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
  2462     using uv yz
  2463     using convex_on_dist [of "ball x e" x, unfolded convex_on_def,
  2464       THEN bspec[where x=y], THEN bspec[where x=z]]
  2465     by auto
  2466   then show "dist x (u *\<^sub>R y + v *\<^sub>R z) < e"
  2467     using convex_bound_lt[OF yz uv] by auto
  2468 qed
  2469 
  2470 lemma convex_cball [iff]:
  2471   fixes x :: "'a::real_normed_vector"
  2472   shows "convex (cball x e)"
  2473 proof -
  2474   {
  2475     fix y z
  2476     assume yz: "dist x y \<le> e" "dist x z \<le> e"
  2477     fix u v :: real
  2478     assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  2479     have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
  2480       using uv yz
  2481       using convex_on_dist [of "cball x e" x, unfolded convex_on_def,
  2482         THEN bspec[where x=y], THEN bspec[where x=z]]
  2483       by auto
  2484     then have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> e"
  2485       using convex_bound_le[OF yz uv] by auto
  2486   }
  2487   then show ?thesis by (auto simp add: convex_def Ball_def)
  2488 qed
  2489 
  2490 lemma connected_ball [iff]:
  2491   fixes x :: "'a::real_normed_vector"
  2492   shows "connected (ball x e)"
  2493   using convex_connected convex_ball by auto
  2494 
  2495 lemma connected_cball [iff]:
  2496   fixes x :: "'a::real_normed_vector"
  2497   shows "connected (cball x e)"
  2498   using convex_connected convex_cball by auto
  2499 
  2500 
  2501 subsection \<open>Convex hull\<close>
  2502 
  2503 lemma convex_convex_hull [iff]: "convex (convex hull s)"
  2504   unfolding hull_def
  2505   using convex_Inter[of "{t. convex t \<and> s \<subseteq> t}"]
  2506   by auto
  2507 
  2508 lemma convex_hull_subset:
  2509     "s \<subseteq> convex hull t \<Longrightarrow> convex hull s \<subseteq> convex hull t"
  2510   by (simp add: convex_convex_hull subset_hull)
  2511 
  2512 lemma convex_hull_eq: "convex hull s = s \<longleftrightarrow> convex s"
  2513   by (metis convex_convex_hull hull_same)
  2514 
  2515 lemma bounded_convex_hull:
  2516   fixes s :: "'a::real_normed_vector set"
  2517   assumes "bounded s"
  2518   shows "bounded (convex hull s)"
  2519 proof -
  2520   from assms obtain B where B: "\<forall>x\<in>s. norm x \<le> B"
  2521     unfolding bounded_iff by auto
  2522   show ?thesis
  2523     apply (rule bounded_subset[OF bounded_cball, of _ 0 B])
  2524     unfolding subset_hull[of convex, OF convex_cball]
  2525     unfolding subset_eq mem_cball dist_norm using B
  2526     apply auto
  2527     done
  2528 qed
  2529 
  2530 lemma finite_imp_bounded_convex_hull:
  2531   fixes s :: "'a::real_normed_vector set"
  2532   shows "finite s \<Longrightarrow> bounded (convex hull s)"
  2533   using bounded_convex_hull finite_imp_bounded
  2534   by auto
  2535 
  2536 
  2537 subsubsection \<open>Convex hull is "preserved" by a linear function\<close>
  2538 
  2539 lemma convex_hull_linear_image:
  2540   assumes f: "linear f"
  2541   shows "f ` (convex hull s) = convex hull (f ` s)"
  2542 proof
  2543   show "convex hull (f ` s) \<subseteq> f ` (convex hull s)"
  2544     by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull)
  2545   show "f ` (convex hull s) \<subseteq> convex hull (f ` s)"
  2546   proof (unfold image_subset_iff_subset_vimage, rule hull_minimal)
  2547     show "s \<subseteq> f -` (convex hull (f ` s))"
  2548       by (fast intro: hull_inc)
  2549     show "convex (f -` (convex hull (f ` s)))"
  2550       by (intro convex_linear_vimage [OF f] convex_convex_hull)
  2551   qed
  2552 qed
  2553 
  2554 lemma in_convex_hull_linear_image:
  2555   assumes "linear f"
  2556     and "x \<in> convex hull s"
  2557   shows "f x \<in> convex hull (f ` s)"
  2558   using convex_hull_linear_image[OF assms(1)] assms(2) by auto
  2559 
  2560 lemma convex_hull_Times:
  2561   "convex hull (s \<times> t) = (convex hull s) \<times> (convex hull t)"
  2562 proof
  2563   show "convex hull (s \<times> t) \<subseteq> (convex hull s) \<times> (convex hull t)"
  2564     by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull)
  2565   have "\<forall>x\<in>convex hull s. \<forall>y\<in>convex hull t. (x, y) \<in> convex hull (s \<times> t)"
  2566   proof (intro hull_induct)
  2567     fix x y assume "x \<in> s" and "y \<in> t"
  2568     then show "(x, y) \<in> convex hull (s \<times> t)"
  2569       by (simp add: hull_inc)
  2570   next
  2571     fix x let ?S = "((\<lambda>y. (0, y)) -` (\<lambda>p. (- x, 0) + p) ` (convex hull s \<times> t))"
  2572     have "convex ?S"
  2573       by (intro convex_linear_vimage convex_translation convex_convex_hull,
  2574         simp add: linear_iff)
  2575     also have "?S = {y. (x, y) \<in> convex hull (s \<times> t)}"
  2576       by (auto simp add: image_def Bex_def)
  2577     finally show "convex {y. (x, y) \<in> convex hull (s \<times> t)}" .
  2578   next
  2579     show "convex {x. \<forall>y\<in>convex hull t. (x, y) \<in> convex hull (s \<times> t)}"
  2580     proof (unfold Collect_ball_eq, rule convex_INT [rule_format])
  2581       fix y let ?S = "((\<lambda>x. (x, 0)) -` (\<lambda>p. (0, - y) + p) ` (convex hull s \<times> t))"
  2582       have "convex ?S"
  2583       by (intro convex_linear_vimage convex_translation convex_convex_hull,
  2584         simp add: linear_iff)
  2585       also have "?S = {x. (x, y) \<in> convex hull (s \<times> t)}"
  2586         by (auto simp add: image_def Bex_def)
  2587       finally show "convex {x. (x, y) \<in> convex hull (s \<times> t)}" .
  2588     qed
  2589   qed
  2590   then show "(convex hull s) \<times> (convex hull t) \<subseteq> convex hull (s \<times> t)"
  2591     unfolding subset_eq split_paired_Ball_Sigma .
  2592 qed
  2593 
  2594 
  2595 subsubsection \<open>Stepping theorems for convex hulls of finite sets\<close>
  2596 
  2597 lemma convex_hull_empty[simp]: "convex hull {} = {}"
  2598   by (rule hull_unique) auto
  2599 
  2600 lemma convex_hull_singleton[simp]: "convex hull {a} = {a}"
  2601   by (rule hull_unique) auto
  2602 
  2603 lemma convex_hull_insert:
  2604   fixes s :: "'a::real_vector set"
  2605   assumes "s \<noteq> {}"
  2606   shows "convex hull (insert a s) =
  2607     {x. \<exists>u\<ge>0. \<exists>v\<ge>0. \<exists>b. (u + v = 1) \<and> b \<in> (convex hull s) \<and> (x = u *\<^sub>R a + v *\<^sub>R b)}"
  2608   (is "_ = ?hull")
  2609   apply (rule, rule hull_minimal, rule)
  2610   unfolding insert_iff
  2611   prefer 3
  2612   apply rule
  2613 proof -
  2614   fix x
  2615   assume x: "x = a \<or> x \<in> s"
  2616   then show "x \<in> ?hull"
  2617     apply rule
  2618     unfolding mem_Collect_eq
  2619     apply (rule_tac x=1 in exI)
  2620     defer
  2621     apply (rule_tac x=0 in exI)
  2622     using assms hull_subset[of s convex]
  2623     apply auto
  2624     done
  2625 next
  2626   fix x
  2627   assume "x \<in> ?hull"
  2628   then obtain u v b where obt: "u\<ge>0" "v\<ge>0" "u + v = 1" "b \<in> convex hull s" "x = u *\<^sub>R a + v *\<^sub>R b"
  2629     by auto
  2630   have "a \<in> convex hull insert a s" "b \<in> convex hull insert a s"
  2631     using hull_mono[of s "insert a s" convex] hull_mono[of "{a}" "insert a s" convex] and obt(4)
  2632     by auto
  2633   then show "x \<in> convex hull insert a s"
  2634     unfolding obt(5) using obt(1-3)
  2635     by (rule convexD [OF convex_convex_hull])
  2636 next
  2637   show "convex ?hull"
  2638   proof (rule convexI)
  2639     fix x y u v
  2640     assume as: "(0::real) \<le> u" "0 \<le> v" "u + v = 1" "x\<in>?hull" "y\<in>?hull"
  2641     from as(4) obtain u1 v1 b1 where
  2642       obt1: "u1\<ge>0" "v1\<ge>0" "u1 + v1 = 1" "b1 \<in> convex hull s" "x = u1 *\<^sub>R a + v1 *\<^sub>R b1"
  2643       by auto
  2644     from as(5) obtain u2 v2 b2 where
  2645       obt2: "u2\<ge>0" "v2\<ge>0" "u2 + v2 = 1" "b2 \<in> convex hull s" "y = u2 *\<^sub>R a + v2 *\<^sub>R b2"
  2646       by auto
  2647     have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  2648       by (auto simp add: algebra_simps)
  2649     have **: "\<exists>b \<in> convex hull s. u *\<^sub>R x + v *\<^sub>R y =
  2650       (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)"
  2651     proof (cases "u * v1 + v * v2 = 0")
  2652       case True
  2653       have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  2654         by (auto simp add: algebra_simps)
  2655       from True have ***: "u * v1 = 0" "v * v2 = 0"
  2656         using mult_nonneg_nonneg[OF \<open>u\<ge>0\<close> \<open>v1\<ge>0\<close>] mult_nonneg_nonneg[OF \<open>v\<ge>0\<close> \<open>v2\<ge>0\<close>]
  2657         by arith+
  2658       then have "u * u1 + v * u2 = 1"
  2659         using as(3) obt1(3) obt2(3) by auto
  2660       then show ?thesis
  2661         unfolding obt1(5) obt2(5) *
  2662         using assms hull_subset[of s convex]
  2663         by (auto simp add: *** scaleR_right_distrib)
  2664     next
  2665       case False
  2666       have "1 - (u * u1 + v * u2) = (u + v) - (u * u1 + v * u2)"
  2667         using as(3) obt1(3) obt2(3) by (auto simp add: field_simps)
  2668       also have "\<dots> = u * (v1 + u1 - u1) + v * (v2 + u2 - u2)"
  2669         using as(3) obt1(3) obt2(3) by (auto simp add: field_simps)
  2670       also have "\<dots> = u * v1 + v * v2"
  2671         by simp
  2672       finally have **:"1 - (u * u1 + v * u2) = u * v1 + v * v2" by auto
  2673       have "0 \<le> u * v1 + v * v2" "0 \<le> u * v1" "0 \<le> u * v1 + v * v2" "0 \<le> v * v2"
  2674         using as(1,2) obt1(1,2) obt2(1,2) by auto
  2675       then show ?thesis
  2676         unfolding obt1(5) obt2(5)
  2677         unfolding * and **
  2678         using False
  2679         apply (rule_tac
  2680           x = "((u * v1) / (u * v1 + v * v2)) *\<^sub>R b1 + ((v * v2) / (u * v1 + v * v2)) *\<^sub>R b2" in bexI)
  2681         defer
  2682         apply (rule convexD [OF convex_convex_hull])
  2683         using obt1(4) obt2(4)
  2684         unfolding add_divide_distrib[symmetric] and zero_le_divide_iff
  2685         apply (auto simp add: scaleR_left_distrib scaleR_right_distrib)
  2686         done
  2687     qed
  2688     have u1: "u1 \<le> 1"
  2689       unfolding obt1(3)[symmetric] and not_le using obt1(2) by auto
  2690     have u2: "u2 \<le> 1"
  2691       unfolding obt2(3)[symmetric] and not_le using obt2(2) by auto
  2692     have "u1 * u + u2 * v \<le> max u1 u2 * u + max u1 u2 * v"
  2693       apply (rule add_mono)
  2694       apply (rule_tac [!] mult_right_mono)
  2695       using as(1,2) obt1(1,2) obt2(1,2)
  2696       apply auto
  2697       done
  2698     also have "\<dots> \<le> 1"
  2699       unfolding distrib_left[symmetric] and as(3) using u1 u2 by auto
  2700     finally show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  2701       unfolding mem_Collect_eq
  2702       apply (rule_tac x="u * u1 + v * u2" in exI)
  2703       apply (rule conjI)
  2704       defer
  2705       apply (rule_tac x="1 - u * u1 - v * u2" in exI)
  2706       unfolding Bex_def
  2707       using as(1,2) obt1(1,2) obt2(1,2) **
  2708       apply (auto simp add: algebra_simps)
  2709       done
  2710   qed
  2711 qed
  2712 
  2713 lemma convex_hull_insert_alt:
  2714    "convex hull (insert a S) =
  2715       (if S = {} then {a}
  2716       else {(1 - u) *\<^sub>R a + u *\<^sub>R x |x u. 0 \<le> u \<and> u \<le> 1 \<and> x \<in> convex hull S})"
  2717   apply (auto simp: convex_hull_insert)
  2718   using diff_eq_eq apply fastforce
  2719   by (metis add.group_left_neutral add_le_imp_le_diff diff_add_cancel)
  2720 
  2721 subsubsection \<open>Explicit expression for convex hull\<close>
  2722 
  2723 lemma convex_hull_indexed:
  2724   fixes s :: "'a::real_vector set"
  2725   shows "convex hull s =
  2726     {y. \<exists>k u x.
  2727       (\<forall>i\<in>{1::nat .. k}. 0 \<le> u i \<and> x i \<in> s) \<and>
  2728       (sum u {1..k} = 1) \<and> (sum (\<lambda>i. u i *\<^sub>R x i) {1..k} = y)}"
  2729   (is "?xyz = ?hull")
  2730   apply (rule hull_unique)
  2731   apply rule
  2732   defer
  2733   apply (rule convexI)
  2734 proof -
  2735   fix x
  2736   assume "x\<in>s"
  2737   then show "x \<in> ?hull"
  2738     unfolding mem_Collect_eq
  2739     apply (rule_tac x=1 in exI, rule_tac x="\<lambda>x. 1" in exI)
  2740     apply auto
  2741     done
  2742 next
  2743   fix t
  2744   assume as: "s \<subseteq> t" "convex t"
  2745   show "?hull \<subseteq> t"
  2746     apply rule
  2747     unfolding mem_Collect_eq
  2748     apply (elim exE conjE)
  2749   proof -
  2750     fix x k u y
  2751     assume assm:
  2752       "\<forall>i\<in>{1::nat..k}. 0 \<le> u i \<and> y i \<in> s"
  2753       "sum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R y i) = x"
  2754     show "x\<in>t"
  2755       unfolding assm(3) [symmetric]
  2756       apply (rule as(2)[unfolded convex, rule_format])
  2757       using assm(1,2) as(1) apply auto
  2758       done
  2759   qed
  2760 next
  2761   fix x y u v
  2762   assume uv: "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
  2763   assume xy: "x \<in> ?hull" "y \<in> ?hull"
  2764   from xy obtain k1 u1 x1 where
  2765     x: "\<forall>i\<in>{1::nat..k1}. 0\<le>u1 i \<and> x1 i \<in> s" "sum u1 {Suc 0..k1} = 1" "(\<Sum>i = Suc 0..k1. u1 i *\<^sub>R x1 i) = x"
  2766     by auto
  2767   from xy obtain k2 u2 x2 where
  2768     y: "\<forall>i\<in>{1::nat..k2}. 0\<le>u2 i \<and> x2 i \<in> s" "sum u2 {Suc 0..k2} = 1" "(\<Sum>i = Suc 0..k2. u2 i *\<^sub>R x2 i) = y"
  2769     by auto
  2770   have *: "\<And>P (x1::'a) x2 s1 s2 i.
  2771     (if P i then s1 else s2) *\<^sub>R (if P i then x1 else x2) = (if P i then s1 *\<^sub>R x1 else s2 *\<^sub>R x2)"
  2772     "{1..k1 + k2} \<inter> {1..k1} = {1..k1}" "{1..k1 + k2} \<inter> - {1..k1} = (\<lambda>i. i + k1) ` {1..k2}"
  2773     prefer 3
  2774     apply (rule, rule)
  2775     unfolding image_iff
  2776     apply (rule_tac x = "x - k1" in bexI)
  2777     apply (auto simp add: not_le)
  2778     done
  2779   have inj: "inj_on (\<lambda>i. i + k1) {1..k2}"
  2780     unfolding inj_on_def by auto
  2781   show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  2782     apply rule
  2783     apply (rule_tac x="k1 + k2" in exI)
  2784     apply (rule_tac x="\<lambda>i. if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)" in exI)
  2785     apply (rule_tac x="\<lambda>i. if i \<in> {1..k1} then x1 i else x2 (i - k1)" in exI)
  2786     apply (rule, rule)
  2787     defer
  2788     apply rule
  2789     unfolding * and sum.If_cases[OF finite_atLeastAtMost[of 1 "k1 + k2"]] and
  2790       sum.reindex[OF inj] and o_def Collect_mem_eq
  2791     unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] sum_distrib_left[symmetric]
  2792   proof -
  2793     fix i
  2794     assume i: "i \<in> {1..k1+k2}"
  2795     show "0 \<le> (if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)) \<and>
  2796       (if i \<in> {1..k1} then x1 i else x2 (i - k1)) \<in> s"
  2797     proof (cases "i\<in>{1..k1}")
  2798       case True
  2799       then show ?thesis
  2800         using uv(1) x(1)[THEN bspec[where x=i]] by auto
  2801     next
  2802       case False
  2803       define j where "j = i - k1"
  2804       from i False have "j \<in> {1..k2}"
  2805         unfolding j_def by auto
  2806       then show ?thesis
  2807         using False uv(2) y(1)[THEN bspec[where x=j]]
  2808         by (auto simp: j_def[symmetric])
  2809     qed
  2810   qed (auto simp add: not_le x(2,3) y(2,3) uv(3))
  2811 qed
  2812 
  2813 lemma convex_hull_finite:
  2814   fixes s :: "'a::real_vector set"
  2815   assumes "finite s"
  2816   shows "convex hull s = {y. \<exists>u. (\<forall>x\<in>s. 0 \<le> u x) \<and>
  2817     sum u s = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y}"
  2818   (is "?HULL = ?set")
  2819 proof (rule hull_unique, auto simp add: convex_def[of ?set])
  2820   fix x
  2821   assume "x \<in> s"
  2822   then show "\<exists>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>x\<in>s. u x *\<^sub>R x) = x"
  2823     apply (rule_tac x="\<lambda>y. if x=y then 1 else 0" in exI)
  2824     apply auto
  2825     unfolding sum.delta'[OF assms] and sum_delta''[OF assms]
  2826     apply auto
  2827     done
  2828 next
  2829   fix u v :: real
  2830   assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  2831   fix ux assume ux: "\<forall>x\<in>s. 0 \<le> ux x" "sum ux s = (1::real)"
  2832   fix uy assume uy: "\<forall>x\<in>s. 0 \<le> uy x" "sum uy s = (1::real)"
  2833   {
  2834     fix x
  2835     assume "x\<in>s"
  2836     then have "0 \<le> u * ux x + v * uy x"
  2837       using ux(1)[THEN bspec[where x=x]] uy(1)[THEN bspec[where x=x]] and uv(1,2)
  2838       by auto
  2839   }
  2840   moreover
  2841   have "(\<Sum>x\<in>s. u * ux x + v * uy x) = 1"
  2842     unfolding sum.distrib and sum_distrib_left[symmetric] and ux(2) uy(2)
  2843     using uv(3) by auto
  2844   moreover
  2845   have "(\<Sum>x\<in>s. (u * ux x + v * uy x) *\<^sub>R x) = u *\<^sub>R (\<Sum>x\<in>s. ux x *\<^sub>R x) + v *\<^sub>R (\<Sum>x\<in>s. uy x *\<^sub>R x)"
  2846     unfolding scaleR_left_distrib and sum.distrib and scaleR_scaleR[symmetric]
  2847       and scaleR_right.sum [symmetric]
  2848     by auto
  2849   ultimately
  2850   show "\<exists>uc. (\<forall>x\<in>s. 0 \<le> uc x) \<and> sum uc s = 1 \<and>
  2851       (\<Sum>x\<in>s. uc x *\<^sub>R x) = u *\<^sub>R (\<Sum>x\<in>s. ux x *\<^sub>R x) + v *\<^sub>R (\<Sum>x\<in>s. uy x *\<^sub>R x)"
  2852     apply (rule_tac x="\<lambda>x. u * ux x + v * uy x" in exI)
  2853     apply auto
  2854     done
  2855 next
  2856   fix t
  2857   assume t: "s \<subseteq> t" "convex t"
  2858   fix u
  2859   assume u: "\<forall>x\<in>s. 0 \<le> u x" "sum u s = (1::real)"
  2860   then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> t"
  2861     using t(2)[unfolded convex_explicit, THEN spec[where x=s], THEN spec[where x=u]]
  2862     using assms and t(1) by auto
  2863 qed
  2864 
  2865 
  2866 subsubsection \<open>Another formulation from Lars Schewe\<close>
  2867 
  2868 lemma convex_hull_explicit:
  2869   fixes p :: "'a::real_vector set"
  2870   shows "convex hull p =
  2871     {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  2872   (is "?lhs = ?rhs")
  2873 proof -
  2874   {
  2875     fix x
  2876     assume "x\<in>?lhs"
  2877     then obtain k u y where
  2878         obt: "\<forall>i\<in>{1::nat..k}. 0 \<le> u i \<and> y i \<in> p" "sum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R y i) = x"
  2879       unfolding convex_hull_indexed by auto
  2880 
  2881     have fin: "finite {1..k}" by auto
  2882     have fin': "\<And>v. finite {i \<in> {1..k}. y i = v}" by auto
  2883     {
  2884       fix j
  2885       assume "j\<in>{1..k}"
  2886       then have "y j \<in> p" "0 \<le> sum u {i. Suc 0 \<le> i \<and> i \<le> k \<and> y i = y j}"
  2887         using obt(1)[THEN bspec[where x=j]] and obt(2)
  2888         apply simp
  2889         apply (rule sum_nonneg)
  2890         using obt(1)
  2891         apply auto
  2892         done
  2893     }
  2894     moreover
  2895     have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v}) = 1"
  2896       unfolding sum_image_gen[OF fin, symmetric] using obt(2) by auto
  2897     moreover have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v} *\<^sub>R v) = x"
  2898       using sum_image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
  2899       unfolding scaleR_left.sum using obt(3) by auto
  2900     ultimately
  2901     have "\<exists>s u. finite s \<and> s \<subseteq> p \<and> (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  2902       apply (rule_tac x="y ` {1..k}" in exI)
  2903       apply (rule_tac x="\<lambda>v. sum u {i\<in>{1..k}. y i = v}" in exI)
  2904       apply auto
  2905       done
  2906     then have "x\<in>?rhs" by auto
  2907   }
  2908   moreover
  2909   {
  2910     fix y
  2911     assume "y\<in>?rhs"
  2912     then obtain s u where
  2913       obt: "finite s" "s \<subseteq> p" "\<forall>x\<in>s. 0 \<le> u x" "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  2914       by auto
  2915 
  2916     obtain f where f: "inj_on f {1..card s}" "f ` {1..card s} = s"
  2917       using ex_bij_betw_nat_finite_1[OF obt(1)] unfolding bij_betw_def by auto
  2918 
  2919     {
  2920       fix i :: nat
  2921       assume "i\<in>{1..card s}"
  2922       then have "f i \<in> s"
  2923         apply (subst f(2)[symmetric])
  2924         apply auto
  2925         done
  2926       then have "0 \<le> u (f i)" "f i \<in> p" using obt(2,3) by auto
  2927     }
  2928     moreover have *: "finite {1..card s}" by auto
  2929     {
  2930       fix y
  2931       assume "y\<in>s"
  2932       then obtain i where "i\<in>{1..card s}" "f i = y"
  2933         using f using image_iff[of y f "{1..card s}"]
  2934         by auto
  2935       then have "{x. Suc 0 \<le> x \<and> x \<le> card s \<and> f x = y} = {i}"
  2936         apply auto
  2937         using f(1)[unfolded inj_on_def]
  2938         apply(erule_tac x=x in ballE)
  2939         apply auto
  2940         done
  2941       then have "card {x. Suc 0 \<le> x \<and> x \<le> card s \<and> f x = y} = 1" by auto
  2942       then have "(\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x)) = u y"
  2943           "(\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x) *\<^sub>R f x) = u y *\<^sub>R y"
  2944         by (auto simp add: sum_constant_scaleR)
  2945     }
  2946     then have "(\<Sum>x = 1..card s. u (f x)) = 1" "(\<Sum>i = 1..card s. u (f i) *\<^sub>R f i) = y"
  2947       unfolding sum_image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
  2948         and sum_image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
  2949       unfolding f
  2950       using sum.cong [of s s "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x) *\<^sub>R f x)" "\<lambda>v. u v *\<^sub>R v"]
  2951       using sum.cong [of s s "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x))" u]
  2952       unfolding obt(4,5)
  2953       by auto
  2954     ultimately
  2955     have "\<exists>k u x. (\<forall>i\<in>{1..k}. 0 \<le> u i \<and> x i \<in> p) \<and> sum u {1..k} = 1 \<and>
  2956         (\<Sum>i::nat = 1..k. u i *\<^sub>R x i) = y"
  2957       apply (rule_tac x="card s" in exI)
  2958       apply (rule_tac x="u \<circ> f" in exI)
  2959       apply (rule_tac x=f in exI)
  2960       apply fastforce
  2961       done
  2962     then have "y \<in> ?lhs"
  2963       unfolding convex_hull_indexed by auto
  2964   }
  2965   ultimately show ?thesis
  2966     unfolding set_eq_iff by blast
  2967 qed
  2968 
  2969 
  2970 subsubsection \<open>A stepping theorem for that expansion\<close>
  2971 
  2972 lemma convex_hull_finite_step:
  2973   fixes s :: "'a::real_vector set"
  2974   assumes "finite s"
  2975   shows
  2976     "(\<exists>u. (\<forall>x\<in>insert a s. 0 \<le> u x) \<and> sum u (insert a s) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a s) = y)
  2977       \<longleftrightarrow> (\<exists>v\<ge>0. \<exists>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y - v *\<^sub>R a)"
  2978   (is "?lhs = ?rhs")
  2979 proof (rule, case_tac[!] "a\<in>s")
  2980   assume "a \<in> s"
  2981   then have *: "insert a s = s" by auto
  2982   assume ?lhs
  2983   then show ?rhs
  2984     unfolding *
  2985     apply (rule_tac x=0 in exI)
  2986     apply auto
  2987     done
  2988 next
  2989   assume ?lhs
  2990   then obtain u where
  2991       u: "\<forall>x\<in>insert a s. 0 \<le> u x" "sum u (insert a s) = w" "(\<Sum>x\<in>insert a s. u x *\<^sub>R x) = y"
  2992     by auto
  2993   assume "a \<notin> s"
  2994   then show ?rhs
  2995     apply (rule_tac x="u a" in exI)
  2996     using u(1)[THEN bspec[where x=a]]
  2997     apply simp
  2998     apply (rule_tac x=u in exI)
  2999     using u[unfolded sum_clauses(2)[OF assms]] and \<open>a\<notin>s\<close>
  3000     apply auto
  3001     done
  3002 next
  3003   assume "a \<in> s"
  3004   then have *: "insert a s = s" by auto
  3005   have fin: "finite (insert a s)" using assms by auto
  3006   assume ?rhs
  3007   then obtain v u where uv: "v\<ge>0" "\<forall>x\<in>s. 0 \<le> u x" "sum u s = w - v" "(\<Sum>x\<in>s. u x *\<^sub>R x) = y - v *\<^sub>R a"
  3008     by auto
  3009   show ?lhs
  3010     apply (rule_tac x = "\<lambda>x. (if a = x then v else 0) + u x" in exI)
  3011     unfolding scaleR_left_distrib and sum.distrib and sum_delta''[OF fin] and sum.delta'[OF fin]
  3012     unfolding sum_clauses(2)[OF assms]
  3013     using uv and uv(2)[THEN bspec[where x=a]] and \<open>a\<in>s\<close>
  3014     apply auto
  3015     done
  3016 next
  3017   assume ?rhs
  3018   then obtain v u where
  3019     uv: "v\<ge>0" "\<forall>x\<in>s. 0 \<le> u x" "sum u s = w - v" "(\<Sum>x\<in>s. u x *\<^sub>R x) = y - v *\<^sub>R a"
  3020     by auto
  3021   moreover
  3022   assume "a \<notin> s"
  3023   moreover
  3024   have "(\<Sum>x\<in>s. if a = x then v else u x) = sum u s"
  3025     and "(\<Sum>x\<in>s. (if a = x then v else u x) *\<^sub>R x) = (\<Sum>x\<in>s. u x *\<^sub>R x)"
  3026     apply (rule_tac sum.cong) apply rule
  3027     defer
  3028     apply (rule_tac sum.cong) apply rule
  3029     using \<open>a \<notin> s\<close>
  3030     apply auto
  3031     done
  3032   ultimately show ?lhs
  3033     apply (rule_tac x="\<lambda>x. if a = x then v else u x" in exI)
  3034     unfolding sum_clauses(2)[OF assms]
  3035     apply auto
  3036     done
  3037 qed
  3038 
  3039 
  3040 subsubsection \<open>Hence some special cases\<close>
  3041 
  3042 lemma convex_hull_2:
  3043   "convex hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b | u v. 0 \<le> u \<and> 0 \<le> v \<and> u + v = 1}"
  3044 proof -
  3045   have *: "\<And>u. (\<forall>x\<in>{a, b}. 0 \<le> u x) \<longleftrightarrow> 0 \<le> u a \<and> 0 \<le> u b"
  3046     by auto
  3047   have **: "finite {b}" by auto
  3048   show ?thesis
  3049     apply (simp add: convex_hull_finite)
  3050     unfolding convex_hull_finite_step[OF **, of a 1, unfolded * conj_assoc]
  3051     apply auto
  3052     apply (rule_tac x=v in exI)
  3053     apply (rule_tac x="1 - v" in exI)
  3054     apply simp
  3055     apply (rule_tac x=u in exI)
  3056     apply simp
  3057     apply (rule_tac x="\<lambda>x. v" in exI)
  3058     apply simp
  3059     done
  3060 qed
  3061 
  3062 lemma convex_hull_2_alt: "convex hull {a,b} = {a + u *\<^sub>R (b - a) | u.  0 \<le> u \<and> u \<le> 1}"
  3063   unfolding convex_hull_2
  3064 proof (rule Collect_cong)
  3065   have *: "\<And>x y ::real. x + y = 1 \<longleftrightarrow> x = 1 - y"
  3066     by auto
  3067   fix x
  3068   show "(\<exists>v u. x = v *\<^sub>R a + u *\<^sub>R b \<and> 0 \<le> v \<and> 0 \<le> u \<and> v + u = 1) \<longleftrightarrow>
  3069     (\<exists>u. x = a + u *\<^sub>R (b - a) \<and> 0 \<le> u \<and> u \<le> 1)"
  3070     unfolding *
  3071     apply auto
  3072     apply (rule_tac[!] x=u in exI)
  3073     apply (auto simp add: algebra_simps)
  3074     done
  3075 qed
  3076 
  3077 lemma convex_hull_3:
  3078   "convex hull {a,b,c} = { u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c | u v w. 0 \<le> u \<and> 0 \<le> v \<and> 0 \<le> w \<and> u + v + w = 1}"
  3079 proof -
  3080   have fin: "finite {a,b,c}" "finite {b,c}" "finite {c}"
  3081     by auto
  3082   have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  3083     by (auto simp add: field_simps)
  3084   show ?thesis
  3085     unfolding convex_hull_finite[OF fin(1)] and convex_hull_finite_step[OF fin(2)] and *
  3086     unfolding convex_hull_finite_step[OF fin(3)]
  3087     apply (rule Collect_cong)
  3088     apply simp
  3089     apply auto
  3090     apply (rule_tac x=va in exI)
  3091     apply (rule_tac x="u c" in exI)
  3092     apply simp
  3093     apply (rule_tac x="1 - v - w" in exI)
  3094     apply simp
  3095     apply (rule_tac x=v in exI)
  3096     apply simp
  3097     apply (rule_tac x="\<lambda>x. w" in exI)
  3098     apply simp
  3099     done
  3100 qed
  3101 
  3102 lemma convex_hull_3_alt:
  3103   "convex hull {a,b,c} = {a + u *\<^sub>R (b - a) + v *\<^sub>R (c - a) | u v.  0 \<le> u \<and> 0 \<le> v \<and> u + v \<le> 1}"
  3104 proof -
  3105   have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  3106     by auto
  3107   show ?thesis
  3108     unfolding convex_hull_3
  3109     apply (auto simp add: *)
  3110     apply (rule_tac x=v in exI)
  3111     apply (rule_tac x=w in exI)
  3112     apply (simp add: algebra_simps)
  3113     apply (rule_tac x=u in exI)
  3114     apply (rule_tac x=v in exI)
  3115     apply (simp add: algebra_simps)
  3116     done
  3117 qed
  3118 
  3119 
  3120 subsection \<open>Relations among closure notions and corresponding hulls\<close>
  3121 
  3122 lemma affine_imp_convex: "affine s \<Longrightarrow> convex s"
  3123   unfolding affine_def convex_def by auto
  3124 
  3125 lemma convex_affine_hull [simp]: "convex (affine hull S)"
  3126   by (simp add: affine_imp_convex)
  3127 
  3128 lemma subspace_imp_convex: "subspace s \<Longrightarrow> convex s"
  3129   using subspace_imp_affine affine_imp_convex by auto
  3130 
  3131 lemma affine_hull_subset_span: "(affine hull s) \<subseteq> (span s)"
  3132   by (metis hull_minimal span_inc subspace_imp_affine subspace_span)
  3133 
  3134 lemma convex_hull_subset_span: "(convex hull s) \<subseteq> (span s)"
  3135   by (metis hull_minimal span_inc subspace_imp_convex subspace_span)
  3136 
  3137 lemma convex_hull_subset_affine_hull: "(convex hull s) \<subseteq> (affine hull s)"
  3138   by (metis affine_affine_hull affine_imp_convex hull_minimal hull_subset)
  3139 
  3140 lemma affine_dependent_imp_dependent: "affine_dependent s \<Longrightarrow> dependent s"
  3141   unfolding affine_dependent_def dependent_def
  3142   using affine_hull_subset_span by auto
  3143 
  3144 lemma dependent_imp_affine_dependent:
  3145   assumes "dependent {x - a| x . x \<in> s}"
  3146     and "a \<notin> s"
  3147   shows "affine_dependent (insert a s)"
  3148 proof -
  3149   from assms(1)[unfolded dependent_explicit] obtain S u v
  3150     where obt: "finite S" "S \<subseteq> {x - a |x. x \<in> s}" "v\<in>S" "u v  \<noteq> 0" "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0"
  3151     by auto
  3152   define t where "t = (\<lambda>x. x + a) ` S"
  3153 
  3154   have inj: "inj_on (\<lambda>x. x + a) S"
  3155     unfolding inj_on_def by auto
  3156   have "0 \<notin> S"
  3157     using obt(2) assms(2) unfolding subset_eq by auto
  3158   have fin: "finite t" and "t \<subseteq> s"
  3159     unfolding t_def using obt(1,2) by auto
  3160   then have "finite (insert a t)" and "insert a t \<subseteq> insert a s"
  3161     by auto
  3162   moreover have *: "\<And>P Q. (\<Sum>x\<in>t. (if x = a then P x else Q x)) = (\<Sum>x\<in>t. Q x)"
  3163     apply (rule sum.cong)
  3164     using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  3165     apply auto
  3166     done
  3167   have "(\<Sum>x\<in>insert a t. if x = a then - (\<Sum>x\<in>t. u (x - a)) else u (x - a)) = 0"
  3168     unfolding sum_clauses(2)[OF fin]
  3169     using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  3170     apply auto
  3171     unfolding *
  3172     apply auto
  3173     done
  3174   moreover have "\<exists>v\<in>insert a t. (if v = a then - (\<Sum>x\<in>t. u (x - a)) else u (v - a)) \<noteq> 0"
  3175     apply (rule_tac x="v + a" in bexI)
  3176     using obt(3,4) and \<open>0\<notin>S\<close>
  3177     unfolding t_def
  3178     apply auto
  3179     done
  3180   moreover have *: "\<And>P Q. (\<Sum>x\<in>t. (if x = a then P x else Q x) *\<^sub>R x) = (\<Sum>x\<in>t. Q x *\<^sub>R x)"
  3181     apply (rule sum.cong)
  3182     using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  3183     apply auto
  3184     done
  3185   have "(\<Sum>x\<in>t. u (x - a)) *\<^sub>R a = (\<Sum>v\<in>t. u (v - a) *\<^sub>R v)"
  3186     unfolding scaleR_left.sum
  3187     unfolding t_def and sum.reindex[OF inj] and o_def
  3188     using obt(5)
  3189     by (auto simp add: sum.distrib scaleR_right_distrib)
  3190   then have "(\<Sum>v\<in>insert a t. (if v = a then - (\<Sum>x\<in>t. u (x - a)) else u (v - a)) *\<^sub>R v) = 0"
  3191     unfolding sum_clauses(2)[OF fin]
  3192     using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  3193     by (auto simp add: *)
  3194   ultimately show ?thesis
  3195     unfolding affine_dependent_explicit
  3196     apply (rule_tac x="insert a t" in exI)
  3197     apply auto
  3198     done
  3199 qed
  3200 
  3201 lemma convex_cone:
  3202   "convex s \<and> cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. (x + y) \<in> s) \<and> (\<forall>x\<in>s. \<forall>c\<ge>0. (c *\<^sub>R x) \<in> s)"
  3203   (is "?lhs = ?rhs")
  3204 proof -
  3205   {
  3206     fix x y
  3207     assume "x\<in>s" "y\<in>s" and ?lhs
  3208     then have "2 *\<^sub>R x \<in>s" "2 *\<^sub>R y \<in> s"
  3209       unfolding cone_def by auto
  3210     then have "x + y \<in> s"
  3211       using \<open>?lhs\<close>[unfolded convex_def, THEN conjunct1]
  3212       apply (erule_tac x="2*\<^sub>R x" in ballE)
  3213       apply (erule_tac x="2*\<^sub>R y" in ballE)
  3214       apply (erule_tac x="1/2" in allE)
  3215       apply simp
  3216       apply (erule_tac x="1/2" in allE)
  3217       apply auto
  3218       done
  3219   }
  3220   then show ?thesis
  3221     unfolding convex_def cone_def by blast
  3222 qed
  3223 
  3224 lemma affine_dependent_biggerset:
  3225   fixes s :: "'a::euclidean_space set"
  3226   assumes "finite s" "card s \<ge> DIM('a) + 2"
  3227   shows "affine_dependent s"
  3228 proof -
  3229   have "s \<noteq> {}" using assms by auto
  3230   then obtain a where "a\<in>s" by auto
  3231   have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  3232     by auto
  3233   have "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  3234     unfolding *
  3235     apply (rule card_image)
  3236     unfolding inj_on_def
  3237     apply auto
  3238     done
  3239   also have "\<dots> > DIM('a)" using assms(2)
  3240     unfolding card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>] by auto
  3241   finally show ?thesis
  3242     apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  3243     apply (rule dependent_imp_affine_dependent)
  3244     apply (rule dependent_biggerset)
  3245     apply auto
  3246     done
  3247 qed
  3248 
  3249 lemma affine_dependent_biggerset_general:
  3250   assumes "finite (s :: 'a::euclidean_space set)"
  3251     and "card s \<ge> dim s + 2"
  3252   shows "affine_dependent s"
  3253 proof -
  3254   from assms(2) have "s \<noteq> {}" by auto
  3255   then obtain a where "a\<in>s" by auto
  3256   have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  3257     by auto
  3258   have **: "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  3259     unfolding *
  3260     apply (rule card_image)
  3261     unfolding inj_on_def
  3262     apply auto
  3263     done
  3264   have "dim {x - a |x. x \<in> s - {a}} \<le> dim s"
  3265     apply (rule subset_le_dim)
  3266     unfolding subset_eq
  3267     using \<open>a\<in>s\<close>
  3268     apply (auto simp add:span_superset span_diff)
  3269     done
  3270   also have "\<dots> < dim s + 1" by auto
  3271   also have "\<dots> \<le> card (s - {a})"
  3272     using assms
  3273     using card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>]
  3274     by auto
  3275   finally show ?thesis
  3276     apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  3277     apply (rule dependent_imp_affine_dependent)
  3278     apply (rule dependent_biggerset_general)
  3279     unfolding **
  3280     apply auto
  3281     done
  3282 qed
  3283 
  3284 
  3285 subsection \<open>Some Properties of Affine Dependent Sets\<close>
  3286 
  3287 lemma affine_independent_0 [simp]: "\<not> affine_dependent {}"
  3288   by (simp add: affine_dependent_def)
  3289 
  3290 lemma affine_independent_1 [simp]: "\<not> affine_dependent {a}"
  3291   by (simp add: affine_dependent_def)
  3292 
  3293 lemma affine_independent_2 [simp]: "\<not> affine_dependent {a,b}"
  3294   by (simp add: affine_dependent_def insert_Diff_if hull_same)
  3295 
  3296 lemma affine_hull_translation: "affine hull ((\<lambda>x. a + x) `  S) = (\<lambda>x. a + x) ` (affine hull S)"
  3297 proof -
  3298   have "affine ((\<lambda>x. a + x) ` (affine hull S))"
  3299     using affine_translation affine_affine_hull by blast
  3300   moreover have "(\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  3301     using hull_subset[of S] by auto
  3302   ultimately have h1: "affine hull ((\<lambda>x. a + x) `  S) \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  3303     by (metis hull_minimal)
  3304   have "affine((\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)))"
  3305     using affine_translation affine_affine_hull by blast
  3306   moreover have "(\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S))"
  3307     using hull_subset[of "(\<lambda>x. a + x) `  S"] by auto
  3308   moreover have "S = (\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S"
  3309     using translation_assoc[of "-a" a] by auto
  3310   ultimately have "(\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)) >= (affine hull S)"
  3311     by (metis hull_minimal)
  3312   then have "affine hull ((\<lambda>x. a + x) ` S) >= (\<lambda>x. a + x) ` (affine hull S)"
  3313     by auto
  3314   then show ?thesis using h1 by auto
  3315 qed
  3316 
  3317 lemma affine_dependent_translation:
  3318   assumes "affine_dependent S"
  3319   shows "affine_dependent ((\<lambda>x. a + x) ` S)"
  3320 proof -
  3321   obtain x where x: "x \<in> S \<and> x \<in> affine hull (S - {x})"
  3322     using assms affine_dependent_def by auto
  3323   have "op + a ` (S - {x}) = op + a ` S - {a + x}"
  3324     by auto
  3325   then have "a + x \<in> affine hull ((\<lambda>x. a + x) ` S - {a + x})"
  3326     using affine_hull_translation[of a "S - {x}"] x by auto
  3327   moreover have "a + x \<in> (\<lambda>x. a + x) ` S"
  3328     using x by auto
  3329   ultimately show ?thesis
  3330     unfolding affine_dependent_def by auto
  3331 qed
  3332 
  3333 lemma affine_dependent_translation_eq:
  3334   "affine_dependent S \<longleftrightarrow> affine_dependent ((\<lambda>x. a + x) ` S)"
  3335 proof -
  3336   {
  3337     assume "affine_dependent ((\<lambda>x. a + x) ` S)"
  3338     then have "affine_dependent S"
  3339       using affine_dependent_translation[of "((\<lambda>x. a + x) ` S)" "-a"] translation_assoc[of "-a" a]
  3340       by auto
  3341   }
  3342   then show ?thesis
  3343     using affine_dependent_translation by auto
  3344 qed
  3345 
  3346 lemma affine_hull_0_dependent:
  3347   assumes "0 \<in> affine hull S"
  3348   shows "dependent S"
  3349 proof -
  3350   obtain s u where s_u: "finite s \<and> s \<noteq> {} \<and> s \<subseteq> S \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  3351     using assms affine_hull_explicit[of S] by auto
  3352   then have "\<exists>v\<in>s. u v \<noteq> 0"
  3353     using sum_not_0[of "u" "s"] by auto
  3354   then have "finite s \<and> s \<subseteq> S \<and> (\<exists>v\<in>s. u v \<noteq> 0 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = 0)"
  3355     using s_u by auto
  3356   then show ?thesis
  3357     unfolding dependent_explicit[of S] by auto
  3358 qed
  3359 
  3360 lemma affine_dependent_imp_dependent2:
  3361   assumes "affine_dependent (insert 0 S)"
  3362   shows "dependent S"
  3363 proof -
  3364   obtain x where x: "x \<in> insert 0 S \<and> x \<in> affine hull (insert 0 S - {x})"
  3365     using affine_dependent_def[of "(insert 0 S)"] assms by blast
  3366   then have "x \<in> span (insert 0 S - {x})"
  3367     using affine_hull_subset_span by auto
  3368   moreover have "span (insert 0 S - {x}) = span (S - {x})"
  3369     using insert_Diff_if[of "0" S "{x}"] span_insert_0[of "S-{x}"] by auto
  3370   ultimately have "x \<in> span (S - {x})" by auto
  3371   then have "x \<noteq> 0 \<Longrightarrow> dependent S"
  3372     using x dependent_def by auto
  3373   moreover
  3374   {
  3375     assume "x = 0"
  3376     then have "0 \<in> affine hull S"
  3377       using x hull_mono[of "S - {0}" S] by auto
  3378     then have "dependent S"
  3379       using affine_hull_0_dependent by auto
  3380   }
  3381   ultimately show ?thesis by auto
  3382 qed
  3383 
  3384 lemma affine_dependent_iff_dependent:
  3385   assumes "a \<notin> S"
  3386   shows "affine_dependent (insert a S) \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` S)"
  3387 proof -
  3388   have "(op + (- a) ` S) = {x - a| x . x : S}" by auto
  3389   then show ?thesis
  3390     using affine_dependent_translation_eq[of "(insert a S)" "-a"]
  3391       affine_dependent_imp_dependent2 assms
  3392       dependent_imp_affine_dependent[of a S]
  3393     by (auto simp del: uminus_add_conv_diff)
  3394 qed
  3395 
  3396 lemma affine_dependent_iff_dependent2:
  3397   assumes "a \<in> S"
  3398   shows "affine_dependent S \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` (S-{a}))"
  3399 proof -
  3400   have "insert a (S - {a}) = S"
  3401     using assms by auto
  3402   then show ?thesis
  3403     using assms affine_dependent_iff_dependent[of a "S-{a}"] by auto
  3404 qed
  3405 
  3406 lemma affine_hull_insert_span_gen:
  3407   "affine hull (insert a s) = (\<lambda>x. a + x) ` span ((\<lambda>x. - a + x) ` s)"
  3408 proof -
  3409   have h1: "{x - a |x. x \<in> s} = ((\<lambda>x. -a+x) ` s)"
  3410     by auto
  3411   {
  3412     assume "a \<notin> s"
  3413     then have ?thesis
  3414       using affine_hull_insert_span[of a s] h1 by auto
  3415   }
  3416   moreover
  3417   {
  3418     assume a1: "a \<in> s"
  3419     have "\<exists>x. x \<in> s \<and> -a+x=0"
  3420       apply (rule exI[of _ a])
  3421       using a1
  3422       apply auto
  3423       done
  3424     then have "insert 0 ((\<lambda>x. -a+x) ` (s - {a})) = (\<lambda>x. -a+x) ` s"
  3425       by auto
  3426     then have "span ((\<lambda>x. -a+x) ` (s - {a}))=span ((\<lambda>x. -a+x) ` s)"
  3427       using span_insert_0[of "op + (- a) ` (s - {a})"] by (auto simp del: uminus_add_conv_diff)
  3428     moreover have "{x - a |x. x \<in> (s - {a})} = ((\<lambda>x. -a+x) ` (s - {a}))"
  3429       by auto
  3430     moreover have "insert a (s - {a}) = insert a s"
  3431       by auto
  3432     ultimately have ?thesis
  3433       using affine_hull_insert_span[of "a" "s-{a}"] by auto
  3434   }
  3435   ultimately show ?thesis by auto
  3436 qed
  3437 
  3438 lemma affine_hull_span2:
  3439   assumes "a \<in> s"
  3440   shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` (s-{a}))"
  3441   using affine_hull_insert_span_gen[of a "s - {a}", unfolded insert_Diff[OF assms]]
  3442   by auto
  3443 
  3444 lemma affine_hull_span_gen:
  3445   assumes "a \<in> affine hull s"
  3446   shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` s)"
  3447 proof -
  3448   have "affine hull (insert a s) = affine hull s"
  3449     using hull_redundant[of a affine s] assms by auto
  3450   then show ?thesis
  3451     using affine_hull_insert_span_gen[of a "s"] by auto
  3452 qed
  3453 
  3454 lemma affine_hull_span_0:
  3455   assumes "0 \<in> affine hull S"
  3456   shows "affine hull S = span S"
  3457   using affine_hull_span_gen[of "0" S] assms by auto
  3458 
  3459 lemma extend_to_affine_basis_nonempty:
  3460   fixes S V :: "'n::euclidean_space set"
  3461   assumes "\<not> affine_dependent S" "S \<subseteq> V" "S \<noteq> {}"
  3462   shows "\<exists>T. \<not> affine_dependent T \<and> S \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  3463 proof -
  3464   obtain a where a: "a \<in> S"
  3465     using assms by auto
  3466   then have h0: "independent  ((\<lambda>x. -a + x) ` (S-{a}))"
  3467     using affine_dependent_iff_dependent2 assms by auto
  3468   then obtain B where B:
  3469     "(\<lambda>x. -a+x) ` (S - {a}) \<subseteq> B \<and> B \<subseteq> (\<lambda>x. -a+x) ` V \<and> independent B \<and> (\<lambda>x. -a+x) ` V \<subseteq> span B"
  3470      using maximal_independent_subset_extend[of "(\<lambda>x. -a+x) ` (S-{a})" "(\<lambda>x. -a + x) ` V"] assms
  3471      by blast
  3472   define T where "T = (\<lambda>x. a+x) ` insert 0 B"
  3473   then have "T = insert a ((\<lambda>x. a+x) ` B)"
  3474     by auto
  3475   then have "affine hull T = (\<lambda>x. a+x) ` span B"
  3476     using affine_hull_insert_span_gen[of a "((\<lambda>x. a+x) ` B)"] translation_assoc[of "-a" a B]
  3477     by auto
  3478   then have "V \<subseteq> affine hull T"
  3479     using B assms translation_inverse_subset[of a V "span B"]
  3480     by auto
  3481   moreover have "T \<subseteq> V"
  3482     using T_def B a assms by auto
  3483   ultimately have "affine hull T = affine hull V"
  3484     by (metis Int_absorb1 Int_absorb2 hull_hull hull_mono)
  3485   moreover have "S \<subseteq> T"
  3486     using T_def B translation_inverse_subset[of a "S-{a}" B]
  3487     by auto
  3488   moreover have "\<not> affine_dependent T"
  3489     using T_def affine_dependent_translation_eq[of "insert 0 B"]
  3490       affine_dependent_imp_dependent2 B
  3491     by auto
  3492   ultimately show ?thesis using \<open>T \<subseteq> V\<close> by auto
  3493 qed
  3494 
  3495 lemma affine_basis_exists:
  3496   fixes V :: "'n::euclidean_space set"
  3497   shows "\<exists>B. B \<subseteq> V \<and> \<not> affine_dependent B \<and> affine hull V = affine hull B"
  3498 proof (cases "V = {}")
  3499   case True
  3500   then show ?thesis
  3501     using affine_independent_0 by auto
  3502 next
  3503   case False
  3504   then obtain x where "x \<in> V" by auto
  3505   then show ?thesis
  3506     using affine_dependent_def[of "{x}"] extend_to_affine_basis_nonempty[of "{x}" V]
  3507     by auto
  3508 qed
  3509 
  3510 proposition extend_to_affine_basis:
  3511   fixes S V :: "'n::euclidean_space set"
  3512   assumes "\<not> affine_dependent S" "S \<subseteq> V"
  3513   obtains T where "\<not> affine_dependent T" "S \<subseteq> T" "T \<subseteq> V" "affine hull T = affine hull V"
  3514 proof (cases "S = {}")
  3515   case True then show ?thesis
  3516     using affine_basis_exists by (metis empty_subsetI that)
  3517 next
  3518   case False
  3519   then show ?thesis by (metis assms extend_to_affine_basis_nonempty that)
  3520 qed
  3521 
  3522 
  3523 subsection \<open>Affine Dimension of a Set\<close>
  3524 
  3525 definition aff_dim :: "('a::euclidean_space) set \<Rightarrow> int"
  3526   where "aff_dim V =
  3527   (SOME d :: int.
  3528     \<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = d + 1)"
  3529 
  3530 lemma aff_dim_basis_exists:
  3531   fixes V :: "('n::euclidean_space) set"
  3532   shows "\<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  3533 proof -
  3534   obtain B where "\<not> affine_dependent B \<and> affine hull B = affine hull V"
  3535     using affine_basis_exists[of V] by auto
  3536   then show ?thesis
  3537     unfolding aff_dim_def
  3538       some_eq_ex[of "\<lambda>d. \<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = d + 1"]
  3539     apply auto
  3540     apply (rule exI[of _ "int (card B) - (1 :: int)"])
  3541     apply (rule exI[of _ "B"])
  3542     apply auto
  3543     done
  3544 qed
  3545 
  3546 lemma affine_hull_nonempty: "S \<noteq> {} \<longleftrightarrow> affine hull S \<noteq> {}"
  3547 proof -
  3548   have "S = {} \<Longrightarrow> affine hull S = {}"
  3549     using affine_hull_empty by auto
  3550   moreover have "affine hull S = {} \<Longrightarrow> S = {}"
  3551     unfolding hull_def by auto
  3552   ultimately show ?thesis by blast
  3553 qed
  3554 
  3555 lemma aff_dim_parallel_subspace_aux:
  3556   fixes B :: "'n::euclidean_space set"
  3557   assumes "\<not> affine_dependent B" "a \<in> B"
  3558   shows "finite B \<and> ((card B) - 1 = dim (span ((\<lambda>x. -a+x) ` (B-{a}))))"
  3559 proof -
  3560   have "independent ((\<lambda>x. -a + x) ` (B-{a}))"
  3561     using affine_dependent_iff_dependent2 assms by auto
  3562   then have fin: "dim (span ((\<lambda>x. -a+x) ` (B-{a}))) = card ((\<lambda>x. -a + x) ` (B-{a}))"
  3563     "finite ((\<lambda>x. -a + x) ` (B - {a}))"
  3564     using indep_card_eq_dim_span[of "(\<lambda>x. -a+x) ` (B-{a})"] by auto
  3565   show ?thesis
  3566   proof (cases "(\<lambda>x. -a + x) ` (B - {a}) = {}")
  3567     case True
  3568     have "B = insert a ((\<lambda>x. a + x) ` (\<lambda>x. -a + x) ` (B - {a}))"
  3569       using translation_assoc[of "a" "-a" "(B - {a})"] assms by auto
  3570     then have "B = {a}" using True by auto
  3571     then show ?thesis using assms fin by auto
  3572   next
  3573     case False
  3574     then have "card ((\<lambda>x. -a + x) ` (B - {a})) > 0"
  3575       using fin by auto
  3576     moreover have h1: "card ((\<lambda>x. -a + x) ` (B-{a})) = card (B-{a})"
  3577        apply (rule card_image)
  3578        using translate_inj_on
  3579        apply (auto simp del: uminus_add_conv_diff)
  3580        done
  3581     ultimately have "card (B-{a}) > 0" by auto
  3582     then have *: "finite (B - {a})"
  3583       using card_gt_0_iff[of "(B - {a})"] by auto
  3584     then have "card (B - {a}) = card B - 1"
  3585       using card_Diff_singleton assms by auto
  3586     with * show ?thesis using fin h1 by auto
  3587   qed
  3588 qed
  3589 
  3590 lemma aff_dim_parallel_subspace:
  3591   fixes V L :: "'n::euclidean_space set"
  3592   assumes "V \<noteq> {}"
  3593     and "subspace L"
  3594     and "affine_parallel (affine hull V) L"
  3595   shows "aff_dim V = int (dim L)"
  3596 proof -
  3597   obtain B where
  3598     B: "affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> int (card B) = aff_dim V + 1"
  3599     using aff_dim_basis_exists by auto
  3600   then have "B \<noteq> {}"
  3601     using assms B affine_hull_nonempty[of V] affine_hull_nonempty[of B]
  3602     by auto
  3603   then obtain a where a: "a \<in> B" by auto
  3604   define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  3605   moreover have "affine_parallel (affine hull B) Lb"
  3606     using Lb_def B assms affine_hull_span2[of a B] a
  3607       affine_parallel_commut[of "Lb" "(affine hull B)"]
  3608     unfolding affine_parallel_def
  3609     by auto
  3610   moreover have "subspace Lb"
  3611     using Lb_def subspace_span by auto
  3612   moreover have "affine hull B \<noteq> {}"
  3613     using assms B affine_hull_nonempty[of V] by auto
  3614   ultimately have "L = Lb"
  3615     using assms affine_parallel_subspace[of "affine hull B"] affine_affine_hull[of B] B
  3616     by auto
  3617   then have "dim L = dim Lb"
  3618     by auto
  3619   moreover have "card B - 1 = dim Lb" and "finite B"
  3620     using Lb_def aff_dim_parallel_subspace_aux a B by auto
  3621   ultimately show ?thesis
  3622     using B \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  3623 qed
  3624 
  3625 lemma aff_independent_finite:
  3626   fixes B :: "'n::euclidean_space set"
  3627   assumes "\<not> affine_dependent B"
  3628   shows "finite B"
  3629 proof -
  3630   {
  3631     assume "B \<noteq> {}"
  3632     then obtain a where "a \<in> B" by auto
  3633     then have ?thesis
  3634       using aff_dim_parallel_subspace_aux assms by auto
  3635   }
  3636   then show ?thesis by auto
  3637 qed
  3638 
  3639 lemma independent_finite:
  3640   fixes B :: "'n::euclidean_space set"
  3641   assumes "independent B"
  3642   shows "finite B"
  3643   using affine_dependent_imp_dependent[of B] aff_independent_finite[of B] assms
  3644   by auto
  3645 
  3646 lemma subspace_dim_equal:
  3647   assumes "subspace (S :: ('n::euclidean_space) set)"
  3648     and "subspace T"
  3649     and "S \<subseteq> T"
  3650     and "dim S \<ge> dim T"
  3651   shows "S = T"
  3652 proof -
  3653   obtain B where B: "B \<le> S" "independent B \<and> S \<subseteq> span B" "card B = dim S"
  3654     using basis_exists[of S] by auto
  3655   then have "span B \<subseteq> S"
  3656     using span_mono[of B S] span_eq[of S] assms by metis
  3657   then have "span B = S"
  3658     using B by auto
  3659   have "dim S = dim T"
  3660     using assms dim_subset[of S T] by auto
  3661   then have "T \<subseteq> span B"
  3662     using card_eq_dim[of B T] B independent_finite assms by auto
  3663   then show ?thesis
  3664     using assms \<open>span B = S\<close> by auto
  3665 qed
  3666 
  3667 corollary dim_eq_span:
  3668   fixes S :: "'a::euclidean_space set"
  3669   shows "\<lbrakk>S \<subseteq> T; dim T \<le> dim S\<rbrakk> \<Longrightarrow> span S = span T"
  3670 by (simp add: span_mono subspace_dim_equal subspace_span)
  3671 
  3672 lemma dim_eq_full:
  3673     fixes S :: "'a :: euclidean_space set"
  3674     shows "dim S = DIM('a) \<longleftrightarrow> span S = UNIV"
  3675 apply (rule iffI)
  3676  apply (metis dim_eq_span dim_subset_UNIV span_Basis span_span subset_UNIV)
  3677 by (metis dim_UNIV dim_span)
  3678 
  3679 lemma span_substd_basis:
  3680   assumes d: "d \<subseteq> Basis"
  3681   shows "span d = {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  3682   (is "_ = ?B")
  3683 proof -
  3684   have "d \<subseteq> ?B"
  3685     using d by (auto simp: inner_Basis)
  3686   moreover have s: "subspace ?B"
  3687     using subspace_substandard[of "\<lambda>i. i \<notin> d"] .
  3688   ultimately have "span d \<subseteq> ?B"
  3689     using span_mono[of d "?B"] span_eq[of "?B"] by blast
  3690   moreover have *: "card d \<le> dim (span d)"
  3691     using independent_card_le_dim[of d "span d"] independent_substdbasis[OF assms] span_inc[of d]
  3692     by auto
  3693   moreover from * have "dim ?B \<le> dim (span d)"
  3694     using dim_substandard[OF assms] by auto
  3695   ultimately show ?thesis
  3696     using s subspace_dim_equal[of "span d" "?B"] subspace_span[of d] by auto
  3697 qed
  3698 
  3699 lemma basis_to_substdbasis_subspace_isomorphism:
  3700   fixes B :: "'a::euclidean_space set"
  3701   assumes "independent B"
  3702   shows "\<exists>f d::'a set. card d = card B \<and> linear f \<and> f ` B = d \<and>
  3703     f ` span B = {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} \<and> inj_on f (span B) \<and> d \<subseteq> Basis"
  3704 proof -
  3705   have B: "card B = dim B"
  3706     using dim_unique[of B B "card B"] assms span_inc[of B] by auto
  3707   have "dim B \<le> card (Basis :: 'a set)"
  3708     using dim_subset_UNIV[of B] by simp
  3709   from ex_card[OF this] obtain d :: "'a set" where d: "d \<subseteq> Basis" and t: "card d = dim B"
  3710     by auto
  3711   let ?t = "{x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  3712   have "\<exists>f. linear f \<and> f ` B = d \<and> f ` span B = ?t \<and> inj_on f (span B)"
  3713     apply (rule basis_to_basis_subspace_isomorphism[of "span B" ?t B "d"])
  3714     apply (rule subspace_span)
  3715     apply (rule subspace_substandard)
  3716     defer
  3717     apply (rule span_inc)
  3718     apply (rule assms)
  3719     defer
  3720     unfolding dim_span[of B]
  3721     apply(rule B)
  3722     unfolding span_substd_basis[OF d, symmetric]
  3723     apply (rule span_inc)
  3724     apply (rule independent_substdbasis[OF d])
  3725     apply rule
  3726     apply assumption
  3727     unfolding t[symmetric] span_substd_basis[OF d] dim_substandard[OF d]
  3728     apply auto
  3729     done
  3730   with t \<open>card B = dim B\<close> d show ?thesis by auto
  3731 qed
  3732 
  3733 lemma aff_dim_empty:
  3734   fixes S :: "'n::euclidean_space set"
  3735   shows "S = {} \<longleftrightarrow> aff_dim S = -1"
  3736 proof -
  3737   obtain B where *: "affine hull B = affine hull S"
  3738     and "\<not> affine_dependent B"
  3739     and "int (card B) = aff_dim S + 1"
  3740     using aff_dim_basis_exists by auto
  3741   moreover
  3742   from * have "S = {} \<longleftrightarrow> B = {}"
  3743     using affine_hull_nonempty[of B] affine_hull_nonempty[of S] by auto
  3744   ultimately show ?thesis
  3745     using aff_independent_finite[of B] card_gt_0_iff[of B] by auto
  3746 qed
  3747 
  3748 lemma aff_dim_empty_eq [simp]: "aff_dim ({}::'a::euclidean_space set) = -1"
  3749   by (simp add: aff_dim_empty [symmetric])
  3750 
  3751 lemma aff_dim_affine_hull [simp]: "aff_dim (affine hull S) = aff_dim S"
  3752   unfolding aff_dim_def using hull_hull[of _ S] by auto
  3753 
  3754 lemma aff_dim_affine_hull2:
  3755   assumes "affine hull S = affine hull T"
  3756   shows "aff_dim S = aff_dim T"
  3757   unfolding aff_dim_def using assms by auto
  3758 
  3759 lemma aff_dim_unique:
  3760   fixes B V :: "'n::euclidean_space set"
  3761   assumes "affine hull B = affine hull V \<and> \<not> affine_dependent B"
  3762   shows "of_nat (card B) = aff_dim V + 1"
  3763 proof (cases "B = {}")
  3764   case True
  3765   then have "V = {}"
  3766     using affine_hull_nonempty[of V] affine_hull_nonempty[of B] assms
  3767     by auto
  3768   then have "aff_dim V = (-1::int)"
  3769     using aff_dim_empty by auto
  3770   then show ?thesis
  3771     using \<open>B = {}\<close> by auto
  3772 next
  3773   case False
  3774   then obtain a where a: "a \<in> B" by auto
  3775   define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  3776   have "affine_parallel (affine hull B) Lb"
  3777     using Lb_def affine_hull_span2[of a B] a
  3778       affine_parallel_commut[of "Lb" "(affine hull B)"]
  3779     unfolding affine_parallel_def by auto
  3780   moreover have "subspace Lb"
  3781     using Lb_def subspace_span by auto
  3782   ultimately have "aff_dim B = int(dim Lb)"
  3783     using aff_dim_parallel_subspace[of B Lb] \<open>B \<noteq> {}\<close> by auto
  3784   moreover have "(card B) - 1 = dim Lb" "finite B"
  3785     using Lb_def aff_dim_parallel_subspace_aux a assms by auto
  3786   ultimately have "of_nat (card B) = aff_dim B + 1"
  3787     using \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  3788   then show ?thesis
  3789     using aff_dim_affine_hull2 assms by auto
  3790 qed
  3791 
  3792 lemma aff_dim_affine_independent:
  3793   fixes B :: "'n::euclidean_space set"
  3794   assumes "\<not> affine_dependent B"
  3795   shows "of_nat (card B) = aff_dim B + 1"
  3796   using aff_dim_unique[of B B] assms by auto
  3797 
  3798 lemma affine_independent_iff_card:
  3799     fixes s :: "'a::euclidean_space set"
  3800     shows "~ affine_dependent s \<longleftrightarrow> finite s \<and> aff_dim s = int(card s) - 1"
  3801   apply (rule iffI)
  3802   apply (simp add: aff_dim_affine_independent aff_independent_finite)
  3803   by (metis affine_basis_exists [of s] aff_dim_unique card_subset_eq diff_add_cancel of_nat_eq_iff)
  3804 
  3805 lemma aff_dim_sing [simp]:
  3806   fixes a :: "'n::euclidean_space"
  3807   shows "aff_dim {a} = 0"
  3808   using aff_dim_affine_independent[of "{a}"] affine_independent_1 by auto
  3809 
  3810 lemma aff_dim_2 [simp]: "aff_dim {a,b} = (if a = b then 0 else 1)"
  3811 proof (clarsimp)
  3812   assume "a \<noteq> b"
  3813   then have "aff_dim{a,b} = card{a,b} - 1"
  3814     using affine_independent_2 [of a b] aff_dim_affine_independent by fastforce
  3815   also have "... = 1"
  3816     using \<open>a \<noteq> b\<close> by simp
  3817   finally show "aff_dim {a, b} = 1" .
  3818 qed
  3819 
  3820 lemma aff_dim_inner_basis_exists:
  3821   fixes V :: "('n::euclidean_space) set"
  3822   shows "\<exists>B. B \<subseteq> V \<and> affine hull B = affine hull V \<and>
  3823     \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  3824 proof -
  3825   obtain B where B: "\<not> affine_dependent B" "B \<subseteq> V" "affine hull B = affine hull V"
  3826     using affine_basis_exists[of V] by auto
  3827   then have "of_nat(card B) = aff_dim V+1" using aff_dim_unique by auto
  3828   with B show ?thesis by auto
  3829 qed
  3830 
  3831 lemma aff_dim_le_card:
  3832   fixes V :: "'n::euclidean_space set"
  3833   assumes "finite V"
  3834   shows "aff_dim V \<le> of_nat (card V) - 1"
  3835 proof -
  3836   obtain B where B: "B \<subseteq> V" "of_nat (card B) = aff_dim V + 1"
  3837     using aff_dim_inner_basis_exists[of V] by auto
  3838   then have "card B \<le> card V"
  3839     using assms card_mono by auto
  3840   with B show ?thesis by auto
  3841 qed
  3842 
  3843 lemma aff_dim_parallel_eq:
  3844   fixes S T :: "'n::euclidean_space set"
  3845   assumes "affine_parallel (affine hull S) (affine hull T)"
  3846   shows "aff_dim S = aff_dim T"
  3847 proof -
  3848   {
  3849     assume "T \<noteq> {}" "S \<noteq> {}"
  3850     then obtain L where L: "subspace L \<and> affine_parallel (affine hull T) L"
  3851       using affine_parallel_subspace[of "affine hull T"]
  3852         affine_affine_hull[of T] affine_hull_nonempty
  3853       by auto
  3854     then have "aff_dim T = int (dim L)"
  3855       using aff_dim_parallel_subspace \<open>T \<noteq> {}\<close> by auto
  3856     moreover have *: "subspace L \<and> affine_parallel (affine hull S) L"
  3857        using L affine_parallel_assoc[of "affine hull S" "affine hull T" L] assms by auto
  3858     moreover from * have "aff_dim S = int (dim L)"
  3859       using aff_dim_parallel_subspace \<open>S \<noteq> {}\<close> by auto
  3860     ultimately have ?thesis by auto
  3861   }
  3862   moreover
  3863   {
  3864     assume "S = {}"
  3865     then have "S = {}" and "T = {}"
  3866       using assms affine_hull_nonempty
  3867       unfolding affine_parallel_def
  3868       by auto
  3869     then have ?thesis using aff_dim_empty by auto
  3870   }
  3871   moreover
  3872   {
  3873     assume "T = {}"
  3874     then have "S = {}" and "T = {}"
  3875       using assms affine_hull_nonempty
  3876       unfolding affine_parallel_def
  3877       by auto
  3878     then have ?thesis
  3879       using aff_dim_empty by auto
  3880   }
  3881   ultimately show ?thesis by blast
  3882 qed
  3883 
  3884 lemma aff_dim_translation_eq:
  3885   fixes a :: "'n::euclidean_space"
  3886   shows "aff_dim ((\<lambda>x. a + x) ` S) = aff_dim S"
  3887 proof -
  3888   have "affine_parallel (affine hull S) (affine hull ((\<lambda>x. a + x) ` S))"
  3889     unfolding affine_parallel_def
  3890     apply (rule exI[of _ "a"])
  3891     using affine_hull_translation[of a S]
  3892     apply auto
  3893     done
  3894   then show ?thesis
  3895     using aff_dim_parallel_eq[of S "(\<lambda>x. a + x) ` S"] by auto
  3896 qed
  3897 
  3898 lemma aff_dim_affine:
  3899   fixes S L :: "'n::euclidean_space set"
  3900   assumes "S \<noteq> {}"
  3901     and "affine S"
  3902     and "subspace L"
  3903     and "affine_parallel S L"
  3904   shows "aff_dim S = int (dim L)"
  3905 proof -
  3906   have *: "affine hull S = S"
  3907     using assms affine_hull_eq[of S] by auto
  3908   then have "affine_parallel (affine hull S) L"
  3909     using assms by (simp add: *)
  3910   then show ?thesis
  3911     using assms aff_dim_parallel_subspace[of S L] by blast
  3912 qed
  3913 
  3914 lemma dim_affine_hull:
  3915   fixes S :: "'n::euclidean_space set"
  3916   shows "dim (affine hull S) = dim S"
  3917 proof -
  3918   have "dim (affine hull S) \<ge> dim S"
  3919     using dim_subset by auto
  3920   moreover have "dim (span S) \<ge> dim (affine hull S)"
  3921     using dim_subset affine_hull_subset_span by blast
  3922   moreover have "dim (span S) = dim S"
  3923     using dim_span by auto
  3924   ultimately show ?thesis by auto
  3925 qed
  3926 
  3927 lemma aff_dim_subspace:
  3928   fixes S :: "'n::euclidean_space set"
  3929   assumes "subspace S"
  3930   shows "aff_dim S = int (dim S)"
  3931 proof (cases "S={}")
  3932   case True with assms show ?thesis
  3933     by (simp add: subspace_affine)
  3934 next
  3935   case False
  3936   with aff_dim_affine[of S S] assms subspace_imp_affine[of S] affine_parallel_reflex[of S] subspace_affine
  3937   show ?thesis by auto
  3938 qed
  3939 
  3940 lemma aff_dim_zero:
  3941   fixes S :: "'n::euclidean_space set"
  3942   assumes "0 \<in> affine hull S"
  3943   shows "aff_dim S = int (dim S)"
  3944 proof -
  3945   have "subspace (affine hull S)"
  3946     using subspace_affine[of "affine hull S"] affine_affine_hull assms
  3947     by auto
  3948   then have "aff_dim (affine hull S) = int (dim (affine hull S))"
  3949     using assms aff_dim_subspace[of "affine hull S"] by auto
  3950   then show ?thesis
  3951     using aff_dim_affine_hull[of S] dim_affine_hull[of S]
  3952     by auto
  3953 qed
  3954 
  3955 lemma aff_dim_eq_dim:
  3956   fixes S :: "'n::euclidean_space set"
  3957   assumes "a \<in> affine hull S"
  3958   shows "aff_dim S = int (dim ((\<lambda>x. -a+x) ` S))"
  3959 proof -
  3960   have "0 \<in> affine hull ((\<lambda>x. -a+x) ` S)"
  3961     unfolding Convex_Euclidean_Space.affine_hull_translation
  3962     using assms by (simp add: ab_group_add_class.ab_left_minus image_iff)
  3963   with aff_dim_zero show ?thesis
  3964     by (metis aff_dim_translation_eq)
  3965 qed
  3966 
  3967 lemma aff_dim_UNIV [simp]: "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  3968   using aff_dim_subspace[of "(UNIV :: 'n::euclidean_space set)"]
  3969     dim_UNIV[where 'a="'n::euclidean_space"]
  3970   by auto
  3971 
  3972 lemma aff_dim_geq:
  3973   fixes V :: "'n::euclidean_space set"
  3974   shows "aff_dim V \<ge> -1"
  3975 proof -
  3976   obtain B where "affine hull B = affine hull V"
  3977     and "\<not> affine_dependent B"
  3978     and "int (card B) = aff_dim V + 1"
  3979     using aff_dim_basis_exists by auto
  3980   then show ?thesis by auto
  3981 qed
  3982 
  3983 lemma aff_dim_negative_iff [simp]:
  3984   fixes S :: "'n::euclidean_space set"
  3985   shows "aff_dim S < 0 \<longleftrightarrow>S = {}"
  3986 by (metis aff_dim_empty aff_dim_geq diff_0 eq_iff zle_diff1_eq)
  3987 
  3988 lemma aff_lowdim_subset_hyperplane:
  3989   fixes S :: "'a::euclidean_space set"
  3990   assumes "aff_dim S < DIM('a)"
  3991   obtains a b where "a \<noteq> 0" "S \<subseteq> {x. a \<bullet> x = b}"
  3992 proof (cases "S={}")
  3993   case True
  3994   moreover
  3995   have "(SOME b. b \<in> Basis) \<noteq> 0"
  3996     by (metis norm_some_Basis norm_zero zero_neq_one)
  3997   ultimately show ?thesis
  3998     using that by blast
  3999 next
  4000   case False
  4001   then obtain c S' where "c \<notin> S'" "S = insert c S'"
  4002     by (meson equals0I mk_disjoint_insert)
  4003   have "dim (op + (-c) ` S) < DIM('a)"
  4004     by (metis \<open>S = insert c S'\<close> aff_dim_eq_dim assms hull_inc insertI1 of_nat_less_imp_less)
  4005   then obtain a where "a \<noteq> 0" "span (op + (-c) ` S) \<subseteq> {x. a \<bullet> x = 0}"
  4006     using lowdim_subset_hyperplane by blast
  4007   moreover
  4008   have "a \<bullet> w = a \<bullet> c" if "span (op + (- c) ` S) \<subseteq> {x. a \<bullet> x = 0}" "w \<in> S" for w
  4009   proof -
  4010     have "w-c \<in> span (op + (- c) ` S)"
  4011       by (simp add: span_superset \<open>w \<in> S\<close>)
  4012     with that have "w-c \<in> {x. a \<bullet> x = 0}"
  4013       by blast
  4014     then show ?thesis
  4015       by (auto simp: algebra_simps)
  4016   qed
  4017   ultimately have "S \<subseteq> {x. a \<bullet> x = a \<bullet> c}"
  4018     by blast
  4019   then show ?thesis
  4020     by (rule that[OF \<open>a \<noteq> 0\<close>])
  4021 qed
  4022 
  4023 lemma affine_independent_card_dim_diffs:
  4024   fixes S :: "'a :: euclidean_space set"
  4025   assumes "~ affine_dependent S" "a \<in> S"
  4026     shows "card S = dim {x - a|x. x \<in> S} + 1"
  4027 proof -
  4028   have 1: "{b - a|b. b \<in> (S - {a})} \<subseteq> {x - a|x. x \<in> S}" by auto
  4029   have 2: "x - a \<in> span {b - a |b. b \<in> S - {a}}" if "x \<in> S" for x
  4030   proof (cases "x = a")
  4031     case True then show ?thesis by simp
  4032   next
  4033     case False then show ?thesis
  4034       using assms by (blast intro: span_superset that)
  4035   qed
  4036   have "\<not> affine_dependent (insert a S)"
  4037     by (simp add: assms insert_absorb)
  4038   then have 3: "independent {b - a |b. b \<in> S - {a}}"
  4039       using dependent_imp_affine_dependent by fastforce
  4040   have "{b - a |b. b \<in> S - {a}} = (\<lambda>b. b-a) ` (S - {a})"
  4041     by blast
  4042   then have "card {b - a |b. b \<in> S - {a}} = card ((\<lambda>b. b-a) ` (S - {a}))"
  4043     by simp
  4044   also have "... = card (S - {a})"
  4045     by (metis (no_types, lifting) card_image diff_add_cancel inj_onI)
  4046   also have "... = card S - 1"
  4047     by (simp add: aff_independent_finite assms)
  4048   finally have 4: "card {b - a |b. b \<in> S - {a}} = card S - 1" .
  4049   have "finite S"
  4050     by (meson assms aff_independent_finite)
  4051   with \<open>a \<in> S\<close> have "card S \<noteq> 0" by auto
  4052   moreover have "dim {x - a |x. x \<in> S} = card S - 1"
  4053     using 2 by (blast intro: dim_unique [OF 1 _ 3 4])
  4054   ultimately show ?thesis
  4055     by auto
  4056 qed
  4057 
  4058 lemma independent_card_le_aff_dim:
  4059   fixes B :: "'n::euclidean_space set"
  4060   assumes "B \<subseteq> V"
  4061   assumes "\<not> affine_dependent B"
  4062   shows "int (card B) \<le> aff_dim V + 1"
  4063 proof -
  4064   obtain T where T: "\<not> affine_dependent T \<and> B \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  4065     by (metis assms extend_to_affine_basis[of B V])
  4066   then have "of_nat (card T) = aff_dim V + 1"
  4067     using aff_dim_unique by auto
  4068   then show ?thesis
  4069     using T card_mono[of T B] aff_independent_finite[of T] by auto
  4070 qed
  4071 
  4072 lemma aff_dim_subset:
  4073   fixes S T :: "'n::euclidean_space set"
  4074   assumes "S \<subseteq> T"
  4075   shows "aff_dim S \<le> aff_dim T"
  4076 proof -
  4077   obtain B where B: "\<not> affine_dependent B" "B \<subseteq> S" "affine hull B = affine hull S"
  4078     "of_nat (card B) = aff_dim S + 1"
  4079     using aff_dim_inner_basis_exists[of S] by auto
  4080   then have "int (card B) \<le> aff_dim T + 1"
  4081     using assms independent_card_le_aff_dim[of B T] by auto
  4082   with B show ?thesis by auto
  4083 qed
  4084 
  4085 lemma aff_dim_le_DIM:
  4086   fixes S :: "'n::euclidean_space set"
  4087   shows "aff_dim S \<le> int (DIM('n))"
  4088 proof -
  4089   have "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  4090     using aff_dim_UNIV by auto
  4091   then show "aff_dim (S:: 'n::euclidean_space set) \<le> int(DIM('n))"
  4092     using aff_dim_subset[of S "(UNIV :: ('n::euclidean_space) set)"] subset_UNIV by auto
  4093 qed
  4094 
  4095 lemma affine_dim_equal:
  4096   fixes S :: "'n::euclidean_space set"
  4097   assumes "affine S" "affine T" "S \<noteq> {}" "S \<subseteq> T" "aff_dim S = aff_dim T"
  4098   shows "S = T"
  4099 proof -
  4100   obtain a where "a \<in> S" using assms by auto
  4101   then have "a \<in> T" using assms by auto
  4102   define LS where "LS = {y. \<exists>x \<in> S. (-a) + x = y}"
  4103   then have ls: "subspace LS" "affine_parallel S LS"
  4104     using assms parallel_subspace_explicit[of S a LS] \<open>a \<in> S\<close> by auto
  4105   then have h1: "int(dim LS) = aff_dim S"
  4106     using assms aff_dim_affine[of S LS] by auto
  4107   have "T \<noteq> {}" using assms by auto
  4108   define LT where "LT = {y. \<exists>x \<in> T. (-a) + x = y}"
  4109   then have lt: "subspace LT \<and> affine_parallel T LT"
  4110     using assms parallel_subspace_explicit[of T a LT] \<open>a \<in> T\<close> by auto
  4111   then have "int(dim LT) = aff_dim T"
  4112     using assms aff_dim_affine[of T LT] \<open>T \<noteq> {}\<close> by auto
  4113   then have "dim LS = dim LT"
  4114     using h1 assms by auto
  4115   moreover have "LS \<le> LT"
  4116     using LS_def LT_def assms by auto
  4117   ultimately have "LS = LT"
  4118     using subspace_dim_equal[of LS LT] ls lt by auto
  4119   moreover have "S = {x. \<exists>y \<in> LS. a+y=x}"
  4120     using LS_def by auto
  4121   moreover have "T = {x. \<exists>y \<in> LT. a+y=x}"
  4122     using LT_def by auto
  4123   ultimately show ?thesis by auto
  4124 qed
  4125 
  4126 lemma aff_dim_eq_0:
  4127   fixes S :: "'a::euclidean_space set"
  4128   shows "aff_dim S = 0 \<longleftrightarrow> (\<exists>a. S = {a})"
  4129 proof (cases "S = {}")
  4130   case True
  4131   then show ?thesis
  4132     by auto
  4133 next
  4134   case False
  4135   then obtain a where "a \<in> S" by auto
  4136   show ?thesis
  4137   proof safe
  4138     assume 0: "aff_dim S = 0"
  4139     have "~ {a,b} \<subseteq> S" if "b \<noteq> a" for b
  4140       by (metis "0" aff_dim_2 aff_dim_subset not_one_le_zero that)
  4141     then show "\<exists>a. S = {a}"
  4142       using \<open>a \<in> S\<close> by blast
  4143   qed auto
  4144 qed
  4145 
  4146 lemma affine_hull_UNIV:
  4147   fixes S :: "'n::euclidean_space set"
  4148   assumes "aff_dim S = int(DIM('n))"
  4149   shows "affine hull S = (UNIV :: ('n::euclidean_space) set)"
  4150 proof -
  4151   have "S \<noteq> {}"
  4152     using assms aff_dim_empty[of S] by auto
  4153   have h0: "S \<subseteq> affine hull S"
  4154     using hull_subset[of S _] by auto
  4155   have h1: "aff_dim (UNIV :: ('n::euclidean_space) set) = aff_dim S"
  4156     using aff_dim_UNIV assms by auto
  4157   then have h2: "aff_dim (affine hull S) \<le> aff_dim (UNIV :: ('n::euclidean_space) set)"
  4158     using aff_dim_le_DIM[of "affine hull S"] assms h0 by auto
  4159   have h3: "aff_dim S \<le> aff_dim (affine hull S)"
  4160     using h0 aff_dim_subset[of S "affine hull S"] assms by auto
  4161   then have h4: "aff_dim (affine hull S) = aff_dim (UNIV :: ('n::euclidean_space) set)"
  4162     using h0 h1 h2 by auto
  4163   then show ?thesis
  4164     using affine_dim_equal[of "affine hull S" "(UNIV :: ('n::euclidean_space) set)"]
  4165       affine_affine_hull[of S] affine_UNIV assms h4 h0 \<open>S \<noteq> {}\<close>
  4166     by auto
  4167 qed
  4168 
  4169 lemma disjoint_affine_hull:
  4170   fixes s :: "'n::euclidean_space set"
  4171   assumes "~ affine_dependent s" "t \<subseteq> s" "u \<subseteq> s" "t \<inter> u = {}"
  4172     shows "(affine hull t) \<inter> (affine hull u) = {}"
  4173 proof -
  4174   have "finite s" using assms by (simp add: aff_independent_finite)
  4175   then have "finite t" "finite u" using assms finite_subset by blast+
  4176   { fix y
  4177     assume yt: "y \<in> affine hull t" and yu: "y \<in> affine hull u"
  4178     then obtain a b
  4179            where a1 [simp]: "sum a t = 1" and [simp]: "sum (\<lambda>v. a v *\<^sub>R v) t = y"
  4180              and [simp]: "sum b u = 1" "sum (\<lambda>v. b v *\<^sub>R v) u = y"
  4181       by (auto simp: affine_hull_finite \<open>finite t\<close> \<open>finite u\<close>)
  4182     define c where "c x = (if x \<in> t then a x else if x \<in> u then -(b x) else 0)" for x
  4183     have [simp]: "s \<inter> t = t" "s \<inter> - t \<inter> u = u" using assms by auto
  4184     have "sum c s = 0"
  4185       by (simp add: c_def comm_monoid_add_class.sum.If_cases \<open>finite s\<close> sum_negf)
  4186     moreover have "~ (\<forall>v\<in>s. c v = 0)"
  4187       by (metis (no_types) IntD1 \<open>s \<inter> t = t\<close> a1 c_def sum_not_0 zero_neq_one)
  4188     moreover have "(\<Sum>v\<in>s. c v *\<^sub>R v) = 0"
  4189       by (simp add: c_def if_smult sum_negf
  4190              comm_monoid_add_class.sum.If_cases \<open>finite s\<close>)
  4191     ultimately have False
  4192       using assms \<open>finite s\<close> by (auto simp: affine_dependent_explicit)
  4193   }
  4194   then show ?thesis by blast
  4195 qed
  4196 
  4197 lemma aff_dim_convex_hull:
  4198   fixes S :: "'n::euclidean_space set"
  4199   shows "aff_dim (convex hull S) = aff_dim S"
  4200   using aff_dim_affine_hull[of S] convex_hull_subset_affine_hull[of S]
  4201     hull_subset[of S "convex"] aff_dim_subset[of S "convex hull S"]
  4202     aff_dim_subset[of "convex hull S" "affine hull S"]
  4203   by auto
  4204 
  4205 lemma aff_dim_cball:
  4206   fixes a :: "'n::euclidean_space"
  4207   assumes "e > 0"
  4208   shows "aff_dim (cball a e) = int (DIM('n))"
  4209 proof -
  4210   have "(\<lambda>x. a + x) ` (cball 0 e) \<subseteq> cball a e"
  4211     unfolding cball_def dist_norm by auto
  4212   then have "aff_dim (cball (0 :: 'n::euclidean_space) e) \<le> aff_dim (cball a e)"
  4213     using aff_dim_translation_eq[of a "cball 0 e"]
  4214           aff_dim_subset[of "op + a ` cball 0 e" "cball a e"]
  4215     by auto
  4216   moreover have "aff_dim (cball (0 :: 'n::euclidean_space) e) = int (DIM('n))"
  4217     using hull_inc[of "(0 :: 'n::euclidean_space)" "cball 0 e"]
  4218       centre_in_cball[of "(0 :: 'n::euclidean_space)"] assms
  4219     by (simp add: dim_cball[of e] aff_dim_zero[of "cball 0 e"])
  4220   ultimately show ?thesis
  4221     using aff_dim_le_DIM[of "cball a e"] by auto
  4222 qed
  4223 
  4224 lemma aff_dim_open:
  4225   fixes S :: "'n::euclidean_space set"
  4226   assumes "open S"
  4227     and "S \<noteq> {}"
  4228   shows "aff_dim S = int (DIM('n))"
  4229 proof -
  4230   obtain x where "x \<in> S"
  4231     using assms by auto
  4232   then obtain e where e: "e > 0" "cball x e \<subseteq> S"
  4233     using open_contains_cball[of S] assms by auto
  4234   then have "aff_dim (cball x e) \<le> aff_dim S"
  4235     using aff_dim_subset by auto
  4236   with e show ?thesis
  4237     using aff_dim_cball[of e x] aff_dim_le_DIM[of S] by auto
  4238 qed
  4239 
  4240 lemma low_dim_interior:
  4241   fixes S :: "'n::euclidean_space set"
  4242   assumes "\<not> aff_dim S = int (DIM('n))"
  4243   shows "interior S = {}"
  4244 proof -
  4245   have "aff_dim(interior S) \<le> aff_dim S"
  4246     using interior_subset aff_dim_subset[of "interior S" S] by auto
  4247   then show ?thesis
  4248     using aff_dim_open[of "interior S"] aff_dim_le_DIM[of S] assms by auto
  4249 qed
  4250 
  4251 corollary empty_interior_lowdim:
  4252   fixes S :: "'n::euclidean_space set"
  4253   shows "dim S < DIM ('n) \<Longrightarrow> interior S = {}"
  4254 by (metis low_dim_interior affine_hull_UNIV dim_affine_hull less_not_refl dim_UNIV)
  4255 
  4256 corollary aff_dim_nonempty_interior:
  4257   fixes S :: "'a::euclidean_space set"
  4258   shows "interior S \<noteq> {} \<Longrightarrow> aff_dim S = DIM('a)"
  4259 by (metis low_dim_interior)
  4260 
  4261 
  4262 subsection \<open>Caratheodory's theorem.\<close>
  4263 
  4264 lemma convex_hull_caratheodory_aff_dim:
  4265   fixes p :: "('a::euclidean_space) set"
  4266   shows "convex hull p =
  4267     {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  4268       (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  4269   unfolding convex_hull_explicit set_eq_iff mem_Collect_eq
  4270 proof (intro allI iffI)
  4271   fix y
  4272   let ?P = "\<lambda>n. \<exists>s u. finite s \<and> card s = n \<and> s \<subseteq> p \<and> (\<forall>x\<in>s. 0 \<le> u x) \<and>
  4273     sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  4274   assume "\<exists>s u. finite s \<and> s \<subseteq> p \<and> (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  4275   then obtain N where "?P N" by auto
  4276   then have "\<exists>n\<le>N. (\<forall>k<n. \<not> ?P k) \<and> ?P n"
  4277     apply (rule_tac ex_least_nat_le)
  4278     apply auto
  4279     done
  4280   then obtain n where "?P n" and smallest: "\<forall>k<n. \<not> ?P k"
  4281     by blast
  4282   then obtain s u where obt: "finite s" "card s = n" "s\<subseteq>p" "\<forall>x\<in>s. 0 \<le> u x"
  4283     "sum u s = 1"  "(\<Sum>v\<in>s. u v *\<^sub>R v) = y" by auto
  4284 
  4285   have "card s \<le> aff_dim p + 1"
  4286   proof (rule ccontr, simp only: not_le)
  4287     assume "aff_dim p + 1 < card s"
  4288     then have "affine_dependent s"
  4289       using affine_dependent_biggerset[OF obt(1)] independent_card_le_aff_dim not_less obt(3)
  4290       by blast
  4291     then obtain w v where wv: "sum w s = 0" "v\<in>s" "w v \<noteq> 0" "(\<Sum>v\<in>s. w v *\<^sub>R v) = 0"
  4292       using affine_dependent_explicit_finite[OF obt(1)] by auto
  4293     define i where "i = (\<lambda>v. (u v) / (- w v)) ` {v\<in>s. w v < 0}"
  4294     define t where "t = Min i"
  4295     have "\<exists>x\<in>s. w x < 0"
  4296     proof (rule ccontr, simp add: not_less)
  4297       assume as:"\<forall>x\<in>s. 0 \<le> w x"
  4298       then have "sum w (s - {v}) \<ge> 0"
  4299         apply (rule_tac sum_nonneg)
  4300         apply auto
  4301         done
  4302       then have "sum w s > 0"
  4303         unfolding sum.remove[OF obt(1) \<open>v\<in>s\<close>]
  4304         using as[THEN bspec[where x=v]]  \<open>v\<in>s\<close>  \<open>w v \<noteq> 0\<close> by auto
  4305       then show False using wv(1) by auto
  4306     qed
  4307     then have "i \<noteq> {}" unfolding i_def by auto
  4308     then have "t \<ge> 0"
  4309       using Min_ge_iff[of i 0 ] and obt(1)
  4310       unfolding t_def i_def
  4311       using obt(4)[unfolded le_less]
  4312       by (auto simp: divide_le_0_iff)
  4313     have t: "\<forall>v\<in>s. u v + t * w v \<ge> 0"
  4314     proof
  4315       fix v
  4316       assume "v \<in> s"
  4317       then have v: "0 \<le> u v"
  4318         using obt(4)[THEN bspec[where x=v]] by auto
  4319       show "0 \<le> u v + t * w v"
  4320       proof (cases "w v < 0")
  4321         case False
  4322         thus ?thesis using v \<open>t\<ge>0\<close> by auto
  4323       next
  4324         case True
  4325         then have "t \<le> u v / (- w v)"
  4326           using \<open>v\<in>s\<close> unfolding t_def i_def
  4327           apply (rule_tac Min_le)
  4328           using obt(1) apply auto
  4329           done
  4330         then show ?thesis
  4331           unfolding real_0_le_add_iff
  4332           using pos_le_divide_eq[OF True[unfolded neg_0_less_iff_less[symmetric]]]
  4333           by auto
  4334       qed
  4335     qed
  4336     obtain a where "a \<in> s" and "t = (\<lambda>v. (u v) / (- w v)) a" and "w a < 0"
  4337       using Min_in[OF _ \<open>i\<noteq>{}\<close>] and obt(1) unfolding i_def t_def by auto
  4338     then have a: "a \<in> s" "u a + t * w a = 0" by auto
  4339     have *: "\<And>f. sum f (s - {a}) = sum f s - ((f a)::'b::ab_group_add)"
  4340       unfolding sum.remove[OF obt(1) \<open>a\<in>s\<close>] by auto
  4341     have "(\<Sum>v\<in>s. u v + t * w v) = 1"
  4342       unfolding sum.distrib wv(1) sum_distrib_left[symmetric] obt(5) by auto
  4343     moreover have "(\<Sum>v\<in>s. u v *\<^sub>R v + (t * w v) *\<^sub>R v) - (u a *\<^sub>R a + (t * w a) *\<^sub>R a) = y"
  4344       unfolding sum.distrib obt(6) scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] wv(4)
  4345       using a(2) [THEN eq_neg_iff_add_eq_0 [THEN iffD2]] by simp
  4346     ultimately have "?P (n - 1)"
  4347       apply (rule_tac x="(s - {a})" in exI)
  4348       apply (rule_tac x="\<lambda>v. u v + t * w v" in exI)
  4349       using obt(1-3) and t and a
  4350       apply (auto simp add: * scaleR_left_distrib)
  4351       done
  4352     then show False
  4353       using smallest[THEN spec[where x="n - 1"]] by auto
  4354   qed
  4355   then show "\<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  4356       (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  4357     using obt by auto
  4358 qed auto
  4359 
  4360 lemma caratheodory_aff_dim:
  4361   fixes p :: "('a::euclidean_space) set"
  4362   shows "convex hull p = {x. \<exists>s. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and> x \<in> convex hull s}"
  4363         (is "?lhs = ?rhs")
  4364 proof
  4365   show "?lhs \<subseteq> ?rhs"
  4366     apply (subst convex_hull_caratheodory_aff_dim)
  4367     apply clarify
  4368     apply (rule_tac x="s" in exI)
  4369     apply (simp add: hull_subset convex_explicit [THEN iffD1, OF convex_convex_hull])
  4370     done
  4371 next
  4372   show "?rhs \<subseteq> ?lhs"
  4373     using hull_mono by blast
  4374 qed
  4375 
  4376 lemma convex_hull_caratheodory:
  4377   fixes p :: "('a::euclidean_space) set"
  4378   shows "convex hull p =
  4379             {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and>
  4380               (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  4381         (is "?lhs = ?rhs")
  4382 proof (intro set_eqI iffI)
  4383   fix x
  4384   assume "x \<in> ?lhs" then show "x \<in> ?rhs"
  4385     apply (simp only: convex_hull_caratheodory_aff_dim Set.mem_Collect_eq)
  4386     apply (erule ex_forward)+
  4387     using aff_dim_le_DIM [of p]
  4388     apply simp
  4389     done
  4390 next
  4391   fix x
  4392   assume "x \<in> ?rhs" then show "x \<in> ?lhs"
  4393     by (auto simp add: convex_hull_explicit)
  4394 qed
  4395 
  4396 theorem caratheodory:
  4397   "convex hull p =
  4398     {x::'a::euclidean_space. \<exists>s. finite s \<and> s \<subseteq> p \<and>
  4399       card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s}"
  4400 proof safe
  4401   fix x
  4402   assume "x \<in> convex hull p"
  4403   then obtain s u where "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1"
  4404     "\<forall>x\<in>s. 0 \<le> u x" "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  4405     unfolding convex_hull_caratheodory by auto
  4406   then show "\<exists>s. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s"
  4407     apply (rule_tac x=s in exI)
  4408     using hull_subset[of s convex]
  4409     using convex_convex_hull[simplified convex_explicit, of s,
  4410       THEN spec[where x=s], THEN spec[where x=u]]
  4411     apply auto
  4412     done
  4413 next
  4414   fix x s
  4415   assume  "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1" "x \<in> convex hull s"
  4416   then show "x \<in> convex hull p"
  4417     using hull_mono[OF \<open>s\<subseteq>p\<close>] by auto
  4418 qed
  4419 
  4420 
  4421 subsection \<open>Relative interior of a set\<close>
  4422 
  4423 definition "rel_interior S =
  4424   {x. \<exists>T. openin (subtopology euclidean (affine hull S)) T \<and> x \<in> T \<and> T \<subseteq> S}"
  4425 
  4426 lemma rel_interior_mono:
  4427    "\<lbrakk>S \<subseteq> T; affine hull S = affine hull T\<rbrakk>
  4428    \<Longrightarrow> (rel_interior S) \<subseteq> (rel_interior T)"
  4429   by (auto simp: rel_interior_def)
  4430 
  4431 lemma rel_interior_maximal:
  4432    "\<lbrakk>T \<subseteq> S; openin(subtopology euclidean (affine hull S)) T\<rbrakk> \<Longrightarrow> T \<subseteq> (rel_interior S)"
  4433   by (auto simp: rel_interior_def)
  4434 
  4435 lemma rel_interior:
  4436   "rel_interior S = {x \<in> S. \<exists>T. open T \<and> x \<in> T \<and> T \<inter> affine hull S \<subseteq> S}"
  4437   unfolding rel_interior_def[of S] openin_open[of "affine hull S"]
  4438   apply auto
  4439 proof -
  4440   fix x T
  4441   assume *: "x \<in> S" "open T" "x \<in> T" "T \<inter> affine hull S \<subseteq> S"
  4442   then have **: "x \<in> T \<inter> affine hull S"
  4443     using hull_inc by auto
  4444   show "\<exists>Tb. (\<exists>Ta. open Ta \<and> Tb = affine hull S \<inter> Ta) \<and> x \<in> Tb \<and> Tb \<subseteq> S"
  4445     apply (rule_tac x = "T \<inter> (affine hull S)" in exI)
  4446     using * **
  4447     apply auto
  4448     done
  4449 qed
  4450 
  4451 lemma mem_rel_interior: "x \<in> rel_interior S \<longleftrightarrow> (\<exists>T. open T \<and> x \<in> T \<inter> S \<and> T \<inter> affine hull S \<subseteq> S)"
  4452   by (auto simp add: rel_interior)
  4453 
  4454 lemma mem_rel_interior_ball:
  4455   "x \<in> rel_interior S \<longleftrightarrow> x \<in> S \<and> (\<exists>e. e > 0 \<and> ball x e \<inter> affine hull S \<subseteq> S)"
  4456   apply (simp add: rel_interior, safe)
  4457   apply (force simp add: open_contains_ball)
  4458   apply (rule_tac x = "ball x e" in exI)
  4459   apply simp
  4460   done
  4461 
  4462 lemma rel_interior_ball:
  4463   "rel_interior S = {x \<in> S. \<exists>e. e > 0 \<and> ball x e \<inter> affine hull S \<subseteq> S}"
  4464   using mem_rel_interior_ball [of _ S] by auto
  4465 
  4466 lemma mem_rel_interior_cball:
  4467   "x \<in> rel_interior S \<longleftrightarrow> x \<in> S \<and> (\<exists>e. e > 0 \<and> cball x e \<inter> affine hull S \<subseteq> S)"
  4468   apply (simp add: rel_interior, safe)
  4469   apply (force simp add: open_contains_cball)
  4470   apply (rule_tac x = "ball x e" in exI)
  4471   apply (simp add: subset_trans [OF ball_subset_cball])
  4472   apply auto
  4473   done
  4474 
  4475 lemma rel_interior_cball:
  4476   "rel_interior S = {x \<in> S. \<exists>e. e > 0 \<and> cball x e \<inter> affine hull S \<subseteq> S}"
  4477   using mem_rel_interior_cball [of _ S] by auto
  4478 
  4479 lemma rel_interior_empty [simp]: "rel_interior {} = {}"
  4480    by (auto simp add: rel_interior_def)
  4481 
  4482 lemma affine_hull_sing [simp]: "affine hull {a :: 'n::euclidean_space} = {a}"
  4483   by (metis affine_hull_eq affine_sing)
  4484 
  4485 lemma rel_interior_sing [simp]:
  4486     fixes a :: "'n::euclidean_space"  shows "rel_interior {a} = {a}"
  4487   apply (auto simp: rel_interior_ball)
  4488   apply (rule_tac x=1 in exI)
  4489   apply force
  4490   done
  4491 
  4492 lemma subset_rel_interior:
  4493   fixes S T :: "'n::euclidean_space set"
  4494   assumes "S \<subseteq> T"
  4495     and "affine hull S = affine hull T"
  4496   shows "rel_interior S \<subseteq> rel_interior T"
  4497   using assms by (auto simp add: rel_interior_def)
  4498 
  4499 lemma rel_interior_subset: "rel_interior S \<subseteq> S"
  4500   by (auto simp add: rel_interior_def)
  4501 
  4502 lemma rel_interior_subset_closure: "rel_interior S \<subseteq> closure S"
  4503   using rel_interior_subset by (auto simp add: closure_def)
  4504 
  4505 lemma interior_subset_rel_interior: "interior S \<subseteq> rel_interior S"
  4506   by (auto simp add: rel_interior interior_def)
  4507 
  4508 lemma interior_rel_interior:
  4509   fixes S :: "'n::euclidean_space set"
  4510   assumes "aff_dim S = int(DIM('n))"
  4511   shows "rel_interior S = interior S"
  4512 proof -
  4513   have "affine hull S = UNIV"
  4514     using assms affine_hull_UNIV[of S] by auto
  4515   then show ?thesis
  4516     unfolding rel_interior interior_def by auto
  4517 qed
  4518 
  4519 lemma rel_interior_interior:
  4520   fixes S :: "'n::euclidean_space set"
  4521   assumes "affine hull S = UNIV"
  4522   shows "rel_interior S = interior S"
  4523   using assms unfolding rel_interior interior_def by auto
  4524 
  4525 lemma rel_interior_open:
  4526   fixes S :: "'n::euclidean_space set"
  4527   assumes "open S"
  4528   shows "rel_interior S = S"
  4529   by (metis assms interior_eq interior_subset_rel_interior rel_interior_subset set_eq_subset)
  4530 
  4531 lemma interior_ball [simp]: "interior (ball x e) = ball x e"
  4532   by (simp add: interior_open)
  4533 
  4534 lemma interior_rel_interior_gen:
  4535   fixes S :: "'n::euclidean_space set"
  4536   shows "interior S = (if aff_dim S = int(DIM('n)) then rel_interior S else {})"
  4537   by (metis interior_rel_interior low_dim_interior)
  4538 
  4539 lemma rel_interior_nonempty_interior:
  4540   fixes S :: "'n::euclidean_space set"
  4541   shows "interior S \<noteq> {} \<Longrightarrow> rel_interior S = interior S"
  4542 by (metis interior_rel_interior_gen)
  4543 
  4544 lemma affine_hull_nonempty_interior:
  4545   fixes S :: "'n::euclidean_space set"
  4546   shows "interior S \<noteq> {} \<Longrightarrow> affine hull S = UNIV"
  4547 by (metis affine_hull_UNIV interior_rel_interior_gen)
  4548 
  4549 lemma rel_interior_affine_hull [simp]:
  4550   fixes S :: "'n::euclidean_space set"
  4551   shows "rel_interior (affine hull S) = affine hull S"
  4552 proof -
  4553   have *: "rel_interior (affine hull S) \<subseteq> affine hull S"
  4554     using rel_interior_subset by auto
  4555   {
  4556     fix x
  4557     assume x: "x \<in> affine hull S"
  4558     define e :: real where "e = 1"
  4559     then have "e > 0" "ball x e \<inter> affine hull (affine hull S) \<subseteq> affine hull S"
  4560       using hull_hull[of _ S] by auto
  4561     then have "x \<in> rel_interior (affine hull S)"
  4562       using x rel_interior_ball[of "affine hull S"] by auto
  4563   }
  4564   then show ?thesis using * by auto
  4565 qed
  4566 
  4567 lemma rel_interior_UNIV [simp]: "rel_interior (UNIV :: ('n::euclidean_space) set) = UNIV"
  4568   by (metis open_UNIV rel_interior_open)
  4569 
  4570 lemma rel_interior_convex_shrink:
  4571   fixes S :: "'a::euclidean_space set"
  4572   assumes "convex S"
  4573     and "c \<in> rel_interior S"
  4574     and "x \<in> S"
  4575     and "0 < e"
  4576     and "e \<le> 1"
  4577   shows "x - e *\<^sub>R (x - c) \<in> rel_interior S"
  4578 proof -
  4579   obtain d where "d > 0" and d: "ball c d \<inter> affine hull S \<subseteq> S"
  4580     using assms(2) unfolding  mem_rel_interior_ball by auto
  4581   {
  4582     fix y
  4583     assume as: "dist (x - e *\<^sub>R (x - c)) y < e * d" "y \<in> affine hull S"
  4584     have *: "y = (1 - (1 - e)) *\<^sub>R ((1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x) + (1 - e) *\<^sub>R x"
  4585       using \<open>e > 0\<close> by (auto simp add: scaleR_left_diff_distrib scaleR_right_diff_distrib)
  4586     have "x \<in> affine hull S"
  4587       using assms hull_subset[of S] by auto
  4588     moreover have "1 / e + - ((1 - e) / e) = 1"
  4589       using \<open>e > 0\<close> left_diff_distrib[of "1" "(1-e)" "1/e"] by auto
  4590     ultimately have **: "(1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x \<in> affine hull S"
  4591       using as affine_affine_hull[of S] mem_affine[of "affine hull S" y x "(1 / e)" "-((1 - e) / e)"]
  4592       by (simp add: algebra_simps)
  4593     have "dist c ((1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x) = \<bar>1/e\<bar> * norm (e *\<^sub>R c - y + (1 - e) *\<^sub>R x)"
  4594       unfolding dist_norm norm_scaleR[symmetric]
  4595       apply (rule arg_cong[where f=norm])
  4596       using \<open>e > 0\<close>
  4597       apply (auto simp add: euclidean_eq_iff[where 'a='a] field_simps inner_simps)
  4598       done
  4599     also have "\<dots> = \<bar>1/e\<bar> * norm (x - e *\<^sub>R (x - c) - y)"
  4600       by (auto intro!:arg_cong[where f=norm] simp add: algebra_simps)
  4601     also have "\<dots> < d"
  4602       using as[unfolded dist_norm] and \<open>e > 0\<close>
  4603       by (auto simp add:pos_divide_less_eq[OF \<open>e > 0\<close>] mult.commute)
  4604     finally have "y \<in> S"
  4605       apply (subst *)
  4606       apply (rule assms(1)[unfolded convex_alt,rule_format])
  4607       apply (rule d[unfolded subset_eq,rule_format])
  4608       unfolding mem_ball
  4609       using assms(3-5) **
  4610       apply auto
  4611       done
  4612   }
  4613   then have "ball (x - e *\<^sub>R (x - c)) (e*d) \<inter> affine hull S \<subseteq> S"
  4614     by auto
  4615   moreover have "e * d > 0"
  4616     using \<open>e > 0\<close> \<open>d > 0\<close> by simp
  4617   moreover have c: "c \<in> S"
  4618     using assms rel_interior_subset by auto
  4619   moreover from c have "x - e *\<^sub>R (x - c) \<in> S"
  4620     using convexD_alt[of S x c e]
  4621     apply (simp add: algebra_simps)
  4622     using assms
  4623     apply auto
  4624     done
  4625   ultimately show ?thesis
  4626     using mem_rel_interior_ball[of "x - e *\<^sub>R (x - c)" S] \<open>e > 0\<close> by auto
  4627 qed
  4628 
  4629 lemma interior_real_semiline:
  4630   fixes a :: real
  4631   shows "interior {a..} = {a<..}"
  4632 proof -
  4633   {
  4634     fix y
  4635     assume "a < y"
  4636     then have "y \<in> interior {a..}"
  4637       apply (simp add: mem_interior)
  4638       apply (rule_tac x="(y-a)" in exI)
  4639       apply (auto simp add: dist_norm)
  4640       done
  4641   }
  4642   moreover
  4643   {
  4644     fix y
  4645     assume "y \<in> interior {a..}"
  4646     then obtain e where e: "e > 0" "cball y e \<subseteq> {a..}"
  4647       using mem_interior_cball[of y "{a..}"] by auto
  4648     moreover from e have "y - e \<in> cball y e"
  4649       by (auto simp add: cball_def dist_norm)
  4650     ultimately have "a \<le> y - e" by blast
  4651     then have "a < y" using e by auto
  4652   }
  4653   ultimately show ?thesis by auto
  4654 qed
  4655 
  4656 lemma continuous_ge_on_Ioo:
  4657   assumes "continuous_on {c..d} g" "\<And>x. x \<in> {c<..<d} \<Longrightarrow> g x \<ge> a" "c < d" "x \<in> {c..d}"
  4658   shows "g (x::real) \<ge> (a::real)"
  4659 proof-
  4660   from assms(3) have "{c..d} = closure {c<..<d}" by (rule closure_greaterThanLessThan[symmetric])
  4661   also from assms(2) have "{c<..<d} \<subseteq> (g -` {a..} \<inter> {c..d})" by auto
  4662   hence "closure {c<..<d} \<subseteq> closure (g -` {a..} \<inter> {c..d})" by (rule closure_mono)
  4663   also from assms(1) have "closed (g -` {a..} \<inter> {c..d})"
  4664     by (auto simp: continuous_on_closed_vimage)
  4665   hence "closure (g -` {a..} \<inter> {c..d}) = g -` {a..} \<inter> {c..d}" by simp
  4666   finally show ?thesis using \<open>x \<in> {c..d}\<close> by auto
  4667 qed
  4668 
  4669 lemma interior_real_semiline':
  4670   fixes a :: real
  4671   shows "interior {..a} = {..<a}"
  4672 proof -
  4673   {
  4674     fix y
  4675     assume "a > y"
  4676     then have "y \<in> interior {..a}"
  4677       apply (simp add: mem_interior)
  4678       apply (rule_tac x="(a-y)" in exI)
  4679       apply (auto simp add: dist_norm)
  4680       done
  4681   }
  4682   moreover
  4683   {
  4684     fix y
  4685     assume "y \<in> interior {..a}"
  4686     then obtain e where e: "e > 0" "cball y e \<subseteq> {..a}"
  4687       using mem_interior_cball[of y "{..a}"] by auto
  4688     moreover from e have "y + e \<in> cball y e"
  4689       by (auto simp add: cball_def dist_norm)
  4690     ultimately have "a \<ge> y + e" by auto
  4691     then have "a > y" using e by auto
  4692   }
  4693   ultimately show ?thesis by auto
  4694 qed
  4695 
  4696 lemma interior_atLeastAtMost_real [simp]: "interior {a..b} = {a<..<b :: real}"
  4697 proof-
  4698   have "{a..b} = {a..} \<inter> {..b}" by auto
  4699   also have "interior ... = {a<..} \<inter> {..<b}"
  4700     by (simp add: interior_real_semiline interior_real_semiline')
  4701   also have "... = {a<..<b}" by auto
  4702   finally show ?thesis .
  4703 qed
  4704 
  4705 lemma interior_atLeastLessThan [simp]:
  4706   fixes a::real shows "interior {a..<b} = {a<..<b}"
  4707   by (metis atLeastLessThan_def greaterThanLessThan_def interior_atLeastAtMost_real interior_Int interior_interior interior_real_semiline)
  4708 
  4709 lemma interior_lessThanAtMost [simp]:
  4710   fixes a::real shows "interior {a<..b} = {a<..<b}"
  4711   by (metis atLeastAtMost_def greaterThanAtMost_def interior_atLeastAtMost_real interior_Int
  4712             interior_interior interior_real_semiline)
  4713 
  4714 lemma interior_greaterThanLessThan_real [simp]: "interior {a<..<b} = {a<..<b :: real}"
  4715   by (metis interior_atLeastAtMost_real interior_interior)
  4716 
  4717 lemma frontier_real_Iic [simp]:
  4718   fixes a :: real
  4719   shows "frontier {..a} = {a}"
  4720   unfolding frontier_def by (auto simp add: interior_real_semiline')
  4721 
  4722 lemma rel_interior_real_box [simp]:
  4723   fixes a b :: real
  4724   assumes "a < b"
  4725   shows "rel_interior {a .. b} = {a <..< b}"
  4726 proof -
  4727   have "box a b \<noteq> {}"
  4728     using assms
  4729     unfolding set_eq_iff
  4730     by (auto intro!: exI[of _ "(a + b) / 2"] simp: box_def)
  4731   then show ?thesis
  4732     using interior_rel_interior_gen[of "cbox a b", symmetric]
  4733     by (simp split: if_split_asm del: box_real add: box_real[symmetric] interior_cbox)
  4734 qed
  4735 
  4736 lemma rel_interior_real_semiline [simp]:
  4737   fixes a :: real
  4738   shows "rel_interior {a..} = {a<..}"
  4739 proof -
  4740   have *: "{a<..} \<noteq> {}"
  4741     unfolding set_eq_iff by (auto intro!: exI[of _ "a + 1"])
  4742   then show ?thesis using interior_real_semiline interior_rel_interior_gen[of "{a..}"]
  4743     by (auto split: if_split_asm)
  4744 qed
  4745 
  4746 subsubsection \<open>Relative open sets\<close>
  4747 
  4748 definition "rel_open S \<longleftrightarrow> rel_interior S = S"
  4749 
  4750 lemma rel_open: "rel_open S \<longleftrightarrow> openin (subtopology euclidean (affine hull S)) S"
  4751   unfolding rel_open_def rel_interior_def
  4752   apply auto
  4753   using openin_subopen[of "subtopology euclidean (affine hull S)" S]
  4754   apply auto
  4755   done
  4756 
  4757 lemma openin_rel_interior: "openin (subtopology euclidean (affine hull S)) (rel_interior S)"
  4758   apply (simp add: rel_interior_def)
  4759   apply (subst openin_subopen)
  4760   apply blast
  4761   done
  4762 
  4763 lemma openin_set_rel_interior:
  4764    "openin (subtopology euclidean S) (rel_interior S)"
  4765 by (rule openin_subset_trans [OF openin_rel_interior rel_interior_subset hull_subset])
  4766 
  4767 lemma affine_rel_open:
  4768   fixes S :: "'n::euclidean_space set"
  4769   assumes "affine S"
  4770   shows "rel_open S"
  4771   unfolding rel_open_def
  4772   using assms rel_interior_affine_hull[of S] affine_hull_eq[of S]
  4773   by metis
  4774 
  4775 lemma affine_closed:
  4776   fixes S :: "'n::euclidean_space set"
  4777   assumes "affine S"
  4778   shows "closed S"
  4779 proof -
  4780   {
  4781     assume "S \<noteq> {}"
  4782     then obtain L where L: "subspace L" "affine_parallel S L"
  4783       using assms affine_parallel_subspace[of S] by auto
  4784     then obtain a where a: "S = (op + a ` L)"
  4785       using affine_parallel_def[of L S] affine_parallel_commut by auto
  4786     from L have "closed L" using closed_subspace by auto
  4787     then have "closed S"
  4788       using closed_translation a by auto
  4789   }
  4790   then show ?thesis by auto
  4791 qed
  4792 
  4793 lemma closure_affine_hull:
  4794   fixes S :: "'n::euclidean_space set"
  4795   shows "closure S \<subseteq> affine hull S"
  4796   by (intro closure_minimal hull_subset affine_closed affine_affine_hull)
  4797 
  4798 lemma closure_same_affine_hull [simp]:
  4799   fixes S :: "'n::euclidean_space set"
  4800   shows "affine hull (closure S) = affine hull S"
  4801 proof -
  4802   have "affine hull (closure S) \<subseteq> affine hull S"
  4803     using hull_mono[of "closure S" "affine hull S" "affine"]
  4804       closure_affine_hull[of S] hull_hull[of "affine" S]
  4805     by auto
  4806   moreover have "affine hull (closure S) \<supseteq> affine hull S"
  4807     using hull_mono[of "S" "closure S" "affine"] closure_subset by auto
  4808   ultimately show ?thesis by auto
  4809 qed
  4810 
  4811 lemma closure_aff_dim [simp]:
  4812   fixes S :: "'n::euclidean_space set"
  4813   shows "aff_dim (closure S) = aff_dim S"
  4814 proof -
  4815   have "aff_dim S \<le> aff_dim (closure S)"
  4816     using aff_dim_subset closure_subset by auto
  4817   moreover have "aff_dim (closure S) \<le> aff_dim (affine hull S)"
  4818     using aff_dim_subset closure_affine_hull by blast
  4819   moreover have "aff_dim (affine hull S) = aff_dim S"
  4820     using aff_dim_affine_hull by auto
  4821   ultimately show ?thesis by auto
  4822 qed
  4823 
  4824 lemma rel_interior_closure_convex_shrink:
  4825   fixes S :: "_::euclidean_space set"
  4826   assumes "convex S"
  4827     and "c \<in> rel_interior S"
  4828     and "x \<in> closure S"
  4829     and "e > 0"
  4830     and "e \<le> 1"
  4831   shows "x - e *\<^sub>R (x - c) \<in> rel_interior S"
  4832 proof -
  4833   obtain d where "d > 0" and d: "ball c d \<inter> affine hull S \<subseteq> S"
  4834     using assms(2) unfolding mem_rel_interior_ball by auto
  4835   have "\<exists>y \<in> S. norm (y - x) * (1 - e) < e * d"
  4836   proof (cases "x \<in> S")
  4837     case True
  4838     then show ?thesis using \<open>e > 0\<close> \<open>d > 0\<close>
  4839       apply (rule_tac bexI[where x=x])
  4840       apply (auto)
  4841       done
  4842   next
  4843     case False
  4844     then have x: "x islimpt S"
  4845       using assms(3)[unfolded closure_def] by auto
  4846     show ?thesis
  4847     proof (cases "e = 1")
  4848       case True
  4849       obtain y where "y \<in> S" "y \<noteq> x" "dist y x < 1"
  4850         using x[unfolded islimpt_approachable,THEN spec[where x=1]] by auto
  4851       then show ?thesis
  4852         apply (rule_tac x=y in bexI)
  4853         unfolding True
  4854         using \<open>d > 0\<close>
  4855         apply auto
  4856         done
  4857     next
  4858       case False
  4859       then have "0 < e * d / (1 - e)" and *: "1 - e > 0"
  4860         using \<open>e \<le> 1\<close> \<open>e > 0\<close> \<open>d > 0\<close> by (auto)
  4861       then obtain y where "y \<in> S" "y \<noteq> x" "dist y x < e * d / (1 - e)"
  4862         using x[unfolded islimpt_approachable,THEN spec[where x="e*d / (1 - e)"]] by auto
  4863       then show ?thesis
  4864         apply (rule_tac x=y in bexI)
  4865         unfolding dist_norm
  4866         using pos_less_divide_eq[OF *]
  4867         apply auto
  4868         done
  4869     qed
  4870   qed
  4871   then obtain y where "y \<in> S" and y: "norm (y - x) * (1 - e) < e * d"
  4872     by auto
  4873   define z where "z = c + ((1 - e) / e) *\<^sub>R (x - y)"
  4874   have *: "x - e *\<^sub>R (x - c) = y - e *\<^sub>R (y - z)"
  4875     unfolding z_def using \<open>e > 0\<close>
  4876     by (auto simp add: scaleR_right_diff_distrib scaleR_right_distrib scaleR_left_diff_distrib)
  4877   have zball: "z \<in> ball c d"
  4878     using mem_ball z_def dist_norm[of c]
  4879     using y and assms(4,5)
  4880     by (auto simp add:field_simps norm_minus_commute)
  4881   have "x \<in> affine hull S"
  4882     using closure_affine_hull assms by auto
  4883   moreover have "y \<in> affine hull S"
  4884     using \<open>y \<in> S\<close> hull_subset[of S] by auto
  4885   moreover have "c \<in> affine hull S"
  4886     using assms rel_interior_subset hull_subset[of S] by auto
  4887   ultimately have "z \<in> affine hull S"
  4888     using z_def affine_affine_hull[of S]
  4889       mem_affine_3_minus [of "affine hull S" c x y "(1 - e) / e"]
  4890       assms
  4891     by (auto simp add: field_simps)
  4892   then have "z \<in> S" using d zball by auto
  4893   obtain d1 where "d1 > 0" and d1: "ball z d1 \<le> ball c d"
  4894     using zball open_ball[of c d] openE[of "ball c d" z] by auto
  4895   then have "ball z d1 \<inter> affine hull S \<subseteq> ball c d \<inter> affine hull S"
  4896     by auto
  4897   then have "ball z d1 \<inter> affine hull S \<subseteq> S"
  4898     using d by auto
  4899   then have "z \<in> rel_interior S"
  4900     using mem_rel_interior_ball using \<open>d1 > 0\<close> \<open>z \<in> S\<close> by auto
  4901   then have "y - e *\<^sub>R (y - z) \<in> rel_interior S"
  4902     using rel_interior_convex_shrink[of S z y e] assms \<open>y \<in> S\<close> by auto
  4903   then show ?thesis using * by auto
  4904 qed
  4905 
  4906 lemma rel_interior_eq:
  4907    "rel_interior s = s \<longleftrightarrow> openin(subtopology euclidean (affine hull s)) s"
  4908 using rel_open rel_open_def by blast
  4909 
  4910 lemma rel_interior_openin:
  4911    "openin(subtopology euclidean (affine hull s)) s \<Longrightarrow> rel_interior s = s"
  4912 by (simp add: rel_interior_eq)
  4913 
  4914 lemma rel_interior_affine:
  4915   fixes S :: "'n::euclidean_space set"
  4916   shows  "affine S \<Longrightarrow> rel_interior S = S"
  4917 using affine_rel_open rel_open_def by auto
  4918 
  4919 lemma rel_interior_eq_closure:
  4920   fixes S :: "'n::euclidean_space set"
  4921   shows "rel_interior S = closure S \<longleftrightarrow> affine S"
  4922 proof (cases "S = {}")
  4923   case True
  4924  then show ?thesis
  4925     by auto
  4926 next
  4927   case False show ?thesis
  4928   proof
  4929     assume eq: "rel_interior S = closure S"
  4930     have "S = {} \<or> S = affine hull S"
  4931       apply (rule connected_clopen [THEN iffD1, rule_format])
  4932        apply (simp add: affine_imp_convex convex_connected)
  4933       apply (rule conjI)
  4934        apply (metis eq closure_subset openin_rel_interior rel_interior_subset subset_antisym)
  4935       apply (metis closed_subset closure_subset_eq eq hull_subset rel_interior_subset)
  4936       done
  4937     with False have "affine hull S = S"
  4938       by auto
  4939     then show "affine S"
  4940       by (metis affine_hull_eq)
  4941   next
  4942     assume "affine S"
  4943     then show "rel_interior S = closure S"
  4944       by (simp add: rel_interior_affine affine_closed)
  4945   qed
  4946 qed
  4947 
  4948 
  4949 subsubsection\<open>Relative interior preserves under linear transformations\<close>
  4950 
  4951 lemma rel_interior_translation_aux:
  4952   fixes a :: "'n::euclidean_space"
  4953   shows "((\<lambda>x. a + x) ` rel_interior S) \<subseteq> rel_interior ((\<lambda>x. a + x) ` S)"
  4954 proof -
  4955   {
  4956     fix x
  4957     assume x: "x \<in> rel_interior S"
  4958     then obtain T where "open T" "x \<in> T \<inter> S" "T \<inter> affine hull S \<subseteq> S"
  4959       using mem_rel_interior[of x S] by auto
  4960     then have "open ((\<lambda>x. a + x) ` T)"
  4961       and "a + x \<in> ((\<lambda>x. a + x) ` T) \<inter> ((\<lambda>x. a + x) ` S)"
  4962       and "((\<lambda>x. a + x) ` T) \<inter> affine hull ((\<lambda>x. a + x) ` S) \<subseteq> (\<lambda>x. a + x) ` S"
  4963       using affine_hull_translation[of a S] open_translation[of T a] x by auto
  4964     then have "a + x \<in> rel_interior ((\<lambda>x. a + x) ` S)"
  4965       using mem_rel_interior[of "a+x" "((\<lambda>x. a + x) ` S)"] by auto
  4966   }
  4967   then show ?thesis by auto
  4968 qed
  4969 
  4970 lemma rel_interior_translation:
  4971   fixes a :: "'n::euclidean_space"
  4972   shows "rel_interior ((\<lambda>x. a + x) ` S) = (\<lambda>x. a + x) ` rel_interior S"
  4973 proof -
  4974   have "(\<lambda>x. (-a) + x) ` rel_interior ((\<lambda>x. a + x) ` S) \<subseteq> rel_interior S"
  4975     using rel_interior_translation_aux[of "-a" "(\<lambda>x. a + x) ` S"]
  4976       translation_assoc[of "-a" "a"]
  4977     by auto
  4978   then have "((\<lambda>x. a + x) ` rel_interior S) \<supseteq> rel_interior ((\<lambda>x. a + x) ` S)"
  4979     using translation_inverse_subset[of a "rel_interior (op + a ` S)" "rel_interior S"]
  4980     by auto
  4981   then show ?thesis
  4982     using rel_interior_translation_aux[of a S] by auto
  4983 qed
  4984 
  4985 
  4986 lemma affine_hull_linear_image:
  4987   assumes "bounded_linear f"
  4988   shows "f ` (affine hull s) = affine hull f ` s"
  4989   apply rule
  4990   unfolding subset_eq ball_simps
  4991   apply (rule_tac[!] hull_induct, rule hull_inc)
  4992   prefer 3
  4993   apply (erule imageE)
  4994   apply (rule_tac x=xa in image_eqI)
  4995   apply assumption
  4996   apply (rule hull_subset[unfolded subset_eq, rule_format])
  4997   apply assumption
  4998 proof -
  4999   interpret f: bounded_linear f by fact
  5000   show "affine {x. f x \<in> affine hull f ` s}"
  5001     unfolding affine_def
  5002     by (auto simp add: f.scaleR f.add affine_affine_hull[unfolded affine_def, rule_format])
  5003   show "affine {x. x \<in> f ` (affine hull s)}"
  5004     using affine_affine_hull[unfolded affine_def, of s]
  5005     unfolding affine_def by (auto simp add: f.scaleR [symmetric] f.add [symmetric])
  5006 qed auto
  5007 
  5008 
  5009 lemma rel_interior_injective_on_span_linear_image:
  5010   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
  5011     and S :: "'m::euclidean_space set"
  5012   assumes "bounded_linear f"
  5013     and "inj_on f (span S)"
  5014   shows "rel_interior (f ` S) = f ` (rel_interior S)"
  5015 proof -
  5016   {
  5017     fix z
  5018     assume z: "z \<in> rel_interior (f ` S)"
  5019     then have "z \<in> f ` S"
  5020       using rel_interior_subset[of "f ` S"] by auto
  5021     then obtain x where x: "x \<in> S" "f x = z" by auto
  5022     obtain e2 where e2: "e2 > 0" "cball z e2 \<inter> affine hull (f ` S) \<subseteq> (f ` S)"
  5023       using z rel_interior_cball[of "f ` S"] by auto
  5024     obtain K where K: "K > 0" "\<And>x. norm (f x) \<le> norm x * K"
  5025      using assms Real_Vector_Spaces.bounded_linear.pos_bounded[of f] by auto
  5026     define e1 where "e1 = 1 / K"
  5027     then have e1: "e1 > 0" "\<And>x. e1 * norm (f x) \<le> norm x"
  5028       using K pos_le_divide_eq[of e1] by auto
  5029     define e where "e = e1 * e2"
  5030     then have "e > 0" using e1 e2 by auto
  5031     {
  5032       fix y
  5033       assume y: "y \<in> cball x e \<inter> affine hull S"
  5034       then have h1: "f y \<in> affine hull (f ` S)"
  5035         using affine_hull_linear_image[of f S] assms by auto
  5036       from y have "norm (x-y) \<le> e1 * e2"
  5037         using cball_def[of x e] dist_norm[of x y] e_def by auto
  5038       moreover have "f x - f y = f (x - y)"
  5039         using assms linear_diff[of f x y] linear_conv_bounded_linear[of f] by auto
  5040       moreover have "e1 * norm (f (x-y)) \<le> norm (x - y)"
  5041         using e1 by auto
  5042       ultimately have "e1 * norm ((f x)-(f y)) \<le> e1 * e2"
  5043         by auto
  5044       then have "f y \<in> cball z e2"
  5045         using cball_def[of "f x" e2] dist_norm[of "f x" "f y"] e1 x by auto
  5046       then have "f y \<in> f ` S"
  5047         using y e2 h1 by auto
  5048       then have "y \<in> S"
  5049         using assms y hull_subset[of S] affine_hull_subset_span
  5050           inj_on_image_mem_iff [OF \<open>inj_on f (span S)\<close>]
  5051         by (metis Int_iff span_inc subsetCE)
  5052     }
  5053     then have "z \<in> f ` (rel_interior S)"
  5054       using mem_rel_interior_cball[of x S] \<open>e > 0\<close> x by auto
  5055   }
  5056   moreover
  5057   {
  5058     fix x
  5059     assume x: "x \<in> rel_interior S"
  5060     then obtain e2 where e2: "e2 > 0" "cball x e2 \<inter> affine hull S \<subseteq> S"
  5061       using rel_interior_cball[of S] by auto
  5062     have "x \<in> S" using x rel_interior_subset by auto
  5063     then have *: "f x \<in> f ` S" by auto
  5064     have "\<forall>x\<in>span S. f x = 0 \<longrightarrow> x = 0"
  5065       using assms subspace_span linear_conv_bounded_linear[of f]
  5066         linear_injective_on_subspace_0[of f "span S"]
  5067       by auto
  5068     then obtain e1 where e1: "e1 > 0" "\<forall>x \<in> span S. e1 * norm x \<le> norm (f x)"
  5069       using assms injective_imp_isometric[of "span S" f]
  5070         subspace_span[of S] closed_subspace[of "span S"]
  5071       by auto
  5072     define e where "e = e1 * e2"
  5073     hence "e > 0" using e1 e2 by auto
  5074     {
  5075       fix y
  5076       assume y: "y \<in> cball (f x) e \<inter> affine hull (f ` S)"
  5077       then have "y \<in> f ` (affine hull S)"
  5078         using affine_hull_linear_image[of f S] assms by auto
  5079       then obtain xy where xy: "xy \<in> affine hull S" "f xy = y" by auto
  5080       with y have "norm (f x - f xy) \<le> e1 * e2"
  5081         using cball_def[of "f x" e] dist_norm[of "f x" y] e_def by auto
  5082       moreover have "f x - f xy = f (x - xy)"
  5083         using assms linear_diff[of f x xy] linear_conv_bounded_linear[of f] by auto
  5084       moreover have *: "x - xy \<in> span S"
  5085         using subspace_diff[of "span S" x xy] subspace_span \<open>x \<in> S\<close> xy
  5086           affine_hull_subset_span[of S] span_inc
  5087         by auto
  5088       moreover from * have "e1 * norm (x - xy) \<le> norm (f (x - xy))"
  5089         using e1 by auto
  5090       ultimately have "e1 * norm (x - xy) \<le> e1 * e2"
  5091         by auto
  5092       then have "xy \<in> cball x e2"
  5093         using cball_def[of x e2] dist_norm[of x xy] e1 by auto
  5094       then have "y \<in> f ` S"
  5095         using xy e2 by auto
  5096     }
  5097     then have "f x \<in> rel_interior (f ` S)"
  5098       using mem_rel_interior_cball[of "(f x)" "(f ` S)"] * \<open>e > 0\<close> by auto
  5099   }
  5100   ultimately show ?thesis by auto
  5101 qed
  5102 
  5103 lemma rel_interior_injective_linear_image:
  5104   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
  5105   assumes "bounded_linear f"
  5106     and "inj f"
  5107   shows "rel_interior (f ` S) = f ` (rel_interior S)"
  5108   using assms rel_interior_injective_on_span_linear_image[of f S]
  5109     subset_inj_on[of f "UNIV" "span S"]
  5110   by auto
  5111 
  5112 
  5113 subsection\<open>Some Properties of subset of standard basis\<close>
  5114 
  5115 lemma affine_hull_substd_basis:
  5116   assumes "d \<subseteq> Basis"
  5117   shows "affine hull (insert 0 d) = {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  5118   (is "affine hull (insert 0 ?A) = ?B")
  5119 proof -
  5120   have *: "\<And>A. op + (0::'a) ` A = A" "\<And>A. op + (- (0::'a)) ` A = A"
  5121     by auto
  5122   show ?thesis
  5123     unfolding affine_hull_insert_span_gen span_substd_basis[OF assms,symmetric] * ..
  5124 qed
  5125 
  5126 lemma affine_hull_convex_hull [simp]: "affine hull (convex hull S) = affine hull S"
  5127   by (metis Int_absorb1 Int_absorb2 convex_hull_subset_affine_hull hull_hull hull_mono hull_subset)
  5128 
  5129 
  5130 subsection \<open>Openness and compactness are preserved by convex hull operation.\<close>
  5131 
  5132 lemma open_convex_hull[intro]:
  5133   fixes s :: "'a::real_normed_vector set"
  5134   assumes "open s"
  5135   shows "open (convex hull s)"
  5136   unfolding open_contains_cball convex_hull_explicit
  5137   unfolding mem_Collect_eq ball_simps(8)
  5138 proof (rule, rule)
  5139   fix a
  5140   assume "\<exists>sa u. finite sa \<and> sa \<subseteq> s \<and> (\<forall>x\<in>sa. 0 \<le> u x) \<and> sum u sa = 1 \<and> (\<Sum>v\<in>sa. u v *\<^sub>R v) = a"
  5141   then obtain t u where obt: "finite t" "t\<subseteq>s" "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1" "(\<Sum>v\<in>t. u v *\<^sub>R v) = a"
  5142     by auto
  5143 
  5144   from assms[unfolded open_contains_cball] obtain b
  5145     where b: "\<forall>x\<in>s. 0 < b x \<and> cball x (b x) \<subseteq> s"
  5146     using bchoice[of s "\<lambda>x e. e > 0 \<and> cball x e \<subseteq> s"] by auto
  5147   have "b ` t \<noteq> {}"
  5148     using obt by auto
  5149   define i where "i = b ` t"
  5150 
  5151   show "\<exists>e > 0.
  5152     cball a e \<subseteq> {y. \<exists>sa u. finite sa \<and> sa \<subseteq> s \<and> (\<forall>x\<in>sa. 0 \<le> u x) \<and> sum u sa = 1 \<and> (\<Sum>v\<in>sa. u v *\<^sub>R v) = y}"
  5153     apply (rule_tac x = "Min i" in exI)
  5154     unfolding subset_eq
  5155     apply rule
  5156     defer
  5157     apply rule
  5158     unfolding mem_Collect_eq
  5159   proof -
  5160     show "0 < Min i"
  5161       unfolding i_def and Min_gr_iff[OF finite_imageI[OF obt(1)] \<open>b ` t\<noteq>{}\<close>]
  5162       using b
  5163       apply simp
  5164       apply rule
  5165       apply (erule_tac x=x in ballE)
  5166       using \<open>t\<subseteq>s\<close>
  5167       apply auto
  5168       done
  5169   next
  5170     fix y
  5171     assume "y \<in> cball a (Min i)"
  5172     then have y: "norm (a - y) \<le> Min i"
  5173       unfolding dist_norm[symmetric] by auto
  5174     {
  5175       fix x
  5176       assume "x \<in> t"
  5177       then have "Min i \<le> b x"
  5178         unfolding i_def
  5179         apply (rule_tac Min_le)
  5180         using obt(1)
  5181         apply auto
  5182         done
  5183       then have "x + (y - a) \<in> cball x (b x)"
  5184         using y unfolding mem_cball dist_norm by auto
  5185       moreover from \<open>x\<in>t\<close> have "x \<in> s"
  5186         using obt(2) by auto
  5187       ultimately have "x + (y - a) \<in> s"
  5188         using y and b[THEN bspec[where x=x]] unfolding subset_eq by fast
  5189     }
  5190     moreover
  5191     have *: "inj_on (\<lambda>v. v + (y - a)) t"
  5192       unfolding inj_on_def by auto
  5193     have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a))) = 1"
  5194       unfolding sum.reindex[OF *] o_def using obt(4) by auto
  5195     moreover have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a)) *\<^sub>R v) = y"
  5196       unfolding sum.reindex[OF *] o_def using obt(4,5)
  5197       by (simp add: sum.distrib sum_subtractf scaleR_left.sum[symmetric] scaleR_right_distrib)
  5198     ultimately
  5199     show "\<exists>sa u. finite sa \<and> (\<forall>x\<in>sa. x \<in> s) \<and> (\<forall>x\<in>sa. 0 \<le> u x) \<and> sum u sa = 1 \<and> (\<Sum>v\<in>sa. u v *\<^sub>R v) = y"
  5200       apply (rule_tac x="(\<lambda>v. v + (y - a)) ` t" in exI)
  5201       apply (rule_tac x="\<lambda>v. u (v - (y - a))" in exI)
  5202       using obt(1, 3)
  5203       apply auto
  5204       done
  5205   qed
  5206 qed
  5207 
  5208 lemma compact_convex_combinations:
  5209   fixes s t :: "'a::real_normed_vector set"
  5210   assumes "compact s" "compact t"
  5211   shows "compact { (1 - u) *\<^sub>R x + u *\<^sub>R y | x y u. 0 \<le> u \<and> u \<le> 1 \<and> x \<in> s \<and> y \<in> t}"
  5212 proof -
  5213   let ?X = "{0..1} \<times> s \<times> t"
  5214   let ?h = "(\<lambda>z. (1 - fst z) *\<^sub>R fst (snd z) + fst z *\<^sub>R snd (snd z))"
  5215   have *: "{ (1 - u) *\<^sub>R x + u *\<^sub>R y | x y u. 0 \<le> u \<and> u \<le> 1 \<and> x \<in> s \<and> y \<in> t} = ?h ` ?X"
  5216     apply (rule set_eqI)
  5217     unfolding image_iff mem_Collect_eq
  5218     apply rule
  5219     apply auto
  5220     apply (rule_tac x=u in rev_bexI)
  5221     apply simp
  5222     apply (erule rev_bexI)
  5223     apply (erule rev_bexI)
  5224     apply simp
  5225     apply auto
  5226     done
  5227   have "continuous_on ?X (\<lambda>z. (1 - fst z) *\<^sub>R fst (snd z) + fst z *\<^sub>R snd (snd z))"
  5228     unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  5229   then show ?thesis
  5230     unfolding *
  5231     apply (rule compact_continuous_image)
  5232     apply (intro compact_Times compact_Icc assms)
  5233     done
  5234 qed
  5235 
  5236 lemma finite_imp_compact_convex_hull:
  5237   fixes s :: "'a::real_normed_vector set"
  5238   assumes "finite s"
  5239   shows "compact (convex hull s)"
  5240 proof (cases "s = {}")
  5241   case True
  5242   then show ?thesis by simp
  5243 next
  5244   case False
  5245   with assms show ?thesis
  5246   proof (induct rule: finite_ne_induct)
  5247     case (singleton x)
  5248     show ?case by simp
  5249   next
  5250     case (insert x A)
  5251     let ?f = "\<lambda>(u, y::'a). u *\<^sub>R x + (1 - u) *\<^sub>R y"
  5252     let ?T = "{0..1::real} \<times> (convex hull A)"
  5253     have "continuous_on ?T ?f"
  5254       unfolding split_def continuous_on by (intro ballI tendsto_intros)
  5255     moreover have "compact ?T"
  5256       by (intro compact_Times compact_Icc insert)
  5257     ultimately have "compact (?f ` ?T)"
  5258       by (rule compact_continuous_image)
  5259     also have "?f ` ?T = convex hull (insert x A)"
  5260       unfolding convex_hull_insert [OF \<open>A \<noteq> {}\<close>]
  5261       apply safe
  5262       apply (rule_tac x=a in exI, simp)
  5263       apply (rule_tac x="1 - a" in exI, simp)
  5264       apply fast
  5265       apply (rule_tac x="(u, b)" in image_eqI, simp_all)
  5266       done
  5267     finally show "compact (convex hull (insert x A))" .
  5268   qed
  5269 qed
  5270 
  5271 lemma compact_convex_hull:
  5272   fixes s :: "'a::euclidean_space set"
  5273   assumes "compact s"
  5274   shows "compact (convex hull s)"
  5275 proof (cases "s = {}")
  5276   case True
  5277   then show ?thesis using compact_empty by simp
  5278 next
  5279   case False
  5280   then obtain w where "w \<in> s" by auto
  5281   show ?thesis
  5282     unfolding caratheodory[of s]
  5283   proof (induct ("DIM('a) + 1"))
  5284     case 0
  5285     have *: "{x.\<exists>sa. finite sa \<and> sa \<subseteq> s \<and> card sa \<le> 0 \<and> x \<in> convex hull sa} = {}"
  5286       using compact_empty by auto
  5287     from 0 show ?case unfolding * by simp
  5288   next
  5289     case (Suc n)
  5290     show ?case
  5291     proof (cases "n = 0")
  5292       case True
  5293       have "{x. \<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t} = s"
  5294         unfolding set_eq_iff and mem_Collect_eq
  5295       proof (rule, rule)
  5296         fix x
  5297         assume "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  5298         then obtain t where t: "finite t" "t \<subseteq> s" "card t \<le> Suc n" "x \<in> convex hull t"
  5299           by auto
  5300         show "x \<in> s"
  5301         proof (cases "card t = 0")
  5302           case True
  5303           then show ?thesis
  5304             using t(4) unfolding card_0_eq[OF t(1)] by simp
  5305         next
  5306           case False
  5307           then have "card t = Suc 0" using t(3) \<open>n=0\<close> by auto
  5308           then obtain a where "t = {a}" unfolding card_Suc_eq by auto
  5309           then show ?thesis using t(2,4) by simp
  5310         qed
  5311       next
  5312         fix x assume "x\<in>s"
  5313         then show "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  5314           apply (rule_tac x="{x}" in exI)
  5315           unfolding convex_hull_singleton
  5316           apply auto
  5317           done
  5318       qed
  5319       then show ?thesis using assms by simp
  5320     next
  5321       case False
  5322       have "{x. \<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t} =
  5323         {(1 - u) *\<^sub>R x + u *\<^sub>R y | x y u.
  5324           0 \<le> u \<and> u \<le> 1 \<and> x \<in> s \<and> y \<in> {x. \<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> n \<and> x \<in> convex hull t}}"
  5325         unfolding set_eq_iff and mem_Collect_eq
  5326       proof (rule, rule)
  5327         fix x
  5328         assume "\<exists>u v c. x = (1 - c) *\<^sub>R u + c *\<^sub>R v \<and>
  5329           0 \<le> c \<and> c \<le> 1 \<and> u \<in> s \<and> (\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> n \<and> v \<in> convex hull t)"
  5330         then obtain u v c t where obt: "x = (1 - c) *\<^sub>R u + c *\<^sub>R v"
  5331           "0 \<le> c \<and> c \<le> 1" "u \<in> s" "finite t" "t \<subseteq> s" "card t \<le> n"  "v \<in> convex hull t"
  5332           by auto
  5333         moreover have "(1 - c) *\<^sub>R u + c *\<^sub>R v \<in> convex hull insert u t"
  5334           apply (rule convexD_alt)
  5335           using obt(2) and convex_convex_hull and hull_subset[of "insert u t" convex]
  5336           using obt(7) and hull_mono[of t "insert u t"]
  5337           apply auto
  5338           done
  5339         ultimately show "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  5340           apply (rule_tac x="insert u t" in exI)
  5341           apply (auto simp add: card_insert_if)
  5342           done
  5343       next
  5344         fix x
  5345         assume "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  5346         then obtain t where t: "finite t" "t \<subseteq> s" "card t \<le> Suc n" "x \<in> convex hull t"
  5347           by auto
  5348         show "\<exists>u v c. x = (1 - c) *\<^sub>R u + c *\<^sub>R v \<and>
  5349           0 \<le> c \<and> c \<le> 1 \<and> u \<in> s \<and> (\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> n \<and> v \<in> convex hull t)"
  5350         proof (cases "card t = Suc n")
  5351           case False
  5352           then have "card t \<le> n" using t(3) by auto
  5353           then show ?thesis
  5354             apply (rule_tac x=w in exI, rule_tac x=x in exI, rule_tac x=1 in exI)
  5355             using \<open>w\<in>s\<close> and t
  5356             apply (auto intro!: exI[where x=t])
  5357             done
  5358         next
  5359           case True
  5360           then obtain a u where au: "t = insert a u" "a\<notin>u"
  5361             apply (drule_tac card_eq_SucD)
  5362             apply auto
  5363             done
  5364           show ?thesis
  5365           proof (cases "u = {}")
  5366             case True
  5367             then have "x = a" using t(4)[unfolded au] by auto
  5368             show ?thesis unfolding \<open>x = a\<close>
  5369               apply (rule_tac x=a in exI)
  5370               apply (rule_tac x=a in exI)
  5371               apply (rule_tac x=1 in exI)
  5372               using t and \<open>n \<noteq> 0\<close>
  5373               unfolding au
  5374               apply (auto intro!: exI[where x="{a}"])
  5375               done
  5376           next
  5377             case False
  5378             obtain ux vx b where obt: "ux\<ge>0" "vx\<ge>0" "ux + vx = 1"
  5379               "b \<in> convex hull u" "x = ux *\<^sub>R a + vx *\<^sub>R b"
  5380               using t(4)[unfolded au convex_hull_insert[OF False]]
  5381               by auto
  5382             have *: "1 - vx = ux" using obt(3) by auto
  5383             show ?thesis
  5384               apply (rule_tac x=a in exI)
  5385               apply (rule_tac x=b in exI)
  5386               apply (rule_tac x=vx in exI)
  5387               using obt and t(1-3)
  5388               unfolding au and * using card_insert_disjoint[OF _ au(2)]
  5389               apply (auto intro!: exI[where x=u])
  5390               done
  5391           qed
  5392         qed
  5393       qed
  5394       then show ?thesis
  5395         using compact_convex_combinations[OF assms Suc] by simp
  5396     qed
  5397   qed
  5398 qed
  5399 
  5400 
  5401 subsection \<open>Extremal points of a simplex are some vertices.\<close>
  5402 
  5403 lemma dist_increases_online:
  5404   fixes a b d :: "'a::real_inner"
  5405   assumes "d \<noteq> 0"
  5406   shows "dist a (b + d) > dist a b \<or> dist a (b - d) > dist a b"
  5407 proof (cases "inner a d - inner b d > 0")
  5408   case True
  5409   then have "0 < inner d d + (inner a d * 2 - inner b d * 2)"
  5410     apply (rule_tac add_pos_pos)
  5411     using assms
  5412     apply auto
  5413     done
  5414   then show ?thesis
  5415     apply (rule_tac disjI2)
  5416     unfolding dist_norm and norm_eq_sqrt_inner and real_sqrt_less_iff
  5417     apply  (simp add: algebra_simps inner_commute)
  5418     done
  5419 next
  5420   case False
  5421   then have "0 < inner d d + (inner b d * 2 - inner a d * 2)"
  5422     apply (rule_tac add_pos_nonneg)
  5423     using assms
  5424     apply auto
  5425     done
  5426   then show ?thesis
  5427     apply (rule_tac disjI1)
  5428     unfolding dist_norm and norm_eq_sqrt_inner and real_sqrt_less_iff
  5429     apply (simp add: algebra_simps inner_commute)
  5430     done
  5431 qed
  5432 
  5433 lemma norm_increases_online:
  5434   fixes d :: "'a::real_inner"
  5435   shows "d \<noteq> 0 \<Longrightarrow> norm (a + d) > norm a \<or> norm(a - d) > norm a"
  5436   using dist_increases_online[of d a 0] unfolding dist_norm by auto
  5437 
  5438 lemma simplex_furthest_lt:
  5439   fixes s :: "'a::real_inner set"
  5440   assumes "finite s"
  5441   shows "\<forall>x \<in> convex hull s.  x \<notin> s \<longrightarrow> (\<exists>y \<in> convex hull s. norm (x - a) < norm(y - a))"
  5442   using assms
  5443 proof induct
  5444   fix x s
  5445   assume as: "finite s" "x\<notin>s" "\<forall>x\<in>convex hull s. x \<notin> s \<longrightarrow> (\<exists>y\<in>convex hull s. norm (x - a) < norm (y - a))"
  5446   show "\<forall>xa\<in>convex hull insert x s. xa \<notin> insert x s \<longrightarrow>
  5447     (\<exists>y\<in>convex hull insert x s. norm (xa - a) < norm (y - a))"
  5448   proof (rule, rule, cases "s = {}")
  5449     case False
  5450     fix y
  5451     assume y: "y \<in> convex hull insert x s" "y \<notin> insert x s"
  5452     obtain u v b where obt: "u\<ge>0" "v\<ge>0" "u + v = 1" "b \<in> convex hull s" "y = u *\<^sub>R x + v *\<^sub>R b"
  5453       using y(1)[unfolded convex_hull_insert[OF False]] by auto
  5454     show "\<exists>z\<in>convex hull insert x s. norm (y - a) < norm (z - a)"
  5455     proof (cases "y \<in> convex hull s")
  5456       case True
  5457       then obtain z where "z \<in> convex hull s" "norm (y - a) < norm (z - a)"
  5458         using as(3)[THEN bspec[where x=y]] and y(2) by auto
  5459       then show ?thesis
  5460         apply (rule_tac x=z in bexI)
  5461         unfolding convex_hull_insert[OF False]
  5462         apply auto
  5463         done
  5464     next
  5465       case False
  5466       show ?thesis
  5467         using obt(3)
  5468       proof (cases "u = 0", case_tac[!] "v = 0")
  5469         assume "u = 0" "v \<noteq> 0"
  5470         then have "y = b" using obt by auto
  5471         then show ?thesis using False and obt(4) by auto
  5472       next
  5473         assume "u \<noteq> 0" "v = 0"
  5474         then have "y = x" using obt by auto
  5475         then show ?thesis using y(2) by auto
  5476       next
  5477         assume "u \<noteq> 0" "v \<noteq> 0"
  5478         then obtain w where w: "w>0" "w<u" "w<v"
  5479           using real_lbound_gt_zero[of u v] and obt(1,2) by auto
  5480         have "x \<noteq> b"
  5481         proof
  5482           assume "x = b"
  5483           then have "y = b" unfolding obt(5)
  5484             using obt(3) by (auto simp add: scaleR_left_distrib[symmetric])
  5485           then show False using obt(4) and False by simp
  5486         qed
  5487         then have *: "w *\<^sub>R (x - b) \<noteq> 0" using w(1) by auto
  5488         show ?thesis
  5489           using dist_increases_online[OF *, of a y]
  5490         proof (elim disjE)
  5491           assume "dist a y < dist a (y + w *\<^sub>R (x - b))"
  5492           then have "norm (y - a) < norm ((u + w) *\<^sub>R x + (v - w) *\<^sub>R b - a)"
  5493             unfolding dist_commute[of a]
  5494             unfolding dist_norm obt(5)
  5495             by (simp add: algebra_simps)
  5496           moreover have "(u + w) *\<^sub>R x + (v - w) *\<^sub>R b \<in> convex hull insert x s"
  5497             unfolding convex_hull_insert[OF \<open>s\<noteq>{}\<close>] and mem_Collect_eq
  5498             apply (rule_tac x="u + w" in exI)
  5499             apply rule
  5500             defer
  5501             apply (rule_tac x="v - w" in exI)
  5502             using \<open>u \<ge> 0\<close> and w and obt(3,4)
  5503             apply auto
  5504             done
  5505           ultimately show ?thesis by auto
  5506         next
  5507           assume "dist a y < dist a (y - w *\<^sub>R (x - b))"
  5508           then have "norm (y - a) < norm ((u - w) *\<^sub>R x + (v + w) *\<^sub>R b - a)"
  5509             unfolding dist_commute[of a]
  5510             unfolding dist_norm obt(5)
  5511             by (simp add: algebra_simps)
  5512           moreover have "(u - w) *\<^sub>R x + (v + w) *\<^sub>R b \<in> convex hull insert x s"
  5513             unfolding convex_hull_insert[OF \<open>s\<noteq>{}\<close>] and mem_Collect_eq
  5514             apply (rule_tac x="u - w" in exI)
  5515             apply rule
  5516             defer
  5517             apply (rule_tac x="v + w" in exI)
  5518             using \<open>u \<ge> 0\<close> and w and obt(3,4)
  5519             apply auto
  5520             done
  5521           ultimately show ?thesis by auto
  5522         qed
  5523       qed auto
  5524     qed
  5525   qed auto
  5526 qed (auto simp add: assms)
  5527 
  5528 lemma simplex_furthest_le:
  5529   fixes s :: "'a::real_inner set"
  5530   assumes "finite s"
  5531     and "s \<noteq> {}"
  5532   shows "\<exists>y\<in>s. \<forall>x\<in> convex hull s. norm (x - a) \<le> norm (y - a)"
  5533 proof -
  5534   have "convex hull s \<noteq> {}"
  5535     using hull_subset[of s convex] and assms(2) by auto
  5536   then obtain x where x: "x \<in> convex hull s" "\<forall>y\<in>convex hull s. norm (y - a) \<le> norm (x - a)"
  5537     using distance_attains_sup[OF finite_imp_compact_convex_hull[OF assms(1)], of a]
  5538     unfolding dist_commute[of a]
  5539     unfolding dist_norm
  5540     by auto
  5541   show ?thesis
  5542   proof (cases "x \<in> s")
  5543     case False
  5544     then obtain y where "y \<in> convex hull s" "norm (x - a) < norm (y - a)"
  5545       using simplex_furthest_lt[OF assms(1), THEN bspec[where x=x]] and x(1)
  5546       by auto
  5547     then show ?thesis
  5548       using x(2)[THEN bspec[where x=y]] by auto
  5549   next
  5550     case True
  5551     with x show ?thesis by auto
  5552   qed
  5553 qed
  5554 
  5555 lemma simplex_furthest_le_exists:
  5556   fixes s :: "('a::real_inner) set"
  5557   shows "finite s \<Longrightarrow> \<forall>x\<in>(convex hull s). \<exists>y\<in>s. norm (x - a) \<le> norm (y - a)"
  5558   using simplex_furthest_le[of s] by (cases "s = {}") auto
  5559 
  5560 lemma simplex_extremal_le:
  5561   fixes s :: "'a::real_inner set"
  5562   assumes "finite s"
  5563     and "s \<noteq> {}"
  5564   shows "\<exists>u\<in>s. \<exists>v\<in>s. \<forall>x\<in>convex hull s. \<forall>y \<in> convex hull s. norm (x - y) \<le> norm (u - v)"
  5565 proof -
  5566   have "convex hull s \<noteq> {}"
  5567     using hull_subset[of s convex] and assms(2) by auto
  5568   then obtain u v where obt: "u \<in> convex hull s" "v \<in> convex hull s"
  5569     "\<forall>x\<in>convex hull s. \<forall>y\<in>convex hull s. norm (x - y) \<le> norm (u - v)"
  5570     using compact_sup_maxdistance[OF finite_imp_compact_convex_hull[OF assms(1)]]
  5571     by (auto simp: dist_norm)
  5572   then show ?thesis
  5573   proof (cases "u\<notin>s \<or> v\<notin>s", elim disjE)
  5574     assume "u \<notin> s"
  5575     then obtain y where "y \<in> convex hull s" "norm (u - v) < norm (y - v)"
  5576       using simplex_furthest_lt[OF assms(1), THEN bspec[where x=u]] and obt(1)
  5577       by auto
  5578     then show ?thesis
  5579       using obt(3)[THEN bspec[where x=y], THEN bspec[where x=v]] and obt(2)
  5580       by auto
  5581   next
  5582     assume "v \<notin> s"
  5583     then obtain y where "y \<in> convex hull s" "norm (v - u) < norm (y - u)"
  5584       using simplex_furthest_lt[OF assms(1), THEN bspec[where x=v]] and obt(2)
  5585       by auto
  5586     then show ?thesis
  5587       using obt(3)[THEN bspec[where x=u], THEN bspec[where x=y]] and obt(1)
  5588       by (auto simp add: norm_minus_commute)
  5589   qed auto
  5590 qed
  5591 
  5592 lemma simplex_extremal_le_exists:
  5593   fixes s :: "'a::real_inner set"
  5594   shows "finite s \<Longrightarrow> x \<in> convex hull s \<Longrightarrow> y \<in> convex hull s \<Longrightarrow>
  5595     \<exists>u\<in>s. \<exists>v\<in>s. norm (x - y) \<le> norm (u - v)"
  5596   using convex_hull_empty simplex_extremal_le[of s]
  5597   by(cases "s = {}") auto
  5598 
  5599 
  5600 subsection \<open>Closest point of a convex set is unique, with a continuous projection.\<close>
  5601 
  5602 definition closest_point :: "'a::{real_inner,heine_borel} set \<Rightarrow> 'a \<Rightarrow> 'a"
  5603   where "closest_point s a = (SOME x. x \<in> s \<and> (\<forall>y\<in>s. dist a x \<le> dist a y))"
  5604 
  5605 lemma closest_point_exists:
  5606   assumes "closed s"
  5607     and "s \<noteq> {}"
  5608   shows "closest_point s a \<in> s"
  5609     and "\<forall>y\<in>s. dist a (closest_point s a) \<le> dist a y"
  5610   unfolding closest_point_def
  5611   apply(rule_tac[!] someI2_ex)
  5612   apply (auto intro: distance_attains_inf[OF assms(1,2), of a])
  5613   done
  5614 
  5615 lemma closest_point_in_set: "closed s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> closest_point s a \<in> s"
  5616   by (meson closest_point_exists)
  5617 
  5618 lemma closest_point_le: "closed s \<Longrightarrow> x \<in> s \<Longrightarrow> dist a (closest_point s a) \<le> dist a x"
  5619   using closest_point_exists[of s] by auto
  5620 
  5621 lemma closest_point_self:
  5622   assumes "x \<in> s"
  5623   shows "closest_point s x = x"
  5624   unfolding closest_point_def
  5625   apply (rule some1_equality, rule ex1I[of _ x])
  5626   using assms
  5627   apply auto
  5628   done
  5629 
  5630 lemma closest_point_refl: "closed s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> closest_point s x = x \<longleftrightarrow> x \<in> s"
  5631   using closest_point_in_set[of s x] closest_point_self[of x s]
  5632   by auto
  5633 
  5634 lemma closer_points_lemma:
  5635   assumes "inner y z > 0"
  5636   shows "\<exists>u>0. \<forall>v>0. v \<le> u \<longrightarrow> norm(v *\<^sub>R z - y) < norm y"
  5637 proof -
  5638   have z: "inner z z > 0"
  5639     unfolding inner_gt_zero_iff using assms by auto
  5640   then show ?thesis
  5641     using assms
  5642     apply (rule_tac x = "inner y z / inner z z" in exI)
  5643     apply rule
  5644     defer
  5645   proof rule+
  5646     fix v
  5647     assume "0 < v" and "v \<le> inner y z / inner z z"
  5648     then show "norm (v *\<^sub>R z - y) < norm y"
  5649       unfolding norm_lt using z and assms
  5650       by (simp add: field_simps inner_diff inner_commute mult_strict_left_mono[OF _ \<open>0<v\<close>])
  5651   qed auto
  5652 qed
  5653 
  5654 lemma closer_point_lemma:
  5655   assumes "inner (y - x) (z - x) > 0"
  5656   shows "\<exists>u>0. u \<le> 1 \<and> dist (x + u *\<^sub>R (z - x)) y < dist x y"
  5657 proof -
  5658   obtain u where "u > 0"
  5659     and u: "\<forall>v>0. v \<le> u \<longrightarrow> norm (v *\<^sub>R (z - x) - (y - x)) < norm (y - x)"
  5660     using closer_points_lemma[OF assms] by auto
  5661   show ?thesis
  5662     apply (rule_tac x="min u 1" in exI)
  5663     using u[THEN spec[where x="min u 1"]] and \<open>u > 0\<close>
  5664     unfolding dist_norm by (auto simp add: norm_minus_commute field_simps)
  5665 qed
  5666 
  5667 lemma any_closest_point_dot:
  5668   assumes "convex s" "closed s" "x \<in> s" "y \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  5669   shows "inner (a - x) (y - x) \<le> 0"
  5670 proof (rule ccontr)
  5671   assume "\<not> ?thesis"
  5672   then obtain u where u: "u>0" "u\<le>1" "dist (x + u *\<^sub>R (y - x)) a < dist x a"
  5673     using closer_point_lemma[of a x y] by auto
  5674   let ?z = "(1 - u) *\<^sub>R x + u *\<^sub>R y"
  5675   have "?z \<in> s"
  5676     using convexD_alt[OF assms(1,3,4), of u] using u by auto
  5677   then show False
  5678     using assms(5)[THEN bspec[where x="?z"]] and u(3)
  5679     by (auto simp add: dist_commute algebra_simps)
  5680 qed
  5681 
  5682 lemma any_closest_point_unique:
  5683   fixes x :: "'a::real_inner"
  5684   assumes "convex s" "closed s" "x \<in> s" "y \<in> s"
  5685     "\<forall>z\<in>s. dist a x \<le> dist a z" "\<forall>z\<in>s. dist a y \<le> dist a z"
  5686   shows "x = y"
  5687   using any_closest_point_dot[OF assms(1-4,5)] and any_closest_point_dot[OF assms(1-2,4,3,6)]
  5688   unfolding norm_pths(1) and norm_le_square
  5689   by (auto simp add: algebra_simps)
  5690 
  5691 lemma closest_point_unique:
  5692   assumes "convex s" "closed s" "x \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  5693   shows "x = closest_point s a"
  5694   using any_closest_point_unique[OF assms(1-3) _ assms(4), of "closest_point s a"]
  5695   using closest_point_exists[OF assms(2)] and assms(3) by auto
  5696 
  5697 lemma closest_point_dot:
  5698   assumes "convex s" "closed s" "x \<in> s"
  5699   shows "inner (a - closest_point s a) (x - closest_point s a) \<le> 0"
  5700   apply (rule any_closest_point_dot[OF assms(1,2) _ assms(3)])
  5701   using closest_point_exists[OF assms(2)] and assms(3)
  5702   apply auto
  5703   done
  5704 
  5705 lemma closest_point_lt:
  5706   assumes "convex s" "closed s" "x \<in> s" "x \<noteq> closest_point s a"
  5707   shows "dist a (closest_point s a) < dist a x"
  5708   apply (rule ccontr)
  5709   apply (rule_tac notE[OF assms(4)])
  5710   apply (rule closest_point_unique[OF assms(1-3), of a])
  5711   using closest_point_le[OF assms(2), of _ a]
  5712   apply fastforce
  5713   done
  5714 
  5715 lemma closest_point_lipschitz:
  5716   assumes "convex s"
  5717     and "closed s" "s \<noteq> {}"
  5718   shows "dist (closest_point s x) (closest_point s y) \<le> dist x y"
  5719 proof -
  5720   have "inner (x - closest_point s x) (closest_point s y - closest_point s x) \<le> 0"
  5721     and "inner (y - closest_point s y) (closest_point s x - closest_point s y) \<le> 0"
  5722     apply (rule_tac[!] any_closest_point_dot[OF assms(1-2)])
  5723     using closest_point_exists[OF assms(2-3)]
  5724     apply auto
  5725     done
  5726   then show ?thesis unfolding dist_norm and norm_le
  5727     using inner_ge_zero[of "(x - closest_point s x) - (y - closest_point s y)"]
  5728     by (simp add: inner_add inner_diff inner_commute)
  5729 qed
  5730 
  5731 lemma continuous_at_closest_point:
  5732   assumes "convex s"
  5733     and "closed s"
  5734     and "s \<noteq> {}"
  5735   shows "continuous (at x) (closest_point s)"
  5736   unfolding continuous_at_eps_delta
  5737   using le_less_trans[OF closest_point_lipschitz[OF assms]] by auto
  5738 
  5739 lemma continuous_on_closest_point:
  5740   assumes "convex s"
  5741     and "closed s"
  5742     and "s \<noteq> {}"
  5743   shows "continuous_on t (closest_point s)"
  5744   by (metis continuous_at_imp_continuous_on continuous_at_closest_point[OF assms])
  5745 
  5746 proposition closest_point_in_rel_interior:
  5747   assumes "closed S" "S \<noteq> {}" and x: "x \<in> affine hull S"
  5748     shows "closest_point S x \<in> rel_interior S \<longleftrightarrow> x \<in> rel_interior S"
  5749 proof (cases "x \<in> S")
  5750   case True
  5751   then show ?thesis
  5752     by (simp add: closest_point_self)
  5753 next
  5754   case False
  5755   then have "False" if asm: "closest_point S x \<in> rel_interior S"
  5756   proof -
  5757     obtain e where "e > 0" and clox: "closest_point S x \<in> S"
  5758                and e: "cball (closest_point S x) e \<inter> affine hull S \<subseteq> S"
  5759       using asm mem_rel_interior_cball by blast
  5760     then have clo_notx: "closest_point S x \<noteq> x"
  5761       using \<open>x \<notin> S\<close> by auto
  5762     define y where "y \<equiv> closest_point S x -
  5763                         (min 1 (e / norm(closest_point S x - x))) *\<^sub>R (closest_point S x - x)"
  5764     have "x - y = (1 - min 1 (e / norm (closest_point S x - x))) *\<^sub>R (x - closest_point S x)"
  5765       by (simp add: y_def algebra_simps)
  5766     then have "norm (x - y) = abs ((1 - min 1 (e / norm (closest_point S x - x)))) * norm(x - closest_point S x)"
  5767       by simp
  5768     also have "... < norm(x - closest_point S x)"
  5769       using clo_notx \<open>e > 0\<close>
  5770       by (auto simp: mult_less_cancel_right2 divide_simps)
  5771     finally have no_less: "norm (x - y) < norm (x - closest_point S x)" .
  5772     have "y \<in> affine hull S"
  5773       unfolding y_def
  5774       by (meson affine_affine_hull clox hull_subset mem_affine_3_minus2 subsetD x)
  5775     moreover have "dist (closest_point S x) y \<le> e"
  5776       using \<open>e > 0\<close> by (auto simp: y_def min_mult_distrib_right)
  5777     ultimately have "y \<in> S"
  5778       using subsetD [OF e] by simp
  5779     then have "dist x (closest_point S x) \<le> dist x y"
  5780       by (simp add: closest_point_le \<open>closed S\<close>)
  5781     with no_less show False
  5782       by (simp add: dist_norm)
  5783   qed
  5784   moreover have "x \<notin> rel_interior S"
  5785     using rel_interior_subset False by blast
  5786   ultimately show ?thesis by blast
  5787 qed
  5788 
  5789 
  5790 subsubsection \<open>Various point-to-set separating/supporting hyperplane theorems.\<close>
  5791 
  5792 lemma supporting_hyperplane_closed_point:
  5793   fixes z :: "'a::{real_inner,heine_borel}"
  5794   assumes "convex s"
  5795     and "closed s"
  5796     and "s \<noteq> {}"
  5797     and "z \<notin> s"
  5798   shows "\<exists>a b. \<exists>y\<in>s. inner a z < b \<and> inner a y = b \<and> (\<forall>x\<in>s. inner a x \<ge> b)"
  5799 proof -
  5800   obtain y where "y \<in> s" and y: "\<forall>x\<in>s. dist z y \<le> dist z x"
  5801     by (metis distance_attains_inf[OF assms(2-3)])
  5802   show ?thesis
  5803     apply (rule_tac x="y - z" in exI)
  5804     apply (rule_tac x="inner (y - z) y" in exI)
  5805     apply (rule_tac x=y in bexI)
  5806     apply rule
  5807     defer
  5808     apply rule
  5809     defer
  5810     apply rule
  5811     apply (rule ccontr)
  5812     using \<open>y \<in> s\<close>
  5813   proof -
  5814     show "inner (y - z) z < inner (y - z) y"
  5815       apply (subst diff_gt_0_iff_gt [symmetric])
  5816       unfolding inner_diff_right[symmetric] and inner_gt_zero_iff
  5817       using \<open>y\<in>s\<close> \<open>z\<notin>s\<close>
  5818       apply auto
  5819       done
  5820   next
  5821     fix x
  5822     assume "x \<in> s"
  5823     have *: "\<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> dist z y \<le> dist z ((1 - u) *\<^sub>R y + u *\<^sub>R x)"
  5824       using assms(1)[unfolded convex_alt] and y and \<open>x\<in>s\<close> and \<open>y\<in>s\<close> by auto
  5825     assume "\<not> inner (y - z) y \<le> inner (y - z) x"
  5826     then obtain v where "v > 0" "v \<le> 1" "dist (y + v *\<^sub>R (x - y)) z < dist y z"
  5827       using closer_point_lemma[of z y x] by (auto simp add: inner_diff)
  5828     then show False
  5829       using *[THEN spec[where x=v]] by (auto simp add: dist_commute algebra_simps)
  5830   qed auto
  5831 qed
  5832 
  5833 lemma separating_hyperplane_closed_point:
  5834   fixes z :: "'a::{real_inner,heine_borel}"
  5835   assumes "convex s"
  5836     and "closed s"
  5837     and "z \<notin> s"
  5838   shows "\<exists>a b. inner a z < b \<and> (\<forall>x\<in>s. inner a x > b)"
  5839 proof (cases "s = {}")
  5840   case True
  5841   then show ?thesis
  5842     apply (rule_tac x="-z" in exI)
  5843     apply (rule_tac x=1 in exI)
  5844     using less_le_trans[OF _ inner_ge_zero[of z]]
  5845     apply auto
  5846     done
  5847 next
  5848   case False
  5849   obtain y where "y \<in> s" and y: "\<forall>x\<in>s. dist z y \<le> dist z x"
  5850     by (metis distance_attains_inf[OF assms(2) False])
  5851   show ?thesis
  5852     apply (rule_tac x="y - z" in exI)
  5853     apply (rule_tac x="inner (y - z) z + (norm (y - z))\<^sup>2 / 2" in exI)
  5854     apply rule
  5855     defer
  5856     apply rule
  5857   proof -
  5858     fix x
  5859     assume "x \<in> s"
  5860     have "\<not> 0 < inner (z - y) (x - y)"
  5861       apply (rule notI)
  5862       apply (drule closer_point_lemma)
  5863     proof -
  5864       assume "\<exists>u>0. u \<le> 1 \<and> dist (y + u *\<^sub>R (x - y)) z < dist y z"
  5865       then obtain u where "u > 0" "u \<le> 1" "dist (y + u *\<^sub>R (x - y)) z < dist y z"
  5866         by auto
  5867       then show False using y[THEN bspec[where x="y + u *\<^sub>R (x - y)"]]
  5868         using assms(1)[unfolded convex_alt, THEN bspec[where x=y]]
  5869         using \<open>x\<in>s\<close> \<open>y\<in>s\<close> by (auto simp add: dist_commute algebra_simps)
  5870     qed
  5871     moreover have "0 < (norm (y - z))\<^sup>2"
  5872       using \<open>y\<in>s\<close> \<open>z\<notin>s\<close> by auto
  5873     then have "0 < inner (y - z) (y - z)"
  5874       unfolding power2_norm_eq_inner by simp
  5875     ultimately show "inner (y - z) z + (norm (y - z))\<^sup>2 / 2 < inner (y - z) x"
  5876       unfolding power2_norm_eq_inner and not_less
  5877       by (auto simp add: field_simps inner_commute inner_diff)
  5878   qed (insert \<open>y\<in>s\<close> \<open>z\<notin>s\<close>, auto)
  5879 qed
  5880 
  5881 lemma separating_hyperplane_closed_0:
  5882   assumes "convex (s::('a::euclidean_space) set)"
  5883     and "closed s"
  5884     and "0 \<notin> s"
  5885   shows "\<exists>a b. a \<noteq> 0 \<and> 0 < b \<and> (\<forall>x\<in>s. inner a x > b)"
  5886 proof (cases "s = {}")
  5887   case True
  5888   have "norm ((SOME i. i\<in>Basis)::'a) = 1" "(SOME i. i\<in>Basis) \<noteq> (0::'a)"
  5889     defer
  5890     apply (subst norm_le_zero_iff[symmetric])
  5891     apply (auto simp: SOME_Basis)
  5892     done
  5893   then show ?thesis
  5894     apply (rule_tac x="SOME i. i\<in>Basis" in exI)
  5895     apply (rule_tac x=1 in exI)
  5896     using True using DIM_positive[where 'a='a]
  5897     apply auto
  5898     done
  5899 next
  5900   case False
  5901   then show ?thesis
  5902     using False using separating_hyperplane_closed_point[OF assms]
  5903     apply (elim exE)
  5904     unfolding inner_zero_right
  5905     apply (rule_tac x=a in exI)
  5906     apply (rule_tac x=b in exI)
  5907     apply auto
  5908     done
  5909 qed
  5910 
  5911 
  5912 subsubsection \<open>Now set-to-set for closed/compact sets\<close>
  5913 
  5914 lemma separating_hyperplane_closed_compact:
  5915   fixes S :: "'a::euclidean_space set"
  5916   assumes "convex S"
  5917     and "closed S"
  5918     and "convex T"
  5919     and "compact T"
  5920     and "T \<noteq> {}"
  5921     and "S \<inter> T = {}"
  5922   shows "\<exists>a b. (\<forall>x\<in>S. inner a x < b) \<and> (\<forall>x\<in>T. inner a x > b)"
  5923 proof (cases "S = {}")
  5924   case True
  5925   obtain b where b: "b > 0" "\<forall>x\<in>T. norm x \<le> b"
  5926     using compact_imp_bounded[OF assms(4)] unfolding bounded_pos by auto
  5927   obtain z :: 'a where z: "norm z = b + 1"
  5928     using vector_choose_size[of "b + 1"] and b(1) by auto
  5929   then have "z \<notin> T" using b(2)[THEN bspec[where x=z]] by auto
  5930   then obtain a b where ab: "inner a z < b" "\<forall>x\<in>T. b < inner a x"
  5931     using separating_hyperplane_closed_point[OF assms(3) compact_imp_closed[OF assms(4)], of z]
  5932     by auto
  5933   then show ?thesis
  5934     using True by auto
  5935 next
  5936   case False
  5937   then obtain y where "y \<in> S" by auto
  5938   obtain a b where "0 < b" "\<forall>x \<in> (\<Union>x\<in> S. \<Union>y \<in> T. {x - y}). b < inner a x"
  5939     using separating_hyperplane_closed_point[OF convex_differences[OF assms(1,3)], of 0]
  5940     using closed_compact_differences[OF assms(2,4)]
  5941     using assms(6) by auto 
  5942   then have ab: "\<forall>x\<in>S. \<forall>y\<in>T. b + inner a y < inner a x"
  5943     apply -
  5944     apply rule
  5945     apply rule
  5946     apply (erule_tac x="x - y" in ballE)
  5947     apply (auto simp add: inner_diff)
  5948     done
  5949   define k where "k = (SUP x:T. a \<bullet> x)"
  5950   show ?thesis
  5951     apply (rule_tac x="-a" in exI)
  5952     apply (rule_tac x="-(k + b / 2)" in exI)
  5953     apply (intro conjI ballI)
  5954     unfolding inner_minus_left and neg_less_iff_less
  5955   proof -
  5956     fix x assume "x \<in> T"
  5957     then have "inner a x - b / 2 < k"
  5958       unfolding k_def
  5959     proof (subst less_cSUP_iff)
  5960       show "T \<noteq> {}" by fact
  5961       show "bdd_above (op \<bullet> a ` T)"
  5962         using ab[rule_format, of y] \<open>y \<in> S\<close>
  5963         by (intro bdd_aboveI2[where M="inner a y - b"]) (auto simp: field_simps intro: less_imp_le)
  5964     qed (auto intro!: bexI[of _ x] \<open>0<b\<close>)
  5965     then show "inner a x < k + b / 2"
  5966       by auto
  5967   next
  5968     fix x
  5969     assume "x \<in> S"
  5970     then have "k \<le> inner a x - b"
  5971       unfolding k_def
  5972       apply (rule_tac cSUP_least)
  5973       using assms(5)
  5974       using ab[THEN bspec[where x=x]]
  5975       apply auto
  5976       done
  5977     then show "k + b / 2 < inner a x"
  5978       using \<open>0 < b\<close> by auto
  5979   qed
  5980 qed
  5981 
  5982 lemma separating_hyperplane_compact_closed:
  5983   fixes S :: "'a::euclidean_space set"
  5984   assumes "convex S"
  5985     and "compact S"
  5986     and "S \<noteq> {}"
  5987     and "convex T"
  5988     and "closed T"
  5989     and "S \<inter> T = {}"
  5990   shows "\<exists>a b. (\<forall>x\<in>S. inner a x < b) \<and> (\<forall>x\<in>T. inner a x > b)"
  5991 proof -
  5992   obtain a b where "(\<forall>x\<in>T. inner a x < b) \<and> (\<forall>x\<in>S. b < inner a x)"
  5993     using separating_hyperplane_closed_compact[OF assms(4-5,1-2,3)] and assms(6)
  5994     by auto
  5995   then show ?thesis
  5996     apply (rule_tac x="-a" in exI)
  5997     apply (rule_tac x="-b" in exI)
  5998     apply auto
  5999     done
  6000 qed
  6001 
  6002 
  6003 subsubsection \<open>General case without assuming closure and getting non-strict separation\<close>
  6004 
  6005 lemma separating_hyperplane_set_0:
  6006   assumes "convex s" "(0::'a::euclidean_space) \<notin> s"
  6007   shows "\<exists>a. a \<noteq> 0 \<and> (\<forall>x\<in>s. 0 \<le> inner a x)"
  6008 proof -
  6009   let ?k = "\<lambda>c. {x::'a. 0 \<le> inner c x}"
  6010   have *: "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}" if as: "f \<subseteq> ?k ` s" "finite f" for f
  6011   proof -
  6012     obtain c where c: "f = ?k ` c" "c \<subseteq> s" "finite c"
  6013       using finite_subset_image[OF as(2,1)] by auto
  6014     then obtain a b where ab: "a \<noteq> 0" "0 < b" "\<forall>x\<in>convex hull c. b < inner a x"
  6015       using separating_hyperplane_closed_0[OF convex_convex_hull, of c]
  6016       using finite_imp_compact_convex_hull[OF c(3), THEN compact_imp_closed] and assms(2)
  6017       using subset_hull[of convex, OF assms(1), symmetric, of c]
  6018       by force
  6019     then have "\<exists>x. norm x = 1 \<and> (\<forall>y\<in>c. 0 \<le> inner y x)"
  6020       apply (rule_tac x = "inverse(norm a) *\<^sub>R a" in exI)
  6021       using hull_subset[of c convex]
  6022       unfolding subset_eq and inner_scaleR
  6023       by (auto simp add: inner_commute del: ballE elim!: ballE)
  6024     then show "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}"
  6025       unfolding c(1) frontier_cball sphere_def dist_norm by auto
  6026   qed
  6027   have "frontier (cball 0 1) \<inter> (\<Inter>(?k ` s)) \<noteq> {}"
  6028     apply (rule compact_imp_fip)
  6029     apply (rule compact_frontier[OF compact_cball])
  6030     using * closed_halfspace_ge
  6031     by auto
  6032   then obtain x where "norm x = 1" "\<forall>y\<in>s. x\<in>?k y"
  6033     unfolding frontier_cball dist_norm sphere_def by auto
  6034   then show ?thesis
  6035     by (metis inner_commute mem_Collect_eq norm_eq_zero zero_neq_one)
  6036 qed
  6037 
  6038 lemma separating_hyperplane_sets:
  6039   fixes s t :: "'a::euclidean_space set"
  6040   assumes "convex s"
  6041     and "convex t"
  6042     and "s \<noteq> {}"
  6043     and "t \<noteq> {}"
  6044     and "s \<inter> t = {}"
  6045   shows "\<exists>a b. a \<noteq> 0 \<and> (\<forall>x\<in>s. inner a x \<le> b) \<and> (\<forall>x\<in>t. inner a x \<ge> b)"
  6046 proof -
  6047   from separating_hyperplane_set_0[OF convex_differences[OF assms(2,1)]]
  6048   obtain a where "a \<noteq> 0" "\<forall>x\<in>{x - y |x y. x \<in> t \<and> y \<in> s}. 0 \<le> inner a x"
  6049     using assms(3-5) by fastforce
  6050   then have *: "\<And>x y. x \<in> t \<Longrightarrow> y \<in> s \<Longrightarrow> inner a y \<le> inner a x"
  6051     by (force simp add: inner_diff)
  6052   then have bdd: "bdd_above ((op \<bullet> a)`s)"
  6053     using \<open>t \<noteq> {}\<close> by (auto intro: bdd_aboveI2[OF *])
  6054   show ?thesis
  6055     using \<open>a\<noteq>0\<close>
  6056     by (intro exI[of _ a] exI[of _ "SUP x:s. a \<bullet> x"])
  6057        (auto intro!: cSUP_upper bdd cSUP_least \<open>a \<noteq> 0\<close> \<open>s \<noteq> {}\<close> *)
  6058 qed
  6059 
  6060 
  6061 subsection \<open>More convexity generaliti