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