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