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