src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy
changeset 63627 6ddb43c6b711
parent 63626 44ce6b524ff3
child 63631 2edc8da89edc
child 63633 2accfb71e33b
     1.1 --- a/src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy	Fri Aug 05 18:34:57 2016 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,12687 +0,0 @@
     1.4 -(*  Title:      HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy
     1.5 -    Authors: Robert Himmelmann, TU Muenchen; Bogdan Grechuk, University of Edinburgh; LCP
     1.6 -*)
     1.7 -
     1.8 -section \<open>Convex sets, functions and related things\<close>
     1.9 -
    1.10 -theory Convex_Euclidean_Space
    1.11 -imports
    1.12 -  Topology_Euclidean_Space
    1.13 -  "~~/src/HOL/Library/Convex"
    1.14 -  "~~/src/HOL/Library/Set_Algebras"
    1.15 -begin
    1.16 -
    1.17 -(*FIXME move up e.g. to Library/Convex.thy, but requires the "support" primitive*)
    1.18 -lemma convex_supp_setsum:
    1.19 -  assumes "convex S" and 1: "supp_setsum u I = 1"
    1.20 -      and "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> u i \<and> (u i = 0 \<or> f i \<in> S)"
    1.21 -    shows "supp_setsum (\<lambda>i. u i *\<^sub>R f i) I \<in> S"
    1.22 -proof -
    1.23 -  have fin: "finite {i \<in> I. u i \<noteq> 0}"
    1.24 -    using 1 setsum.infinite by (force simp: supp_setsum_def support_on_def)
    1.25 -  then have eq: "supp_setsum (\<lambda>i. u i *\<^sub>R f i) I = setsum (\<lambda>i. u i *\<^sub>R f i) {i \<in> I. u i \<noteq> 0}"
    1.26 -    by (force intro: setsum.mono_neutral_left simp: supp_setsum_def support_on_def)
    1.27 -  show ?thesis
    1.28 -    apply (simp add: eq)
    1.29 -    apply (rule convex_setsum [OF fin \<open>convex S\<close>])
    1.30 -    using 1 assms apply (auto simp: supp_setsum_def support_on_def)
    1.31 -    done
    1.32 -qed
    1.33 -
    1.34 -
    1.35 -lemma dim_image_eq:
    1.36 -  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
    1.37 -  assumes lf: "linear f"
    1.38 -    and fi: "inj_on f (span S)"
    1.39 -  shows "dim (f ` S) = dim (S::'n::euclidean_space set)"
    1.40 -proof -
    1.41 -  obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S"
    1.42 -    using basis_exists[of S] by auto
    1.43 -  then have "span S = span B"
    1.44 -    using span_mono[of B S] span_mono[of S "span B"] span_span[of B] by auto
    1.45 -  then have "independent (f ` B)"
    1.46 -    using independent_inj_on_image[of B f] B assms by auto
    1.47 -  moreover have "card (f ` B) = card B"
    1.48 -    using assms card_image[of f B] subset_inj_on[of f "span S" B] B span_inc by auto
    1.49 -  moreover have "(f ` B) \<subseteq> (f ` S)"
    1.50 -    using B by auto
    1.51 -  ultimately have "dim (f ` S) \<ge> dim S"
    1.52 -    using independent_card_le_dim[of "f ` B" "f ` S"] B by auto
    1.53 -  then show ?thesis
    1.54 -    using dim_image_le[of f S] assms by auto
    1.55 -qed
    1.56 -
    1.57 -lemma linear_injective_on_subspace_0:
    1.58 -  assumes lf: "linear f"
    1.59 -    and "subspace S"
    1.60 -  shows "inj_on f S \<longleftrightarrow> (\<forall>x \<in> S. f x = 0 \<longrightarrow> x = 0)"
    1.61 -proof -
    1.62 -  have "inj_on f S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f x = f y \<longrightarrow> x = y)"
    1.63 -    by (simp add: inj_on_def)
    1.64 -  also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f x - f y = 0 \<longrightarrow> x - y = 0)"
    1.65 -    by simp
    1.66 -  also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. f (x - y) = 0 \<longrightarrow> x - y = 0)"
    1.67 -    by (simp add: linear_diff[OF lf])
    1.68 -  also have "\<dots> \<longleftrightarrow> (\<forall>x \<in> S. f x = 0 \<longrightarrow> x = 0)"
    1.69 -    using \<open>subspace S\<close> subspace_def[of S] subspace_diff[of S] by auto
    1.70 -  finally show ?thesis .
    1.71 -qed
    1.72 -
    1.73 -lemma subspace_Inter: "\<forall>s \<in> f. subspace s \<Longrightarrow> subspace (\<Inter>f)"
    1.74 -  unfolding subspace_def by auto
    1.75 -
    1.76 -lemma span_eq[simp]: "span s = s \<longleftrightarrow> subspace s"
    1.77 -  unfolding span_def by (rule hull_eq) (rule subspace_Inter)
    1.78 -
    1.79 -lemma substdbasis_expansion_unique:
    1.80 -  assumes d: "d \<subseteq> Basis"
    1.81 -  shows "(\<Sum>i\<in>d. f i *\<^sub>R i) = (x::'a::euclidean_space) \<longleftrightarrow>
    1.82 -    (\<forall>i\<in>Basis. (i \<in> d \<longrightarrow> f i = x \<bullet> i) \<and> (i \<notin> d \<longrightarrow> x \<bullet> i = 0))"
    1.83 -proof -
    1.84 -  have *: "\<And>x a b P. x * (if P then a else b) = (if P then x * a else x * b)"
    1.85 -    by auto
    1.86 -  have **: "finite d"
    1.87 -    by (auto intro: finite_subset[OF assms])
    1.88 -  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)"
    1.89 -    using d
    1.90 -    by (auto intro!: setsum.cong simp: inner_Basis inner_setsum_left)
    1.91 -  show ?thesis
    1.92 -    unfolding euclidean_eq_iff[where 'a='a] by (auto simp: setsum.delta[OF **] ***)
    1.93 -qed
    1.94 -
    1.95 -lemma independent_substdbasis: "d \<subseteq> Basis \<Longrightarrow> independent d"
    1.96 -  by (rule independent_mono[OF independent_Basis])
    1.97 -
    1.98 -lemma dim_cball:
    1.99 -  assumes "e > 0"
   1.100 -  shows "dim (cball (0 :: 'n::euclidean_space) e) = DIM('n)"
   1.101 -proof -
   1.102 -  {
   1.103 -    fix x :: "'n::euclidean_space"
   1.104 -    define y where "y = (e / norm x) *\<^sub>R x"
   1.105 -    then have "y \<in> cball 0 e"
   1.106 -      using assms by auto
   1.107 -    moreover have *: "x = (norm x / e) *\<^sub>R y"
   1.108 -      using y_def assms by simp
   1.109 -    moreover from * have "x = (norm x/e) *\<^sub>R y"
   1.110 -      by auto
   1.111 -    ultimately have "x \<in> span (cball 0 e)"
   1.112 -      using span_mul[of y "cball 0 e" "norm x/e"] span_inc[of "cball 0 e"]
   1.113 -      by (simp add: span_superset)
   1.114 -  }
   1.115 -  then have "span (cball 0 e) = (UNIV :: 'n::euclidean_space set)"
   1.116 -    by auto
   1.117 -  then show ?thesis
   1.118 -    using dim_span[of "cball (0 :: 'n::euclidean_space) e"] by (auto simp add: dim_UNIV)
   1.119 -qed
   1.120 -
   1.121 -lemma indep_card_eq_dim_span:
   1.122 -  fixes B :: "'n::euclidean_space set"
   1.123 -  assumes "independent B"
   1.124 -  shows "finite B \<and> card B = dim (span B)"
   1.125 -  using assms basis_card_eq_dim[of B "span B"] span_inc by auto
   1.126 -
   1.127 -lemma setsum_not_0: "setsum f A \<noteq> 0 \<Longrightarrow> \<exists>a \<in> A. f a \<noteq> 0"
   1.128 -  by (rule ccontr) auto
   1.129 -
   1.130 -lemma subset_translation_eq [simp]:
   1.131 -    fixes a :: "'a::real_vector" shows "op + a ` s \<subseteq> op + a ` t \<longleftrightarrow> s \<subseteq> t"
   1.132 -  by auto
   1.133 -
   1.134 -lemma translate_inj_on:
   1.135 -  fixes A :: "'a::ab_group_add set"
   1.136 -  shows "inj_on (\<lambda>x. a + x) A"
   1.137 -  unfolding inj_on_def by auto
   1.138 -
   1.139 -lemma translation_assoc:
   1.140 -  fixes a b :: "'a::ab_group_add"
   1.141 -  shows "(\<lambda>x. b + x) ` ((\<lambda>x. a + x) ` S) = (\<lambda>x. (a + b) + x) ` S"
   1.142 -  by auto
   1.143 -
   1.144 -lemma translation_invert:
   1.145 -  fixes a :: "'a::ab_group_add"
   1.146 -  assumes "(\<lambda>x. a + x) ` A = (\<lambda>x. a + x) ` B"
   1.147 -  shows "A = B"
   1.148 -proof -
   1.149 -  have "(\<lambda>x. -a + x) ` ((\<lambda>x. a + x) ` A) = (\<lambda>x. - a + x) ` ((\<lambda>x. a + x) ` B)"
   1.150 -    using assms by auto
   1.151 -  then show ?thesis
   1.152 -    using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto
   1.153 -qed
   1.154 -
   1.155 -lemma translation_galois:
   1.156 -  fixes a :: "'a::ab_group_add"
   1.157 -  shows "T = ((\<lambda>x. a + x) ` S) \<longleftrightarrow> S = ((\<lambda>x. (- a) + x) ` T)"
   1.158 -  using translation_assoc[of "-a" a S]
   1.159 -  apply auto
   1.160 -  using translation_assoc[of a "-a" T]
   1.161 -  apply auto
   1.162 -  done
   1.163 -
   1.164 -lemma convex_translation_eq [simp]: "convex ((\<lambda>x. a + x) ` s) \<longleftrightarrow> convex s"
   1.165 -  by (metis convex_translation translation_galois)
   1.166 -
   1.167 -lemma translation_inverse_subset:
   1.168 -  assumes "((\<lambda>x. - a + x) ` V) \<le> (S :: 'n::ab_group_add set)"
   1.169 -  shows "V \<le> ((\<lambda>x. a + x) ` S)"
   1.170 -proof -
   1.171 -  {
   1.172 -    fix x
   1.173 -    assume "x \<in> V"
   1.174 -    then have "x-a \<in> S" using assms by auto
   1.175 -    then have "x \<in> {a + v |v. v \<in> S}"
   1.176 -      apply auto
   1.177 -      apply (rule exI[of _ "x-a"])
   1.178 -      apply simp
   1.179 -      done
   1.180 -    then have "x \<in> ((\<lambda>x. a+x) ` S)" by auto
   1.181 -  }
   1.182 -  then show ?thesis by auto
   1.183 -qed
   1.184 -
   1.185 -lemma convex_linear_image_eq [simp]:
   1.186 -    fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
   1.187 -    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> convex (f ` s) \<longleftrightarrow> convex s"
   1.188 -    by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq)
   1.189 -
   1.190 -lemma basis_to_basis_subspace_isomorphism:
   1.191 -  assumes s: "subspace (S:: ('n::euclidean_space) set)"
   1.192 -    and t: "subspace (T :: ('m::euclidean_space) set)"
   1.193 -    and d: "dim S = dim T"
   1.194 -    and B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S"
   1.195 -    and C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "card C = dim T"
   1.196 -  shows "\<exists>f. linear f \<and> f ` B = C \<and> f ` S = T \<and> inj_on f S"
   1.197 -proof -
   1.198 -  from B independent_bound have fB: "finite B"
   1.199 -    by blast
   1.200 -  from C independent_bound have fC: "finite C"
   1.201 -    by blast
   1.202 -  from B(4) C(4) card_le_inj[of B C] d obtain f where
   1.203 -    f: "f ` B \<subseteq> C" "inj_on f B" using \<open>finite B\<close> \<open>finite C\<close> by auto
   1.204 -  from linear_independent_extend[OF B(2)] obtain g where
   1.205 -    g: "linear g" "\<forall>x \<in> B. g x = f x" by blast
   1.206 -  from inj_on_iff_eq_card[OF fB, of f] f(2)
   1.207 -  have "card (f ` B) = card B" by simp
   1.208 -  with B(4) C(4) have ceq: "card (f ` B) = card C" using d
   1.209 -    by simp
   1.210 -  have "g ` B = f ` B" using g(2)
   1.211 -    by (auto simp add: image_iff)
   1.212 -  also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
   1.213 -  finally have gBC: "g ` B = C" .
   1.214 -  have gi: "inj_on g B" using f(2) g(2)
   1.215 -    by (auto simp add: inj_on_def)
   1.216 -  note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
   1.217 -  {
   1.218 -    fix x y
   1.219 -    assume x: "x \<in> S" and y: "y \<in> S" and gxy: "g x = g y"
   1.220 -    from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B"
   1.221 -      by blast+
   1.222 -    from gxy have th0: "g (x - y) = 0"
   1.223 -      by (simp add: linear_diff[OF g(1)])
   1.224 -    have th1: "x - y \<in> span B" using x' y'
   1.225 -      by (metis span_sub)
   1.226 -    have "x = y" using g0[OF th1 th0] by simp
   1.227 -  }
   1.228 -  then have giS: "inj_on g S" unfolding inj_on_def by blast
   1.229 -  from span_subspace[OF B(1,3) s]
   1.230 -  have "g ` S = span (g ` B)"
   1.231 -    by (simp add: span_linear_image[OF g(1)])
   1.232 -  also have "\<dots> = span C"
   1.233 -    unfolding gBC ..
   1.234 -  also have "\<dots> = T"
   1.235 -    using span_subspace[OF C(1,3) t] .
   1.236 -  finally have gS: "g ` S = T" .
   1.237 -  from g(1) gS giS gBC show ?thesis
   1.238 -    by blast
   1.239 -qed
   1.240 -
   1.241 -lemma closure_bounded_linear_image_subset:
   1.242 -  assumes f: "bounded_linear f"
   1.243 -  shows "f ` closure S \<subseteq> closure (f ` S)"
   1.244 -  using linear_continuous_on [OF f] closed_closure closure_subset
   1.245 -  by (rule image_closure_subset)
   1.246 -
   1.247 -lemma closure_linear_image_subset:
   1.248 -  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::real_normed_vector"
   1.249 -  assumes "linear f"
   1.250 -  shows "f ` (closure S) \<subseteq> closure (f ` S)"
   1.251 -  using assms unfolding linear_conv_bounded_linear
   1.252 -  by (rule closure_bounded_linear_image_subset)
   1.253 -
   1.254 -lemma closed_injective_linear_image:
   1.255 -    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   1.256 -    assumes S: "closed S" and f: "linear f" "inj f"
   1.257 -    shows "closed (f ` S)"
   1.258 -proof -
   1.259 -  obtain g where g: "linear g" "g \<circ> f = id"
   1.260 -    using linear_injective_left_inverse [OF f] by blast
   1.261 -  then have confg: "continuous_on (range f) g"
   1.262 -    using linear_continuous_on linear_conv_bounded_linear by blast
   1.263 -  have [simp]: "g ` f ` S = S"
   1.264 -    using g by (simp add: image_comp)
   1.265 -  have cgf: "closed (g ` f ` S)"
   1.266 -    by (simp add: \<open>g \<circ> f = id\<close> S image_comp)
   1.267 -  have [simp]: "{x \<in> range f. g x \<in> S} = f ` S"
   1.268 -    using g by (simp add: o_def id_def image_def) metis
   1.269 -  show ?thesis
   1.270 -    apply (rule closedin_closed_trans [of "range f"])
   1.271 -    apply (rule continuous_closedin_preimage [OF confg cgf, simplified])
   1.272 -    apply (rule closed_injective_image_subspace)
   1.273 -    using f
   1.274 -    apply (auto simp: linear_linear linear_injective_0)
   1.275 -    done
   1.276 -qed
   1.277 -
   1.278 -lemma closed_injective_linear_image_eq:
   1.279 -    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   1.280 -    assumes f: "linear f" "inj f"
   1.281 -      shows "(closed(image f s) \<longleftrightarrow> closed s)"
   1.282 -  by (metis closed_injective_linear_image closure_eq closure_linear_image_subset closure_subset_eq f(1) f(2) inj_image_subset_iff)
   1.283 -
   1.284 -lemma closure_injective_linear_image:
   1.285 -    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   1.286 -    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
   1.287 -  apply (rule subset_antisym)
   1.288 -  apply (simp add: closure_linear_image_subset)
   1.289 -  by (simp add: closure_minimal closed_injective_linear_image closure_subset image_mono)
   1.290 -
   1.291 -lemma closure_bounded_linear_image:
   1.292 -    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   1.293 -    shows "\<lbrakk>linear f; bounded S\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
   1.294 -  apply (rule subset_antisym, simp add: closure_linear_image_subset)
   1.295 -  apply (rule closure_minimal, simp add: closure_subset image_mono)
   1.296 -  by (meson bounded_closure closed_closure compact_continuous_image compact_eq_bounded_closed linear_continuous_on linear_conv_bounded_linear)
   1.297 -
   1.298 -lemma closure_scaleR:
   1.299 -  fixes S :: "'a::real_normed_vector set"
   1.300 -  shows "(op *\<^sub>R c) ` (closure S) = closure ((op *\<^sub>R c) ` S)"
   1.301 -proof
   1.302 -  show "(op *\<^sub>R c) ` (closure S) \<subseteq> closure ((op *\<^sub>R c) ` S)"
   1.303 -    using bounded_linear_scaleR_right
   1.304 -    by (rule closure_bounded_linear_image_subset)
   1.305 -  show "closure ((op *\<^sub>R c) ` S) \<subseteq> (op *\<^sub>R c) ` (closure S)"
   1.306 -    by (intro closure_minimal image_mono closure_subset closed_scaling closed_closure)
   1.307 -qed
   1.308 -
   1.309 -lemma fst_linear: "linear fst"
   1.310 -  unfolding linear_iff by (simp add: algebra_simps)
   1.311 -
   1.312 -lemma snd_linear: "linear snd"
   1.313 -  unfolding linear_iff by (simp add: algebra_simps)
   1.314 -
   1.315 -lemma fst_snd_linear: "linear (\<lambda>(x,y). x + y)"
   1.316 -  unfolding linear_iff by (simp add: algebra_simps)
   1.317 -
   1.318 -lemma scaleR_2:
   1.319 -  fixes x :: "'a::real_vector"
   1.320 -  shows "scaleR 2 x = x + x"
   1.321 -  unfolding one_add_one [symmetric] scaleR_left_distrib by simp
   1.322 -
   1.323 -lemma scaleR_half_double [simp]:
   1.324 -  fixes a :: "'a::real_normed_vector"
   1.325 -  shows "(1 / 2) *\<^sub>R (a + a) = a"
   1.326 -proof -
   1.327 -  have "\<And>r. r *\<^sub>R (a + a) = (r * 2) *\<^sub>R a"
   1.328 -    by (metis scaleR_2 scaleR_scaleR)
   1.329 -  then show ?thesis
   1.330 -    by simp
   1.331 -qed
   1.332 -
   1.333 -lemma vector_choose_size:
   1.334 -  assumes "0 \<le> c"
   1.335 -  obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c"
   1.336 -proof -
   1.337 -  obtain a::'a where "a \<noteq> 0"
   1.338 -    using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce
   1.339 -  then show ?thesis
   1.340 -    by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms)
   1.341 -qed
   1.342 -
   1.343 -lemma vector_choose_dist:
   1.344 -  assumes "0 \<le> c"
   1.345 -  obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c"
   1.346 -by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size)
   1.347 -
   1.348 -lemma sphere_eq_empty [simp]:
   1.349 -  fixes a :: "'a::{real_normed_vector, perfect_space}"
   1.350 -  shows "sphere a r = {} \<longleftrightarrow> r < 0"
   1.351 -by (auto simp: sphere_def dist_norm) (metis dist_norm le_less_linear vector_choose_dist)
   1.352 -
   1.353 -lemma setsum_delta_notmem:
   1.354 -  assumes "x \<notin> s"
   1.355 -  shows "setsum (\<lambda>y. if (y = x) then P x else Q y) s = setsum Q s"
   1.356 -    and "setsum (\<lambda>y. if (x = y) then P x else Q y) s = setsum Q s"
   1.357 -    and "setsum (\<lambda>y. if (y = x) then P y else Q y) s = setsum Q s"
   1.358 -    and "setsum (\<lambda>y. if (x = y) then P y else Q y) s = setsum Q s"
   1.359 -  apply (rule_tac [!] setsum.cong)
   1.360 -  using assms
   1.361 -  apply auto
   1.362 -  done
   1.363 -
   1.364 -lemma setsum_delta'':
   1.365 -  fixes s::"'a::real_vector set"
   1.366 -  assumes "finite s"
   1.367 -  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)"
   1.368 -proof -
   1.369 -  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)"
   1.370 -    by auto
   1.371 -  show ?thesis
   1.372 -    unfolding * using setsum.delta[OF assms, of y "\<lambda>x. f x *\<^sub>R x"] by auto
   1.373 -qed
   1.374 -
   1.375 -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)"
   1.376 -  by (fact if_distrib)
   1.377 -
   1.378 -lemma dist_triangle_eq:
   1.379 -  fixes x y z :: "'a::real_inner"
   1.380 -  shows "dist x z = dist x y + dist y z \<longleftrightarrow>
   1.381 -    norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)"
   1.382 -proof -
   1.383 -  have *: "x - y + (y - z) = x - z" by auto
   1.384 -  show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *]
   1.385 -    by (auto simp add:norm_minus_commute)
   1.386 -qed
   1.387 -
   1.388 -lemma norm_minus_eqI: "x = - y \<Longrightarrow> norm x = norm y" by auto
   1.389 -
   1.390 -lemma Min_grI:
   1.391 -  assumes "finite A" "A \<noteq> {}" "\<forall>a\<in>A. x < a"
   1.392 -  shows "x < Min A"
   1.393 -  unfolding Min_gr_iff[OF assms(1,2)] using assms(3) by auto
   1.394 -
   1.395 -lemma norm_lt: "norm x < norm y \<longleftrightarrow> inner x x < inner y y"
   1.396 -  unfolding norm_eq_sqrt_inner by simp
   1.397 -
   1.398 -lemma norm_le: "norm x \<le> norm y \<longleftrightarrow> inner x x \<le> inner y y"
   1.399 -  unfolding norm_eq_sqrt_inner by simp
   1.400 -
   1.401 -
   1.402 -subsection \<open>Affine set and affine hull\<close>
   1.403 -
   1.404 -definition affine :: "'a::real_vector set \<Rightarrow> bool"
   1.405 -  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)"
   1.406 -
   1.407 -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)"
   1.408 -  unfolding affine_def by (metis eq_diff_eq')
   1.409 -
   1.410 -lemma affine_empty [iff]: "affine {}"
   1.411 -  unfolding affine_def by auto
   1.412 -
   1.413 -lemma affine_sing [iff]: "affine {x}"
   1.414 -  unfolding affine_alt by (auto simp add: scaleR_left_distrib [symmetric])
   1.415 -
   1.416 -lemma affine_UNIV [iff]: "affine UNIV"
   1.417 -  unfolding affine_def by auto
   1.418 -
   1.419 -lemma affine_Inter [intro]: "(\<And>s. s\<in>f \<Longrightarrow> affine s) \<Longrightarrow> affine (\<Inter>f)"
   1.420 -  unfolding affine_def by auto
   1.421 -
   1.422 -lemma affine_Int[intro]: "affine s \<Longrightarrow> affine t \<Longrightarrow> affine (s \<inter> t)"
   1.423 -  unfolding affine_def by auto
   1.424 -
   1.425 -lemma affine_scaling: "affine s \<Longrightarrow> affine (image (\<lambda>x. c *\<^sub>R x) s)"
   1.426 -  apply (clarsimp simp add: affine_def)
   1.427 -  apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI)
   1.428 -  apply (auto simp: algebra_simps)
   1.429 -  done
   1.430 -
   1.431 -lemma affine_affine_hull [simp]: "affine(affine hull s)"
   1.432 -  unfolding hull_def
   1.433 -  using affine_Inter[of "{t. affine t \<and> s \<subseteq> t}"] by auto
   1.434 -
   1.435 -lemma affine_hull_eq[simp]: "(affine hull s = s) \<longleftrightarrow> affine s"
   1.436 -  by (metis affine_affine_hull hull_same)
   1.437 -
   1.438 -lemma affine_hyperplane: "affine {x. a \<bullet> x = b}"
   1.439 -  by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
   1.440 -
   1.441 -
   1.442 -subsubsection \<open>Some explicit formulations (from Lars Schewe)\<close>
   1.443 -
   1.444 -lemma affine:
   1.445 -  fixes V::"'a::real_vector set"
   1.446 -  shows "affine V \<longleftrightarrow>
   1.447 -    (\<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)"
   1.448 -  unfolding affine_def
   1.449 -  apply rule
   1.450 -  apply(rule, rule, rule)
   1.451 -  apply(erule conjE)+
   1.452 -  defer
   1.453 -  apply (rule, rule, rule, rule, rule)
   1.454 -proof -
   1.455 -  fix x y u v
   1.456 -  assume as: "x \<in> V" "y \<in> V" "u + v = (1::real)"
   1.457 -    "\<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"
   1.458 -  then show "u *\<^sub>R x + v *\<^sub>R y \<in> V"
   1.459 -    apply (cases "x = y")
   1.460 -    using as(4)[THEN spec[where x="{x,y}"], THEN spec[where x="\<lambda>w. if w = x then u else v"]]
   1.461 -      and as(1-3)
   1.462 -    apply (auto simp add: scaleR_left_distrib[symmetric])
   1.463 -    done
   1.464 -next
   1.465 -  fix s u
   1.466 -  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"
   1.467 -    "finite s" "s \<noteq> {}" "s \<subseteq> V" "setsum u s = (1::real)"
   1.468 -  define n where "n = card s"
   1.469 -  have "card s = 0 \<or> card s = 1 \<or> card s = 2 \<or> card s > 2" by auto
   1.470 -  then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
   1.471 -  proof (auto simp only: disjE)
   1.472 -    assume "card s = 2"
   1.473 -    then have "card s = Suc (Suc 0)"
   1.474 -      by auto
   1.475 -    then obtain a b where "s = {a, b}"
   1.476 -      unfolding card_Suc_eq by auto
   1.477 -    then show ?thesis
   1.478 -      using as(1)[THEN bspec[where x=a], THEN bspec[where x=b]] using as(4,5)
   1.479 -      by (auto simp add: setsum_clauses(2))
   1.480 -  next
   1.481 -    assume "card s > 2"
   1.482 -    then show ?thesis using as and n_def
   1.483 -    proof (induct n arbitrary: u s)
   1.484 -      case 0
   1.485 -      then show ?case by auto
   1.486 -    next
   1.487 -      case (Suc n)
   1.488 -      fix s :: "'a set" and u :: "'a \<Rightarrow> real"
   1.489 -      assume IA:
   1.490 -        "\<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;
   1.491 -          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"
   1.492 -        and as:
   1.493 -          "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"
   1.494 -           "finite s" "s \<noteq> {}" "s \<subseteq> V" "setsum u s = 1"
   1.495 -      have "\<exists>x\<in>s. u x \<noteq> 1"
   1.496 -      proof (rule ccontr)
   1.497 -        assume "\<not> ?thesis"
   1.498 -        then have "setsum u s = real_of_nat (card s)"
   1.499 -          unfolding card_eq_setsum by auto
   1.500 -        then show False
   1.501 -          using as(7) and \<open>card s > 2\<close>
   1.502 -          by (metis One_nat_def less_Suc0 Zero_not_Suc of_nat_1 of_nat_eq_iff numeral_2_eq_2)
   1.503 -      qed
   1.504 -      then obtain x where x:"x \<in> s" "u x \<noteq> 1" by auto
   1.505 -
   1.506 -      have c: "card (s - {x}) = card s - 1"
   1.507 -        apply (rule card_Diff_singleton)
   1.508 -        using \<open>x\<in>s\<close> as(4)
   1.509 -        apply auto
   1.510 -        done
   1.511 -      have *: "s = insert x (s - {x})" "finite (s - {x})"
   1.512 -        using \<open>x\<in>s\<close> and as(4) by auto
   1.513 -      have **: "setsum u (s - {x}) = 1 - u x"
   1.514 -        using setsum_clauses(2)[OF *(2), of u x, unfolded *(1)[symmetric] as(7)] by auto
   1.515 -      have ***: "inverse (1 - u x) * setsum u (s - {x}) = 1"
   1.516 -        unfolding ** using \<open>u x \<noteq> 1\<close> by auto
   1.517 -      have "(\<Sum>xa\<in>s - {x}. (inverse (1 - u x) * u xa) *\<^sub>R xa) \<in> V"
   1.518 -      proof (cases "card (s - {x}) > 2")
   1.519 -        case True
   1.520 -        then have "s - {x} \<noteq> {}" "card (s - {x}) = n"
   1.521 -          unfolding c and as(1)[symmetric]
   1.522 -        proof (rule_tac ccontr)
   1.523 -          assume "\<not> s - {x} \<noteq> {}"
   1.524 -          then have "card (s - {x}) = 0" unfolding card_0_eq[OF *(2)] by simp
   1.525 -          then show False using True by auto
   1.526 -        qed auto
   1.527 -        then show ?thesis
   1.528 -          apply (rule_tac IA[of "s - {x}" "\<lambda>y. (inverse (1 - u x) * u y)"])
   1.529 -          unfolding setsum_right_distrib[symmetric]
   1.530 -          using as and *** and True
   1.531 -          apply auto
   1.532 -          done
   1.533 -      next
   1.534 -        case False
   1.535 -        then have "card (s - {x}) = Suc (Suc 0)"
   1.536 -          using as(2) and c by auto
   1.537 -        then obtain a b where "(s - {x}) = {a, b}" "a\<noteq>b"
   1.538 -          unfolding card_Suc_eq by auto
   1.539 -        then show ?thesis
   1.540 -          using as(3)[THEN bspec[where x=a], THEN bspec[where x=b]]
   1.541 -          using *** *(2) and \<open>s \<subseteq> V\<close>
   1.542 -          unfolding setsum_right_distrib
   1.543 -          by (auto simp add: setsum_clauses(2))
   1.544 -      qed
   1.545 -      then have "u x + (1 - u x) = 1 \<Longrightarrow>
   1.546 -          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"
   1.547 -        apply -
   1.548 -        apply (rule as(3)[rule_format])
   1.549 -        unfolding  Real_Vector_Spaces.scaleR_right.setsum
   1.550 -        using x(1) as(6)
   1.551 -        apply auto
   1.552 -        done
   1.553 -      then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> V"
   1.554 -        unfolding scaleR_scaleR[symmetric] and scaleR_right.setsum [symmetric]
   1.555 -        apply (subst *)
   1.556 -        unfolding setsum_clauses(2)[OF *(2)]
   1.557 -        using \<open>u x \<noteq> 1\<close>
   1.558 -        apply auto
   1.559 -        done
   1.560 -    qed
   1.561 -  next
   1.562 -    assume "card s = 1"
   1.563 -    then obtain a where "s={a}"
   1.564 -      by (auto simp add: card_Suc_eq)
   1.565 -    then show ?thesis
   1.566 -      using as(4,5) by simp
   1.567 -  qed (insert \<open>s\<noteq>{}\<close> \<open>finite s\<close>, auto)
   1.568 -qed
   1.569 -
   1.570 -lemma affine_hull_explicit:
   1.571 -  "affine hull p =
   1.572 -    {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}"
   1.573 -  apply (rule hull_unique)
   1.574 -  apply (subst subset_eq)
   1.575 -  prefer 3
   1.576 -  apply rule
   1.577 -  unfolding mem_Collect_eq
   1.578 -  apply (erule exE)+
   1.579 -  apply (erule conjE)+
   1.580 -  prefer 2
   1.581 -  apply rule
   1.582 -proof -
   1.583 -  fix x
   1.584 -  assume "x\<in>p"
   1.585 -  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"
   1.586 -    apply (rule_tac x="{x}" in exI)
   1.587 -    apply (rule_tac x="\<lambda>x. 1" in exI)
   1.588 -    apply auto
   1.589 -    done
   1.590 -next
   1.591 -  fix t x s u
   1.592 -  assume as: "p \<subseteq> t" "affine t" "finite s" "s \<noteq> {}"
   1.593 -    "s \<subseteq> p" "setsum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
   1.594 -  then show "x \<in> t"
   1.595 -    using as(2)[unfolded affine, THEN spec[where x=s], THEN spec[where x=u]]
   1.596 -    by auto
   1.597 -next
   1.598 -  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}"
   1.599 -    unfolding affine_def
   1.600 -    apply (rule, rule, rule, rule, rule)
   1.601 -    unfolding mem_Collect_eq
   1.602 -  proof -
   1.603 -    fix u v :: real
   1.604 -    assume uv: "u + v = 1"
   1.605 -    fix x
   1.606 -    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"
   1.607 -    then obtain sx ux where
   1.608 -      x: "finite sx" "sx \<noteq> {}" "sx \<subseteq> p" "setsum ux sx = 1" "(\<Sum>v\<in>sx. ux v *\<^sub>R v) = x"
   1.609 -      by auto
   1.610 -    fix y
   1.611 -    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"
   1.612 -    then obtain sy uy where
   1.613 -      y: "finite sy" "sy \<noteq> {}" "sy \<subseteq> p" "setsum uy sy = 1" "(\<Sum>v\<in>sy. uy v *\<^sub>R v) = y" by auto
   1.614 -    have xy: "finite (sx \<union> sy)"
   1.615 -      using x(1) y(1) by auto
   1.616 -    have **: "(sx \<union> sy) \<inter> sx = sx" "(sx \<union> sy) \<inter> sy = sy"
   1.617 -      by auto
   1.618 -    show "\<exists>s ua. finite s \<and> s \<noteq> {} \<and> s \<subseteq> p \<and>
   1.619 -        setsum ua s = 1 \<and> (\<Sum>v\<in>s. ua v *\<^sub>R v) = u *\<^sub>R x + v *\<^sub>R y"
   1.620 -      apply (rule_tac x="sx \<union> sy" in exI)
   1.621 -      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)
   1.622 -      unfolding scaleR_left_distrib setsum.distrib if_smult scaleR_zero_left
   1.623 -        ** setsum.inter_restrict[OF xy, symmetric]
   1.624 -      unfolding scaleR_scaleR[symmetric] Real_Vector_Spaces.scaleR_right.setsum [symmetric]
   1.625 -        and setsum_right_distrib[symmetric]
   1.626 -      unfolding x y
   1.627 -      using x(1-3) y(1-3) uv
   1.628 -      apply simp
   1.629 -      done
   1.630 -  qed
   1.631 -qed
   1.632 -
   1.633 -lemma affine_hull_finite:
   1.634 -  assumes "finite s"
   1.635 -  shows "affine hull s = {y. \<exists>u. setsum u s = 1 \<and> setsum (\<lambda>v. u v *\<^sub>R v) s = y}"
   1.636 -  unfolding affine_hull_explicit and set_eq_iff and mem_Collect_eq
   1.637 -  apply (rule, rule)
   1.638 -  apply (erule exE)+
   1.639 -  apply (erule conjE)+
   1.640 -  defer
   1.641 -  apply (erule exE)
   1.642 -  apply (erule conjE)
   1.643 -proof -
   1.644 -  fix x u
   1.645 -  assume "setsum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
   1.646 -  then show "\<exists>sa u. finite sa \<and>
   1.647 -      \<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"
   1.648 -    apply (rule_tac x=s in exI, rule_tac x=u in exI)
   1.649 -    using assms
   1.650 -    apply auto
   1.651 -    done
   1.652 -next
   1.653 -  fix x t u
   1.654 -  assume "t \<subseteq> s"
   1.655 -  then have *: "s \<inter> t = t"
   1.656 -    by auto
   1.657 -  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"
   1.658 -  then show "\<exists>u. setsum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = x"
   1.659 -    apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
   1.660 -    unfolding if_smult scaleR_zero_left and setsum.inter_restrict[OF assms, symmetric] and *
   1.661 -    apply auto
   1.662 -    done
   1.663 -qed
   1.664 -
   1.665 -
   1.666 -subsubsection \<open>Stepping theorems and hence small special cases\<close>
   1.667 -
   1.668 -lemma affine_hull_empty[simp]: "affine hull {} = {}"
   1.669 -  by (rule hull_unique) auto
   1.670 -
   1.671 -lemma affine_hull_finite_step:
   1.672 -  fixes y :: "'a::real_vector"
   1.673 -  shows
   1.674 -    "(\<exists>u. setsum u {} = w \<and> setsum (\<lambda>x. u x *\<^sub>R x) {} = y) \<longleftrightarrow> w = 0 \<and> y = 0" (is ?th1)
   1.675 -    and
   1.676 -    "finite s \<Longrightarrow>
   1.677 -      (\<exists>u. setsum u (insert a s) = w \<and> setsum (\<lambda>x. u x *\<^sub>R x) (insert a s) = y) \<longleftrightarrow>
   1.678 -      (\<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")
   1.679 -proof -
   1.680 -  show ?th1 by simp
   1.681 -  assume fin: "finite s"
   1.682 -  show "?lhs = ?rhs"
   1.683 -  proof
   1.684 -    assume ?lhs
   1.685 -    then obtain u where u: "setsum u (insert a s) = w \<and> (\<Sum>x\<in>insert a s. u x *\<^sub>R x) = y"
   1.686 -      by auto
   1.687 -    show ?rhs
   1.688 -    proof (cases "a \<in> s")
   1.689 -      case True
   1.690 -      then have *: "insert a s = s" by auto
   1.691 -      show ?thesis
   1.692 -        using u[unfolded *]
   1.693 -        apply(rule_tac x=0 in exI)
   1.694 -        apply auto
   1.695 -        done
   1.696 -    next
   1.697 -      case False
   1.698 -      then show ?thesis
   1.699 -        apply (rule_tac x="u a" in exI)
   1.700 -        using u and fin
   1.701 -        apply auto
   1.702 -        done
   1.703 -    qed
   1.704 -  next
   1.705 -    assume ?rhs
   1.706 -    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"
   1.707 -      by auto
   1.708 -    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)"
   1.709 -      by auto
   1.710 -    show ?lhs
   1.711 -    proof (cases "a \<in> s")
   1.712 -      case True
   1.713 -      then show ?thesis
   1.714 -        apply (rule_tac x="\<lambda>x. (if x=a then v else 0) + u x" in exI)
   1.715 -        unfolding setsum_clauses(2)[OF fin]
   1.716 -        apply simp
   1.717 -        unfolding scaleR_left_distrib and setsum.distrib
   1.718 -        unfolding vu and * and scaleR_zero_left
   1.719 -        apply (auto simp add: setsum.delta[OF fin])
   1.720 -        done
   1.721 -    next
   1.722 -      case False
   1.723 -      then have **:
   1.724 -        "\<And>x. x \<in> s \<Longrightarrow> u x = (if x = a then v else u x)"
   1.725 -        "\<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
   1.726 -      from False show ?thesis
   1.727 -        apply (rule_tac x="\<lambda>x. if x=a then v else u x" in exI)
   1.728 -        unfolding setsum_clauses(2)[OF fin] and * using vu
   1.729 -        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)]
   1.730 -        using setsum.cong [of s _ u "\<lambda>x. if x = a then v else u x", OF _ **(1)]
   1.731 -        apply auto
   1.732 -        done
   1.733 -    qed
   1.734 -  qed
   1.735 -qed
   1.736 -
   1.737 -lemma affine_hull_2:
   1.738 -  fixes a b :: "'a::real_vector"
   1.739 -  shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}"
   1.740 -  (is "?lhs = ?rhs")
   1.741 -proof -
   1.742 -  have *:
   1.743 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
   1.744 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
   1.745 -  have "?lhs = {y. \<exists>u. setsum u {a, b} = 1 \<and> (\<Sum>v\<in>{a, b}. u v *\<^sub>R v) = y}"
   1.746 -    using affine_hull_finite[of "{a,b}"] by auto
   1.747 -  also have "\<dots> = {y. \<exists>v u. u b = 1 - v \<and> u b *\<^sub>R b = y - v *\<^sub>R a}"
   1.748 -    by (simp add: affine_hull_finite_step(2)[of "{b}" a])
   1.749 -  also have "\<dots> = ?rhs" unfolding * by auto
   1.750 -  finally show ?thesis by auto
   1.751 -qed
   1.752 -
   1.753 -lemma affine_hull_3:
   1.754 -  fixes a b c :: "'a::real_vector"
   1.755 -  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}"
   1.756 -proof -
   1.757 -  have *:
   1.758 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
   1.759 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
   1.760 -  show ?thesis
   1.761 -    apply (simp add: affine_hull_finite affine_hull_finite_step)
   1.762 -    unfolding *
   1.763 -    apply auto
   1.764 -    apply (rule_tac x=v in exI)
   1.765 -    apply (rule_tac x=va in exI)
   1.766 -    apply auto
   1.767 -    apply (rule_tac x=u in exI)
   1.768 -    apply force
   1.769 -    done
   1.770 -qed
   1.771 -
   1.772 -lemma mem_affine:
   1.773 -  assumes "affine S" "x \<in> S" "y \<in> S" "u + v = 1"
   1.774 -  shows "u *\<^sub>R x + v *\<^sub>R y \<in> S"
   1.775 -  using assms affine_def[of S] by auto
   1.776 -
   1.777 -lemma mem_affine_3:
   1.778 -  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S" "u + v + w = 1"
   1.779 -  shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> S"
   1.780 -proof -
   1.781 -  have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> affine hull {x, y, z}"
   1.782 -    using affine_hull_3[of x y z] assms by auto
   1.783 -  moreover
   1.784 -  have "affine hull {x, y, z} \<subseteq> affine hull S"
   1.785 -    using hull_mono[of "{x, y, z}" "S"] assms by auto
   1.786 -  moreover
   1.787 -  have "affine hull S = S"
   1.788 -    using assms affine_hull_eq[of S] by auto
   1.789 -  ultimately show ?thesis by auto
   1.790 -qed
   1.791 -
   1.792 -lemma mem_affine_3_minus:
   1.793 -  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S"
   1.794 -  shows "x + v *\<^sub>R (y-z) \<in> S"
   1.795 -  using mem_affine_3[of S x y z 1 v "-v"] assms
   1.796 -  by (simp add: algebra_simps)
   1.797 -
   1.798 -corollary mem_affine_3_minus2:
   1.799 -    "\<lbrakk>affine S; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> x - v *\<^sub>R (y-z) \<in> S"
   1.800 -  by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left)
   1.801 -
   1.802 -
   1.803 -subsubsection \<open>Some relations between affine hull and subspaces\<close>
   1.804 -
   1.805 -lemma affine_hull_insert_subset_span:
   1.806 -  "affine hull (insert a s) \<subseteq> {a + v| v . v \<in> span {x - a | x . x \<in> s}}"
   1.807 -  unfolding subset_eq Ball_def
   1.808 -  unfolding affine_hull_explicit span_explicit mem_Collect_eq
   1.809 -  apply (rule, rule)
   1.810 -  apply (erule exE)+
   1.811 -  apply (erule conjE)+
   1.812 -proof -
   1.813 -  fix x t u
   1.814 -  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"
   1.815 -  have "(\<lambda>x. x - a) ` (t - {a}) \<subseteq> {x - a |x. x \<in> s}"
   1.816 -    using as(3) by auto
   1.817 -  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)"
   1.818 -    apply (rule_tac x="x - a" in exI)
   1.819 -    apply (rule conjI, simp)
   1.820 -    apply (rule_tac x="(\<lambda>x. x - a) ` (t - {a})" in exI)
   1.821 -    apply (rule_tac x="\<lambda>x. u (x + a)" in exI)
   1.822 -    apply (rule conjI) using as(1) apply simp
   1.823 -    apply (erule conjI)
   1.824 -    using as(1)
   1.825 -    apply (simp add: setsum.reindex[unfolded inj_on_def] scaleR_right_diff_distrib
   1.826 -      setsum_subtractf scaleR_left.setsum[symmetric] setsum_diff1 scaleR_left_diff_distrib)
   1.827 -    unfolding as
   1.828 -    apply simp
   1.829 -    done
   1.830 -qed
   1.831 -
   1.832 -lemma affine_hull_insert_span:
   1.833 -  assumes "a \<notin> s"
   1.834 -  shows "affine hull (insert a s) = {a + v | v . v \<in> span {x - a | x.  x \<in> s}}"
   1.835 -  apply (rule, rule affine_hull_insert_subset_span)
   1.836 -  unfolding subset_eq Ball_def
   1.837 -  unfolding affine_hull_explicit and mem_Collect_eq
   1.838 -proof (rule, rule, erule exE, erule conjE)
   1.839 -  fix y v
   1.840 -  assume "y = a + v" "v \<in> span {x - a |x. x \<in> s}"
   1.841 -  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"
   1.842 -    unfolding span_explicit by auto
   1.843 -  define f where "f = (\<lambda>x. x + a) ` t"
   1.844 -  have f: "finite f" "f \<subseteq> s" "(\<Sum>v\<in>f. u (v - a) *\<^sub>R (v - a)) = y - a"
   1.845 -    unfolding f_def using obt by (auto simp add: setsum.reindex[unfolded inj_on_def])
   1.846 -  have *: "f \<inter> {a} = {}" "f \<inter> - {a} = f"
   1.847 -    using f(2) assms by auto
   1.848 -  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"
   1.849 -    apply (rule_tac x = "insert a f" in exI)
   1.850 -    apply (rule_tac x = "\<lambda>x. if x=a then 1 - setsum (\<lambda>x. u (x - a)) f else u (x - a)" in exI)
   1.851 -    using assms and f
   1.852 -    unfolding setsum_clauses(2)[OF f(1)] and if_smult
   1.853 -    unfolding setsum.If_cases[OF f(1), of "\<lambda>x. x = a"]
   1.854 -    apply (auto simp add: setsum_subtractf scaleR_left.setsum algebra_simps *)
   1.855 -    done
   1.856 -qed
   1.857 -
   1.858 -lemma affine_hull_span:
   1.859 -  assumes "a \<in> s"
   1.860 -  shows "affine hull s = {a + v | v. v \<in> span {x - a | x. x \<in> s - {a}}}"
   1.861 -  using affine_hull_insert_span[of a "s - {a}", unfolded insert_Diff[OF assms]] by auto
   1.862 -
   1.863 -
   1.864 -subsubsection \<open>Parallel affine sets\<close>
   1.865 -
   1.866 -definition affine_parallel :: "'a::real_vector set \<Rightarrow> 'a::real_vector set \<Rightarrow> bool"
   1.867 -  where "affine_parallel S T \<longleftrightarrow> (\<exists>a. T = (\<lambda>x. a + x) ` S)"
   1.868 -
   1.869 -lemma affine_parallel_expl_aux:
   1.870 -  fixes S T :: "'a::real_vector set"
   1.871 -  assumes "\<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T"
   1.872 -  shows "T = (\<lambda>x. a + x) ` S"
   1.873 -proof -
   1.874 -  {
   1.875 -    fix x
   1.876 -    assume "x \<in> T"
   1.877 -    then have "( - a) + x \<in> S"
   1.878 -      using assms by auto
   1.879 -    then have "x \<in> ((\<lambda>x. a + x) ` S)"
   1.880 -      using imageI[of "-a+x" S "(\<lambda>x. a+x)"] by auto
   1.881 -  }
   1.882 -  moreover have "T \<ge> (\<lambda>x. a + x) ` S"
   1.883 -    using assms by auto
   1.884 -  ultimately show ?thesis by auto
   1.885 -qed
   1.886 -
   1.887 -lemma affine_parallel_expl: "affine_parallel S T \<longleftrightarrow> (\<exists>a. \<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T)"
   1.888 -  unfolding affine_parallel_def
   1.889 -  using affine_parallel_expl_aux[of S _ T] by auto
   1.890 -
   1.891 -lemma affine_parallel_reflex: "affine_parallel S S"
   1.892 -  unfolding affine_parallel_def
   1.893 -  apply (rule exI[of _ "0"])
   1.894 -  apply auto
   1.895 -  done
   1.896 -
   1.897 -lemma affine_parallel_commut:
   1.898 -  assumes "affine_parallel A B"
   1.899 -  shows "affine_parallel B A"
   1.900 -proof -
   1.901 -  from assms obtain a where B: "B = (\<lambda>x. a + x) ` A"
   1.902 -    unfolding affine_parallel_def by auto
   1.903 -  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
   1.904 -  from B show ?thesis
   1.905 -    using translation_galois [of B a A]
   1.906 -    unfolding affine_parallel_def by auto
   1.907 -qed
   1.908 -
   1.909 -lemma affine_parallel_assoc:
   1.910 -  assumes "affine_parallel A B"
   1.911 -    and "affine_parallel B C"
   1.912 -  shows "affine_parallel A C"
   1.913 -proof -
   1.914 -  from assms obtain ab where "B = (\<lambda>x. ab + x) ` A"
   1.915 -    unfolding affine_parallel_def by auto
   1.916 -  moreover
   1.917 -  from assms obtain bc where "C = (\<lambda>x. bc + x) ` B"
   1.918 -    unfolding affine_parallel_def by auto
   1.919 -  ultimately show ?thesis
   1.920 -    using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto
   1.921 -qed
   1.922 -
   1.923 -lemma affine_translation_aux:
   1.924 -  fixes a :: "'a::real_vector"
   1.925 -  assumes "affine ((\<lambda>x. a + x) ` S)"
   1.926 -  shows "affine S"
   1.927 -proof -
   1.928 -  {
   1.929 -    fix x y u v
   1.930 -    assume xy: "x \<in> S" "y \<in> S" "(u :: real) + v = 1"
   1.931 -    then have "(a + x) \<in> ((\<lambda>x. a + x) ` S)" "(a + y) \<in> ((\<lambda>x. a + x) ` S)"
   1.932 -      by auto
   1.933 -    then have h1: "u *\<^sub>R  (a + x) + v *\<^sub>R (a + y) \<in> (\<lambda>x. a + x) ` S"
   1.934 -      using xy assms unfolding affine_def by auto
   1.935 -    have "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) = (u + v) *\<^sub>R a + (u *\<^sub>R x + v *\<^sub>R y)"
   1.936 -      by (simp add: algebra_simps)
   1.937 -    also have "\<dots> = a + (u *\<^sub>R x + v *\<^sub>R y)"
   1.938 -      using \<open>u + v = 1\<close> by auto
   1.939 -    ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \<in> (\<lambda>x. a + x) ` S"
   1.940 -      using h1 by auto
   1.941 -    then have "u *\<^sub>R x + v *\<^sub>R y : S" by auto
   1.942 -  }
   1.943 -  then show ?thesis unfolding affine_def by auto
   1.944 -qed
   1.945 -
   1.946 -lemma affine_translation:
   1.947 -  fixes a :: "'a::real_vector"
   1.948 -  shows "affine S \<longleftrightarrow> affine ((\<lambda>x. a + x) ` S)"
   1.949 -proof -
   1.950 -  have "affine S \<Longrightarrow> affine ((\<lambda>x. a + x) ` S)"
   1.951 -    using affine_translation_aux[of "-a" "((\<lambda>x. a + x) ` S)"]
   1.952 -    using translation_assoc[of "-a" a S] by auto
   1.953 -  then show ?thesis using affine_translation_aux by auto
   1.954 -qed
   1.955 -
   1.956 -lemma parallel_is_affine:
   1.957 -  fixes S T :: "'a::real_vector set"
   1.958 -  assumes "affine S" "affine_parallel S T"
   1.959 -  shows "affine T"
   1.960 -proof -
   1.961 -  from assms obtain a where "T = (\<lambda>x. a + x) ` S"
   1.962 -    unfolding affine_parallel_def by auto
   1.963 -  then show ?thesis
   1.964 -    using affine_translation assms by auto
   1.965 -qed
   1.966 -
   1.967 -lemma subspace_imp_affine: "subspace s \<Longrightarrow> affine s"
   1.968 -  unfolding subspace_def affine_def by auto
   1.969 -
   1.970 -
   1.971 -subsubsection \<open>Subspace parallel to an affine set\<close>
   1.972 -
   1.973 -lemma subspace_affine: "subspace S \<longleftrightarrow> affine S \<and> 0 \<in> S"
   1.974 -proof -
   1.975 -  have h0: "subspace S \<Longrightarrow> affine S \<and> 0 \<in> S"
   1.976 -    using subspace_imp_affine[of S] subspace_0 by auto
   1.977 -  {
   1.978 -    assume assm: "affine S \<and> 0 \<in> S"
   1.979 -    {
   1.980 -      fix c :: real
   1.981 -      fix x
   1.982 -      assume x: "x \<in> S"
   1.983 -      have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto
   1.984 -      moreover
   1.985 -      have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \<in> S"
   1.986 -        using affine_alt[of S] assm x by auto
   1.987 -      ultimately have "c *\<^sub>R x \<in> S" by auto
   1.988 -    }
   1.989 -    then have h1: "\<forall>c. \<forall>x \<in> S. c *\<^sub>R x \<in> S" by auto
   1.990 -
   1.991 -    {
   1.992 -      fix x y
   1.993 -      assume xy: "x \<in> S" "y \<in> S"
   1.994 -      define u where "u = (1 :: real)/2"
   1.995 -      have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)"
   1.996 -        by auto
   1.997 -      moreover
   1.998 -      have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y"
   1.999 -        by (simp add: algebra_simps)
  1.1000 -      moreover
  1.1001 -      have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> S"
  1.1002 -        using affine_alt[of S] assm xy by auto
  1.1003 -      ultimately
  1.1004 -      have "(1/2) *\<^sub>R (x+y) \<in> S"
  1.1005 -        using u_def by auto
  1.1006 -      moreover
  1.1007 -      have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))"
  1.1008 -        by auto
  1.1009 -      ultimately
  1.1010 -      have "x + y \<in> S"
  1.1011 -        using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto
  1.1012 -    }
  1.1013 -    then have "\<forall>x \<in> S. \<forall>y \<in> S. x + y \<in> S"
  1.1014 -      by auto
  1.1015 -    then have "subspace S"
  1.1016 -      using h1 assm unfolding subspace_def by auto
  1.1017 -  }
  1.1018 -  then show ?thesis using h0 by metis
  1.1019 -qed
  1.1020 -
  1.1021 -lemma affine_diffs_subspace:
  1.1022 -  assumes "affine S" "a \<in> S"
  1.1023 -  shows "subspace ((\<lambda>x. (-a)+x) ` S)"
  1.1024 -proof -
  1.1025 -  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  1.1026 -  have "affine ((\<lambda>x. (-a)+x) ` S)"
  1.1027 -    using  affine_translation assms by auto
  1.1028 -  moreover have "0 : ((\<lambda>x. (-a)+x) ` S)"
  1.1029 -    using assms exI[of "(\<lambda>x. x\<in>S \<and> -a+x = 0)" a] by auto
  1.1030 -  ultimately show ?thesis using subspace_affine by auto
  1.1031 -qed
  1.1032 -
  1.1033 -lemma parallel_subspace_explicit:
  1.1034 -  assumes "affine S"
  1.1035 -    and "a \<in> S"
  1.1036 -  assumes "L \<equiv> {y. \<exists>x \<in> S. (-a) + x = y}"
  1.1037 -  shows "subspace L \<and> affine_parallel S L"
  1.1038 -proof -
  1.1039 -  from assms have "L = plus (- a) ` S" by auto
  1.1040 -  then have par: "affine_parallel S L"
  1.1041 -    unfolding affine_parallel_def ..
  1.1042 -  then have "affine L" using assms parallel_is_affine by auto
  1.1043 -  moreover have "0 \<in> L"
  1.1044 -    using assms by auto
  1.1045 -  ultimately show ?thesis
  1.1046 -    using subspace_affine par by auto
  1.1047 -qed
  1.1048 -
  1.1049 -lemma parallel_subspace_aux:
  1.1050 -  assumes "subspace A"
  1.1051 -    and "subspace B"
  1.1052 -    and "affine_parallel A B"
  1.1053 -  shows "A \<supseteq> B"
  1.1054 -proof -
  1.1055 -  from assms obtain a where a: "\<forall>x. x \<in> A \<longleftrightarrow> a + x \<in> B"
  1.1056 -    using affine_parallel_expl[of A B] by auto
  1.1057 -  then have "-a \<in> A"
  1.1058 -    using assms subspace_0[of B] by auto
  1.1059 -  then have "a \<in> A"
  1.1060 -    using assms subspace_neg[of A "-a"] by auto
  1.1061 -  then show ?thesis
  1.1062 -    using assms a unfolding subspace_def by auto
  1.1063 -qed
  1.1064 -
  1.1065 -lemma parallel_subspace:
  1.1066 -  assumes "subspace A"
  1.1067 -    and "subspace B"
  1.1068 -    and "affine_parallel A B"
  1.1069 -  shows "A = B"
  1.1070 -proof
  1.1071 -  show "A \<supseteq> B"
  1.1072 -    using assms parallel_subspace_aux by auto
  1.1073 -  show "A \<subseteq> B"
  1.1074 -    using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto
  1.1075 -qed
  1.1076 -
  1.1077 -lemma affine_parallel_subspace:
  1.1078 -  assumes "affine S" "S \<noteq> {}"
  1.1079 -  shows "\<exists>!L. subspace L \<and> affine_parallel S L"
  1.1080 -proof -
  1.1081 -  have ex: "\<exists>L. subspace L \<and> affine_parallel S L"
  1.1082 -    using assms parallel_subspace_explicit by auto
  1.1083 -  {
  1.1084 -    fix L1 L2
  1.1085 -    assume ass: "subspace L1 \<and> affine_parallel S L1" "subspace L2 \<and> affine_parallel S L2"
  1.1086 -    then have "affine_parallel L1 L2"
  1.1087 -      using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto
  1.1088 -    then have "L1 = L2"
  1.1089 -      using ass parallel_subspace by auto
  1.1090 -  }
  1.1091 -  then show ?thesis using ex by auto
  1.1092 -qed
  1.1093 -
  1.1094 -
  1.1095 -subsection \<open>Cones\<close>
  1.1096 -
  1.1097 -definition cone :: "'a::real_vector set \<Rightarrow> bool"
  1.1098 -  where "cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>c\<ge>0. c *\<^sub>R x \<in> s)"
  1.1099 -
  1.1100 -lemma cone_empty[intro, simp]: "cone {}"
  1.1101 -  unfolding cone_def by auto
  1.1102 -
  1.1103 -lemma cone_univ[intro, simp]: "cone UNIV"
  1.1104 -  unfolding cone_def by auto
  1.1105 -
  1.1106 -lemma cone_Inter[intro]: "\<forall>s\<in>f. cone s \<Longrightarrow> cone (\<Inter>f)"
  1.1107 -  unfolding cone_def by auto
  1.1108 -
  1.1109 -lemma subspace_imp_cone: "subspace S \<Longrightarrow> cone S"
  1.1110 -  by (simp add: cone_def subspace_mul)
  1.1111 -
  1.1112 -
  1.1113 -subsubsection \<open>Conic hull\<close>
  1.1114 -
  1.1115 -lemma cone_cone_hull: "cone (cone hull s)"
  1.1116 -  unfolding hull_def by auto
  1.1117 -
  1.1118 -lemma cone_hull_eq: "cone hull s = s \<longleftrightarrow> cone s"
  1.1119 -  apply (rule hull_eq)
  1.1120 -  using cone_Inter
  1.1121 -  unfolding subset_eq
  1.1122 -  apply auto
  1.1123 -  done
  1.1124 -
  1.1125 -lemma mem_cone:
  1.1126 -  assumes "cone S" "x \<in> S" "c \<ge> 0"
  1.1127 -  shows "c *\<^sub>R x : S"
  1.1128 -  using assms cone_def[of S] by auto
  1.1129 -
  1.1130 -lemma cone_contains_0:
  1.1131 -  assumes "cone S"
  1.1132 -  shows "S \<noteq> {} \<longleftrightarrow> 0 \<in> S"
  1.1133 -proof -
  1.1134 -  {
  1.1135 -    assume "S \<noteq> {}"
  1.1136 -    then obtain a where "a \<in> S" by auto
  1.1137 -    then have "0 \<in> S"
  1.1138 -      using assms mem_cone[of S a 0] by auto
  1.1139 -  }
  1.1140 -  then show ?thesis by auto
  1.1141 -qed
  1.1142 -
  1.1143 -lemma cone_0: "cone {0}"
  1.1144 -  unfolding cone_def by auto
  1.1145 -
  1.1146 -lemma cone_Union[intro]: "(\<forall>s\<in>f. cone s) \<longrightarrow> cone (\<Union>f)"
  1.1147 -  unfolding cone_def by blast
  1.1148 -
  1.1149 -lemma cone_iff:
  1.1150 -  assumes "S \<noteq> {}"
  1.1151 -  shows "cone S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  1.1152 -proof -
  1.1153 -  {
  1.1154 -    assume "cone S"
  1.1155 -    {
  1.1156 -      fix c :: real
  1.1157 -      assume "c > 0"
  1.1158 -      {
  1.1159 -        fix x
  1.1160 -        assume "x \<in> S"
  1.1161 -        then have "x \<in> (op *\<^sub>R c) ` S"
  1.1162 -          unfolding image_def
  1.1163 -          using \<open>cone S\<close> \<open>c>0\<close> mem_cone[of S x "1/c"]
  1.1164 -            exI[of "(\<lambda>t. t \<in> S \<and> x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"]
  1.1165 -          by auto
  1.1166 -      }
  1.1167 -      moreover
  1.1168 -      {
  1.1169 -        fix x
  1.1170 -        assume "x \<in> (op *\<^sub>R c) ` S"
  1.1171 -        then have "x \<in> S"
  1.1172 -          using \<open>cone S\<close> \<open>c > 0\<close>
  1.1173 -          unfolding cone_def image_def \<open>c > 0\<close> by auto
  1.1174 -      }
  1.1175 -      ultimately have "(op *\<^sub>R c) ` S = S" by auto
  1.1176 -    }
  1.1177 -    then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  1.1178 -      using \<open>cone S\<close> cone_contains_0[of S] assms by auto
  1.1179 -  }
  1.1180 -  moreover
  1.1181 -  {
  1.1182 -    assume a: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (op *\<^sub>R c) ` S = S)"
  1.1183 -    {
  1.1184 -      fix x
  1.1185 -      assume "x \<in> S"
  1.1186 -      fix c1 :: real
  1.1187 -      assume "c1 \<ge> 0"
  1.1188 -      then have "c1 = 0 \<or> c1 > 0" by auto
  1.1189 -      then have "c1 *\<^sub>R x \<in> S" using a \<open>x \<in> S\<close> by auto
  1.1190 -    }
  1.1191 -    then have "cone S" unfolding cone_def by auto
  1.1192 -  }
  1.1193 -  ultimately show ?thesis by blast
  1.1194 -qed
  1.1195 -
  1.1196 -lemma cone_hull_empty: "cone hull {} = {}"
  1.1197 -  by (metis cone_empty cone_hull_eq)
  1.1198 -
  1.1199 -lemma cone_hull_empty_iff: "S = {} \<longleftrightarrow> cone hull S = {}"
  1.1200 -  by (metis bot_least cone_hull_empty hull_subset xtrans(5))
  1.1201 -
  1.1202 -lemma cone_hull_contains_0: "S \<noteq> {} \<longleftrightarrow> 0 \<in> cone hull S"
  1.1203 -  using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S]
  1.1204 -  by auto
  1.1205 -
  1.1206 -lemma mem_cone_hull:
  1.1207 -  assumes "x \<in> S" "c \<ge> 0"
  1.1208 -  shows "c *\<^sub>R x \<in> cone hull S"
  1.1209 -  by (metis assms cone_cone_hull hull_inc mem_cone)
  1.1210 -
  1.1211 -lemma cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \<ge> 0 \<and> x \<in> S}"
  1.1212 -  (is "?lhs = ?rhs")
  1.1213 -proof -
  1.1214 -  {
  1.1215 -    fix x
  1.1216 -    assume "x \<in> ?rhs"
  1.1217 -    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.1218 -      by auto
  1.1219 -    fix c :: real
  1.1220 -    assume c: "c \<ge> 0"
  1.1221 -    then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx"
  1.1222 -      using x by (simp add: algebra_simps)
  1.1223 -    moreover
  1.1224 -    have "c * cx \<ge> 0" using c x by auto
  1.1225 -    ultimately
  1.1226 -    have "c *\<^sub>R x \<in> ?rhs" using x by auto
  1.1227 -  }
  1.1228 -  then have "cone ?rhs"
  1.1229 -    unfolding cone_def by auto
  1.1230 -  then have "?rhs \<in> Collect cone"
  1.1231 -    unfolding mem_Collect_eq by auto
  1.1232 -  {
  1.1233 -    fix x
  1.1234 -    assume "x \<in> S"
  1.1235 -    then have "1 *\<^sub>R x \<in> ?rhs"
  1.1236 -      apply auto
  1.1237 -      apply (rule_tac x = 1 in exI)
  1.1238 -      apply auto
  1.1239 -      done
  1.1240 -    then have "x \<in> ?rhs" by auto
  1.1241 -  }
  1.1242 -  then have "S \<subseteq> ?rhs" by auto
  1.1243 -  then have "?lhs \<subseteq> ?rhs"
  1.1244 -    using \<open>?rhs \<in> Collect cone\<close> hull_minimal[of S "?rhs" "cone"] by auto
  1.1245 -  moreover
  1.1246 -  {
  1.1247 -    fix x
  1.1248 -    assume "x \<in> ?rhs"
  1.1249 -    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.1250 -      by auto
  1.1251 -    then have "xx \<in> cone hull S"
  1.1252 -      using hull_subset[of S] by auto
  1.1253 -    then have "x \<in> ?lhs"
  1.1254 -      using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto
  1.1255 -  }
  1.1256 -  ultimately show ?thesis by auto
  1.1257 -qed
  1.1258 -
  1.1259 -lemma cone_closure:
  1.1260 -  fixes S :: "'a::real_normed_vector set"
  1.1261 -  assumes "cone S"
  1.1262 -  shows "cone (closure S)"
  1.1263 -proof (cases "S = {}")
  1.1264 -  case True
  1.1265 -  then show ?thesis by auto
  1.1266 -next
  1.1267 -  case False
  1.1268 -  then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> op *\<^sub>R c ` S = S)"
  1.1269 -    using cone_iff[of S] assms by auto
  1.1270 -  then have "0 \<in> closure S \<and> (\<forall>c. c > 0 \<longrightarrow> op *\<^sub>R c ` closure S = closure S)"
  1.1271 -    using closure_subset by (auto simp add: closure_scaleR)
  1.1272 -  then show ?thesis
  1.1273 -    using False cone_iff[of "closure S"] by auto
  1.1274 -qed
  1.1275 -
  1.1276 -
  1.1277 -subsection \<open>Affine dependence and consequential theorems (from Lars Schewe)\<close>
  1.1278 -
  1.1279 -definition affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
  1.1280 -  where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
  1.1281 -
  1.1282 -lemma affine_dependent_subset:
  1.1283 -   "\<lbrakk>affine_dependent s; s \<subseteq> t\<rbrakk> \<Longrightarrow> affine_dependent t"
  1.1284 -apply (simp add: affine_dependent_def Bex_def)
  1.1285 -apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]])
  1.1286 -done
  1.1287 -
  1.1288 -lemma affine_independent_subset:
  1.1289 -  shows "\<lbrakk>~ affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> ~ affine_dependent s"
  1.1290 -by (metis affine_dependent_subset)
  1.1291 -
  1.1292 -lemma affine_independent_Diff:
  1.1293 -   "~ affine_dependent s \<Longrightarrow> ~ affine_dependent(s - t)"
  1.1294 -by (meson Diff_subset affine_dependent_subset)
  1.1295 -
  1.1296 -lemma affine_dependent_explicit:
  1.1297 -  "affine_dependent p \<longleftrightarrow>
  1.1298 -    (\<exists>s u. finite s \<and> s \<subseteq> p \<and> setsum u s = 0 \<and>
  1.1299 -      (\<exists>v\<in>s. u v \<noteq> 0) \<and> setsum (\<lambda>v. u v *\<^sub>R v) s = 0)"
  1.1300 -  unfolding affine_dependent_def affine_hull_explicit mem_Collect_eq
  1.1301 -  apply rule
  1.1302 -  apply (erule bexE, erule exE, erule exE)
  1.1303 -  apply (erule conjE)+
  1.1304 -  defer
  1.1305 -  apply (erule exE, erule exE)
  1.1306 -  apply (erule conjE)+
  1.1307 -  apply (erule bexE)
  1.1308 -proof -
  1.1309 -  fix x s u
  1.1310 -  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"
  1.1311 -  have "x \<notin> s" using as(1,4) by auto
  1.1312 -  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"
  1.1313 -    apply (rule_tac x="insert x s" in exI, rule_tac x="\<lambda>v. if v = x then - 1 else u v" in exI)
  1.1314 -    unfolding if_smult and setsum_clauses(2)[OF as(2)] and setsum_delta_notmem[OF \<open>x\<notin>s\<close>] and as
  1.1315 -    using as
  1.1316 -    apply auto
  1.1317 -    done
  1.1318 -next
  1.1319 -  fix s u v
  1.1320 -  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"
  1.1321 -  have "s \<noteq> {v}"
  1.1322 -    using as(3,6) by auto
  1.1323 -  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"
  1.1324 -    apply (rule_tac x=v in bexI)
  1.1325 -    apply (rule_tac x="s - {v}" in exI)
  1.1326 -    apply (rule_tac x="\<lambda>x. - (1 / u v) * u x" in exI)
  1.1327 -    unfolding scaleR_scaleR[symmetric] and scaleR_right.setsum [symmetric]
  1.1328 -    unfolding setsum_right_distrib[symmetric] and setsum_diff1[OF as(1)]
  1.1329 -    using as
  1.1330 -    apply auto
  1.1331 -    done
  1.1332 -qed
  1.1333 -
  1.1334 -lemma affine_dependent_explicit_finite:
  1.1335 -  fixes s :: "'a::real_vector set"
  1.1336 -  assumes "finite s"
  1.1337 -  shows "affine_dependent s \<longleftrightarrow>
  1.1338 -    (\<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)"
  1.1339 -  (is "?lhs = ?rhs")
  1.1340 -proof
  1.1341 -  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)"
  1.1342 -    by auto
  1.1343 -  assume ?lhs
  1.1344 -  then obtain t u v where
  1.1345 -    "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"
  1.1346 -    unfolding affine_dependent_explicit by auto
  1.1347 -  then show ?rhs
  1.1348 -    apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
  1.1349 -    apply auto unfolding * and setsum.inter_restrict[OF assms, symmetric]
  1.1350 -    unfolding Int_absorb1[OF \<open>t\<subseteq>s\<close>]
  1.1351 -    apply auto
  1.1352 -    done
  1.1353 -next
  1.1354 -  assume ?rhs
  1.1355 -  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"
  1.1356 -    by auto
  1.1357 -  then show ?lhs unfolding affine_dependent_explicit
  1.1358 -    using assms by auto
  1.1359 -qed
  1.1360 -
  1.1361 -
  1.1362 -subsection \<open>Connectedness of convex sets\<close>
  1.1363 -
  1.1364 -lemma connectedD:
  1.1365 -  "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 = {}"
  1.1366 -  by (rule Topological_Spaces.topological_space_class.connectedD)
  1.1367 -
  1.1368 -lemma convex_connected:
  1.1369 -  fixes s :: "'a::real_normed_vector set"
  1.1370 -  assumes "convex s"
  1.1371 -  shows "connected s"
  1.1372 -proof (rule connectedI)
  1.1373 -  fix A B
  1.1374 -  assume "open A" "open B" "A \<inter> B \<inter> s = {}" "s \<subseteq> A \<union> B"
  1.1375 -  moreover
  1.1376 -  assume "A \<inter> s \<noteq> {}" "B \<inter> s \<noteq> {}"
  1.1377 -  then obtain a b where a: "a \<in> A" "a \<in> s" and b: "b \<in> B" "b \<in> s" by auto
  1.1378 -  define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u
  1.1379 -  then have "continuous_on {0 .. 1} f"
  1.1380 -    by (auto intro!: continuous_intros)
  1.1381 -  then have "connected (f ` {0 .. 1})"
  1.1382 -    by (auto intro!: connected_continuous_image)
  1.1383 -  note connectedD[OF this, of A B]
  1.1384 -  moreover have "a \<in> A \<inter> f ` {0 .. 1}"
  1.1385 -    using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def)
  1.1386 -  moreover have "b \<in> B \<inter> f ` {0 .. 1}"
  1.1387 -    using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def)
  1.1388 -  moreover have "f ` {0 .. 1} \<subseteq> s"
  1.1389 -    using \<open>convex s\<close> a b unfolding convex_def f_def by auto
  1.1390 -  ultimately show False by auto
  1.1391 -qed
  1.1392 -
  1.1393 -corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
  1.1394 -  by(simp add: convex_connected)
  1.1395 -
  1.1396 -proposition clopen:
  1.1397 -  fixes s :: "'a :: real_normed_vector set"
  1.1398 -  shows "closed s \<and> open s \<longleftrightarrow> s = {} \<or> s = UNIV"
  1.1399 -apply (rule iffI)
  1.1400 - apply (rule connected_UNIV [unfolded connected_clopen, rule_format])
  1.1401 - apply (force simp add: open_openin closed_closedin, force)
  1.1402 -done
  1.1403 -
  1.1404 -corollary compact_open:
  1.1405 -  fixes s :: "'a :: euclidean_space set"
  1.1406 -  shows "compact s \<and> open s \<longleftrightarrow> s = {}"
  1.1407 -  by (auto simp: compact_eq_bounded_closed clopen)
  1.1408 -
  1.1409 -corollary finite_imp_not_open:
  1.1410 -    fixes S :: "'a::{real_normed_vector, perfect_space} set"
  1.1411 -    shows "\<lbrakk>finite S; open S\<rbrakk> \<Longrightarrow> S={}"
  1.1412 -  using clopen [of S] finite_imp_closed not_bounded_UNIV by blast
  1.1413 -
  1.1414 -corollary empty_interior_finite:
  1.1415 -    fixes S :: "'a::{real_normed_vector, perfect_space} set"
  1.1416 -    shows "finite S \<Longrightarrow> interior S = {}"
  1.1417 -  by (metis interior_subset finite_subset open_interior [of S] finite_imp_not_open)
  1.1418 -
  1.1419 -text \<open>Balls, being convex, are connected.\<close>
  1.1420 -
  1.1421 -lemma convex_prod:
  1.1422 -  assumes "\<And>i. i \<in> Basis \<Longrightarrow> convex {x. P i x}"
  1.1423 -  shows "convex {x. \<forall>i\<in>Basis. P i (x\<bullet>i)}"
  1.1424 -  using assms unfolding convex_def
  1.1425 -  by (auto simp: inner_add_left)
  1.1426 -
  1.1427 -lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i)}"
  1.1428 -  by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval)
  1.1429 -
  1.1430 -lemma convex_local_global_minimum:
  1.1431 -  fixes s :: "'a::real_normed_vector set"
  1.1432 -  assumes "e > 0"
  1.1433 -    and "convex_on s f"
  1.1434 -    and "ball x e \<subseteq> s"
  1.1435 -    and "\<forall>y\<in>ball x e. f x \<le> f y"
  1.1436 -  shows "\<forall>y\<in>s. f x \<le> f y"
  1.1437 -proof (rule ccontr)
  1.1438 -  have "x \<in> s" using assms(1,3) by auto
  1.1439 -  assume "\<not> ?thesis"
  1.1440 -  then obtain y where "y\<in>s" and y: "f x > f y" by auto
  1.1441 -  then have xy: "0 < dist x y"  by auto
  1.1442 -  then obtain u where "0 < u" "u \<le> 1" and u: "u < e / dist x y"
  1.1443 -    using real_lbound_gt_zero[of 1 "e / dist x y"] xy \<open>e>0\<close> by auto
  1.1444 -  then have "f ((1-u) *\<^sub>R x + u *\<^sub>R y) \<le> (1-u) * f x + u * f y"
  1.1445 -    using \<open>x\<in>s\<close> \<open>y\<in>s\<close>
  1.1446 -    using assms(2)[unfolded convex_on_def,
  1.1447 -      THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x="1-u"]]
  1.1448 -    by auto
  1.1449 -  moreover
  1.1450 -  have *: "x - ((1 - u) *\<^sub>R x + u *\<^sub>R y) = u *\<^sub>R (x - y)"
  1.1451 -    by (simp add: algebra_simps)
  1.1452 -  have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> ball x e"
  1.1453 -    unfolding mem_ball dist_norm
  1.1454 -    unfolding * and norm_scaleR and abs_of_pos[OF \<open>0<u\<close>]
  1.1455 -    unfolding dist_norm[symmetric]
  1.1456 -    using u
  1.1457 -    unfolding pos_less_divide_eq[OF xy]
  1.1458 -    by auto
  1.1459 -  then have "f x \<le> f ((1 - u) *\<^sub>R x + u *\<^sub>R y)"
  1.1460 -    using assms(4) by auto
  1.1461 -  ultimately show False
  1.1462 -    using mult_strict_left_mono[OF y \<open>u>0\<close>]
  1.1463 -    unfolding left_diff_distrib
  1.1464 -    by auto
  1.1465 -qed
  1.1466 -
  1.1467 -lemma convex_ball [iff]:
  1.1468 -  fixes x :: "'a::real_normed_vector"
  1.1469 -  shows "convex (ball x e)"
  1.1470 -proof (auto simp add: convex_def)
  1.1471 -  fix y z
  1.1472 -  assume yz: "dist x y < e" "dist x z < e"
  1.1473 -  fix u v :: real
  1.1474 -  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  1.1475 -  have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
  1.1476 -    using uv yz
  1.1477 -    using convex_on_dist [of "ball x e" x, unfolded convex_on_def,
  1.1478 -      THEN bspec[where x=y], THEN bspec[where x=z]]
  1.1479 -    by auto
  1.1480 -  then show "dist x (u *\<^sub>R y + v *\<^sub>R z) < e"
  1.1481 -    using convex_bound_lt[OF yz uv] by auto
  1.1482 -qed
  1.1483 -
  1.1484 -lemma convex_cball [iff]:
  1.1485 -  fixes x :: "'a::real_normed_vector"
  1.1486 -  shows "convex (cball x e)"
  1.1487 -proof -
  1.1488 -  {
  1.1489 -    fix y z
  1.1490 -    assume yz: "dist x y \<le> e" "dist x z \<le> e"
  1.1491 -    fix u v :: real
  1.1492 -    assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  1.1493 -    have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
  1.1494 -      using uv yz
  1.1495 -      using convex_on_dist [of "cball x e" x, unfolded convex_on_def,
  1.1496 -        THEN bspec[where x=y], THEN bspec[where x=z]]
  1.1497 -      by auto
  1.1498 -    then have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> e"
  1.1499 -      using convex_bound_le[OF yz uv] by auto
  1.1500 -  }
  1.1501 -  then show ?thesis by (auto simp add: convex_def Ball_def)
  1.1502 -qed
  1.1503 -
  1.1504 -lemma connected_ball [iff]:
  1.1505 -  fixes x :: "'a::real_normed_vector"
  1.1506 -  shows "connected (ball x e)"
  1.1507 -  using convex_connected convex_ball by auto
  1.1508 -
  1.1509 -lemma connected_cball [iff]:
  1.1510 -  fixes x :: "'a::real_normed_vector"
  1.1511 -  shows "connected (cball x e)"
  1.1512 -  using convex_connected convex_cball by auto
  1.1513 -
  1.1514 -
  1.1515 -subsection \<open>Convex hull\<close>
  1.1516 -
  1.1517 -lemma convex_convex_hull [iff]: "convex (convex hull s)"
  1.1518 -  unfolding hull_def
  1.1519 -  using convex_Inter[of "{t. convex t \<and> s \<subseteq> t}"]
  1.1520 -  by auto
  1.1521 -
  1.1522 -lemma convex_hull_subset:
  1.1523 -    "s \<subseteq> convex hull t \<Longrightarrow> convex hull s \<subseteq> convex hull t"
  1.1524 -  by (simp add: convex_convex_hull subset_hull)
  1.1525 -
  1.1526 -lemma convex_hull_eq: "convex hull s = s \<longleftrightarrow> convex s"
  1.1527 -  by (metis convex_convex_hull hull_same)
  1.1528 -
  1.1529 -lemma bounded_convex_hull:
  1.1530 -  fixes s :: "'a::real_normed_vector set"
  1.1531 -  assumes "bounded s"
  1.1532 -  shows "bounded (convex hull s)"
  1.1533 -proof -
  1.1534 -  from assms obtain B where B: "\<forall>x\<in>s. norm x \<le> B"
  1.1535 -    unfolding bounded_iff by auto
  1.1536 -  show ?thesis
  1.1537 -    apply (rule bounded_subset[OF bounded_cball, of _ 0 B])
  1.1538 -    unfolding subset_hull[of convex, OF convex_cball]
  1.1539 -    unfolding subset_eq mem_cball dist_norm using B
  1.1540 -    apply auto
  1.1541 -    done
  1.1542 -qed
  1.1543 -
  1.1544 -lemma finite_imp_bounded_convex_hull:
  1.1545 -  fixes s :: "'a::real_normed_vector set"
  1.1546 -  shows "finite s \<Longrightarrow> bounded (convex hull s)"
  1.1547 -  using bounded_convex_hull finite_imp_bounded
  1.1548 -  by auto
  1.1549 -
  1.1550 -
  1.1551 -subsubsection \<open>Convex hull is "preserved" by a linear function\<close>
  1.1552 -
  1.1553 -lemma convex_hull_linear_image:
  1.1554 -  assumes f: "linear f"
  1.1555 -  shows "f ` (convex hull s) = convex hull (f ` s)"
  1.1556 -proof
  1.1557 -  show "convex hull (f ` s) \<subseteq> f ` (convex hull s)"
  1.1558 -    by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull)
  1.1559 -  show "f ` (convex hull s) \<subseteq> convex hull (f ` s)"
  1.1560 -  proof (unfold image_subset_iff_subset_vimage, rule hull_minimal)
  1.1561 -    show "s \<subseteq> f -` (convex hull (f ` s))"
  1.1562 -      by (fast intro: hull_inc)
  1.1563 -    show "convex (f -` (convex hull (f ` s)))"
  1.1564 -      by (intro convex_linear_vimage [OF f] convex_convex_hull)
  1.1565 -  qed
  1.1566 -qed
  1.1567 -
  1.1568 -lemma in_convex_hull_linear_image:
  1.1569 -  assumes "linear f"
  1.1570 -    and "x \<in> convex hull s"
  1.1571 -  shows "f x \<in> convex hull (f ` s)"
  1.1572 -  using convex_hull_linear_image[OF assms(1)] assms(2) by auto
  1.1573 -
  1.1574 -lemma convex_hull_Times:
  1.1575 -  "convex hull (s \<times> t) = (convex hull s) \<times> (convex hull t)"
  1.1576 -proof
  1.1577 -  show "convex hull (s \<times> t) \<subseteq> (convex hull s) \<times> (convex hull t)"
  1.1578 -    by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull)
  1.1579 -  have "\<forall>x\<in>convex hull s. \<forall>y\<in>convex hull t. (x, y) \<in> convex hull (s \<times> t)"
  1.1580 -  proof (intro hull_induct)
  1.1581 -    fix x y assume "x \<in> s" and "y \<in> t"
  1.1582 -    then show "(x, y) \<in> convex hull (s \<times> t)"
  1.1583 -      by (simp add: hull_inc)
  1.1584 -  next
  1.1585 -    fix x let ?S = "((\<lambda>y. (0, y)) -` (\<lambda>p. (- x, 0) + p) ` (convex hull s \<times> t))"
  1.1586 -    have "convex ?S"
  1.1587 -      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  1.1588 -        simp add: linear_iff)
  1.1589 -    also have "?S = {y. (x, y) \<in> convex hull (s \<times> t)}"
  1.1590 -      by (auto simp add: image_def Bex_def)
  1.1591 -    finally show "convex {y. (x, y) \<in> convex hull (s \<times> t)}" .
  1.1592 -  next
  1.1593 -    show "convex {x. \<forall>y\<in>convex hull t. (x, y) \<in> convex hull (s \<times> t)}"
  1.1594 -    proof (unfold Collect_ball_eq, rule convex_INT [rule_format])
  1.1595 -      fix y let ?S = "((\<lambda>x. (x, 0)) -` (\<lambda>p. (0, - y) + p) ` (convex hull s \<times> t))"
  1.1596 -      have "convex ?S"
  1.1597 -      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  1.1598 -        simp add: linear_iff)
  1.1599 -      also have "?S = {x. (x, y) \<in> convex hull (s \<times> t)}"
  1.1600 -        by (auto simp add: image_def Bex_def)
  1.1601 -      finally show "convex {x. (x, y) \<in> convex hull (s \<times> t)}" .
  1.1602 -    qed
  1.1603 -  qed
  1.1604 -  then show "(convex hull s) \<times> (convex hull t) \<subseteq> convex hull (s \<times> t)"
  1.1605 -    unfolding subset_eq split_paired_Ball_Sigma .
  1.1606 -qed
  1.1607 -
  1.1608 -
  1.1609 -subsubsection \<open>Stepping theorems for convex hulls of finite sets\<close>
  1.1610 -
  1.1611 -lemma convex_hull_empty[simp]: "convex hull {} = {}"
  1.1612 -  by (rule hull_unique) auto
  1.1613 -
  1.1614 -lemma convex_hull_singleton[simp]: "convex hull {a} = {a}"
  1.1615 -  by (rule hull_unique) auto
  1.1616 -
  1.1617 -lemma convex_hull_insert:
  1.1618 -  fixes s :: "'a::real_vector set"
  1.1619 -  assumes "s \<noteq> {}"
  1.1620 -  shows "convex hull (insert a s) =
  1.1621 -    {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)}"
  1.1622 -  (is "_ = ?hull")
  1.1623 -  apply (rule, rule hull_minimal, rule)
  1.1624 -  unfolding insert_iff
  1.1625 -  prefer 3
  1.1626 -  apply rule
  1.1627 -proof -
  1.1628 -  fix x
  1.1629 -  assume x: "x = a \<or> x \<in> s"
  1.1630 -  then show "x \<in> ?hull"
  1.1631 -    apply rule
  1.1632 -    unfolding mem_Collect_eq
  1.1633 -    apply (rule_tac x=1 in exI)
  1.1634 -    defer
  1.1635 -    apply (rule_tac x=0 in exI)
  1.1636 -    using assms hull_subset[of s convex]
  1.1637 -    apply auto
  1.1638 -    done
  1.1639 -next
  1.1640 -  fix x
  1.1641 -  assume "x \<in> ?hull"
  1.1642 -  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"
  1.1643 -    by auto
  1.1644 -  have "a \<in> convex hull insert a s" "b \<in> convex hull insert a s"
  1.1645 -    using hull_mono[of s "insert a s" convex] hull_mono[of "{a}" "insert a s" convex] and obt(4)
  1.1646 -    by auto
  1.1647 -  then show "x \<in> convex hull insert a s"
  1.1648 -    unfolding obt(5) using obt(1-3)
  1.1649 -    by (rule convexD [OF convex_convex_hull])
  1.1650 -next
  1.1651 -  show "convex ?hull"
  1.1652 -  proof (rule convexI)
  1.1653 -    fix x y u v
  1.1654 -    assume as: "(0::real) \<le> u" "0 \<le> v" "u + v = 1" "x\<in>?hull" "y\<in>?hull"
  1.1655 -    from as(4) obtain u1 v1 b1 where
  1.1656 -      obt1: "u1\<ge>0" "v1\<ge>0" "u1 + v1 = 1" "b1 \<in> convex hull s" "x = u1 *\<^sub>R a + v1 *\<^sub>R b1"
  1.1657 -      by auto
  1.1658 -    from as(5) obtain u2 v2 b2 where
  1.1659 -      obt2: "u2\<ge>0" "v2\<ge>0" "u2 + v2 = 1" "b2 \<in> convex hull s" "y = u2 *\<^sub>R a + v2 *\<^sub>R b2"
  1.1660 -      by auto
  1.1661 -    have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  1.1662 -      by (auto simp add: algebra_simps)
  1.1663 -    have **: "\<exists>b \<in> convex hull s. u *\<^sub>R x + v *\<^sub>R y =
  1.1664 -      (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)"
  1.1665 -    proof (cases "u * v1 + v * v2 = 0")
  1.1666 -      case True
  1.1667 -      have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  1.1668 -        by (auto simp add: algebra_simps)
  1.1669 -      from True have ***: "u * v1 = 0" "v * v2 = 0"
  1.1670 -        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>]
  1.1671 -        by arith+
  1.1672 -      then have "u * u1 + v * u2 = 1"
  1.1673 -        using as(3) obt1(3) obt2(3) by auto
  1.1674 -      then show ?thesis
  1.1675 -        unfolding obt1(5) obt2(5) *
  1.1676 -        using assms hull_subset[of s convex]
  1.1677 -        by (auto simp add: *** scaleR_right_distrib)
  1.1678 -    next
  1.1679 -      case False
  1.1680 -      have "1 - (u * u1 + v * u2) = (u + v) - (u * u1 + v * u2)"
  1.1681 -        using as(3) obt1(3) obt2(3) by (auto simp add: field_simps)
  1.1682 -      also have "\<dots> = u * (v1 + u1 - u1) + v * (v2 + u2 - u2)"
  1.1683 -        using as(3) obt1(3) obt2(3) by (auto simp add: field_simps)
  1.1684 -      also have "\<dots> = u * v1 + v * v2"
  1.1685 -        by simp
  1.1686 -      finally have **:"1 - (u * u1 + v * u2) = u * v1 + v * v2" by auto
  1.1687 -      have "0 \<le> u * v1 + v * v2" "0 \<le> u * v1" "0 \<le> u * v1 + v * v2" "0 \<le> v * v2"
  1.1688 -        using as(1,2) obt1(1,2) obt2(1,2) by auto
  1.1689 -      then show ?thesis
  1.1690 -        unfolding obt1(5) obt2(5)
  1.1691 -        unfolding * and **
  1.1692 -        using False
  1.1693 -        apply (rule_tac
  1.1694 -          x = "((u * v1) / (u * v1 + v * v2)) *\<^sub>R b1 + ((v * v2) / (u * v1 + v * v2)) *\<^sub>R b2" in bexI)
  1.1695 -        defer
  1.1696 -        apply (rule convexD [OF convex_convex_hull])
  1.1697 -        using obt1(4) obt2(4)
  1.1698 -        unfolding add_divide_distrib[symmetric] and zero_le_divide_iff
  1.1699 -        apply (auto simp add: scaleR_left_distrib scaleR_right_distrib)
  1.1700 -        done
  1.1701 -    qed
  1.1702 -    have u1: "u1 \<le> 1"
  1.1703 -      unfolding obt1(3)[symmetric] and not_le using obt1(2) by auto
  1.1704 -    have u2: "u2 \<le> 1"
  1.1705 -      unfolding obt2(3)[symmetric] and not_le using obt2(2) by auto
  1.1706 -    have "u1 * u + u2 * v \<le> max u1 u2 * u + max u1 u2 * v"
  1.1707 -      apply (rule add_mono)
  1.1708 -      apply (rule_tac [!] mult_right_mono)
  1.1709 -      using as(1,2) obt1(1,2) obt2(1,2)
  1.1710 -      apply auto
  1.1711 -      done
  1.1712 -    also have "\<dots> \<le> 1"
  1.1713 -      unfolding distrib_left[symmetric] and as(3) using u1 u2 by auto
  1.1714 -    finally show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  1.1715 -      unfolding mem_Collect_eq
  1.1716 -      apply (rule_tac x="u * u1 + v * u2" in exI)
  1.1717 -      apply (rule conjI)
  1.1718 -      defer
  1.1719 -      apply (rule_tac x="1 - u * u1 - v * u2" in exI)
  1.1720 -      unfolding Bex_def
  1.1721 -      using as(1,2) obt1(1,2) obt2(1,2) **
  1.1722 -      apply (auto simp add: algebra_simps)
  1.1723 -      done
  1.1724 -  qed
  1.1725 -qed
  1.1726 -
  1.1727 -
  1.1728 -subsubsection \<open>Explicit expression for convex hull\<close>
  1.1729 -
  1.1730 -lemma convex_hull_indexed:
  1.1731 -  fixes s :: "'a::real_vector set"
  1.1732 -  shows "convex hull s =
  1.1733 -    {y. \<exists>k u x.
  1.1734 -      (\<forall>i\<in>{1::nat .. k}. 0 \<le> u i \<and> x i \<in> s) \<and>
  1.1735 -      (setsum u {1..k} = 1) \<and> (setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} = y)}"
  1.1736 -  (is "?xyz = ?hull")
  1.1737 -  apply (rule hull_unique)
  1.1738 -  apply rule
  1.1739 -  defer
  1.1740 -  apply (rule convexI)
  1.1741 -proof -
  1.1742 -  fix x
  1.1743 -  assume "x\<in>s"
  1.1744 -  then show "x \<in> ?hull"
  1.1745 -    unfolding mem_Collect_eq
  1.1746 -    apply (rule_tac x=1 in exI, rule_tac x="\<lambda>x. 1" in exI)
  1.1747 -    apply auto
  1.1748 -    done
  1.1749 -next
  1.1750 -  fix t
  1.1751 -  assume as: "s \<subseteq> t" "convex t"
  1.1752 -  show "?hull \<subseteq> t"
  1.1753 -    apply rule
  1.1754 -    unfolding mem_Collect_eq
  1.1755 -    apply (elim exE conjE)
  1.1756 -  proof -
  1.1757 -    fix x k u y
  1.1758 -    assume assm:
  1.1759 -      "\<forall>i\<in>{1::nat..k}. 0 \<le> u i \<and> y i \<in> s"
  1.1760 -      "setsum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R y i) = x"
  1.1761 -    show "x\<in>t"
  1.1762 -      unfolding assm(3) [symmetric]
  1.1763 -      apply (rule as(2)[unfolded convex, rule_format])
  1.1764 -      using assm(1,2) as(1) apply auto
  1.1765 -      done
  1.1766 -  qed
  1.1767 -next
  1.1768 -  fix x y u v
  1.1769 -  assume uv: "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
  1.1770 -  assume xy: "x \<in> ?hull" "y \<in> ?hull"
  1.1771 -  from xy obtain k1 u1 x1 where
  1.1772 -    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"
  1.1773 -    by auto
  1.1774 -  from xy obtain k2 u2 x2 where
  1.1775 -    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"
  1.1776 -    by auto
  1.1777 -  have *: "\<And>P (x1::'a) x2 s1 s2 i.
  1.1778 -    (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)"
  1.1779 -    "{1..k1 + k2} \<inter> {1..k1} = {1..k1}" "{1..k1 + k2} \<inter> - {1..k1} = (\<lambda>i. i + k1) ` {1..k2}"
  1.1780 -    prefer 3
  1.1781 -    apply (rule, rule)
  1.1782 -    unfolding image_iff
  1.1783 -    apply (rule_tac x = "x - k1" in bexI)
  1.1784 -    apply (auto simp add: not_le)
  1.1785 -    done
  1.1786 -  have inj: "inj_on (\<lambda>i. i + k1) {1..k2}"
  1.1787 -    unfolding inj_on_def by auto
  1.1788 -  show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  1.1789 -    apply rule
  1.1790 -    apply (rule_tac x="k1 + k2" in exI)
  1.1791 -    apply (rule_tac x="\<lambda>i. if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)" in exI)
  1.1792 -    apply (rule_tac x="\<lambda>i. if i \<in> {1..k1} then x1 i else x2 (i - k1)" in exI)
  1.1793 -    apply (rule, rule)
  1.1794 -    defer
  1.1795 -    apply rule
  1.1796 -    unfolding * and setsum.If_cases[OF finite_atLeastAtMost[of 1 "k1 + k2"]] and
  1.1797 -      setsum.reindex[OF inj] and o_def Collect_mem_eq
  1.1798 -    unfolding scaleR_scaleR[symmetric] scaleR_right.setsum [symmetric] setsum_right_distrib[symmetric]
  1.1799 -  proof -
  1.1800 -    fix i
  1.1801 -    assume i: "i \<in> {1..k1+k2}"
  1.1802 -    show "0 \<le> (if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)) \<and>
  1.1803 -      (if i \<in> {1..k1} then x1 i else x2 (i - k1)) \<in> s"
  1.1804 -    proof (cases "i\<in>{1..k1}")
  1.1805 -      case True
  1.1806 -      then show ?thesis
  1.1807 -        using uv(1) x(1)[THEN bspec[where x=i]] by auto
  1.1808 -    next
  1.1809 -      case False
  1.1810 -      define j where "j = i - k1"
  1.1811 -      from i False have "j \<in> {1..k2}"
  1.1812 -        unfolding j_def by auto
  1.1813 -      then show ?thesis
  1.1814 -        using False uv(2) y(1)[THEN bspec[where x=j]]
  1.1815 -        by (auto simp: j_def[symmetric])
  1.1816 -    qed
  1.1817 -  qed (auto simp add: not_le x(2,3) y(2,3) uv(3))
  1.1818 -qed
  1.1819 -
  1.1820 -lemma convex_hull_finite:
  1.1821 -  fixes s :: "'a::real_vector set"
  1.1822 -  assumes "finite s"
  1.1823 -  shows "convex hull s = {y. \<exists>u. (\<forall>x\<in>s. 0 \<le> u x) \<and>
  1.1824 -    setsum u s = 1 \<and> setsum (\<lambda>x. u x *\<^sub>R x) s = y}"
  1.1825 -  (is "?HULL = ?set")
  1.1826 -proof (rule hull_unique, auto simp add: convex_def[of ?set])
  1.1827 -  fix x
  1.1828 -  assume "x \<in> s"
  1.1829 -  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"
  1.1830 -    apply (rule_tac x="\<lambda>y. if x=y then 1 else 0" in exI)
  1.1831 -    apply auto
  1.1832 -    unfolding setsum.delta'[OF assms] and setsum_delta''[OF assms]
  1.1833 -    apply auto
  1.1834 -    done
  1.1835 -next
  1.1836 -  fix u v :: real
  1.1837 -  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  1.1838 -  fix ux assume ux: "\<forall>x\<in>s. 0 \<le> ux x" "setsum ux s = (1::real)"
  1.1839 -  fix uy assume uy: "\<forall>x\<in>s. 0 \<le> uy x" "setsum uy s = (1::real)"
  1.1840 -  {
  1.1841 -    fix x
  1.1842 -    assume "x\<in>s"
  1.1843 -    then have "0 \<le> u * ux x + v * uy x"
  1.1844 -      using ux(1)[THEN bspec[where x=x]] uy(1)[THEN bspec[where x=x]] and uv(1,2)
  1.1845 -      by auto
  1.1846 -  }
  1.1847 -  moreover
  1.1848 -  have "(\<Sum>x\<in>s. u * ux x + v * uy x) = 1"
  1.1849 -    unfolding setsum.distrib and setsum_right_distrib[symmetric] and ux(2) uy(2)
  1.1850 -    using uv(3) by auto
  1.1851 -  moreover
  1.1852 -  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)"
  1.1853 -    unfolding scaleR_left_distrib and setsum.distrib and scaleR_scaleR[symmetric]
  1.1854 -      and scaleR_right.setsum [symmetric]
  1.1855 -    by auto
  1.1856 -  ultimately
  1.1857 -  show "\<exists>uc. (\<forall>x\<in>s. 0 \<le> uc x) \<and> setsum uc s = 1 \<and>
  1.1858 -      (\<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)"
  1.1859 -    apply (rule_tac x="\<lambda>x. u * ux x + v * uy x" in exI)
  1.1860 -    apply auto
  1.1861 -    done
  1.1862 -next
  1.1863 -  fix t
  1.1864 -  assume t: "s \<subseteq> t" "convex t"
  1.1865 -  fix u
  1.1866 -  assume u: "\<forall>x\<in>s. 0 \<le> u x" "setsum u s = (1::real)"
  1.1867 -  then show "(\<Sum>x\<in>s. u x *\<^sub>R x) \<in> t"
  1.1868 -    using t(2)[unfolded convex_explicit, THEN spec[where x=s], THEN spec[where x=u]]
  1.1869 -    using assms and t(1) by auto
  1.1870 -qed
  1.1871 -
  1.1872 -
  1.1873 -subsubsection \<open>Another formulation from Lars Schewe\<close>
  1.1874 -
  1.1875 -lemma convex_hull_explicit:
  1.1876 -  fixes p :: "'a::real_vector set"
  1.1877 -  shows "convex hull p =
  1.1878 -    {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}"
  1.1879 -  (is "?lhs = ?rhs")
  1.1880 -proof -
  1.1881 -  {
  1.1882 -    fix x
  1.1883 -    assume "x\<in>?lhs"
  1.1884 -    then obtain k u y where
  1.1885 -        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"
  1.1886 -      unfolding convex_hull_indexed by auto
  1.1887 -
  1.1888 -    have fin: "finite {1..k}" by auto
  1.1889 -    have fin': "\<And>v. finite {i \<in> {1..k}. y i = v}" by auto
  1.1890 -    {
  1.1891 -      fix j
  1.1892 -      assume "j\<in>{1..k}"
  1.1893 -      then have "y j \<in> p" "0 \<le> setsum u {i. Suc 0 \<le> i \<and> i \<le> k \<and> y i = y j}"
  1.1894 -        using obt(1)[THEN bspec[where x=j]] and obt(2)
  1.1895 -        apply simp
  1.1896 -        apply (rule setsum_nonneg)
  1.1897 -        using obt(1)
  1.1898 -        apply auto
  1.1899 -        done
  1.1900 -    }
  1.1901 -    moreover
  1.1902 -    have "(\<Sum>v\<in>y ` {1..k}. setsum u {i \<in> {1..k}. y i = v}) = 1"
  1.1903 -      unfolding setsum_image_gen[OF fin, symmetric] using obt(2) by auto
  1.1904 -    moreover have "(\<Sum>v\<in>y ` {1..k}. setsum u {i \<in> {1..k}. y i = v} *\<^sub>R v) = x"
  1.1905 -      using setsum_image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
  1.1906 -      unfolding scaleR_left.setsum using obt(3) by auto
  1.1907 -    ultimately
  1.1908 -    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"
  1.1909 -      apply (rule_tac x="y ` {1..k}" in exI)
  1.1910 -      apply (rule_tac x="\<lambda>v. setsum u {i\<in>{1..k}. y i = v}" in exI)
  1.1911 -      apply auto
  1.1912 -      done
  1.1913 -    then have "x\<in>?rhs" by auto
  1.1914 -  }
  1.1915 -  moreover
  1.1916 -  {
  1.1917 -    fix y
  1.1918 -    assume "y\<in>?rhs"
  1.1919 -    then obtain s u where
  1.1920 -      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"
  1.1921 -      by auto
  1.1922 -
  1.1923 -    obtain f where f: "inj_on f {1..card s}" "f ` {1..card s} = s"
  1.1924 -      using ex_bij_betw_nat_finite_1[OF obt(1)] unfolding bij_betw_def by auto
  1.1925 -
  1.1926 -    {
  1.1927 -      fix i :: nat
  1.1928 -      assume "i\<in>{1..card s}"
  1.1929 -      then have "f i \<in> s"
  1.1930 -        apply (subst f(2)[symmetric])
  1.1931 -        apply auto
  1.1932 -        done
  1.1933 -      then have "0 \<le> u (f i)" "f i \<in> p" using obt(2,3) by auto
  1.1934 -    }
  1.1935 -    moreover have *: "finite {1..card s}" by auto
  1.1936 -    {
  1.1937 -      fix y
  1.1938 -      assume "y\<in>s"
  1.1939 -      then obtain i where "i\<in>{1..card s}" "f i = y"
  1.1940 -        using f using image_iff[of y f "{1..card s}"]
  1.1941 -        by auto
  1.1942 -      then have "{x. Suc 0 \<le> x \<and> x \<le> card s \<and> f x = y} = {i}"
  1.1943 -        apply auto
  1.1944 -        using f(1)[unfolded inj_on_def]
  1.1945 -        apply(erule_tac x=x in ballE)
  1.1946 -        apply auto
  1.1947 -        done
  1.1948 -      then have "card {x. Suc 0 \<le> x \<and> x \<le> card s \<and> f x = y} = 1" by auto
  1.1949 -      then have "(\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x)) = u y"
  1.1950 -          "(\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x) *\<^sub>R f x) = u y *\<^sub>R y"
  1.1951 -        by (auto simp add: setsum_constant_scaleR)
  1.1952 -    }
  1.1953 -    then have "(\<Sum>x = 1..card s. u (f x)) = 1" "(\<Sum>i = 1..card s. u (f i) *\<^sub>R f i) = y"
  1.1954 -      unfolding setsum_image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
  1.1955 -        and setsum_image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
  1.1956 -      unfolding f
  1.1957 -      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"]
  1.1958 -      using setsum.cong [of s s "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card s}. f x = y}. u (f x))" u]
  1.1959 -      unfolding obt(4,5)
  1.1960 -      by auto
  1.1961 -    ultimately
  1.1962 -    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>
  1.1963 -        (\<Sum>i::nat = 1..k. u i *\<^sub>R x i) = y"
  1.1964 -      apply (rule_tac x="card s" in exI)
  1.1965 -      apply (rule_tac x="u \<circ> f" in exI)
  1.1966 -      apply (rule_tac x=f in exI)
  1.1967 -      apply fastforce
  1.1968 -      done
  1.1969 -    then have "y \<in> ?lhs"
  1.1970 -      unfolding convex_hull_indexed by auto
  1.1971 -  }
  1.1972 -  ultimately show ?thesis
  1.1973 -    unfolding set_eq_iff by blast
  1.1974 -qed
  1.1975 -
  1.1976 -
  1.1977 -subsubsection \<open>A stepping theorem for that expansion\<close>
  1.1978 -
  1.1979 -lemma convex_hull_finite_step:
  1.1980 -  fixes s :: "'a::real_vector set"
  1.1981 -  assumes "finite s"
  1.1982 -  shows
  1.1983 -    "(\<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)
  1.1984 -      \<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)"
  1.1985 -  (is "?lhs = ?rhs")
  1.1986 -proof (rule, case_tac[!] "a\<in>s")
  1.1987 -  assume "a \<in> s"
  1.1988 -  then have *: "insert a s = s" by auto
  1.1989 -  assume ?lhs
  1.1990 -  then show ?rhs
  1.1991 -    unfolding *
  1.1992 -    apply (rule_tac x=0 in exI)
  1.1993 -    apply auto
  1.1994 -    done
  1.1995 -next
  1.1996 -  assume ?lhs
  1.1997 -  then obtain u where
  1.1998 -      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"
  1.1999 -    by auto
  1.2000 -  assume "a \<notin> s"
  1.2001 -  then show ?rhs
  1.2002 -    apply (rule_tac x="u a" in exI)
  1.2003 -    using u(1)[THEN bspec[where x=a]]
  1.2004 -    apply simp
  1.2005 -    apply (rule_tac x=u in exI)
  1.2006 -    using u[unfolded setsum_clauses(2)[OF assms]] and \<open>a\<notin>s\<close>
  1.2007 -    apply auto
  1.2008 -    done
  1.2009 -next
  1.2010 -  assume "a \<in> s"
  1.2011 -  then have *: "insert a s = s" by auto
  1.2012 -  have fin: "finite (insert a s)" using assms by auto
  1.2013 -  assume ?rhs
  1.2014 -  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"
  1.2015 -    by auto
  1.2016 -  show ?lhs
  1.2017 -    apply (rule_tac x = "\<lambda>x. (if a = x then v else 0) + u x" in exI)
  1.2018 -    unfolding scaleR_left_distrib and setsum.distrib and setsum_delta''[OF fin] and setsum.delta'[OF fin]
  1.2019 -    unfolding setsum_clauses(2)[OF assms]
  1.2020 -    using uv and uv(2)[THEN bspec[where x=a]] and \<open>a\<in>s\<close>
  1.2021 -    apply auto
  1.2022 -    done
  1.2023 -next
  1.2024 -  assume ?rhs
  1.2025 -  then obtain v u where
  1.2026 -    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"
  1.2027 -    by auto
  1.2028 -  moreover
  1.2029 -  assume "a \<notin> s"
  1.2030 -  moreover
  1.2031 -  have "(\<Sum>x\<in>s. if a = x then v else u x) = setsum u s"
  1.2032 -    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)"
  1.2033 -    apply (rule_tac setsum.cong) apply rule
  1.2034 -    defer
  1.2035 -    apply (rule_tac setsum.cong) apply rule
  1.2036 -    using \<open>a \<notin> s\<close>
  1.2037 -    apply auto
  1.2038 -    done
  1.2039 -  ultimately show ?lhs
  1.2040 -    apply (rule_tac x="\<lambda>x. if a = x then v else u x" in exI)
  1.2041 -    unfolding setsum_clauses(2)[OF assms]
  1.2042 -    apply auto
  1.2043 -    done
  1.2044 -qed
  1.2045 -
  1.2046 -
  1.2047 -subsubsection \<open>Hence some special cases\<close>
  1.2048 -
  1.2049 -lemma convex_hull_2:
  1.2050 -  "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}"
  1.2051 -proof -
  1.2052 -  have *: "\<And>u. (\<forall>x\<in>{a, b}. 0 \<le> u x) \<longleftrightarrow> 0 \<le> u a \<and> 0 \<le> u b"
  1.2053 -    by auto
  1.2054 -  have **: "finite {b}" by auto
  1.2055 -  show ?thesis
  1.2056 -    apply (simp add: convex_hull_finite)
  1.2057 -    unfolding convex_hull_finite_step[OF **, of a 1, unfolded * conj_assoc]
  1.2058 -    apply auto
  1.2059 -    apply (rule_tac x=v in exI)
  1.2060 -    apply (rule_tac x="1 - v" in exI)
  1.2061 -    apply simp
  1.2062 -    apply (rule_tac x=u in exI)
  1.2063 -    apply simp
  1.2064 -    apply (rule_tac x="\<lambda>x. v" in exI)
  1.2065 -    apply simp
  1.2066 -    done
  1.2067 -qed
  1.2068 -
  1.2069 -lemma convex_hull_2_alt: "convex hull {a,b} = {a + u *\<^sub>R (b - a) | u.  0 \<le> u \<and> u \<le> 1}"
  1.2070 -  unfolding convex_hull_2
  1.2071 -proof (rule Collect_cong)
  1.2072 -  have *: "\<And>x y ::real. x + y = 1 \<longleftrightarrow> x = 1 - y"
  1.2073 -    by auto
  1.2074 -  fix x
  1.2075 -  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>
  1.2076 -    (\<exists>u. x = a + u *\<^sub>R (b - a) \<and> 0 \<le> u \<and> u \<le> 1)"
  1.2077 -    unfolding *
  1.2078 -    apply auto
  1.2079 -    apply (rule_tac[!] x=u in exI)
  1.2080 -    apply (auto simp add: algebra_simps)
  1.2081 -    done
  1.2082 -qed
  1.2083 -
  1.2084 -lemma convex_hull_3:
  1.2085 -  "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}"
  1.2086 -proof -
  1.2087 -  have fin: "finite {a,b,c}" "finite {b,c}" "finite {c}"
  1.2088 -    by auto
  1.2089 -  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  1.2090 -    by (auto simp add: field_simps)
  1.2091 -  show ?thesis
  1.2092 -    unfolding convex_hull_finite[OF fin(1)] and convex_hull_finite_step[OF fin(2)] and *
  1.2093 -    unfolding convex_hull_finite_step[OF fin(3)]
  1.2094 -    apply (rule Collect_cong)
  1.2095 -    apply simp
  1.2096 -    apply auto
  1.2097 -    apply (rule_tac x=va in exI)
  1.2098 -    apply (rule_tac x="u c" in exI)
  1.2099 -    apply simp
  1.2100 -    apply (rule_tac x="1 - v - w" in exI)
  1.2101 -    apply simp
  1.2102 -    apply (rule_tac x=v in exI)
  1.2103 -    apply simp
  1.2104 -    apply (rule_tac x="\<lambda>x. w" in exI)
  1.2105 -    apply simp
  1.2106 -    done
  1.2107 -qed
  1.2108 -
  1.2109 -lemma convex_hull_3_alt:
  1.2110 -  "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}"
  1.2111 -proof -
  1.2112 -  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  1.2113 -    by auto
  1.2114 -  show ?thesis
  1.2115 -    unfolding convex_hull_3
  1.2116 -    apply (auto simp add: *)
  1.2117 -    apply (rule_tac x=v in exI)
  1.2118 -    apply (rule_tac x=w in exI)
  1.2119 -    apply (simp add: algebra_simps)
  1.2120 -    apply (rule_tac x=u in exI)
  1.2121 -    apply (rule_tac x=v in exI)
  1.2122 -    apply (simp add: algebra_simps)
  1.2123 -    done
  1.2124 -qed
  1.2125 -
  1.2126 -
  1.2127 -subsection \<open>Relations among closure notions and corresponding hulls\<close>
  1.2128 -
  1.2129 -lemma affine_imp_convex: "affine s \<Longrightarrow> convex s"
  1.2130 -  unfolding affine_def convex_def by auto
  1.2131 -
  1.2132 -lemma subspace_imp_convex: "subspace s \<Longrightarrow> convex s"
  1.2133 -  using subspace_imp_affine affine_imp_convex by auto
  1.2134 -
  1.2135 -lemma affine_hull_subset_span: "(affine hull s) \<subseteq> (span s)"
  1.2136 -  by (metis hull_minimal span_inc subspace_imp_affine subspace_span)
  1.2137 -
  1.2138 -lemma convex_hull_subset_span: "(convex hull s) \<subseteq> (span s)"
  1.2139 -  by (metis hull_minimal span_inc subspace_imp_convex subspace_span)
  1.2140 -
  1.2141 -lemma convex_hull_subset_affine_hull: "(convex hull s) \<subseteq> (affine hull s)"
  1.2142 -  by (metis affine_affine_hull affine_imp_convex hull_minimal hull_subset)
  1.2143 -
  1.2144 -lemma affine_dependent_imp_dependent: "affine_dependent s \<Longrightarrow> dependent s"
  1.2145 -  unfolding affine_dependent_def dependent_def
  1.2146 -  using affine_hull_subset_span by auto
  1.2147 -
  1.2148 -lemma dependent_imp_affine_dependent:
  1.2149 -  assumes "dependent {x - a| x . x \<in> s}"
  1.2150 -    and "a \<notin> s"
  1.2151 -  shows "affine_dependent (insert a s)"
  1.2152 -proof -
  1.2153 -  from assms(1)[unfolded dependent_explicit] obtain S u v
  1.2154 -    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"
  1.2155 -    by auto
  1.2156 -  define t where "t = (\<lambda>x. x + a) ` S"
  1.2157 -
  1.2158 -  have inj: "inj_on (\<lambda>x. x + a) S"
  1.2159 -    unfolding inj_on_def by auto
  1.2160 -  have "0 \<notin> S"
  1.2161 -    using obt(2) assms(2) unfolding subset_eq by auto
  1.2162 -  have fin: "finite t" and "t \<subseteq> s"
  1.2163 -    unfolding t_def using obt(1,2) by auto
  1.2164 -  then have "finite (insert a t)" and "insert a t \<subseteq> insert a s"
  1.2165 -    by auto
  1.2166 -  moreover have *: "\<And>P Q. (\<Sum>x\<in>t. (if x = a then P x else Q x)) = (\<Sum>x\<in>t. Q x)"
  1.2167 -    apply (rule setsum.cong)
  1.2168 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2169 -    apply auto
  1.2170 -    done
  1.2171 -  have "(\<Sum>x\<in>insert a t. if x = a then - (\<Sum>x\<in>t. u (x - a)) else u (x - a)) = 0"
  1.2172 -    unfolding setsum_clauses(2)[OF fin]
  1.2173 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2174 -    apply auto
  1.2175 -    unfolding *
  1.2176 -    apply auto
  1.2177 -    done
  1.2178 -  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"
  1.2179 -    apply (rule_tac x="v + a" in bexI)
  1.2180 -    using obt(3,4) and \<open>0\<notin>S\<close>
  1.2181 -    unfolding t_def
  1.2182 -    apply auto
  1.2183 -    done
  1.2184 -  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)"
  1.2185 -    apply (rule setsum.cong)
  1.2186 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2187 -    apply auto
  1.2188 -    done
  1.2189 -  have "(\<Sum>x\<in>t. u (x - a)) *\<^sub>R a = (\<Sum>v\<in>t. u (v - a) *\<^sub>R v)"
  1.2190 -    unfolding scaleR_left.setsum
  1.2191 -    unfolding t_def and setsum.reindex[OF inj] and o_def
  1.2192 -    using obt(5)
  1.2193 -    by (auto simp add: setsum.distrib scaleR_right_distrib)
  1.2194 -  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"
  1.2195 -    unfolding setsum_clauses(2)[OF fin]
  1.2196 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2197 -    by (auto simp add: *)
  1.2198 -  ultimately show ?thesis
  1.2199 -    unfolding affine_dependent_explicit
  1.2200 -    apply (rule_tac x="insert a t" in exI)
  1.2201 -    apply auto
  1.2202 -    done
  1.2203 -qed
  1.2204 -
  1.2205 -lemma convex_cone:
  1.2206 -  "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)"
  1.2207 -  (is "?lhs = ?rhs")
  1.2208 -proof -
  1.2209 -  {
  1.2210 -    fix x y
  1.2211 -    assume "x\<in>s" "y\<in>s" and ?lhs
  1.2212 -    then have "2 *\<^sub>R x \<in>s" "2 *\<^sub>R y \<in> s"
  1.2213 -      unfolding cone_def by auto
  1.2214 -    then have "x + y \<in> s"
  1.2215 -      using \<open>?lhs\<close>[unfolded convex_def, THEN conjunct1]
  1.2216 -      apply (erule_tac x="2*\<^sub>R x" in ballE)
  1.2217 -      apply (erule_tac x="2*\<^sub>R y" in ballE)
  1.2218 -      apply (erule_tac x="1/2" in allE)
  1.2219 -      apply simp
  1.2220 -      apply (erule_tac x="1/2" in allE)
  1.2221 -      apply auto
  1.2222 -      done
  1.2223 -  }
  1.2224 -  then show ?thesis
  1.2225 -    unfolding convex_def cone_def by blast
  1.2226 -qed
  1.2227 -
  1.2228 -lemma affine_dependent_biggerset:
  1.2229 -  fixes s :: "'a::euclidean_space set"
  1.2230 -  assumes "finite s" "card s \<ge> DIM('a) + 2"
  1.2231 -  shows "affine_dependent s"
  1.2232 -proof -
  1.2233 -  have "s \<noteq> {}" using assms by auto
  1.2234 -  then obtain a where "a\<in>s" by auto
  1.2235 -  have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  1.2236 -    by auto
  1.2237 -  have "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  1.2238 -    unfolding *
  1.2239 -    apply (rule card_image)
  1.2240 -    unfolding inj_on_def
  1.2241 -    apply auto
  1.2242 -    done
  1.2243 -  also have "\<dots> > DIM('a)" using assms(2)
  1.2244 -    unfolding card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>] by auto
  1.2245 -  finally show ?thesis
  1.2246 -    apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  1.2247 -    apply (rule dependent_imp_affine_dependent)
  1.2248 -    apply (rule dependent_biggerset)
  1.2249 -    apply auto
  1.2250 -    done
  1.2251 -qed
  1.2252 -
  1.2253 -lemma affine_dependent_biggerset_general:
  1.2254 -  assumes "finite (s :: 'a::euclidean_space set)"
  1.2255 -    and "card s \<ge> dim s + 2"
  1.2256 -  shows "affine_dependent s"
  1.2257 -proof -
  1.2258 -  from assms(2) have "s \<noteq> {}" by auto
  1.2259 -  then obtain a where "a\<in>s" by auto
  1.2260 -  have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  1.2261 -    by auto
  1.2262 -  have **: "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  1.2263 -    unfolding *
  1.2264 -    apply (rule card_image)
  1.2265 -    unfolding inj_on_def
  1.2266 -    apply auto
  1.2267 -    done
  1.2268 -  have "dim {x - a |x. x \<in> s - {a}} \<le> dim s"
  1.2269 -    apply (rule subset_le_dim)
  1.2270 -    unfolding subset_eq
  1.2271 -    using \<open>a\<in>s\<close>
  1.2272 -    apply (auto simp add:span_superset span_sub)
  1.2273 -    done
  1.2274 -  also have "\<dots> < dim s + 1" by auto
  1.2275 -  also have "\<dots> \<le> card (s - {a})"
  1.2276 -    using assms
  1.2277 -    using card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>]
  1.2278 -    by auto
  1.2279 -  finally show ?thesis
  1.2280 -    apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  1.2281 -    apply (rule dependent_imp_affine_dependent)
  1.2282 -    apply (rule dependent_biggerset_general)
  1.2283 -    unfolding **
  1.2284 -    apply auto
  1.2285 -    done
  1.2286 -qed
  1.2287 -
  1.2288 -
  1.2289 -subsection \<open>Some Properties of Affine Dependent Sets\<close>
  1.2290 -
  1.2291 -lemma affine_independent_0: "\<not> affine_dependent {}"
  1.2292 -  by (simp add: affine_dependent_def)
  1.2293 -
  1.2294 -lemma affine_independent_1: "\<not> affine_dependent {a}"
  1.2295 -  by (simp add: affine_dependent_def)
  1.2296 -
  1.2297 -lemma affine_independent_2: "\<not> affine_dependent {a,b}"
  1.2298 -  by (simp add: affine_dependent_def insert_Diff_if hull_same)
  1.2299 -
  1.2300 -lemma affine_hull_translation: "affine hull ((\<lambda>x. a + x) `  S) = (\<lambda>x. a + x) ` (affine hull S)"
  1.2301 -proof -
  1.2302 -  have "affine ((\<lambda>x. a + x) ` (affine hull S))"
  1.2303 -    using affine_translation affine_affine_hull by blast
  1.2304 -  moreover have "(\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  1.2305 -    using hull_subset[of S] by auto
  1.2306 -  ultimately have h1: "affine hull ((\<lambda>x. a + x) `  S) \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  1.2307 -    by (metis hull_minimal)
  1.2308 -  have "affine((\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)))"
  1.2309 -    using affine_translation affine_affine_hull by blast
  1.2310 -  moreover have "(\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S))"
  1.2311 -    using hull_subset[of "(\<lambda>x. a + x) `  S"] by auto
  1.2312 -  moreover have "S = (\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S"
  1.2313 -    using translation_assoc[of "-a" a] by auto
  1.2314 -  ultimately have "(\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)) >= (affine hull S)"
  1.2315 -    by (metis hull_minimal)
  1.2316 -  then have "affine hull ((\<lambda>x. a + x) ` S) >= (\<lambda>x. a + x) ` (affine hull S)"
  1.2317 -    by auto
  1.2318 -  then show ?thesis using h1 by auto
  1.2319 -qed
  1.2320 -
  1.2321 -lemma affine_dependent_translation:
  1.2322 -  assumes "affine_dependent S"
  1.2323 -  shows "affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2324 -proof -
  1.2325 -  obtain x where x: "x \<in> S \<and> x \<in> affine hull (S - {x})"
  1.2326 -    using assms affine_dependent_def by auto
  1.2327 -  have "op + a ` (S - {x}) = op + a ` S - {a + x}"
  1.2328 -    by auto
  1.2329 -  then have "a + x \<in> affine hull ((\<lambda>x. a + x) ` S - {a + x})"
  1.2330 -    using affine_hull_translation[of a "S - {x}"] x by auto
  1.2331 -  moreover have "a + x \<in> (\<lambda>x. a + x) ` S"
  1.2332 -    using x by auto
  1.2333 -  ultimately show ?thesis
  1.2334 -    unfolding affine_dependent_def by auto
  1.2335 -qed
  1.2336 -
  1.2337 -lemma affine_dependent_translation_eq:
  1.2338 -  "affine_dependent S \<longleftrightarrow> affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2339 -proof -
  1.2340 -  {
  1.2341 -    assume "affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2342 -    then have "affine_dependent S"
  1.2343 -      using affine_dependent_translation[of "((\<lambda>x. a + x) ` S)" "-a"] translation_assoc[of "-a" a]
  1.2344 -      by auto
  1.2345 -  }
  1.2346 -  then show ?thesis
  1.2347 -    using affine_dependent_translation by auto
  1.2348 -qed
  1.2349 -
  1.2350 -lemma affine_hull_0_dependent:
  1.2351 -  assumes "0 \<in> affine hull S"
  1.2352 -  shows "dependent S"
  1.2353 -proof -
  1.2354 -  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"
  1.2355 -    using assms affine_hull_explicit[of S] by auto
  1.2356 -  then have "\<exists>v\<in>s. u v \<noteq> 0"
  1.2357 -    using setsum_not_0[of "u" "s"] by auto
  1.2358 -  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)"
  1.2359 -    using s_u by auto
  1.2360 -  then show ?thesis
  1.2361 -    unfolding dependent_explicit[of S] by auto
  1.2362 -qed
  1.2363 -
  1.2364 -lemma affine_dependent_imp_dependent2:
  1.2365 -  assumes "affine_dependent (insert 0 S)"
  1.2366 -  shows "dependent S"
  1.2367 -proof -
  1.2368 -  obtain x where x: "x \<in> insert 0 S \<and> x \<in> affine hull (insert 0 S - {x})"
  1.2369 -    using affine_dependent_def[of "(insert 0 S)"] assms by blast
  1.2370 -  then have "x \<in> span (insert 0 S - {x})"
  1.2371 -    using affine_hull_subset_span by auto
  1.2372 -  moreover have "span (insert 0 S - {x}) = span (S - {x})"
  1.2373 -    using insert_Diff_if[of "0" S "{x}"] span_insert_0[of "S-{x}"] by auto
  1.2374 -  ultimately have "x \<in> span (S - {x})" by auto
  1.2375 -  then have "x \<noteq> 0 \<Longrightarrow> dependent S"
  1.2376 -    using x dependent_def by auto
  1.2377 -  moreover
  1.2378 -  {
  1.2379 -    assume "x = 0"
  1.2380 -    then have "0 \<in> affine hull S"
  1.2381 -      using x hull_mono[of "S - {0}" S] by auto
  1.2382 -    then have "dependent S"
  1.2383 -      using affine_hull_0_dependent by auto
  1.2384 -  }
  1.2385 -  ultimately show ?thesis by auto
  1.2386 -qed
  1.2387 -
  1.2388 -lemma affine_dependent_iff_dependent:
  1.2389 -  assumes "a \<notin> S"
  1.2390 -  shows "affine_dependent (insert a S) \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` S)"
  1.2391 -proof -
  1.2392 -  have "(op + (- a) ` S) = {x - a| x . x : S}" by auto
  1.2393 -  then show ?thesis
  1.2394 -    using affine_dependent_translation_eq[of "(insert a S)" "-a"]
  1.2395 -      affine_dependent_imp_dependent2 assms
  1.2396 -      dependent_imp_affine_dependent[of a S]
  1.2397 -    by (auto simp del: uminus_add_conv_diff)
  1.2398 -qed
  1.2399 -
  1.2400 -lemma affine_dependent_iff_dependent2:
  1.2401 -  assumes "a \<in> S"
  1.2402 -  shows "affine_dependent S \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` (S-{a}))"
  1.2403 -proof -
  1.2404 -  have "insert a (S - {a}) = S"
  1.2405 -    using assms by auto
  1.2406 -  then show ?thesis
  1.2407 -    using assms affine_dependent_iff_dependent[of a "S-{a}"] by auto
  1.2408 -qed
  1.2409 -
  1.2410 -lemma affine_hull_insert_span_gen:
  1.2411 -  "affine hull (insert a s) = (\<lambda>x. a + x) ` span ((\<lambda>x. - a + x) ` s)"
  1.2412 -proof -
  1.2413 -  have h1: "{x - a |x. x \<in> s} = ((\<lambda>x. -a+x) ` s)"
  1.2414 -    by auto
  1.2415 -  {
  1.2416 -    assume "a \<notin> s"
  1.2417 -    then have ?thesis
  1.2418 -      using affine_hull_insert_span[of a s] h1 by auto
  1.2419 -  }
  1.2420 -  moreover
  1.2421 -  {
  1.2422 -    assume a1: "a \<in> s"
  1.2423 -    have "\<exists>x. x \<in> s \<and> -a+x=0"
  1.2424 -      apply (rule exI[of _ a])
  1.2425 -      using a1
  1.2426 -      apply auto
  1.2427 -      done
  1.2428 -    then have "insert 0 ((\<lambda>x. -a+x) ` (s - {a})) = (\<lambda>x. -a+x) ` s"
  1.2429 -      by auto
  1.2430 -    then have "span ((\<lambda>x. -a+x) ` (s - {a}))=span ((\<lambda>x. -a+x) ` s)"
  1.2431 -      using span_insert_0[of "op + (- a) ` (s - {a})"] by (auto simp del: uminus_add_conv_diff)
  1.2432 -    moreover have "{x - a |x. x \<in> (s - {a})} = ((\<lambda>x. -a+x) ` (s - {a}))"
  1.2433 -      by auto
  1.2434 -    moreover have "insert a (s - {a}) = insert a s"
  1.2435 -      by auto
  1.2436 -    ultimately have ?thesis
  1.2437 -      using affine_hull_insert_span[of "a" "s-{a}"] by auto
  1.2438 -  }
  1.2439 -  ultimately show ?thesis by auto
  1.2440 -qed
  1.2441 -
  1.2442 -lemma affine_hull_span2:
  1.2443 -  assumes "a \<in> s"
  1.2444 -  shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` (s-{a}))"
  1.2445 -  using affine_hull_insert_span_gen[of a "s - {a}", unfolded insert_Diff[OF assms]]
  1.2446 -  by auto
  1.2447 -
  1.2448 -lemma affine_hull_span_gen:
  1.2449 -  assumes "a \<in> affine hull s"
  1.2450 -  shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` s)"
  1.2451 -proof -
  1.2452 -  have "affine hull (insert a s) = affine hull s"
  1.2453 -    using hull_redundant[of a affine s] assms by auto
  1.2454 -  then show ?thesis
  1.2455 -    using affine_hull_insert_span_gen[of a "s"] by auto
  1.2456 -qed
  1.2457 -
  1.2458 -lemma affine_hull_span_0:
  1.2459 -  assumes "0 \<in> affine hull S"
  1.2460 -  shows "affine hull S = span S"
  1.2461 -  using affine_hull_span_gen[of "0" S] assms by auto
  1.2462 -
  1.2463 -lemma extend_to_affine_basis_nonempty:
  1.2464 -  fixes S V :: "'n::euclidean_space set"
  1.2465 -  assumes "\<not> affine_dependent S" "S \<subseteq> V" "S \<noteq> {}"
  1.2466 -  shows "\<exists>T. \<not> affine_dependent T \<and> S \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  1.2467 -proof -
  1.2468 -  obtain a where a: "a \<in> S"
  1.2469 -    using assms by auto
  1.2470 -  then have h0: "independent  ((\<lambda>x. -a + x) ` (S-{a}))"
  1.2471 -    using affine_dependent_iff_dependent2 assms by auto
  1.2472 -  then obtain B where B:
  1.2473 -    "(\<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"
  1.2474 -     using maximal_independent_subset_extend[of "(\<lambda>x. -a+x) ` (S-{a})" "(\<lambda>x. -a + x) ` V"] assms
  1.2475 -     by blast
  1.2476 -  define T where "T = (\<lambda>x. a+x) ` insert 0 B"
  1.2477 -  then have "T = insert a ((\<lambda>x. a+x) ` B)"
  1.2478 -    by auto
  1.2479 -  then have "affine hull T = (\<lambda>x. a+x) ` span B"
  1.2480 -    using affine_hull_insert_span_gen[of a "((\<lambda>x. a+x) ` B)"] translation_assoc[of "-a" a B]
  1.2481 -    by auto
  1.2482 -  then have "V \<subseteq> affine hull T"
  1.2483 -    using B assms translation_inverse_subset[of a V "span B"]
  1.2484 -    by auto
  1.2485 -  moreover have "T \<subseteq> V"
  1.2486 -    using T_def B a assms by auto
  1.2487 -  ultimately have "affine hull T = affine hull V"
  1.2488 -    by (metis Int_absorb1 Int_absorb2 hull_hull hull_mono)
  1.2489 -  moreover have "S \<subseteq> T"
  1.2490 -    using T_def B translation_inverse_subset[of a "S-{a}" B]
  1.2491 -    by auto
  1.2492 -  moreover have "\<not> affine_dependent T"
  1.2493 -    using T_def affine_dependent_translation_eq[of "insert 0 B"]
  1.2494 -      affine_dependent_imp_dependent2 B
  1.2495 -    by auto
  1.2496 -  ultimately show ?thesis using \<open>T \<subseteq> V\<close> by auto
  1.2497 -qed
  1.2498 -
  1.2499 -lemma affine_basis_exists:
  1.2500 -  fixes V :: "'n::euclidean_space set"
  1.2501 -  shows "\<exists>B. B \<subseteq> V \<and> \<not> affine_dependent B \<and> affine hull V = affine hull B"
  1.2502 -proof (cases "V = {}")
  1.2503 -  case True
  1.2504 -  then show ?thesis
  1.2505 -    using affine_independent_0 by auto
  1.2506 -next
  1.2507 -  case False
  1.2508 -  then obtain x where "x \<in> V" by auto
  1.2509 -  then show ?thesis
  1.2510 -    using affine_dependent_def[of "{x}"] extend_to_affine_basis_nonempty[of "{x}" V]
  1.2511 -    by auto
  1.2512 -qed
  1.2513 -
  1.2514 -proposition extend_to_affine_basis:
  1.2515 -  fixes S V :: "'n::euclidean_space set"
  1.2516 -  assumes "\<not> affine_dependent S" "S \<subseteq> V"
  1.2517 -  obtains T where "\<not> affine_dependent T" "S \<subseteq> T" "T \<subseteq> V" "affine hull T = affine hull V"
  1.2518 -proof (cases "S = {}")
  1.2519 -  case True then show ?thesis
  1.2520 -    using affine_basis_exists by (metis empty_subsetI that)
  1.2521 -next
  1.2522 -  case False
  1.2523 -  then show ?thesis by (metis assms extend_to_affine_basis_nonempty that)
  1.2524 -qed
  1.2525 -
  1.2526 -
  1.2527 -subsection \<open>Affine Dimension of a Set\<close>
  1.2528 -
  1.2529 -definition aff_dim :: "('a::euclidean_space) set \<Rightarrow> int"
  1.2530 -  where "aff_dim V =
  1.2531 -  (SOME d :: int.
  1.2532 -    \<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = d + 1)"
  1.2533 -
  1.2534 -lemma aff_dim_basis_exists:
  1.2535 -  fixes V :: "('n::euclidean_space) set"
  1.2536 -  shows "\<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  1.2537 -proof -
  1.2538 -  obtain B where "\<not> affine_dependent B \<and> affine hull B = affine hull V"
  1.2539 -    using affine_basis_exists[of V] by auto
  1.2540 -  then show ?thesis
  1.2541 -    unfolding aff_dim_def
  1.2542 -      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"]
  1.2543 -    apply auto
  1.2544 -    apply (rule exI[of _ "int (card B) - (1 :: int)"])
  1.2545 -    apply (rule exI[of _ "B"])
  1.2546 -    apply auto
  1.2547 -    done
  1.2548 -qed
  1.2549 -
  1.2550 -lemma affine_hull_nonempty: "S \<noteq> {} \<longleftrightarrow> affine hull S \<noteq> {}"
  1.2551 -proof -
  1.2552 -  have "S = {} \<Longrightarrow> affine hull S = {}"
  1.2553 -    using affine_hull_empty by auto
  1.2554 -  moreover have "affine hull S = {} \<Longrightarrow> S = {}"
  1.2555 -    unfolding hull_def by auto
  1.2556 -  ultimately show ?thesis by blast
  1.2557 -qed
  1.2558 -
  1.2559 -lemma aff_dim_parallel_subspace_aux:
  1.2560 -  fixes B :: "'n::euclidean_space set"
  1.2561 -  assumes "\<not> affine_dependent B" "a \<in> B"
  1.2562 -  shows "finite B \<and> ((card B) - 1 = dim (span ((\<lambda>x. -a+x) ` (B-{a}))))"
  1.2563 -proof -
  1.2564 -  have "independent ((\<lambda>x. -a + x) ` (B-{a}))"
  1.2565 -    using affine_dependent_iff_dependent2 assms by auto
  1.2566 -  then have fin: "dim (span ((\<lambda>x. -a+x) ` (B-{a}))) = card ((\<lambda>x. -a + x) ` (B-{a}))"
  1.2567 -    "finite ((\<lambda>x. -a + x) ` (B - {a}))"
  1.2568 -    using indep_card_eq_dim_span[of "(\<lambda>x. -a+x) ` (B-{a})"] by auto
  1.2569 -  show ?thesis
  1.2570 -  proof (cases "(\<lambda>x. -a + x) ` (B - {a}) = {}")
  1.2571 -    case True
  1.2572 -    have "B = insert a ((\<lambda>x. a + x) ` (\<lambda>x. -a + x) ` (B - {a}))"
  1.2573 -      using translation_assoc[of "a" "-a" "(B - {a})"] assms by auto
  1.2574 -    then have "B = {a}" using True by auto
  1.2575 -    then show ?thesis using assms fin by auto
  1.2576 -  next
  1.2577 -    case False
  1.2578 -    then have "card ((\<lambda>x. -a + x) ` (B - {a})) > 0"
  1.2579 -      using fin by auto
  1.2580 -    moreover have h1: "card ((\<lambda>x. -a + x) ` (B-{a})) = card (B-{a})"
  1.2581 -       apply (rule card_image)
  1.2582 -       using translate_inj_on
  1.2583 -       apply (auto simp del: uminus_add_conv_diff)
  1.2584 -       done
  1.2585 -    ultimately have "card (B-{a}) > 0" by auto
  1.2586 -    then have *: "finite (B - {a})"
  1.2587 -      using card_gt_0_iff[of "(B - {a})"] by auto
  1.2588 -    then have "card (B - {a}) = card B - 1"
  1.2589 -      using card_Diff_singleton assms by auto
  1.2590 -    with * show ?thesis using fin h1 by auto
  1.2591 -  qed
  1.2592 -qed
  1.2593 -
  1.2594 -lemma aff_dim_parallel_subspace:
  1.2595 -  fixes V L :: "'n::euclidean_space set"
  1.2596 -  assumes "V \<noteq> {}"
  1.2597 -    and "subspace L"
  1.2598 -    and "affine_parallel (affine hull V) L"
  1.2599 -  shows "aff_dim V = int (dim L)"
  1.2600 -proof -
  1.2601 -  obtain B where
  1.2602 -    B: "affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> int (card B) = aff_dim V + 1"
  1.2603 -    using aff_dim_basis_exists by auto
  1.2604 -  then have "B \<noteq> {}"
  1.2605 -    using assms B affine_hull_nonempty[of V] affine_hull_nonempty[of B]
  1.2606 -    by auto
  1.2607 -  then obtain a where a: "a \<in> B" by auto
  1.2608 -  define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  1.2609 -  moreover have "affine_parallel (affine hull B) Lb"
  1.2610 -    using Lb_def B assms affine_hull_span2[of a B] a
  1.2611 -      affine_parallel_commut[of "Lb" "(affine hull B)"]
  1.2612 -    unfolding affine_parallel_def
  1.2613 -    by auto
  1.2614 -  moreover have "subspace Lb"
  1.2615 -    using Lb_def subspace_span by auto
  1.2616 -  moreover have "affine hull B \<noteq> {}"
  1.2617 -    using assms B affine_hull_nonempty[of V] by auto
  1.2618 -  ultimately have "L = Lb"
  1.2619 -    using assms affine_parallel_subspace[of "affine hull B"] affine_affine_hull[of B] B
  1.2620 -    by auto
  1.2621 -  then have "dim L = dim Lb"
  1.2622 -    by auto
  1.2623 -  moreover have "card B - 1 = dim Lb" and "finite B"
  1.2624 -    using Lb_def aff_dim_parallel_subspace_aux a B by auto
  1.2625 -  ultimately show ?thesis
  1.2626 -    using B \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  1.2627 -qed
  1.2628 -
  1.2629 -lemma aff_independent_finite:
  1.2630 -  fixes B :: "'n::euclidean_space set"
  1.2631 -  assumes "\<not> affine_dependent B"
  1.2632 -  shows "finite B"
  1.2633 -proof -
  1.2634 -  {
  1.2635 -    assume "B \<noteq> {}"
  1.2636 -    then obtain a where "a \<in> B" by auto
  1.2637 -    then have ?thesis
  1.2638 -      using aff_dim_parallel_subspace_aux assms by auto
  1.2639 -  }
  1.2640 -  then show ?thesis by auto
  1.2641 -qed
  1.2642 -
  1.2643 -lemma independent_finite:
  1.2644 -  fixes B :: "'n::euclidean_space set"
  1.2645 -  assumes "independent B"
  1.2646 -  shows "finite B"
  1.2647 -  using affine_dependent_imp_dependent[of B] aff_independent_finite[of B] assms
  1.2648 -  by auto
  1.2649 -
  1.2650 -lemma subspace_dim_equal:
  1.2651 -  assumes "subspace (S :: ('n::euclidean_space) set)"
  1.2652 -    and "subspace T"
  1.2653 -    and "S \<subseteq> T"
  1.2654 -    and "dim S \<ge> dim T"
  1.2655 -  shows "S = T"
  1.2656 -proof -
  1.2657 -  obtain B where B: "B \<le> S" "independent B \<and> S \<subseteq> span B" "card B = dim S"
  1.2658 -    using basis_exists[of S] by auto
  1.2659 -  then have "span B \<subseteq> S"
  1.2660 -    using span_mono[of B S] span_eq[of S] assms by metis
  1.2661 -  then have "span B = S"
  1.2662 -    using B by auto
  1.2663 -  have "dim S = dim T"
  1.2664 -    using assms dim_subset[of S T] by auto
  1.2665 -  then have "T \<subseteq> span B"
  1.2666 -    using card_eq_dim[of B T] B independent_finite assms by auto
  1.2667 -  then show ?thesis
  1.2668 -    using assms \<open>span B = S\<close> by auto
  1.2669 -qed
  1.2670 -
  1.2671 -corollary dim_eq_span:
  1.2672 -  fixes S :: "'a::euclidean_space set"
  1.2673 -  shows "\<lbrakk>S \<subseteq> T; dim T \<le> dim S\<rbrakk> \<Longrightarrow> span S = span T"
  1.2674 -by (simp add: span_mono subspace_dim_equal subspace_span)
  1.2675 -
  1.2676 -lemma dim_eq_full:
  1.2677 -    fixes S :: "'a :: euclidean_space set"
  1.2678 -    shows "dim S = DIM('a) \<longleftrightarrow> span S = UNIV"
  1.2679 -apply (rule iffI)
  1.2680 - apply (metis dim_eq_span dim_subset_UNIV span_Basis span_span subset_UNIV)
  1.2681 -by (metis dim_UNIV dim_span)
  1.2682 -
  1.2683 -lemma span_substd_basis:
  1.2684 -  assumes d: "d \<subseteq> Basis"
  1.2685 -  shows "span d = {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.2686 -  (is "_ = ?B")
  1.2687 -proof -
  1.2688 -  have "d \<subseteq> ?B"
  1.2689 -    using d by (auto simp: inner_Basis)
  1.2690 -  moreover have s: "subspace ?B"
  1.2691 -    using subspace_substandard[of "\<lambda>i. i \<notin> d"] .
  1.2692 -  ultimately have "span d \<subseteq> ?B"
  1.2693 -    using span_mono[of d "?B"] span_eq[of "?B"] by blast
  1.2694 -  moreover have *: "card d \<le> dim (span d)"
  1.2695 -    using independent_card_le_dim[of d "span d"] independent_substdbasis[OF assms] span_inc[of d]
  1.2696 -    by auto
  1.2697 -  moreover from * have "dim ?B \<le> dim (span d)"
  1.2698 -    using dim_substandard[OF assms] by auto
  1.2699 -  ultimately show ?thesis
  1.2700 -    using s subspace_dim_equal[of "span d" "?B"] subspace_span[of d] by auto
  1.2701 -qed
  1.2702 -
  1.2703 -lemma basis_to_substdbasis_subspace_isomorphism:
  1.2704 -  fixes B :: "'a::euclidean_space set"
  1.2705 -  assumes "independent B"
  1.2706 -  shows "\<exists>f d::'a set. card d = card B \<and> linear f \<and> f ` B = d \<and>
  1.2707 -    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"
  1.2708 -proof -
  1.2709 -  have B: "card B = dim B"
  1.2710 -    using dim_unique[of B B "card B"] assms span_inc[of B] by auto
  1.2711 -  have "dim B \<le> card (Basis :: 'a set)"
  1.2712 -    using dim_subset_UNIV[of B] by simp
  1.2713 -  from ex_card[OF this] obtain d :: "'a set" where d: "d \<subseteq> Basis" and t: "card d = dim B"
  1.2714 -    by auto
  1.2715 -  let ?t = "{x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.2716 -  have "\<exists>f. linear f \<and> f ` B = d \<and> f ` span B = ?t \<and> inj_on f (span B)"
  1.2717 -    apply (rule basis_to_basis_subspace_isomorphism[of "span B" ?t B "d"])
  1.2718 -    apply (rule subspace_span)
  1.2719 -    apply (rule subspace_substandard)
  1.2720 -    defer
  1.2721 -    apply (rule span_inc)
  1.2722 -    apply (rule assms)
  1.2723 -    defer
  1.2724 -    unfolding dim_span[of B]
  1.2725 -    apply(rule B)
  1.2726 -    unfolding span_substd_basis[OF d, symmetric]
  1.2727 -    apply (rule span_inc)
  1.2728 -    apply (rule independent_substdbasis[OF d])
  1.2729 -    apply rule
  1.2730 -    apply assumption
  1.2731 -    unfolding t[symmetric] span_substd_basis[OF d] dim_substandard[OF d]
  1.2732 -    apply auto
  1.2733 -    done
  1.2734 -  with t \<open>card B = dim B\<close> d show ?thesis by auto
  1.2735 -qed
  1.2736 -
  1.2737 -lemma aff_dim_empty:
  1.2738 -  fixes S :: "'n::euclidean_space set"
  1.2739 -  shows "S = {} \<longleftrightarrow> aff_dim S = -1"
  1.2740 -proof -
  1.2741 -  obtain B where *: "affine hull B = affine hull S"
  1.2742 -    and "\<not> affine_dependent B"
  1.2743 -    and "int (card B) = aff_dim S + 1"
  1.2744 -    using aff_dim_basis_exists by auto
  1.2745 -  moreover
  1.2746 -  from * have "S = {} \<longleftrightarrow> B = {}"
  1.2747 -    using affine_hull_nonempty[of B] affine_hull_nonempty[of S] by auto
  1.2748 -  ultimately show ?thesis
  1.2749 -    using aff_independent_finite[of B] card_gt_0_iff[of B] by auto
  1.2750 -qed
  1.2751 -
  1.2752 -lemma aff_dim_empty_eq [simp]: "aff_dim ({}::'a::euclidean_space set) = -1"
  1.2753 -  by (simp add: aff_dim_empty [symmetric])
  1.2754 -
  1.2755 -lemma aff_dim_affine_hull [simp]: "aff_dim (affine hull S) = aff_dim S"
  1.2756 -  unfolding aff_dim_def using hull_hull[of _ S] by auto
  1.2757 -
  1.2758 -lemma aff_dim_affine_hull2:
  1.2759 -  assumes "affine hull S = affine hull T"
  1.2760 -  shows "aff_dim S = aff_dim T"
  1.2761 -  unfolding aff_dim_def using assms by auto
  1.2762 -
  1.2763 -lemma aff_dim_unique:
  1.2764 -  fixes B V :: "'n::euclidean_space set"
  1.2765 -  assumes "affine hull B = affine hull V \<and> \<not> affine_dependent B"
  1.2766 -  shows "of_nat (card B) = aff_dim V + 1"
  1.2767 -proof (cases "B = {}")
  1.2768 -  case True
  1.2769 -  then have "V = {}"
  1.2770 -    using affine_hull_nonempty[of V] affine_hull_nonempty[of B] assms
  1.2771 -    by auto
  1.2772 -  then have "aff_dim V = (-1::int)"
  1.2773 -    using aff_dim_empty by auto
  1.2774 -  then show ?thesis
  1.2775 -    using \<open>B = {}\<close> by auto
  1.2776 -next
  1.2777 -  case False
  1.2778 -  then obtain a where a: "a \<in> B" by auto
  1.2779 -  define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  1.2780 -  have "affine_parallel (affine hull B) Lb"
  1.2781 -    using Lb_def affine_hull_span2[of a B] a
  1.2782 -      affine_parallel_commut[of "Lb" "(affine hull B)"]
  1.2783 -    unfolding affine_parallel_def by auto
  1.2784 -  moreover have "subspace Lb"
  1.2785 -    using Lb_def subspace_span by auto
  1.2786 -  ultimately have "aff_dim B = int(dim Lb)"
  1.2787 -    using aff_dim_parallel_subspace[of B Lb] \<open>B \<noteq> {}\<close> by auto
  1.2788 -  moreover have "(card B) - 1 = dim Lb" "finite B"
  1.2789 -    using Lb_def aff_dim_parallel_subspace_aux a assms by auto
  1.2790 -  ultimately have "of_nat (card B) = aff_dim B + 1"
  1.2791 -    using \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  1.2792 -  then show ?thesis
  1.2793 -    using aff_dim_affine_hull2 assms by auto
  1.2794 -qed
  1.2795 -
  1.2796 -lemma aff_dim_affine_independent:
  1.2797 -  fixes B :: "'n::euclidean_space set"
  1.2798 -  assumes "\<not> affine_dependent B"
  1.2799 -  shows "of_nat (card B) = aff_dim B + 1"
  1.2800 -  using aff_dim_unique[of B B] assms by auto
  1.2801 -
  1.2802 -lemma affine_independent_iff_card:
  1.2803 -    fixes s :: "'a::euclidean_space set"
  1.2804 -    shows "~ affine_dependent s \<longleftrightarrow> finite s \<and> aff_dim s = int(card s) - 1"
  1.2805 -  apply (rule iffI)
  1.2806 -  apply (simp add: aff_dim_affine_independent aff_independent_finite)
  1.2807 -  by (metis affine_basis_exists [of s] aff_dim_unique card_subset_eq diff_add_cancel of_nat_eq_iff)
  1.2808 -
  1.2809 -lemma aff_dim_sing [simp]:
  1.2810 -  fixes a :: "'n::euclidean_space"
  1.2811 -  shows "aff_dim {a} = 0"
  1.2812 -  using aff_dim_affine_independent[of "{a}"] affine_independent_1 by auto
  1.2813 -
  1.2814 -lemma aff_dim_inner_basis_exists:
  1.2815 -  fixes V :: "('n::euclidean_space) set"
  1.2816 -  shows "\<exists>B. B \<subseteq> V \<and> affine hull B = affine hull V \<and>
  1.2817 -    \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  1.2818 -proof -
  1.2819 -  obtain B where B: "\<not> affine_dependent B" "B \<subseteq> V" "affine hull B = affine hull V"
  1.2820 -    using affine_basis_exists[of V] by auto
  1.2821 -  then have "of_nat(card B) = aff_dim V+1" using aff_dim_unique by auto
  1.2822 -  with B show ?thesis by auto
  1.2823 -qed
  1.2824 -
  1.2825 -lemma aff_dim_le_card:
  1.2826 -  fixes V :: "'n::euclidean_space set"
  1.2827 -  assumes "finite V"
  1.2828 -  shows "aff_dim V \<le> of_nat (card V) - 1"
  1.2829 -proof -
  1.2830 -  obtain B where B: "B \<subseteq> V" "of_nat (card B) = aff_dim V + 1"
  1.2831 -    using aff_dim_inner_basis_exists[of V] by auto
  1.2832 -  then have "card B \<le> card V"
  1.2833 -    using assms card_mono by auto
  1.2834 -  with B show ?thesis by auto
  1.2835 -qed
  1.2836 -
  1.2837 -lemma aff_dim_parallel_eq:
  1.2838 -  fixes S T :: "'n::euclidean_space set"
  1.2839 -  assumes "affine_parallel (affine hull S) (affine hull T)"
  1.2840 -  shows "aff_dim S = aff_dim T"
  1.2841 -proof -
  1.2842 -  {
  1.2843 -    assume "T \<noteq> {}" "S \<noteq> {}"
  1.2844 -    then obtain L where L: "subspace L \<and> affine_parallel (affine hull T) L"
  1.2845 -      using affine_parallel_subspace[of "affine hull T"]
  1.2846 -        affine_affine_hull[of T] affine_hull_nonempty
  1.2847 -      by auto
  1.2848 -    then have "aff_dim T = int (dim L)"
  1.2849 -      using aff_dim_parallel_subspace \<open>T \<noteq> {}\<close> by auto
  1.2850 -    moreover have *: "subspace L \<and> affine_parallel (affine hull S) L"
  1.2851 -       using L affine_parallel_assoc[of "affine hull S" "affine hull T" L] assms by auto
  1.2852 -    moreover from * have "aff_dim S = int (dim L)"
  1.2853 -      using aff_dim_parallel_subspace \<open>S \<noteq> {}\<close> by auto
  1.2854 -    ultimately have ?thesis by auto
  1.2855 -  }
  1.2856 -  moreover
  1.2857 -  {
  1.2858 -    assume "S = {}"
  1.2859 -    then have "S = {}" and "T = {}"
  1.2860 -      using assms affine_hull_nonempty
  1.2861 -      unfolding affine_parallel_def
  1.2862 -      by auto
  1.2863 -    then have ?thesis using aff_dim_empty by auto
  1.2864 -  }
  1.2865 -  moreover
  1.2866 -  {
  1.2867 -    assume "T = {}"
  1.2868 -    then have "S = {}" and "T = {}"
  1.2869 -      using assms affine_hull_nonempty
  1.2870 -      unfolding affine_parallel_def
  1.2871 -      by auto
  1.2872 -    then have ?thesis
  1.2873 -      using aff_dim_empty by auto
  1.2874 -  }
  1.2875 -  ultimately show ?thesis by blast
  1.2876 -qed
  1.2877 -
  1.2878 -lemma aff_dim_translation_eq:
  1.2879 -  fixes a :: "'n::euclidean_space"
  1.2880 -  shows "aff_dim ((\<lambda>x. a + x) ` S) = aff_dim S"
  1.2881 -proof -
  1.2882 -  have "affine_parallel (affine hull S) (affine hull ((\<lambda>x. a + x) ` S))"
  1.2883 -    unfolding affine_parallel_def
  1.2884 -    apply (rule exI[of _ "a"])
  1.2885 -    using affine_hull_translation[of a S]
  1.2886 -    apply auto
  1.2887 -    done
  1.2888 -  then show ?thesis
  1.2889 -    using aff_dim_parallel_eq[of S "(\<lambda>x. a + x) ` S"] by auto
  1.2890 -qed
  1.2891 -
  1.2892 -lemma aff_dim_affine:
  1.2893 -  fixes S L :: "'n::euclidean_space set"
  1.2894 -  assumes "S \<noteq> {}"
  1.2895 -    and "affine S"
  1.2896 -    and "subspace L"
  1.2897 -    and "affine_parallel S L"
  1.2898 -  shows "aff_dim S = int (dim L)"
  1.2899 -proof -
  1.2900 -  have *: "affine hull S = S"
  1.2901 -    using assms affine_hull_eq[of S] by auto
  1.2902 -  then have "affine_parallel (affine hull S) L"
  1.2903 -    using assms by (simp add: *)
  1.2904 -  then show ?thesis
  1.2905 -    using assms aff_dim_parallel_subspace[of S L] by blast
  1.2906 -qed
  1.2907 -
  1.2908 -lemma dim_affine_hull:
  1.2909 -  fixes S :: "'n::euclidean_space set"
  1.2910 -  shows "dim (affine hull S) = dim S"
  1.2911 -proof -
  1.2912 -  have "dim (affine hull S) \<ge> dim S"
  1.2913 -    using dim_subset by auto
  1.2914 -  moreover have "dim (span S) \<ge> dim (affine hull S)"
  1.2915 -    using dim_subset affine_hull_subset_span by blast
  1.2916 -  moreover have "dim (span S) = dim S"
  1.2917 -    using dim_span by auto
  1.2918 -  ultimately show ?thesis by auto
  1.2919 -qed
  1.2920 -
  1.2921 -lemma aff_dim_subspace:
  1.2922 -  fixes S :: "'n::euclidean_space set"
  1.2923 -  assumes "subspace S"
  1.2924 -  shows "aff_dim S = int (dim S)"
  1.2925 -proof (cases "S={}")
  1.2926 -  case True with assms show ?thesis
  1.2927 -    by (simp add: subspace_affine)
  1.2928 -next
  1.2929 -  case False
  1.2930 -  with aff_dim_affine[of S S] assms subspace_imp_affine[of S] affine_parallel_reflex[of S] subspace_affine
  1.2931 -  show ?thesis by auto
  1.2932 -qed
  1.2933 -
  1.2934 -lemma aff_dim_zero:
  1.2935 -  fixes S :: "'n::euclidean_space set"
  1.2936 -  assumes "0 \<in> affine hull S"
  1.2937 -  shows "aff_dim S = int (dim S)"
  1.2938 -proof -
  1.2939 -  have "subspace (affine hull S)"
  1.2940 -    using subspace_affine[of "affine hull S"] affine_affine_hull assms
  1.2941 -    by auto
  1.2942 -  then have "aff_dim (affine hull S) = int (dim (affine hull S))"
  1.2943 -    using assms aff_dim_subspace[of "affine hull S"] by auto
  1.2944 -  then show ?thesis
  1.2945 -    using aff_dim_affine_hull[of S] dim_affine_hull[of S]
  1.2946 -    by auto
  1.2947 -qed
  1.2948 -
  1.2949 -lemma aff_dim_eq_dim:
  1.2950 -  fixes S :: "'n::euclidean_space set"
  1.2951 -  assumes "a \<in> affine hull S"
  1.2952 -  shows "aff_dim S = int (dim ((\<lambda>x. -a+x) ` S))"
  1.2953 -proof -
  1.2954 -  have "0 \<in> affine hull ((\<lambda>x. -a+x) ` S)"
  1.2955 -    unfolding Convex_Euclidean_Space.affine_hull_translation
  1.2956 -    using assms by (simp add: ab_group_add_class.ab_left_minus image_iff)
  1.2957 -  with aff_dim_zero show ?thesis
  1.2958 -    by (metis aff_dim_translation_eq)
  1.2959 -qed
  1.2960 -
  1.2961 -lemma aff_dim_UNIV [simp]: "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  1.2962 -  using aff_dim_subspace[of "(UNIV :: 'n::euclidean_space set)"]
  1.2963 -    dim_UNIV[where 'a="'n::euclidean_space"]
  1.2964 -  by auto
  1.2965 -
  1.2966 -lemma aff_dim_geq:
  1.2967 -  fixes V :: "'n::euclidean_space set"
  1.2968 -  shows "aff_dim V \<ge> -1"
  1.2969 -proof -
  1.2970 -  obtain B where "affine hull B = affine hull V"
  1.2971 -    and "\<not> affine_dependent B"
  1.2972 -    and "int (card B) = aff_dim V + 1"
  1.2973 -    using aff_dim_basis_exists by auto
  1.2974 -  then show ?thesis by auto
  1.2975 -qed
  1.2976 -
  1.2977 -lemma aff_dim_negative_iff [simp]:
  1.2978 -  fixes S :: "'n::euclidean_space set"
  1.2979 -  shows "aff_dim S < 0 \<longleftrightarrow>S = {}"
  1.2980 -by (metis aff_dim_empty aff_dim_geq diff_0 eq_iff zle_diff1_eq)
  1.2981 -
  1.2982 -lemma affine_independent_card_dim_diffs:
  1.2983 -  fixes S :: "'a :: euclidean_space set"
  1.2984 -  assumes "~ affine_dependent S" "a \<in> S"
  1.2985 -    shows "card S = dim {x - a|x. x \<in> S} + 1"
  1.2986 -proof -
  1.2987 -  have 1: "{b - a|b. b \<in> (S - {a})} \<subseteq> {x - a|x. x \<in> S}" by auto
  1.2988 -  have 2: "x - a \<in> span {b - a |b. b \<in> S - {a}}" if "x \<in> S" for x
  1.2989 -  proof (cases "x = a")
  1.2990 -    case True then show ?thesis by simp
  1.2991 -  next
  1.2992 -    case False then show ?thesis
  1.2993 -      using assms by (blast intro: span_superset that)
  1.2994 -  qed
  1.2995 -  have "\<not> affine_dependent (insert a S)"
  1.2996 -    by (simp add: assms insert_absorb)
  1.2997 -  then have 3: "independent {b - a |b. b \<in> S - {a}}"
  1.2998 -      using dependent_imp_affine_dependent by fastforce
  1.2999 -  have "{b - a |b. b \<in> S - {a}} = (\<lambda>b. b-a) ` (S - {a})"
  1.3000 -    by blast
  1.3001 -  then have "card {b - a |b. b \<in> S - {a}} = card ((\<lambda>b. b-a) ` (S - {a}))"
  1.3002 -    by simp
  1.3003 -  also have "... = card (S - {a})"
  1.3004 -    by (metis (no_types, lifting) card_image diff_add_cancel inj_onI)
  1.3005 -  also have "... = card S - 1"
  1.3006 -    by (simp add: aff_independent_finite assms)
  1.3007 -  finally have 4: "card {b - a |b. b \<in> S - {a}} = card S - 1" .
  1.3008 -  have "finite S"
  1.3009 -    by (meson assms aff_independent_finite)
  1.3010 -  with \<open>a \<in> S\<close> have "card S \<noteq> 0" by auto
  1.3011 -  moreover have "dim {x - a |x. x \<in> S} = card S - 1"
  1.3012 -    using 2 by (blast intro: dim_unique [OF 1 _ 3 4])
  1.3013 -  ultimately show ?thesis
  1.3014 -    by auto
  1.3015 -qed
  1.3016 -
  1.3017 -lemma independent_card_le_aff_dim:
  1.3018 -  fixes B :: "'n::euclidean_space set"
  1.3019 -  assumes "B \<subseteq> V"
  1.3020 -  assumes "\<not> affine_dependent B"
  1.3021 -  shows "int (card B) \<le> aff_dim V + 1"
  1.3022 -proof -
  1.3023 -  obtain T where T: "\<not> affine_dependent T \<and> B \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  1.3024 -    by (metis assms extend_to_affine_basis[of B V])
  1.3025 -  then have "of_nat (card T) = aff_dim V + 1"
  1.3026 -    using aff_dim_unique by auto
  1.3027 -  then show ?thesis
  1.3028 -    using T card_mono[of T B] aff_independent_finite[of T] by auto
  1.3029 -qed
  1.3030 -
  1.3031 -lemma aff_dim_subset:
  1.3032 -  fixes S T :: "'n::euclidean_space set"
  1.3033 -  assumes "S \<subseteq> T"
  1.3034 -  shows "aff_dim S \<le> aff_dim T"
  1.3035 -proof -
  1.3036 -  obtain B where B: "\<not> affine_dependent B" "B \<subseteq> S" "affine hull B = affine hull S"
  1.3037 -    "of_nat (card B) = aff_dim S + 1"
  1.3038 -    using aff_dim_inner_basis_exists[of S] by auto
  1.3039 -  then have "int (card B) \<le> aff_dim T + 1"
  1.3040 -    using assms independent_card_le_aff_dim[of B T] by auto
  1.3041 -  with B show ?thesis by auto
  1.3042 -qed
  1.3043 -
  1.3044 -lemma aff_dim_le_DIM:
  1.3045 -  fixes S :: "'n::euclidean_space set"
  1.3046 -  shows "aff_dim S \<le> int (DIM('n))"
  1.3047 -proof -
  1.3048 -  have "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  1.3049 -    using aff_dim_UNIV by auto
  1.3050 -  then show "aff_dim (S:: 'n::euclidean_space set) \<le> int(DIM('n))"
  1.3051 -    using aff_dim_subset[of S "(UNIV :: ('n::euclidean_space) set)"] subset_UNIV by auto
  1.3052 -qed
  1.3053 -
  1.3054 -lemma affine_dim_equal:
  1.3055 -  fixes S :: "'n::euclidean_space set"
  1.3056 -  assumes "affine S" "affine T" "S \<noteq> {}" "S \<subseteq> T" "aff_dim S = aff_dim T"
  1.3057 -  shows "S = T"
  1.3058 -proof -
  1.3059 -  obtain a where "a \<in> S" using assms by auto
  1.3060 -  then have "a \<in> T" using assms by auto
  1.3061 -  define LS where "LS = {y. \<exists>x \<in> S. (-a) + x = y}"
  1.3062 -  then have ls: "subspace LS" "affine_parallel S LS"
  1.3063 -    using assms parallel_subspace_explicit[of S a LS] \<open>a \<in> S\<close> by auto
  1.3064 -  then have h1: "int(dim LS) = aff_dim S"
  1.3065 -    using assms aff_dim_affine[of S LS] by auto
  1.3066 -  have "T \<noteq> {}" using assms by auto
  1.3067 -  define LT where "LT = {y. \<exists>x \<in> T. (-a) + x = y}"
  1.3068 -  then have lt: "subspace LT \<and> affine_parallel T LT"
  1.3069 -    using assms parallel_subspace_explicit[of T a LT] \<open>a \<in> T\<close> by auto
  1.3070 -  then have "int(dim LT) = aff_dim T"
  1.3071 -    using assms aff_dim_affine[of T LT] \<open>T \<noteq> {}\<close> by auto
  1.3072 -  then have "dim LS = dim LT"
  1.3073 -    using h1 assms by auto
  1.3074 -  moreover have "LS \<le> LT"
  1.3075 -    using LS_def LT_def assms by auto
  1.3076 -  ultimately have "LS = LT"
  1.3077 -    using subspace_dim_equal[of LS LT] ls lt by auto
  1.3078 -  moreover have "S = {x. \<exists>y \<in> LS. a+y=x}"
  1.3079 -    using LS_def by auto
  1.3080 -  moreover have "T = {x. \<exists>y \<in> LT. a+y=x}"
  1.3081 -    using LT_def by auto
  1.3082 -  ultimately show ?thesis by auto
  1.3083 -qed
  1.3084 -
  1.3085 -lemma affine_hull_UNIV:
  1.3086 -  fixes S :: "'n::euclidean_space set"
  1.3087 -  assumes "aff_dim S = int(DIM('n))"
  1.3088 -  shows "affine hull S = (UNIV :: ('n::euclidean_space) set)"
  1.3089 -proof -
  1.3090 -  have "S \<noteq> {}"
  1.3091 -    using assms aff_dim_empty[of S] by auto
  1.3092 -  have h0: "S \<subseteq> affine hull S"
  1.3093 -    using hull_subset[of S _] by auto
  1.3094 -  have h1: "aff_dim (UNIV :: ('n::euclidean_space) set) = aff_dim S"
  1.3095 -    using aff_dim_UNIV assms by auto
  1.3096 -  then have h2: "aff_dim (affine hull S) \<le> aff_dim (UNIV :: ('n::euclidean_space) set)"
  1.3097 -    using aff_dim_le_DIM[of "affine hull S"] assms h0 by auto
  1.3098 -  have h3: "aff_dim S \<le> aff_dim (affine hull S)"
  1.3099 -    using h0 aff_dim_subset[of S "affine hull S"] assms by auto
  1.3100 -  then have h4: "aff_dim (affine hull S) = aff_dim (UNIV :: ('n::euclidean_space) set)"
  1.3101 -    using h0 h1 h2 by auto
  1.3102 -  then show ?thesis
  1.3103 -    using affine_dim_equal[of "affine hull S" "(UNIV :: ('n::euclidean_space) set)"]
  1.3104 -      affine_affine_hull[of S] affine_UNIV assms h4 h0 \<open>S \<noteq> {}\<close>
  1.3105 -    by auto
  1.3106 -qed
  1.3107 -
  1.3108 -lemma disjoint_affine_hull:
  1.3109 -  fixes s :: "'n::euclidean_space set"
  1.3110 -  assumes "~ affine_dependent s" "t \<subseteq> s" "u \<subseteq> s" "t \<inter> u = {}"
  1.3111 -    shows "(affine hull t) \<inter> (affine hull u) = {}"
  1.3112 -proof -
  1.3113 -  have "finite s" using assms by (simp add: aff_independent_finite)
  1.3114 -  then have "finite t" "finite u" using assms finite_subset by blast+
  1.3115 -  { fix y
  1.3116 -    assume yt: "y \<in> affine hull t" and yu: "y \<in> affine hull u"
  1.3117 -    then obtain a b
  1.3118 -           where a1 [simp]: "setsum a t = 1" and [simp]: "setsum (\<lambda>v. a v *\<^sub>R v) t = y"
  1.3119 -             and [simp]: "setsum b u = 1" "setsum (\<lambda>v. b v *\<^sub>R v) u = y"
  1.3120 -      by (auto simp: affine_hull_finite \<open>finite t\<close> \<open>finite u\<close>)
  1.3121 -    define c where "c x = (if x \<in> t then a x else if x \<in> u then -(b x) else 0)" for x
  1.3122 -    have [simp]: "s \<inter> t = t" "s \<inter> - t \<inter> u = u" using assms by auto
  1.3123 -    have "setsum c s = 0"
  1.3124 -      by (simp add: c_def comm_monoid_add_class.setsum.If_cases \<open>finite s\<close> setsum_negf)
  1.3125 -    moreover have "~ (\<forall>v\<in>s. c v = 0)"
  1.3126 -      by (metis (no_types) IntD1 \<open>s \<inter> t = t\<close> a1 c_def setsum_not_0 zero_neq_one)
  1.3127 -    moreover have "(\<Sum>v\<in>s. c v *\<^sub>R v) = 0"
  1.3128 -      by (simp add: c_def if_smult setsum_negf
  1.3129 -             comm_monoid_add_class.setsum.If_cases \<open>finite s\<close>)
  1.3130 -    ultimately have False
  1.3131 -      using assms \<open>finite s\<close> by (auto simp: affine_dependent_explicit)
  1.3132 -  }
  1.3133 -  then show ?thesis by blast
  1.3134 -qed
  1.3135 -
  1.3136 -lemma aff_dim_convex_hull:
  1.3137 -  fixes S :: "'n::euclidean_space set"
  1.3138 -  shows "aff_dim (convex hull S) = aff_dim S"
  1.3139 -  using aff_dim_affine_hull[of S] convex_hull_subset_affine_hull[of S]
  1.3140 -    hull_subset[of S "convex"] aff_dim_subset[of S "convex hull S"]
  1.3141 -    aff_dim_subset[of "convex hull S" "affine hull S"]
  1.3142 -  by auto
  1.3143 -
  1.3144 -lemma aff_dim_cball:
  1.3145 -  fixes a :: "'n::euclidean_space"
  1.3146 -  assumes "e > 0"
  1.3147 -  shows "aff_dim (cball a e) = int (DIM('n))"
  1.3148 -proof -
  1.3149 -  have "(\<lambda>x. a + x) ` (cball 0 e) \<subseteq> cball a e"
  1.3150 -    unfolding cball_def dist_norm by auto
  1.3151 -  then have "aff_dim (cball (0 :: 'n::euclidean_space) e) \<le> aff_dim (cball a e)"
  1.3152 -    using aff_dim_translation_eq[of a "cball 0 e"]
  1.3153 -          aff_dim_subset[of "op + a ` cball 0 e" "cball a e"]
  1.3154 -    by auto
  1.3155 -  moreover have "aff_dim (cball (0 :: 'n::euclidean_space) e) = int (DIM('n))"
  1.3156 -    using hull_inc[of "(0 :: 'n::euclidean_space)" "cball 0 e"]
  1.3157 -      centre_in_cball[of "(0 :: 'n::euclidean_space)"] assms
  1.3158 -    by (simp add: dim_cball[of e] aff_dim_zero[of "cball 0 e"])
  1.3159 -  ultimately show ?thesis
  1.3160 -    using aff_dim_le_DIM[of "cball a e"] by auto
  1.3161 -qed
  1.3162 -
  1.3163 -lemma aff_dim_open:
  1.3164 -  fixes S :: "'n::euclidean_space set"
  1.3165 -  assumes "open S"
  1.3166 -    and "S \<noteq> {}"
  1.3167 -  shows "aff_dim S = int (DIM('n))"
  1.3168 -proof -
  1.3169 -  obtain x where "x \<in> S"
  1.3170 -    using assms by auto
  1.3171 -  then obtain e where e: "e > 0" "cball x e \<subseteq> S"
  1.3172 -    using open_contains_cball[of S] assms by auto
  1.3173 -  then have "aff_dim (cball x e) \<le> aff_dim S"
  1.3174 -    using aff_dim_subset by auto
  1.3175 -  with e show ?thesis
  1.3176 -    using aff_dim_cball[of e x] aff_dim_le_DIM[of S] by auto
  1.3177 -qed
  1.3178 -
  1.3179 -lemma low_dim_interior:
  1.3180 -  fixes S :: "'n::euclidean_space set"
  1.3181 -  assumes "\<not> aff_dim S = int (DIM('n))"
  1.3182 -  shows "interior S = {}"
  1.3183 -proof -
  1.3184 -  have "aff_dim(interior S) \<le> aff_dim S"
  1.3185 -    using interior_subset aff_dim_subset[of "interior S" S] by auto
  1.3186 -  then show ?thesis
  1.3187 -    using aff_dim_open[of "interior S"] aff_dim_le_DIM[of S] assms by auto
  1.3188 -qed
  1.3189 -
  1.3190 -corollary empty_interior_lowdim:
  1.3191 -  fixes S :: "'n::euclidean_space set"
  1.3192 -  shows "dim S < DIM ('n) \<Longrightarrow> interior S = {}"
  1.3193 -by (metis low_dim_interior affine_hull_UNIV dim_affine_hull less_not_refl dim_UNIV)
  1.3194 -
  1.3195 -corollary aff_dim_nonempty_interior:
  1.3196 -  fixes S :: "'a::euclidean_space set"
  1.3197 -  shows "interior S \<noteq> {} \<Longrightarrow> aff_dim S = DIM('a)"
  1.3198 -by (metis low_dim_interior)
  1.3199 -
  1.3200 -subsection \<open>Caratheodory's theorem.\<close>
  1.3201 -
  1.3202 -lemma convex_hull_caratheodory_aff_dim:
  1.3203 -  fixes p :: "('a::euclidean_space) set"
  1.3204 -  shows "convex hull p =
  1.3205 -    {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  1.3206 -      (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<and> setsum (\<lambda>v. u v *\<^sub>R v) s = y}"
  1.3207 -  unfolding convex_hull_explicit set_eq_iff mem_Collect_eq
  1.3208 -proof (intro allI iffI)
  1.3209 -  fix y
  1.3210 -  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>
  1.3211 -    setsum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1.3212 -  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"
  1.3213 -  then obtain N where "?P N" by auto
  1.3214 -  then have "\<exists>n\<le>N. (\<forall>k<n. \<not> ?P k) \<and> ?P n"
  1.3215 -    apply (rule_tac ex_least_nat_le)
  1.3216 -    apply auto
  1.3217 -    done
  1.3218 -  then obtain n where "?P n" and smallest: "\<forall>k<n. \<not> ?P k"
  1.3219 -    by blast
  1.3220 -  then obtain s u where obt: "finite s" "card s = n" "s\<subseteq>p" "\<forall>x\<in>s. 0 \<le> u x"
  1.3221 -    "setsum u s = 1"  "(\<Sum>v\<in>s. u v *\<^sub>R v) = y" by auto
  1.3222 -
  1.3223 -  have "card s \<le> aff_dim p + 1"
  1.3224 -  proof (rule ccontr, simp only: not_le)
  1.3225 -    assume "aff_dim p + 1 < card s"
  1.3226 -    then have "affine_dependent s"
  1.3227 -      using affine_dependent_biggerset[OF obt(1)] independent_card_le_aff_dim not_less obt(3)
  1.3228 -      by blast
  1.3229 -    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"
  1.3230 -      using affine_dependent_explicit_finite[OF obt(1)] by auto
  1.3231 -    define i where "i = (\<lambda>v. (u v) / (- w v)) ` {v\<in>s. w v < 0}"
  1.3232 -    define t where "t = Min i"
  1.3233 -    have "\<exists>x\<in>s. w x < 0"
  1.3234 -    proof (rule ccontr, simp add: not_less)
  1.3235 -      assume as:"\<forall>x\<in>s. 0 \<le> w x"
  1.3236 -      then have "setsum w (s - {v}) \<ge> 0"
  1.3237 -        apply (rule_tac setsum_nonneg)
  1.3238 -        apply auto
  1.3239 -        done
  1.3240 -      then have "setsum w s > 0"
  1.3241 -        unfolding setsum.remove[OF obt(1) \<open>v\<in>s\<close>]
  1.3242 -        using as[THEN bspec[where x=v]]  \<open>v\<in>s\<close>  \<open>w v \<noteq> 0\<close> by auto
  1.3243 -      then show False using wv(1) by auto
  1.3244 -    qed
  1.3245 -    then have "i \<noteq> {}" unfolding i_def by auto
  1.3246 -    then have "t \<ge> 0"
  1.3247 -      using Min_ge_iff[of i 0 ] and obt(1)
  1.3248 -      unfolding t_def i_def
  1.3249 -      using obt(4)[unfolded le_less]
  1.3250 -      by (auto simp: divide_le_0_iff)
  1.3251 -    have t: "\<forall>v\<in>s. u v + t * w v \<ge> 0"
  1.3252 -    proof
  1.3253 -      fix v
  1.3254 -      assume "v \<in> s"
  1.3255 -      then have v: "0 \<le> u v"
  1.3256 -        using obt(4)[THEN bspec[where x=v]] by auto
  1.3257 -      show "0 \<le> u v + t * w v"
  1.3258 -      proof (cases "w v < 0")
  1.3259 -        case False
  1.3260 -        thus ?thesis using v \<open>t\<ge>0\<close> by auto
  1.3261 -      next
  1.3262 -        case True
  1.3263 -        then have "t \<le> u v / (- w v)"
  1.3264 -          using \<open>v\<in>s\<close> unfolding t_def i_def
  1.3265 -          apply (rule_tac Min_le)
  1.3266 -          using obt(1) apply auto
  1.3267 -          done
  1.3268 -        then show ?thesis
  1.3269 -          unfolding real_0_le_add_iff
  1.3270 -          using pos_le_divide_eq[OF True[unfolded neg_0_less_iff_less[symmetric]]]
  1.3271 -          by auto
  1.3272 -      qed
  1.3273 -    qed
  1.3274 -    obtain a where "a \<in> s" and "t = (\<lambda>v. (u v) / (- w v)) a" and "w a < 0"
  1.3275 -      using Min_in[OF _ \<open>i\<noteq>{}\<close>] and obt(1) unfolding i_def t_def by auto
  1.3276 -    then have a: "a \<in> s" "u a + t * w a = 0" by auto
  1.3277 -    have *: "\<And>f. setsum f (s - {a}) = setsum f s - ((f a)::'b::ab_group_add)"
  1.3278 -      unfolding setsum.remove[OF obt(1) \<open>a\<in>s\<close>] by auto
  1.3279 -    have "(\<Sum>v\<in>s. u v + t * w v) = 1"
  1.3280 -      unfolding setsum.distrib wv(1) setsum_right_distrib[symmetric] obt(5) by auto
  1.3281 -    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"
  1.3282 -      unfolding setsum.distrib obt(6) scaleR_scaleR[symmetric] scaleR_right.setsum [symmetric] wv(4)
  1.3283 -      using a(2) [THEN eq_neg_iff_add_eq_0 [THEN iffD2]] by simp
  1.3284 -    ultimately have "?P (n - 1)"
  1.3285 -      apply (rule_tac x="(s - {a})" in exI)
  1.3286 -      apply (rule_tac x="\<lambda>v. u v + t * w v" in exI)
  1.3287 -      using obt(1-3) and t and a
  1.3288 -      apply (auto simp add: * scaleR_left_distrib)
  1.3289 -      done
  1.3290 -    then show False
  1.3291 -      using smallest[THEN spec[where x="n - 1"]] by auto
  1.3292 -  qed
  1.3293 -  then show "\<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  1.3294 -      (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1.3295 -    using obt by auto
  1.3296 -qed auto
  1.3297 -
  1.3298 -lemma caratheodory_aff_dim:
  1.3299 -  fixes p :: "('a::euclidean_space) set"
  1.3300 -  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}"
  1.3301 -        (is "?lhs = ?rhs")
  1.3302 -proof
  1.3303 -  show "?lhs \<subseteq> ?rhs"
  1.3304 -    apply (subst convex_hull_caratheodory_aff_dim)
  1.3305 -    apply clarify
  1.3306 -    apply (rule_tac x="s" in exI)
  1.3307 -    apply (simp add: hull_subset convex_explicit [THEN iffD1, OF convex_convex_hull])
  1.3308 -    done
  1.3309 -next
  1.3310 -  show "?rhs \<subseteq> ?lhs"
  1.3311 -    using hull_mono by blast
  1.3312 -qed
  1.3313 -
  1.3314 -lemma convex_hull_caratheodory:
  1.3315 -  fixes p :: "('a::euclidean_space) set"
  1.3316 -  shows "convex hull p =
  1.3317 -            {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and>
  1.3318 -              (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<and> setsum (\<lambda>v. u v *\<^sub>R v) s = y}"
  1.3319 -        (is "?lhs = ?rhs")
  1.3320 -proof (intro set_eqI iffI)
  1.3321 -  fix x
  1.3322 -  assume "x \<in> ?lhs" then show "x \<in> ?rhs"
  1.3323 -    apply (simp only: convex_hull_caratheodory_aff_dim Set.mem_Collect_eq)
  1.3324 -    apply (erule ex_forward)+
  1.3325 -    using aff_dim_le_DIM [of p]
  1.3326 -    apply simp
  1.3327 -    done
  1.3328 -next
  1.3329 -  fix x
  1.3330 -  assume "x \<in> ?rhs" then show "x \<in> ?lhs"
  1.3331 -    by (auto simp add: convex_hull_explicit)
  1.3332 -qed
  1.3333 -
  1.3334 -theorem caratheodory:
  1.3335 -  "convex hull p =
  1.3336 -    {x::'a::euclidean_space. \<exists>s. finite s \<and> s \<subseteq> p \<and>
  1.3337 -      card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s}"
  1.3338 -proof safe
  1.3339 -  fix x
  1.3340 -  assume "x \<in> convex hull p"
  1.3341 -  then obtain s u where "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1"
  1.3342 -    "\<forall>x\<in>s. 0 \<le> u x" "setsum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1.3343 -    unfolding convex_hull_caratheodory by auto
  1.3344 -  then show "\<exists>s. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s"
  1.3345 -    apply (rule_tac x=s in exI)
  1.3346 -    using hull_subset[of s convex]
  1.3347 -    using convex_convex_hull[simplified convex_explicit, of s,
  1.3348 -      THEN spec[where x=s], THEN spec[where x=u]]
  1.3349 -    apply auto
  1.3350 -    done
  1.3351 -next
  1.3352 -  fix x s
  1.3353 -  assume  "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1" "x \<in> convex hull s"
  1.3354 -  then show "x \<in> convex hull p"
  1.3355 -    using hull_mono[OF \<open>s\<subseteq>p\<close>] by auto
  1.3356 -qed
  1.3357 -
  1.3358 -
  1.3359 -subsection \<open>Relative interior of a set\<close>
  1.3360 -
  1.3361 -definition "rel_interior S =
  1.3362 -  {x. \<exists>T. openin (subtopology euclidean (affine hull S)) T \<and> x \<in> T \<and> T \<subseteq> S}"
  1.3363 -
  1.3364 -lemma rel_interior:
  1.3365 -  "rel_interior S = {x \<in> S. \<exists>T. open T \<and> x \<in> T \<and> T \<inter> affine hull S \<subseteq> S}"
  1.3366 -  unfolding rel_interior_def[of S] openin_open[of "affine hull S"]
  1.3367 -  apply auto
  1.3368 -proof -
  1.3369 -  fix x T
  1.3370 -  assume *: "x \<in> S" "open T" "x \<in> T" "T \<inter> affine hull S \<subseteq> S"
  1.3371 -  then have **: "x \<in> T \<inter> affine hull S"
  1.3372 -    using hull_inc by auto
  1.3373 -  show "\<exists>Tb. (\<exists>Ta. open Ta \<and> Tb = affine hull S \<inter> Ta) \<and> x \<in> Tb \<and> Tb \<subseteq> S"
  1.3374 -    apply (rule_tac x = "T \<inter> (affine hull S)" in exI)
  1.3375 -    using * **
  1.3376 -    apply auto
  1.3377 -    done
  1.3378 -qed
  1.3379 -
  1.3380 -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)"
  1.3381 -  by (auto simp add: rel_interior)
  1.3382 -
  1.3383 -lemma mem_rel_interior_ball:
  1.3384 -  "x \<in> rel_interior S \<longleftrightarrow> x \<in> S \<and> (\<exists>e. e > 0 \<and> ball x e \<inter> affine hull S \<subseteq> S)"
  1.3385 -  apply (simp add: rel_interior, safe)
  1.3386 -  apply (force simp add: open_contains_ball)
  1.3387 -  apply (rule_tac x = "ball x e" in exI)
  1.3388 -  apply simp
  1.3389 -  done
  1.3390 -
  1.3391 -lemma rel_interior_ball:
  1.3392 -  "rel_interior S = {x \<in> S. \<exists>e. e > 0 \<and> ball x e \<inter> affine hull S \<subseteq> S}"
  1.3393 -  using mem_rel_interior_ball [of _ S] by auto
  1.3394 -
  1.3395 -lemma mem_rel_interior_cball:
  1.3396 -  "x \<in> rel_interior S \<longleftrightarrow> x \<in> S \<and> (\<exists>e. e > 0 \<and> cball x e \<inter> affine hull S \<subseteq> S)"
  1.3397 -  apply (simp add: rel_interior, safe)
  1.3398 -  apply (force simp add: open_contains_cball)
  1.3399 -  apply (rule_tac x = "ball x e" in exI)
  1.3400 -  apply (simp add: subset_trans [OF ball_subset_cball])
  1.3401 -  apply auto
  1.3402 -  done
  1.3403 -
  1.3404 -lemma rel_interior_cball:
  1.3405 -  "rel_interior S = {x \<in> S. \<exists>e. e > 0 \<and> cball x e \<inter> affine hull S \<subseteq> S}"
  1.3406 -  using mem_rel_interior_cball [of _ S] by auto
  1.3407 -
  1.3408 -lemma rel_interior_empty [simp]: "rel_interior {} = {}"
  1.3409 -   by (auto simp add: rel_interior_def)
  1.3410 -
  1.3411 -lemma affine_hull_sing [simp]: "affine hull {a :: 'n::euclidean_space} = {a}"
  1.3412 -  by (metis affine_hull_eq affine_sing)
  1.3413 -
  1.3414 -lemma rel_interior_sing [simp]:
  1.3415 -    fixes a :: "'n::euclidean_space"  shows "rel_interior {a} = {a}"
  1.3416 -  apply (auto simp: rel_interior_ball)
  1.3417 -  apply (rule_tac x=1 in exI)
  1.3418 -  apply force
  1.3419 -  done
  1.3420 -
  1.3421 -lemma subset_rel_interior:
  1.3422 -  fixes S T :: "'n::euclidean_space set"
  1.3423 -  assumes "S \<subseteq> T"
  1.3424 -    and "affine hull S = affine hull T"
  1.3425 -  shows "rel_interior S \<subseteq> rel_interior T"
  1.3426 -  using assms by (auto simp add: rel_interior_def)
  1.3427 -
  1.3428 -lemma rel_interior_subset: "rel_interior S \<subseteq> S"
  1.3429 -  by (auto simp add: rel_interior_def)
  1.3430 -
  1.3431 -lemma rel_interior_subset_closure: "rel_interior S \<subseteq> closure S"
  1.3432 -  using rel_interior_subset by (auto simp add: closure_def)
  1.3433 -
  1.3434 -lemma interior_subset_rel_interior: "interior S \<subseteq> rel_interior S"
  1.3435 -  by (auto simp add: rel_interior interior_def)
  1.3436 -
  1.3437 -lemma interior_rel_interior:
  1.3438 -  fixes S :: "'n::euclidean_space set"
  1.3439 -  assumes "aff_dim S = int(DIM('n))"
  1.3440 -  shows "rel_interior S = interior S"
  1.3441 -proof -
  1.3442 -  have "affine hull S = UNIV"
  1.3443 -    using assms affine_hull_UNIV[of S] by auto
  1.3444 -  then show ?thesis
  1.3445 -    unfolding rel_interior interior_def by auto
  1.3446 -qed
  1.3447 -
  1.3448 -lemma rel_interior_interior:
  1.3449 -  fixes S :: "'n::euclidean_space set"
  1.3450 -  assumes "affine hull S = UNIV"
  1.3451 -  shows "rel_interior S = interior S"
  1.3452 -  using assms unfolding rel_interior interior_def by auto
  1.3453 -
  1.3454 -lemma rel_interior_open:
  1.3455 -  fixes S :: "'n::euclidean_space set"
  1.3456 -  assumes "open S"
  1.3457 -  shows "rel_interior S = S"
  1.3458 -  by (metis assms interior_eq interior_subset_rel_interior rel_interior_subset set_eq_subset)
  1.3459 -
  1.3460 -lemma interior_ball [simp]: "interior (ball x e) = ball x e"
  1.3461 -  by (simp add: interior_open)
  1.3462 -
  1.3463 -lemma interior_rel_interior_gen:
  1.3464 -  fixes S :: "'n::euclidean_space set"
  1.3465 -  shows "interior S = (if aff_dim S = int(DIM('n)) then rel_interior S else {})"
  1.3466 -  by (metis interior_rel_interior low_dim_interior)
  1.3467 -
  1.3468 -lemma rel_interior_nonempty_interior:
  1.3469 -  fixes S :: "'n::euclidean_space set"
  1.3470 -  shows "interior S \<noteq> {} \<Longrightarrow> rel_interior S = interior S"
  1.3471 -by (metis interior_rel_interior_gen)
  1.3472 -
  1.3473 -lemma affine_hull_nonempty_interior:
  1.3474 -  fixes S :: "'n::euclidean_space set"
  1.3475 -  shows "interior S \<noteq> {} \<Longrightarrow> affine hull S = UNIV"
  1.3476 -by (metis affine_hull_UNIV interior_rel_interior_gen)
  1.3477 -
  1.3478 -lemma rel_interior_affine_hull [simp]:
  1.3479 -  fixes S :: "'n::euclidean_space set"
  1.3480 -  shows "rel_interior (affine hull S) = affine hull S"
  1.3481 -proof -
  1.3482 -  have *: "rel_interior (affine hull S) \<subseteq> affine hull S"
  1.3483 -    using rel_interior_subset by auto
  1.3484 -  {
  1.3485 -    fix x
  1.3486 -    assume x: "x \<in> affine hull S"
  1.3487 -    define e :: real where "e = 1"
  1.3488 -    then have "e > 0" "ball x e \<inter> affine hull (affine hull S) \<subseteq> affine hull S"
  1.3489 -      using hull_hull[of _ S] by auto
  1.3490 -    then have "x \<in> rel_interior (affine hull S)"
  1.3491 -      using x rel_interior_ball[of "affine hull S"] by auto
  1.3492 -  }
  1.3493 -  then show ?thesis using * by auto
  1.3494 -qed
  1.3495 -
  1.3496 -lemma rel_interior_UNIV [simp]: "rel_interior (UNIV :: ('n::euclidean_space) set) = UNIV"
  1.3497 -  by (metis open_UNIV rel_interior_open)
  1.3498 -
  1.3499 -lemma rel_interior_convex_shrink:
  1.3500 -  fixes S :: "'a::euclidean_space set"
  1.3501 -  assumes "convex S"
  1.3502 -    and "c \<in> rel_interior S"
  1.3503 -    and "x \<in> S"
  1.3504 -    and "0 < e"
  1.3505 -    and "e \<le> 1"
  1.3506 -  shows "x - e *\<^sub>R (x - c) \<in> rel_interior S"
  1.3507 -proof -
  1.3508 -  obtain d where "d > 0" and d: "ball c d \<inter> affine hull S \<subseteq> S"
  1.3509 -    using assms(2) unfolding  mem_rel_interior_ball by auto
  1.3510 -  {
  1.3511 -    fix y
  1.3512 -    assume as: "dist (x - e *\<^sub>R (x - c)) y < e * d" "y \<in> affine hull S"
  1.3513 -    have *: "y = (1 - (1 - e)) *\<^sub>R ((1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x) + (1 - e) *\<^sub>R x"
  1.3514 -      using \<open>e > 0\<close> by (auto simp add: scaleR_left_diff_distrib scaleR_right_diff_distrib)
  1.3515 -    have "x \<in> affine hull S"
  1.3516 -      using assms hull_subset[of S] by auto
  1.3517 -    moreover have "1 / e + - ((1 - e) / e) = 1"
  1.3518 -      using \<open>e > 0\<close> left_diff_distrib[of "1" "(1-e)" "1/e"] by auto
  1.3519 -    ultimately have **: "(1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x \<in> affine hull S"
  1.3520 -      using as affine_affine_hull[of S] mem_affine[of "affine hull S" y x "(1 / e)" "-((1 - e) / e)"]
  1.3521 -      by (simp add: algebra_simps)
  1.3522 -    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)"
  1.3523 -      unfolding dist_norm norm_scaleR[symmetric]
  1.3524 -      apply (rule arg_cong[where f=norm])
  1.3525 -      using \<open>e > 0\<close>
  1.3526 -      apply (auto simp add: euclidean_eq_iff[where 'a='a] field_simps inner_simps)
  1.3527 -      done
  1.3528 -    also have "\<dots> = \<bar>1/e\<bar> * norm (x - e *\<^sub>R (x - c) - y)"
  1.3529 -      by (auto intro!:arg_cong[where f=norm] simp add: algebra_simps)
  1.3530 -    also have "\<dots> < d"
  1.3531 -      using as[unfolded dist_norm] and \<open>e > 0\<close>
  1.3532 -      by (auto simp add:pos_divide_less_eq[OF \<open>e > 0\<close>] mult.commute)
  1.3533 -    finally have "y \<in> S"
  1.3534 -      apply (subst *)
  1.3535 -      apply (rule assms(1)[unfolded convex_alt,rule_format])
  1.3536 -      apply (rule d[unfolded subset_eq,rule_format])
  1.3537 -      unfolding mem_ball
  1.3538 -      using assms(3-5) **
  1.3539 -      apply auto
  1.3540 -      done
  1.3541 -  }
  1.3542 -  then have "ball (x - e *\<^sub>R (x - c)) (e*d) \<inter> affine hull S \<subseteq> S"
  1.3543 -    by auto
  1.3544 -  moreover have "e * d > 0"
  1.3545 -    using \<open>e > 0\<close> \<open>d > 0\<close> by simp
  1.3546 -  moreover have c: "c \<in> S"
  1.3547 -    using assms rel_interior_subset by auto
  1.3548 -  moreover from c have "x - e *\<^sub>R (x - c) \<in> S"
  1.3549 -    using convexD_alt[of S x c e]
  1.3550 -    apply (simp add: algebra_simps)
  1.3551 -    using assms
  1.3552 -    apply auto
  1.3553 -    done
  1.3554 -  ultimately show ?thesis
  1.3555 -    using mem_rel_interior_ball[of "x - e *\<^sub>R (x - c)" S] \<open>e > 0\<close> by auto
  1.3556 -qed
  1.3557 -
  1.3558 -lemma interior_real_semiline:
  1.3559 -  fixes a :: real
  1.3560 -  shows "interior {a..} = {a<..}"
  1.3561 -proof -
  1.3562 -  {
  1.3563 -    fix y
  1.3564 -    assume "a < y"
  1.3565 -    then have "y \<in> interior {a..}"
  1.3566 -      apply (simp add: mem_interior)
  1.3567 -      apply (rule_tac x="(y-a)" in exI)
  1.3568 -      apply (auto simp add: dist_norm)
  1.3569 -      done
  1.3570 -  }
  1.3571 -  moreover
  1.3572 -  {
  1.3573 -    fix y
  1.3574 -    assume "y \<in> interior {a..}"
  1.3575 -    then obtain e where e: "e > 0" "cball y e \<subseteq> {a..}"
  1.3576 -      using mem_interior_cball[of y "{a..}"] by auto
  1.3577 -    moreover from e have "y - e \<in> cball y e"
  1.3578 -      by (auto simp add: cball_def dist_norm)
  1.3579 -    ultimately have "a \<le> y - e" by blast
  1.3580 -    then have "a < y" using e by auto
  1.3581 -  }
  1.3582 -  ultimately show ?thesis by auto
  1.3583 -qed
  1.3584 -
  1.3585 -lemma continuous_ge_on_Ioo:
  1.3586 -  assumes "continuous_on {c..d} g" "\<And>x. x \<in> {c<..<d} \<Longrightarrow> g x \<ge> a" "c < d" "x \<in> {c..d}"
  1.3587 -  shows "g (x::real) \<ge> (a::real)"
  1.3588 -proof-
  1.3589 -  from assms(3) have "{c..d} = closure {c<..<d}" by (rule closure_greaterThanLessThan[symmetric])
  1.3590 -  also from assms(2) have "{c<..<d} \<subseteq> (g -` {a..} \<inter> {c..d})" by auto
  1.3591 -  hence "closure {c<..<d} \<subseteq> closure (g -` {a..} \<inter> {c..d})" by (rule closure_mono)
  1.3592 -  also from assms(1) have "closed (g -` {a..} \<inter> {c..d})"
  1.3593 -    by (auto simp: continuous_on_closed_vimage)
  1.3594 -  hence "closure (g -` {a..} \<inter> {c..d}) = g -` {a..} \<inter> {c..d}" by simp
  1.3595 -  finally show ?thesis using \<open>x \<in> {c..d}\<close> by auto
  1.3596 -qed
  1.3597 -
  1.3598 -lemma interior_real_semiline':
  1.3599 -  fixes a :: real
  1.3600 -  shows "interior {..a} = {..<a}"
  1.3601 -proof -
  1.3602 -  {
  1.3603 -    fix y
  1.3604 -    assume "a > y"
  1.3605 -    then have "y \<in> interior {..a}"
  1.3606 -      apply (simp add: mem_interior)
  1.3607 -      apply (rule_tac x="(a-y)" in exI)
  1.3608 -      apply (auto simp add: dist_norm)
  1.3609 -      done
  1.3610 -  }
  1.3611 -  moreover
  1.3612 -  {
  1.3613 -    fix y
  1.3614 -    assume "y \<in> interior {..a}"
  1.3615 -    then obtain e where e: "e > 0" "cball y e \<subseteq> {..a}"
  1.3616 -      using mem_interior_cball[of y "{..a}"] by auto
  1.3617 -    moreover from e have "y + e \<in> cball y e"
  1.3618 -      by (auto simp add: cball_def dist_norm)
  1.3619 -    ultimately have "a \<ge> y + e" by auto
  1.3620 -    then have "a > y" using e by auto
  1.3621 -  }
  1.3622 -  ultimately show ?thesis by auto
  1.3623 -qed
  1.3624 -
  1.3625 -lemma interior_atLeastAtMost_real: "interior {a..b} = {a<..<b :: real}"
  1.3626 -proof-
  1.3627 -  have "{a..b} = {a..} \<inter> {..b}" by auto
  1.3628 -  also have "interior ... = {a<..} \<inter> {..<b}"
  1.3629 -    by (simp add: interior_real_semiline interior_real_semiline')
  1.3630 -  also have "... = {a<..<b}" by auto
  1.3631 -  finally show ?thesis .
  1.3632 -qed
  1.3633 -
  1.3634 -lemma frontier_real_Iic:
  1.3635 -  fixes a :: real
  1.3636 -  shows "frontier {..a} = {a}"
  1.3637 -  unfolding frontier_def by (auto simp add: interior_real_semiline')
  1.3638 -
  1.3639 -lemma rel_interior_real_box:
  1.3640 -  fixes a b :: real
  1.3641 -  assumes "a < b"
  1.3642 -  shows "rel_interior {a .. b} = {a <..< b}"
  1.3643 -proof -
  1.3644 -  have "box a b \<noteq> {}"
  1.3645 -    using assms
  1.3646 -    unfolding set_eq_iff
  1.3647 -    by (auto intro!: exI[of _ "(a + b) / 2"] simp: box_def)
  1.3648 -  then show ?thesis
  1.3649 -    using interior_rel_interior_gen[of "cbox a b", symmetric]
  1.3650 -    by (simp split: if_split_asm del: box_real add: box_real[symmetric] interior_cbox)
  1.3651 -qed
  1.3652 -
  1.3653 -lemma rel_interior_real_semiline:
  1.3654 -  fixes a :: real
  1.3655 -  shows "rel_interior {a..} = {a<..}"
  1.3656 -proof -
  1.3657 -  have *: "{a<..} \<noteq> {}"
  1.3658 -    unfolding set_eq_iff by (auto intro!: exI[of _ "a + 1"])
  1.3659 -  then show ?thesis using interior_real_semiline interior_rel_interior_gen[of "{a..}"]
  1.3660 -    by (auto split: if_split_asm)
  1.3661 -qed
  1.3662 -
  1.3663 -subsubsection \<open>Relative open sets\<close>
  1.3664 -
  1.3665 -definition "rel_open S \<longleftrightarrow> rel_interior S = S"
  1.3666 -
  1.3667 -lemma rel_open: "rel_open S \<longleftrightarrow> openin (subtopology euclidean (affine hull S)) S"
  1.3668 -  unfolding rel_open_def rel_interior_def
  1.3669 -  apply auto
  1.3670 -  using openin_subopen[of "subtopology euclidean (affine hull S)" S]
  1.3671 -  apply auto
  1.3672 -  done
  1.3673 -
  1.3674 -lemma openin_rel_interior: "openin (subtopology euclidean (affine hull S)) (rel_interior S)"
  1.3675 -  apply (simp add: rel_interior_def)
  1.3676 -  apply (subst openin_subopen)
  1.3677 -  apply blast
  1.3678 -  done
  1.3679 -
  1.3680 -lemma openin_set_rel_interior:
  1.3681 -   "openin (subtopology euclidean S) (rel_interior S)"
  1.3682 -by (rule openin_subset_trans [OF openin_rel_interior rel_interior_subset hull_subset])
  1.3683 -
  1.3684 -lemma affine_rel_open:
  1.3685 -  fixes S :: "'n::euclidean_space set"
  1.3686 -  assumes "affine S"
  1.3687 -  shows "rel_open S"
  1.3688 -  unfolding rel_open_def
  1.3689 -  using assms rel_interior_affine_hull[of S] affine_hull_eq[of S]
  1.3690 -  by metis
  1.3691 -
  1.3692 -lemma affine_closed:
  1.3693 -  fixes S :: "'n::euclidean_space set"
  1.3694 -  assumes "affine S"
  1.3695 -  shows "closed S"
  1.3696 -proof -
  1.3697 -  {
  1.3698 -    assume "S \<noteq> {}"
  1.3699 -    then obtain L where L: "subspace L" "affine_parallel S L"
  1.3700 -      using assms affine_parallel_subspace[of S] by auto
  1.3701 -    then obtain a where a: "S = (op + a ` L)"
  1.3702 -      using affine_parallel_def[of L S] affine_parallel_commut by auto
  1.3703 -    from L have "closed L" using closed_subspace by auto
  1.3704 -    then have "closed S"
  1.3705 -      using closed_translation a by auto
  1.3706 -  }
  1.3707 -  then show ?thesis by auto
  1.3708 -qed
  1.3709 -
  1.3710 -lemma closure_affine_hull:
  1.3711 -  fixes S :: "'n::euclidean_space set"
  1.3712 -  shows "closure S \<subseteq> affine hull S"
  1.3713 -  by (intro closure_minimal hull_subset affine_closed affine_affine_hull)
  1.3714 -
  1.3715 -lemma closure_same_affine_hull [simp]:
  1.3716 -  fixes S :: "'n::euclidean_space set"
  1.3717 -  shows "affine hull (closure S) = affine hull S"
  1.3718 -proof -
  1.3719 -  have "affine hull (closure S) \<subseteq> affine hull S"
  1.3720 -    using hull_mono[of "closure S" "affine hull S" "affine"]
  1.3721 -      closure_affine_hull[of S] hull_hull[of "affine" S]
  1.3722 -    by auto
  1.3723 -  moreover have "affine hull (closure S) \<supseteq> affine hull S"
  1.3724 -    using hull_mono[of "S" "closure S" "affine"] closure_subset by auto
  1.3725 -  ultimately show ?thesis by auto
  1.3726 -qed
  1.3727 -
  1.3728 -lemma closure_aff_dim [simp]:
  1.3729 -  fixes S :: "'n::euclidean_space set"
  1.3730 -  shows "aff_dim (closure S) = aff_dim S"
  1.3731 -proof -
  1.3732 -  have "aff_dim S \<le> aff_dim (closure S)"
  1.3733 -    using aff_dim_subset closure_subset by auto
  1.3734 -  moreover have "aff_dim (closure S) \<le> aff_dim (affine hull S)"
  1.3735 -    using aff_dim_subset closure_affine_hull by blast
  1.3736 -  moreover have "aff_dim (affine hull S) = aff_dim S"
  1.3737 -    using aff_dim_affine_hull by auto
  1.3738 -  ultimately show ?thesis by auto
  1.3739 -qed
  1.3740 -
  1.3741 -lemma rel_interior_closure_convex_shrink:
  1.3742 -  fixes S :: "_::euclidean_space set"
  1.3743 -  assumes "convex S"
  1.3744 -    and "c \<in> rel_interior S"
  1.3745 -    and "x \<in> closure S"
  1.3746 -    and "e > 0"
  1.3747 -    and "e \<le> 1"
  1.3748 -  shows "x - e *\<^sub>R (x - c) \<in> rel_interior S"
  1.3749 -proof -
  1.3750 -  obtain d where "d > 0" and d: "ball c d \<inter> affine hull S \<subseteq> S"
  1.3751 -    using assms(2) unfolding mem_rel_interior_ball by auto
  1.3752 -  have "\<exists>y \<in> S. norm (y - x) * (1 - e) < e * d"
  1.3753 -  proof (cases "x \<in> S")
  1.3754 -    case True
  1.3755 -    then show ?thesis using \<open>e > 0\<close> \<open>d > 0\<close>
  1.3756 -      apply (rule_tac bexI[where x=x])
  1.3757 -      apply (auto)
  1.3758 -      done
  1.3759 -  next
  1.3760 -    case False
  1.3761 -    then have x: "x islimpt S"
  1.3762 -      using assms(3)[unfolded closure_def] by auto
  1.3763 -    show ?thesis
  1.3764 -    proof (cases "e = 1")
  1.3765 -      case True
  1.3766 -      obtain y where "y \<in> S" "y \<noteq> x" "dist y x < 1"
  1.3767 -        using x[unfolded islimpt_approachable,THEN spec[where x=1]] by auto
  1.3768 -      then show ?thesis
  1.3769 -        apply (rule_tac x=y in bexI)
  1.3770 -        unfolding True
  1.3771 -        using \<open>d > 0\<close>
  1.3772 -        apply auto
  1.3773 -        done
  1.3774 -    next
  1.3775 -      case False
  1.3776 -      then have "0 < e * d / (1 - e)" and *: "1 - e > 0"
  1.3777 -        using \<open>e \<le> 1\<close> \<open>e > 0\<close> \<open>d > 0\<close> by (auto)
  1.3778 -      then obtain y where "y \<in> S" "y \<noteq> x" "dist y x < e * d / (1 - e)"
  1.3779 -        using x[unfolded islimpt_approachable,THEN spec[where x="e*d / (1 - e)"]] by auto
  1.3780 -      then show ?thesis
  1.3781 -        apply (rule_tac x=y in bexI)
  1.3782 -        unfolding dist_norm
  1.3783 -        using pos_less_divide_eq[OF *]
  1.3784 -        apply auto
  1.3785 -        done
  1.3786 -    qed
  1.3787 -  qed
  1.3788 -  then obtain y where "y \<in> S" and y: "norm (y - x) * (1 - e) < e * d"
  1.3789 -    by auto
  1.3790 -  define z where "z = c + ((1 - e) / e) *\<^sub>R (x - y)"
  1.3791 -  have *: "x - e *\<^sub>R (x - c) = y - e *\<^sub>R (y - z)"
  1.3792 -    unfolding z_def using \<open>e > 0\<close>
  1.3793 -    by (auto simp add: scaleR_right_diff_distrib scaleR_right_distrib scaleR_left_diff_distrib)
  1.3794 -  have zball: "z \<in> ball c d"
  1.3795 -    using mem_ball z_def dist_norm[of c]
  1.3796 -    using y and assms(4,5)
  1.3797 -    by (auto simp add:field_simps norm_minus_commute)
  1.3798 -  have "x \<in> affine hull S"
  1.3799 -    using closure_affine_hull assms by auto
  1.3800 -  moreover have "y \<in> affine hull S"
  1.3801 -    using \<open>y \<in> S\<close> hull_subset[of S] by auto
  1.3802 -  moreover have "c \<in> affine hull S"
  1.3803 -    using assms rel_interior_subset hull_subset[of S] by auto
  1.3804 -  ultimately have "z \<in> affine hull S"
  1.3805 -    using z_def affine_affine_hull[of S]
  1.3806 -      mem_affine_3_minus [of "affine hull S" c x y "(1 - e) / e"]
  1.3807 -      assms
  1.3808 -    by (auto simp add: field_simps)
  1.3809 -  then have "z \<in> S" using d zball by auto
  1.3810 -  obtain d1 where "d1 > 0" and d1: "ball z d1 \<le> ball c d"
  1.3811 -    using zball open_ball[of c d] openE[of "ball c d" z] by auto
  1.3812 -  then have "ball z d1 \<inter> affine hull S \<subseteq> ball c d \<inter> affine hull S"
  1.3813 -    by auto
  1.3814 -  then have "ball z d1 \<inter> affine hull S \<subseteq> S"
  1.3815 -    using d by auto
  1.3816 -  then have "z \<in> rel_interior S"
  1.3817 -    using mem_rel_interior_ball using \<open>d1 > 0\<close> \<open>z \<in> S\<close> by auto
  1.3818 -  then have "y - e *\<^sub>R (y - z) \<in> rel_interior S"
  1.3819 -    using rel_interior_convex_shrink[of S z y e] assms \<open>y \<in> S\<close> by auto
  1.3820 -  then show ?thesis using * by auto
  1.3821 -qed
  1.3822 -
  1.3823 -lemma rel_interior_eq:
  1.3824 -   "rel_interior s = s \<longleftrightarrow> openin(subtopology euclidean (affine hull s)) s"
  1.3825 -using rel_open rel_open_def by blast
  1.3826 -
  1.3827 -lemma rel_interior_openin:
  1.3828 -   "openin(subtopology euclidean (affine hull s)) s \<Longrightarrow> rel_interior s = s"
  1.3829 -by (simp add: rel_interior_eq)
  1.3830 -
  1.3831 -lemma rel_interior_affine:
  1.3832 -  fixes S :: "'n::euclidean_space set"
  1.3833 -  shows  "affine S \<Longrightarrow> rel_interior S = S"
  1.3834 -using affine_rel_open rel_open_def by auto
  1.3835 -
  1.3836 -lemma rel_interior_eq_closure:
  1.3837 -  fixes S :: "'n::euclidean_space set"
  1.3838 -  shows "rel_interior S = closure S \<longleftrightarrow> affine S"
  1.3839 -proof (cases "S = {}")
  1.3840 -  case True
  1.3841 - then show ?thesis
  1.3842 -    by auto
  1.3843 -next
  1.3844 -  case False show ?thesis
  1.3845 -  proof
  1.3846 -    assume eq: "rel_interior S = closure S"
  1.3847 -    have "S = {} \<or> S = affine hull S"
  1.3848 -      apply (rule connected_clopen [THEN iffD1, rule_format])
  1.3849 -       apply (simp add: affine_imp_convex convex_connected)
  1.3850 -      apply (rule conjI)
  1.3851 -       apply (metis eq closure_subset openin_rel_interior rel_interior_subset subset_antisym)
  1.3852 -      apply (metis closed_subset closure_subset_eq eq hull_subset rel_interior_subset)
  1.3853 -      done
  1.3854 -    with False have "affine hull S = S"
  1.3855 -      by auto
  1.3856 -    then show "affine S"
  1.3857 -      by (metis affine_hull_eq)
  1.3858 -  next
  1.3859 -    assume "affine S"
  1.3860 -    then show "rel_interior S = closure S"
  1.3861 -      by (simp add: rel_interior_affine affine_closed)
  1.3862 -  qed
  1.3863 -qed
  1.3864 -
  1.3865 -
  1.3866 -subsubsection\<open>Relative interior preserves under linear transformations\<close>
  1.3867 -
  1.3868 -lemma rel_interior_translation_aux:
  1.3869 -  fixes a :: "'n::euclidean_space"
  1.3870 -  shows "((\<lambda>x. a + x) ` rel_interior S) \<subseteq> rel_interior ((\<lambda>x. a + x) ` S)"
  1.3871 -proof -
  1.3872 -  {
  1.3873 -    fix x
  1.3874 -    assume x: "x \<in> rel_interior S"
  1.3875 -    then obtain T where "open T" "x \<in> T \<inter> S" "T \<inter> affine hull S \<subseteq> S"
  1.3876 -      using mem_rel_interior[of x S] by auto
  1.3877 -    then have "open ((\<lambda>x. a + x) ` T)"
  1.3878 -      and "a + x \<in> ((\<lambda>x. a + x) ` T) \<inter> ((\<lambda>x. a + x) ` S)"
  1.3879 -      and "((\<lambda>x. a + x) ` T) \<inter> affine hull ((\<lambda>x. a + x) ` S) \<subseteq> (\<lambda>x. a + x) ` S"
  1.3880 -      using affine_hull_translation[of a S] open_translation[of T a] x by auto
  1.3881 -    then have "a + x \<in> rel_interior ((\<lambda>x. a + x) ` S)"
  1.3882 -      using mem_rel_interior[of "a+x" "((\<lambda>x. a + x) ` S)"] by auto
  1.3883 -  }
  1.3884 -  then show ?thesis by auto
  1.3885 -qed
  1.3886 -
  1.3887 -lemma rel_interior_translation:
  1.3888 -  fixes a :: "'n::euclidean_space"
  1.3889 -  shows "rel_interior ((\<lambda>x. a + x) ` S) = (\<lambda>x. a + x) ` rel_interior S"
  1.3890 -proof -
  1.3891 -  have "(\<lambda>x. (-a) + x) ` rel_interior ((\<lambda>x. a + x) ` S) \<subseteq> rel_interior S"
  1.3892 -    using rel_interior_translation_aux[of "-a" "(\<lambda>x. a + x) ` S"]
  1.3893 -      translation_assoc[of "-a" "a"]
  1.3894 -    by auto
  1.3895 -  then have "((\<lambda>x. a + x) ` rel_interior S) \<supseteq> rel_interior ((\<lambda>x. a + x) ` S)"
  1.3896 -    using translation_inverse_subset[of a "rel_interior (op + a ` S)" "rel_interior S"]
  1.3897 -    by auto
  1.3898 -  then show ?thesis
  1.3899 -    using rel_interior_translation_aux[of a S] by auto
  1.3900 -qed
  1.3901 -
  1.3902 -
  1.3903 -lemma affine_hull_linear_image:
  1.3904 -  assumes "bounded_linear f"
  1.3905 -  shows "f ` (affine hull s) = affine hull f ` s"
  1.3906 -  apply rule
  1.3907 -  unfolding subset_eq ball_simps
  1.3908 -  apply (rule_tac[!] hull_induct, rule hull_inc)
  1.3909 -  prefer 3
  1.3910 -  apply (erule imageE)
  1.3911 -  apply (rule_tac x=xa in image_eqI)
  1.3912 -  apply assumption
  1.3913 -  apply (rule hull_subset[unfolded subset_eq, rule_format])
  1.3914 -  apply assumption
  1.3915 -proof -
  1.3916 -  interpret f: bounded_linear f by fact
  1.3917 -  show "affine {x. f x \<in> affine hull f ` s}"
  1.3918 -    unfolding affine_def
  1.3919 -    by (auto simp add: f.scaleR f.add affine_affine_hull[unfolded affine_def, rule_format])
  1.3920 -  show "affine {x. x \<in> f ` (affine hull s)}"
  1.3921 -    using affine_affine_hull[unfolded affine_def, of s]
  1.3922 -    unfolding affine_def by (auto simp add: f.scaleR [symmetric] f.add [symmetric])
  1.3923 -qed auto
  1.3924 -
  1.3925 -
  1.3926 -lemma rel_interior_injective_on_span_linear_image:
  1.3927 -  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
  1.3928 -    and S :: "'m::euclidean_space set"
  1.3929 -  assumes "bounded_linear f"
  1.3930 -    and "inj_on f (span S)"
  1.3931 -  shows "rel_interior (f ` S) = f ` (rel_interior S)"
  1.3932 -proof -
  1.3933 -  {
  1.3934 -    fix z
  1.3935 -    assume z: "z \<in> rel_interior (f ` S)"
  1.3936 -    then have "z \<in> f ` S"
  1.3937 -      using rel_interior_subset[of "f ` S"] by auto
  1.3938 -    then obtain x where x: "x \<in> S" "f x = z" by auto
  1.3939 -    obtain e2 where e2: "e2 > 0" "cball z e2 \<inter> affine hull (f ` S) \<subseteq> (f ` S)"
  1.3940 -      using z rel_interior_cball[of "f ` S"] by auto
  1.3941 -    obtain K where K: "K > 0" "\<And>x. norm (f x) \<le> norm x * K"
  1.3942 -     using assms Real_Vector_Spaces.bounded_linear.pos_bounded[of f] by auto
  1.3943 -    define e1 where "e1 = 1 / K"
  1.3944 -    then have e1: "e1 > 0" "\<And>x. e1 * norm (f x) \<le> norm x"
  1.3945 -      using K pos_le_divide_eq[of e1] by auto
  1.3946 -    define e where "e = e1 * e2"
  1.3947 -    then have "e > 0" using e1 e2 by auto
  1.3948 -    {
  1.3949 -      fix y
  1.3950 -      assume y: "y \<in> cball x e \<inter> affine hull S"
  1.3951 -      then have h1: "f y \<in> affine hull (f ` S)"
  1.3952 -        using affine_hull_linear_image[of f S] assms by auto
  1.3953 -      from y have "norm (x-y) \<le> e1 * e2"
  1.3954 -        using cball_def[of x e] dist_norm[of x y] e_def by auto
  1.3955 -      moreover have "f x - f y = f (x - y)"
  1.3956 -        using assms linear_diff[of f x y] linear_conv_bounded_linear[of f] by auto
  1.3957 -      moreover have "e1 * norm (f (x-y)) \<le> norm (x - y)"
  1.3958 -        using e1 by auto
  1.3959 -      ultimately have "e1 * norm ((f x)-(f y)) \<le> e1 * e2"
  1.3960 -        by auto
  1.3961 -      then have "f y \<in> cball z e2"
  1.3962 -        using cball_def[of "f x" e2] dist_norm[of "f x" "f y"] e1 x by auto
  1.3963 -      then have "f y \<in> f ` S"
  1.3964 -        using y e2 h1 by auto
  1.3965 -      then have "y \<in> S"
  1.3966 -        using assms y hull_subset[of S] affine_hull_subset_span
  1.3967 -          inj_on_image_mem_iff [OF \<open>inj_on f (span S)\<close>]
  1.3968 -        by (metis Int_iff span_inc subsetCE)
  1.3969 -    }
  1.3970 -    then have "z \<in> f ` (rel_interior S)"
  1.3971 -      using mem_rel_interior_cball[of x S] \<open>e > 0\<close> x by auto
  1.3972 -  }
  1.3973 -  moreover
  1.3974 -  {
  1.3975 -    fix x
  1.3976 -    assume x: "x \<in> rel_interior S"
  1.3977 -    then obtain e2 where e2: "e2 > 0" "cball x e2 \<inter> affine hull S \<subseteq> S"
  1.3978 -      using rel_interior_cball[of S] by auto
  1.3979 -    have "x \<in> S" using x rel_interior_subset by auto
  1.3980 -    then have *: "f x \<in> f ` S" by auto
  1.3981 -    have "\<forall>x\<in>span S. f x = 0 \<longrightarrow> x = 0"
  1.3982 -      using assms subspace_span linear_conv_bounded_linear[of f]
  1.3983 -        linear_injective_on_subspace_0[of f "span S"]
  1.3984 -      by auto
  1.3985 -    then obtain e1 where e1: "e1 > 0" "\<forall>x \<in> span S. e1 * norm x \<le> norm (f x)"
  1.3986 -      using assms injective_imp_isometric[of "span S" f]
  1.3987 -        subspace_span[of S] closed_subspace[of "span S"]
  1.3988 -      by auto
  1.3989 -    define e where "e = e1 * e2"
  1.3990 -    hence "e > 0" using e1 e2 by auto
  1.3991 -    {
  1.3992 -      fix y
  1.3993 -      assume y: "y \<in> cball (f x) e \<inter> affine hull (f ` S)"
  1.3994 -      then have "y \<in> f ` (affine hull S)"
  1.3995 -        using affine_hull_linear_image[of f S] assms by auto
  1.3996 -      then obtain xy where xy: "xy \<in> affine hull S" "f xy = y" by auto
  1.3997 -      with y have "norm (f x - f xy) \<le> e1 * e2"
  1.3998 -        using cball_def[of "f x" e] dist_norm[of "f x" y] e_def by auto
  1.3999 -      moreover have "f x - f xy = f (x - xy)"
  1.4000 -        using assms linear_diff[of f x xy] linear_conv_bounded_linear[of f] by auto
  1.4001 -      moreover have *: "x - xy \<in> span S"
  1.4002 -        using subspace_diff[of "span S" x xy] subspace_span \<open>x \<in> S\<close> xy
  1.4003 -          affine_hull_subset_span[of S] span_inc
  1.4004 -        by auto
  1.4005 -      moreover from * have "e1 * norm (x - xy) \<le> norm (f (x - xy))"
  1.4006 -        using e1 by auto
  1.4007 -      ultimately have "e1 * norm (x - xy) \<le> e1 * e2"
  1.4008 -        by auto
  1.4009 -      then have "xy \<in> cball x e2"
  1.4010 -        using cball_def[of x e2] dist_norm[of x xy] e1 by auto
  1.4011 -      then have "y \<in> f ` S"
  1.4012 -        using xy e2 by auto
  1.4013 -    }
  1.4014 -    then have "f x \<in> rel_interior (f ` S)"
  1.4015 -      using mem_rel_interior_cball[of "(f x)" "(f ` S)"] * \<open>e > 0\<close> by auto
  1.4016 -  }
  1.4017 -  ultimately show ?thesis by auto
  1.4018 -qed
  1.4019 -
  1.4020 -lemma rel_interior_injective_linear_image:
  1.4021 -  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
  1.4022 -  assumes "bounded_linear f"
  1.4023 -    and "inj f"
  1.4024 -  shows "rel_interior (f ` S) = f ` (rel_interior S)"
  1.4025 -  using assms rel_interior_injective_on_span_linear_image[of f S]
  1.4026 -    subset_inj_on[of f "UNIV" "span S"]
  1.4027 -  by auto
  1.4028 -
  1.4029 -
  1.4030 -subsection\<open>Some Properties of subset of standard basis\<close>
  1.4031 -
  1.4032 -lemma affine_hull_substd_basis:
  1.4033 -  assumes "d \<subseteq> Basis"
  1.4034 -  shows "affine hull (insert 0 d) = {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.4035 -  (is "affine hull (insert 0 ?A) = ?B")
  1.4036 -proof -
  1.4037 -  have *: "\<And>A. op + (0::'a) ` A = A" "\<And>A. op + (- (0::'a)) ` A = A"
  1.4038 -    by auto
  1.4039 -  show ?thesis
  1.4040 -    unfolding affine_hull_insert_span_gen span_substd_basis[OF assms,symmetric] * ..
  1.4041 -qed
  1.4042 -
  1.4043 -lemma affine_hull_convex_hull [simp]: "affine hull (convex hull S) = affine hull S"
  1.4044 -  by (metis Int_absorb1 Int_absorb2 convex_hull_subset_affine_hull hull_hull hull_mono hull_subset)
  1.4045 -
  1.4046 -
  1.4047 -subsection \<open>Openness and compactness are preserved by convex hull operation.\<close>
  1.4048 -
  1.4049 -lemma open_convex_hull[intro]:
  1.4050 -  fixes s :: "'a::real_normed_vector set"
  1.4051 -  assumes "open s"
  1.4052 -  shows "open (convex hull s)"
  1.4053 -  unfolding open_contains_cball convex_hull_explicit
  1.4054 -  unfolding mem_Collect_eq ball_simps(8)
  1.4055 -proof (rule, rule)
  1.4056 -  fix a
  1.4057 -  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"
  1.4058 -  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"
  1.4059 -    by auto
  1.4060 -
  1.4061 -  from assms[unfolded open_contains_cball] obtain b
  1.4062 -    where b: "\<forall>x\<in>s. 0 < b x \<and> cball x (b x) \<subseteq> s"
  1.4063 -    using bchoice[of s "\<lambda>x e. e > 0 \<and> cball x e \<subseteq> s"] by auto
  1.4064 -  have "b ` t \<noteq> {}"
  1.4065 -    using obt by auto
  1.4066 -  define i where "i = b ` t"
  1.4067 -
  1.4068 -  show "\<exists>e > 0.
  1.4069 -    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}"
  1.4070 -    apply (rule_tac x = "Min i" in exI)
  1.4071 -    unfolding subset_eq
  1.4072 -    apply rule
  1.4073 -    defer
  1.4074 -    apply rule
  1.4075 -    unfolding mem_Collect_eq
  1.4076 -  proof -
  1.4077 -    show "0 < Min i"
  1.4078 -      unfolding i_def and Min_gr_iff[OF finite_imageI[OF obt(1)] \<open>b ` t\<noteq>{}\<close>]
  1.4079 -      using b
  1.4080 -      apply simp
  1.4081 -      apply rule
  1.4082 -      apply (erule_tac x=x in ballE)
  1.4083 -      using \<open>t\<subseteq>s\<close>
  1.4084 -      apply auto
  1.4085 -      done
  1.4086 -  next
  1.4087 -    fix y
  1.4088 -    assume "y \<in> cball a (Min i)"
  1.4089 -    then have y: "norm (a - y) \<le> Min i"
  1.4090 -      unfolding dist_norm[symmetric] by auto
  1.4091 -    {
  1.4092 -      fix x
  1.4093 -      assume "x \<in> t"
  1.4094 -      then have "Min i \<le> b x"
  1.4095 -        unfolding i_def
  1.4096 -        apply (rule_tac Min_le)
  1.4097 -        using obt(1)
  1.4098 -        apply auto
  1.4099 -        done
  1.4100 -      then have "x + (y - a) \<in> cball x (b x)"
  1.4101 -        using y unfolding mem_cball dist_norm by auto
  1.4102 -      moreover from \<open>x\<in>t\<close> have "x \<in> s"
  1.4103 -        using obt(2) by auto
  1.4104 -      ultimately have "x + (y - a) \<in> s"
  1.4105 -        using y and b[THEN bspec[where x=x]] unfolding subset_eq by fast
  1.4106 -    }
  1.4107 -    moreover
  1.4108 -    have *: "inj_on (\<lambda>v. v + (y - a)) t"
  1.4109 -      unfolding inj_on_def by auto
  1.4110 -    have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a))) = 1"
  1.4111 -      unfolding setsum.reindex[OF *] o_def using obt(4) by auto
  1.4112 -    moreover have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a)) *\<^sub>R v) = y"
  1.4113 -      unfolding setsum.reindex[OF *] o_def using obt(4,5)
  1.4114 -      by (simp add: setsum.distrib setsum_subtractf scaleR_left.setsum[symmetric] scaleR_right_distrib)
  1.4115 -    ultimately
  1.4116 -    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"
  1.4117 -      apply (rule_tac x="(\<lambda>v. v + (y - a)) ` t" in exI)
  1.4118 -      apply (rule_tac x="\<lambda>v. u (v - (y - a))" in exI)
  1.4119 -      using obt(1, 3)
  1.4120 -      apply auto
  1.4121 -      done
  1.4122 -  qed
  1.4123 -qed
  1.4124 -
  1.4125 -lemma compact_convex_combinations:
  1.4126 -  fixes s t :: "'a::real_normed_vector set"
  1.4127 -  assumes "compact s" "compact t"
  1.4128 -  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}"
  1.4129 -proof -
  1.4130 -  let ?X = "{0..1} \<times> s \<times> t"
  1.4131 -  let ?h = "(\<lambda>z. (1 - fst z) *\<^sub>R fst (snd z) + fst z *\<^sub>R snd (snd z))"
  1.4132 -  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"
  1.4133 -    apply (rule set_eqI)
  1.4134 -    unfolding image_iff mem_Collect_eq
  1.4135 -    apply rule
  1.4136 -    apply auto
  1.4137 -    apply (rule_tac x=u in rev_bexI)
  1.4138 -    apply simp
  1.4139 -    apply (erule rev_bexI)
  1.4140 -    apply (erule rev_bexI)
  1.4141 -    apply simp
  1.4142 -    apply auto
  1.4143 -    done
  1.4144 -  have "continuous_on ?X (\<lambda>z. (1 - fst z) *\<^sub>R fst (snd z) + fst z *\<^sub>R snd (snd z))"
  1.4145 -    unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  1.4146 -  then show ?thesis
  1.4147 -    unfolding *
  1.4148 -    apply (rule compact_continuous_image)
  1.4149 -    apply (intro compact_Times compact_Icc assms)
  1.4150 -    done
  1.4151 -qed
  1.4152 -
  1.4153 -lemma finite_imp_compact_convex_hull:
  1.4154 -  fixes s :: "'a::real_normed_vector set"
  1.4155 -  assumes "finite s"
  1.4156 -  shows "compact (convex hull s)"
  1.4157 -proof (cases "s = {}")
  1.4158 -  case True
  1.4159 -  then show ?thesis by simp
  1.4160 -next
  1.4161 -  case False
  1.4162 -  with assms show ?thesis
  1.4163 -  proof (induct rule: finite_ne_induct)
  1.4164 -    case (singleton x)
  1.4165 -    show ?case by simp
  1.4166 -  next
  1.4167 -    case (insert x A)
  1.4168 -    let ?f = "\<lambda>(u, y::'a). u *\<^sub>R x + (1 - u) *\<^sub>R y"
  1.4169 -    let ?T = "{0..1::real} \<times> (convex hull A)"
  1.4170 -    have "continuous_on ?T ?f"
  1.4171 -      unfolding split_def continuous_on by (intro ballI tendsto_intros)
  1.4172 -    moreover have "compact ?T"
  1.4173 -      by (intro compact_Times compact_Icc insert)
  1.4174 -    ultimately have "compact (?f ` ?T)"
  1.4175 -      by (rule compact_continuous_image)
  1.4176 -    also have "?f ` ?T = convex hull (insert x A)"
  1.4177 -      unfolding convex_hull_insert [OF \<open>A \<noteq> {}\<close>]
  1.4178 -      apply safe
  1.4179 -      apply (rule_tac x=a in exI, simp)
  1.4180 -      apply (rule_tac x="1 - a" in exI, simp)
  1.4181 -      apply fast
  1.4182 -      apply (rule_tac x="(u, b)" in image_eqI, simp_all)
  1.4183 -      done
  1.4184 -    finally show "compact (convex hull (insert x A))" .
  1.4185 -  qed
  1.4186 -qed
  1.4187 -
  1.4188 -lemma compact_convex_hull:
  1.4189 -  fixes s :: "'a::euclidean_space set"
  1.4190 -  assumes "compact s"
  1.4191 -  shows "compact (convex hull s)"
  1.4192 -proof (cases "s = {}")
  1.4193 -  case True
  1.4194 -  then show ?thesis using compact_empty by simp
  1.4195 -next
  1.4196 -  case False
  1.4197 -  then obtain w where "w \<in> s" by auto
  1.4198 -  show ?thesis
  1.4199 -    unfolding caratheodory[of s]
  1.4200 -  proof (induct ("DIM('a) + 1"))
  1.4201 -    case 0
  1.4202 -    have *: "{x.\<exists>sa. finite sa \<and> sa \<subseteq> s \<and> card sa \<le> 0 \<and> x \<in> convex hull sa} = {}"
  1.4203 -      using compact_empty by auto
  1.4204 -    from 0 show ?case unfolding * by simp
  1.4205 -  next
  1.4206 -    case (Suc n)
  1.4207 -    show ?case
  1.4208 -    proof (cases "n = 0")
  1.4209 -      case True
  1.4210 -      have "{x. \<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t} = s"
  1.4211 -        unfolding set_eq_iff and mem_Collect_eq
  1.4212 -      proof (rule, rule)
  1.4213 -        fix x
  1.4214 -        assume "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  1.4215 -        then obtain t where t: "finite t" "t \<subseteq> s" "card t \<le> Suc n" "x \<in> convex hull t"
  1.4216 -          by auto
  1.4217 -        show "x \<in> s"
  1.4218 -        proof (cases "card t = 0")
  1.4219 -          case True
  1.4220 -          then show ?thesis
  1.4221 -            using t(4) unfolding card_0_eq[OF t(1)] by simp
  1.4222 -        next
  1.4223 -          case False
  1.4224 -          then have "card t = Suc 0" using t(3) \<open>n=0\<close> by auto
  1.4225 -          then obtain a where "t = {a}" unfolding card_Suc_eq by auto
  1.4226 -          then show ?thesis using t(2,4) by simp
  1.4227 -        qed
  1.4228 -      next
  1.4229 -        fix x assume "x\<in>s"
  1.4230 -        then show "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  1.4231 -          apply (rule_tac x="{x}" in exI)
  1.4232 -          unfolding convex_hull_singleton
  1.4233 -          apply auto
  1.4234 -          done
  1.4235 -      qed
  1.4236 -      then show ?thesis using assms by simp
  1.4237 -    next
  1.4238 -      case False
  1.4239 -      have "{x. \<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t} =
  1.4240 -        {(1 - u) *\<^sub>R x + u *\<^sub>R y | x y u.
  1.4241 -          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}}"
  1.4242 -        unfolding set_eq_iff and mem_Collect_eq
  1.4243 -      proof (rule, rule)
  1.4244 -        fix x
  1.4245 -        assume "\<exists>u v c. x = (1 - c) *\<^sub>R u + c *\<^sub>R v \<and>
  1.4246 -          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)"
  1.4247 -        then obtain u v c t where obt: "x = (1 - c) *\<^sub>R u + c *\<^sub>R v"
  1.4248 -          "0 \<le> c \<and> c \<le> 1" "u \<in> s" "finite t" "t \<subseteq> s" "card t \<le> n"  "v \<in> convex hull t"
  1.4249 -          by auto
  1.4250 -        moreover have "(1 - c) *\<^sub>R u + c *\<^sub>R v \<in> convex hull insert u t"
  1.4251 -          apply (rule convexD_alt)
  1.4252 -          using obt(2) and convex_convex_hull and hull_subset[of "insert u t" convex]
  1.4253 -          using obt(7) and hull_mono[of t "insert u t"]
  1.4254 -          apply auto
  1.4255 -          done
  1.4256 -        ultimately show "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  1.4257 -          apply (rule_tac x="insert u t" in exI)
  1.4258 -          apply (auto simp add: card_insert_if)
  1.4259 -          done
  1.4260 -      next
  1.4261 -        fix x
  1.4262 -        assume "\<exists>t. finite t \<and> t \<subseteq> s \<and> card t \<le> Suc n \<and> x \<in> convex hull t"
  1.4263 -        then obtain t where t: "finite t" "t \<subseteq> s" "card t \<le> Suc n" "x \<in> convex hull t"
  1.4264 -          by auto
  1.4265 -        show "\<exists>u v c. x = (1 - c) *\<^sub>R u + c *\<^sub>R v \<and>
  1.4266 -          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)"
  1.4267 -        proof (cases "card t = Suc n")
  1.4268 -          case False
  1.4269 -          then have "card t \<le> n" using t(3) by auto
  1.4270 -          then show ?thesis
  1.4271 -            apply (rule_tac x=w in exI, rule_tac x=x in exI, rule_tac x=1 in exI)
  1.4272 -            using \<open>w\<in>s\<close> and t
  1.4273 -            apply (auto intro!: exI[where x=t])
  1.4274 -            done
  1.4275 -        next
  1.4276 -          case True
  1.4277 -          then obtain a u where au: "t = insert a u" "a\<notin>u"
  1.4278 -            apply (drule_tac card_eq_SucD)
  1.4279 -            apply auto
  1.4280 -            done
  1.4281 -          show ?thesis
  1.4282 -          proof (cases "u = {}")
  1.4283 -            case True
  1.4284 -            then have "x = a" using t(4)[unfolded au] by auto
  1.4285 -            show ?thesis unfolding \<open>x = a\<close>
  1.4286 -              apply (rule_tac x=a in exI)
  1.4287 -              apply (rule_tac x=a in exI)
  1.4288 -              apply (rule_tac x=1 in exI)
  1.4289 -              using t and \<open>n \<noteq> 0\<close>
  1.4290 -              unfolding au
  1.4291 -              apply (auto intro!: exI[where x="{a}"])
  1.4292 -              done
  1.4293 -          next
  1.4294 -            case False
  1.4295 -            obtain ux vx b where obt: "ux\<ge>0" "vx\<ge>0" "ux + vx = 1"
  1.4296 -              "b \<in> convex hull u" "x = ux *\<^sub>R a + vx *\<^sub>R b"
  1.4297 -              using t(4)[unfolded au convex_hull_insert[OF False]]
  1.4298 -              by auto
  1.4299 -            have *: "1 - vx = ux" using obt(3) by auto
  1.4300 -            show ?thesis
  1.4301 -              apply (rule_tac x=a in exI)
  1.4302 -              apply (rule_tac x=b in exI)
  1.4303 -              apply (rule_tac x=vx in exI)
  1.4304 -              using obt and t(1-3)
  1.4305 -              unfolding au and * using card_insert_disjoint[OF _ au(2)]
  1.4306 -              apply (auto intro!: exI[where x=u])
  1.4307 -              done
  1.4308 -          qed
  1.4309 -        qed
  1.4310 -      qed
  1.4311 -      then show ?thesis
  1.4312 -        using compact_convex_combinations[OF assms Suc] by simp
  1.4313 -    qed
  1.4314 -  qed
  1.4315 -qed
  1.4316 -
  1.4317 -
  1.4318 -subsection \<open>Extremal points of a simplex are some vertices.\<close>
  1.4319 -
  1.4320 -lemma dist_increases_online:
  1.4321 -  fixes a b d :: "'a::real_inner"
  1.4322 -  assumes "d \<noteq> 0"
  1.4323 -  shows "dist a (b + d) > dist a b \<or> dist a (b - d) > dist a b"
  1.4324 -proof (cases "inner a d - inner b d > 0")
  1.4325 -  case True
  1.4326 -  then have "0 < inner d d + (inner a d * 2 - inner b d * 2)"
  1.4327 -    apply (rule_tac add_pos_pos)
  1.4328 -    using assms
  1.4329 -    apply auto
  1.4330 -    done
  1.4331 -  then show ?thesis
  1.4332 -    apply (rule_tac disjI2)
  1.4333 -    unfolding dist_norm and norm_eq_sqrt_inner and real_sqrt_less_iff
  1.4334 -    apply  (simp add: algebra_simps inner_commute)
  1.4335 -    done
  1.4336 -next
  1.4337 -  case False
  1.4338 -  then have "0 < inner d d + (inner b d * 2 - inner a d * 2)"
  1.4339 -    apply (rule_tac add_pos_nonneg)
  1.4340 -    using assms
  1.4341 -    apply auto
  1.4342 -    done
  1.4343 -  then show ?thesis
  1.4344 -    apply (rule_tac disjI1)
  1.4345 -    unfolding dist_norm and norm_eq_sqrt_inner and real_sqrt_less_iff
  1.4346 -    apply (simp add: algebra_simps inner_commute)
  1.4347 -    done
  1.4348 -qed
  1.4349 -
  1.4350 -lemma norm_increases_online:
  1.4351 -  fixes d :: "'a::real_inner"
  1.4352 -  shows "d \<noteq> 0 \<Longrightarrow> norm (a + d) > norm a \<or> norm(a - d) > norm a"
  1.4353 -  using dist_increases_online[of d a 0] unfolding dist_norm by auto
  1.4354 -
  1.4355 -lemma simplex_furthest_lt:
  1.4356 -  fixes s :: "'a::real_inner set"
  1.4357 -  assumes "finite s"
  1.4358 -  shows "\<forall>x \<in> convex hull s.  x \<notin> s \<longrightarrow> (\<exists>y \<in> convex hull s. norm (x - a) < norm(y - a))"
  1.4359 -  using assms
  1.4360 -proof induct
  1.4361 -  fix x s
  1.4362 -  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))"
  1.4363 -  show "\<forall>xa\<in>convex hull insert x s. xa \<notin> insert x s \<longrightarrow>
  1.4364 -    (\<exists>y\<in>convex hull insert x s. norm (xa - a) < norm (y - a))"
  1.4365 -  proof (rule, rule, cases "s = {}")
  1.4366 -    case False
  1.4367 -    fix y
  1.4368 -    assume y: "y \<in> convex hull insert x s" "y \<notin> insert x s"
  1.4369 -    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"
  1.4370 -      using y(1)[unfolded convex_hull_insert[OF False]] by auto
  1.4371 -    show "\<exists>z\<in>convex hull insert x s. norm (y - a) < norm (z - a)"
  1.4372 -    proof (cases "y \<in> convex hull s")
  1.4373 -      case True
  1.4374 -      then obtain z where "z \<in> convex hull s" "norm (y - a) < norm (z - a)"
  1.4375 -        using as(3)[THEN bspec[where x=y]] and y(2) by auto
  1.4376 -      then show ?thesis
  1.4377 -        apply (rule_tac x=z in bexI)
  1.4378 -        unfolding convex_hull_insert[OF False]
  1.4379 -        apply auto
  1.4380 -        done
  1.4381 -    next
  1.4382 -      case False
  1.4383 -      show ?thesis
  1.4384 -        using obt(3)
  1.4385 -      proof (cases "u = 0", case_tac[!] "v = 0")
  1.4386 -        assume "u = 0" "v \<noteq> 0"
  1.4387 -        then have "y = b" using obt by auto
  1.4388 -        then show ?thesis using False and obt(4) by auto
  1.4389 -      next
  1.4390 -        assume "u \<noteq> 0" "v = 0"
  1.4391 -        then have "y = x" using obt by auto
  1.4392 -        then show ?thesis using y(2) by auto
  1.4393 -      next
  1.4394 -        assume "u \<noteq> 0" "v \<noteq> 0"
  1.4395 -        then obtain w where w: "w>0" "w<u" "w<v"
  1.4396 -          using real_lbound_gt_zero[of u v] and obt(1,2) by auto
  1.4397 -        have "x \<noteq> b"
  1.4398 -        proof
  1.4399 -          assume "x = b"
  1.4400 -          then have "y = b" unfolding obt(5)
  1.4401 -            using obt(3) by (auto simp add: scaleR_left_distrib[symmetric])
  1.4402 -          then show False using obt(4) and False by simp
  1.4403 -        qed
  1.4404 -        then have *: "w *\<^sub>R (x - b) \<noteq> 0" using w(1) by auto
  1.4405 -        show ?thesis
  1.4406 -          using dist_increases_online[OF *, of a y]
  1.4407 -        proof (elim disjE)
  1.4408 -          assume "dist a y < dist a (y + w *\<^sub>R (x - b))"
  1.4409 -          then have "norm (y - a) < norm ((u + w) *\<^sub>R x + (v - w) *\<^sub>R b - a)"
  1.4410 -            unfolding dist_commute[of a]
  1.4411 -            unfolding dist_norm obt(5)
  1.4412 -            by (simp add: algebra_simps)
  1.4413 -          moreover have "(u + w) *\<^sub>R x + (v - w) *\<^sub>R b \<in> convex hull insert x s"
  1.4414 -            unfolding convex_hull_insert[OF \<open>s\<noteq>{}\<close>] and mem_Collect_eq
  1.4415 -            apply (rule_tac x="u + w" in exI)
  1.4416 -            apply rule
  1.4417 -            defer
  1.4418 -            apply (rule_tac x="v - w" in exI)
  1.4419 -            using \<open>u \<ge> 0\<close> and w and obt(3,4)
  1.4420 -            apply auto
  1.4421 -            done
  1.4422 -          ultimately show ?thesis by auto
  1.4423 -        next
  1.4424 -          assume "dist a y < dist a (y - w *\<^sub>R (x - b))"
  1.4425 -          then have "norm (y - a) < norm ((u - w) *\<^sub>R x + (v + w) *\<^sub>R b - a)"
  1.4426 -            unfolding dist_commute[of a]
  1.4427 -            unfolding dist_norm obt(5)
  1.4428 -            by (simp add: algebra_simps)
  1.4429 -          moreover have "(u - w) *\<^sub>R x + (v + w) *\<^sub>R b \<in> convex hull insert x s"
  1.4430 -            unfolding convex_hull_insert[OF \<open>s\<noteq>{}\<close>] and mem_Collect_eq
  1.4431 -            apply (rule_tac x="u - w" in exI)
  1.4432 -            apply rule
  1.4433 -            defer
  1.4434 -            apply (rule_tac x="v + w" in exI)
  1.4435 -            using \<open>u \<ge> 0\<close> and w and obt(3,4)
  1.4436 -            apply auto
  1.4437 -            done
  1.4438 -          ultimately show ?thesis by auto
  1.4439 -        qed
  1.4440 -      qed auto
  1.4441 -    qed
  1.4442 -  qed auto
  1.4443 -qed (auto simp add: assms)
  1.4444 -
  1.4445 -lemma simplex_furthest_le:
  1.4446 -  fixes s :: "'a::real_inner set"
  1.4447 -  assumes "finite s"
  1.4448 -    and "s \<noteq> {}"
  1.4449 -  shows "\<exists>y\<in>s. \<forall>x\<in> convex hull s. norm (x - a) \<le> norm (y - a)"
  1.4450 -proof -
  1.4451 -  have "convex hull s \<noteq> {}"
  1.4452 -    using hull_subset[of s convex] and assms(2) by auto
  1.4453 -  then obtain x where x: "x \<in> convex hull s" "\<forall>y\<in>convex hull s. norm (y - a) \<le> norm (x - a)"
  1.4454 -    using distance_attains_sup[OF finite_imp_compact_convex_hull[OF assms(1)], of a]
  1.4455 -    unfolding dist_commute[of a]
  1.4456 -    unfolding dist_norm
  1.4457 -    by auto
  1.4458 -  show ?thesis
  1.4459 -  proof (cases "x \<in> s")
  1.4460 -    case False
  1.4461 -    then obtain y where "y \<in> convex hull s" "norm (x - a) < norm (y - a)"
  1.4462 -      using simplex_furthest_lt[OF assms(1), THEN bspec[where x=x]] and x(1)
  1.4463 -      by auto
  1.4464 -    then show ?thesis
  1.4465 -      using x(2)[THEN bspec[where x=y]] by auto
  1.4466 -  next
  1.4467 -    case True
  1.4468 -    with x show ?thesis by auto
  1.4469 -  qed
  1.4470 -qed
  1.4471 -
  1.4472 -lemma simplex_furthest_le_exists:
  1.4473 -  fixes s :: "('a::real_inner) set"
  1.4474 -  shows "finite s \<Longrightarrow> \<forall>x\<in>(convex hull s). \<exists>y\<in>s. norm (x - a) \<le> norm (y - a)"
  1.4475 -  using simplex_furthest_le[of s] by (cases "s = {}") auto
  1.4476 -
  1.4477 -lemma simplex_extremal_le:
  1.4478 -  fixes s :: "'a::real_inner set"
  1.4479 -  assumes "finite s"
  1.4480 -    and "s \<noteq> {}"
  1.4481 -  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)"
  1.4482 -proof -
  1.4483 -  have "convex hull s \<noteq> {}"
  1.4484 -    using hull_subset[of s convex] and assms(2) by auto
  1.4485 -  then obtain u v where obt: "u \<in> convex hull s" "v \<in> convex hull s"
  1.4486 -    "\<forall>x\<in>convex hull s. \<forall>y\<in>convex hull s. norm (x - y) \<le> norm (u - v)"
  1.4487 -    using compact_sup_maxdistance[OF finite_imp_compact_convex_hull[OF assms(1)]]
  1.4488 -    by (auto simp: dist_norm)
  1.4489 -  then show ?thesis
  1.4490 -  proof (cases "u\<notin>s \<or> v\<notin>s", elim disjE)
  1.4491 -    assume "u \<notin> s"
  1.4492 -    then obtain y where "y \<in> convex hull s" "norm (u - v) < norm (y - v)"
  1.4493 -      using simplex_furthest_lt[OF assms(1), THEN bspec[where x=u]] and obt(1)
  1.4494 -      by auto
  1.4495 -    then show ?thesis
  1.4496 -      using obt(3)[THEN bspec[where x=y], THEN bspec[where x=v]] and obt(2)
  1.4497 -      by auto
  1.4498 -  next
  1.4499 -    assume "v \<notin> s"
  1.4500 -    then obtain y where "y \<in> convex hull s" "norm (v - u) < norm (y - u)"
  1.4501 -      using simplex_furthest_lt[OF assms(1), THEN bspec[where x=v]] and obt(2)
  1.4502 -      by auto
  1.4503 -    then show ?thesis
  1.4504 -      using obt(3)[THEN bspec[where x=u], THEN bspec[where x=y]] and obt(1)
  1.4505 -      by (auto simp add: norm_minus_commute)
  1.4506 -  qed auto
  1.4507 -qed
  1.4508 -
  1.4509 -lemma simplex_extremal_le_exists:
  1.4510 -  fixes s :: "'a::real_inner set"
  1.4511 -  shows "finite s \<Longrightarrow> x \<in> convex hull s \<Longrightarrow> y \<in> convex hull s \<Longrightarrow>
  1.4512 -    \<exists>u\<in>s. \<exists>v\<in>s. norm (x - y) \<le> norm (u - v)"
  1.4513 -  using convex_hull_empty simplex_extremal_le[of s]
  1.4514 -  by(cases "s = {}") auto
  1.4515 -
  1.4516 -
  1.4517 -subsection \<open>Closest point of a convex set is unique, with a continuous projection.\<close>
  1.4518 -
  1.4519 -definition closest_point :: "'a::{real_inner,heine_borel} set \<Rightarrow> 'a \<Rightarrow> 'a"
  1.4520 -  where "closest_point s a = (SOME x. x \<in> s \<and> (\<forall>y\<in>s. dist a x \<le> dist a y))"
  1.4521 -
  1.4522 -lemma closest_point_exists:
  1.4523 -  assumes "closed s"
  1.4524 -    and "s \<noteq> {}"
  1.4525 -  shows "closest_point s a \<in> s"
  1.4526 -    and "\<forall>y\<in>s. dist a (closest_point s a) \<le> dist a y"
  1.4527 -  unfolding closest_point_def
  1.4528 -  apply(rule_tac[!] someI2_ex)
  1.4529 -  apply (auto intro: distance_attains_inf[OF assms(1,2), of a])
  1.4530 -  done
  1.4531 -
  1.4532 -lemma closest_point_in_set: "closed s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> closest_point s a \<in> s"
  1.4533 -  by (meson closest_point_exists)
  1.4534 -
  1.4535 -lemma closest_point_le: "closed s \<Longrightarrow> x \<in> s \<Longrightarrow> dist a (closest_point s a) \<le> dist a x"
  1.4536 -  using closest_point_exists[of s] by auto
  1.4537 -
  1.4538 -lemma closest_point_self:
  1.4539 -  assumes "x \<in> s"
  1.4540 -  shows "closest_point s x = x"
  1.4541 -  unfolding closest_point_def
  1.4542 -  apply (rule some1_equality, rule ex1I[of _ x])
  1.4543 -  using assms
  1.4544 -  apply auto
  1.4545 -  done
  1.4546 -
  1.4547 -lemma closest_point_refl: "closed s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> closest_point s x = x \<longleftrightarrow> x \<in> s"
  1.4548 -  using closest_point_in_set[of s x] closest_point_self[of x s]
  1.4549 -  by auto
  1.4550 -
  1.4551 -lemma closer_points_lemma:
  1.4552 -  assumes "inner y z > 0"
  1.4553 -  shows "\<exists>u>0. \<forall>v>0. v \<le> u \<longrightarrow> norm(v *\<^sub>R z - y) < norm y"
  1.4554 -proof -
  1.4555 -  have z: "inner z z > 0"
  1.4556 -    unfolding inner_gt_zero_iff using assms by auto
  1.4557 -  then show ?thesis
  1.4558 -    using assms
  1.4559 -    apply (rule_tac x = "inner y z / inner z z" in exI)
  1.4560 -    apply rule
  1.4561 -    defer
  1.4562 -  proof rule+
  1.4563 -    fix v
  1.4564 -    assume "0 < v" and "v \<le> inner y z / inner z z"
  1.4565 -    then show "norm (v *\<^sub>R z - y) < norm y"
  1.4566 -      unfolding norm_lt using z and assms
  1.4567 -      by (simp add: field_simps inner_diff inner_commute mult_strict_left_mono[OF _ \<open>0<v\<close>])
  1.4568 -  qed auto
  1.4569 -qed
  1.4570 -
  1.4571 -lemma closer_point_lemma:
  1.4572 -  assumes "inner (y - x) (z - x) > 0"
  1.4573 -  shows "\<exists>u>0. u \<le> 1 \<and> dist (x + u *\<^sub>R (z - x)) y < dist x y"
  1.4574 -proof -
  1.4575 -  obtain u where "u > 0"
  1.4576 -    and u: "\<forall>v>0. v \<le> u \<longrightarrow> norm (v *\<^sub>R (z - x) - (y - x)) < norm (y - x)"
  1.4577 -    using closer_points_lemma[OF assms] by auto
  1.4578 -  show ?thesis
  1.4579 -    apply (rule_tac x="min u 1" in exI)
  1.4580 -    using u[THEN spec[where x="min u 1"]] and \<open>u > 0\<close>
  1.4581 -    unfolding dist_norm by (auto simp add: norm_minus_commute field_simps)
  1.4582 -qed
  1.4583 -
  1.4584 -lemma any_closest_point_dot:
  1.4585 -  assumes "convex s" "closed s" "x \<in> s" "y \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  1.4586 -  shows "inner (a - x) (y - x) \<le> 0"
  1.4587 -proof (rule ccontr)
  1.4588 -  assume "\<not> ?thesis"
  1.4589 -  then obtain u where u: "u>0" "u\<le>1" "dist (x + u *\<^sub>R (y - x)) a < dist x a"
  1.4590 -    using closer_point_lemma[of a x y] by auto
  1.4591 -  let ?z = "(1 - u) *\<^sub>R x + u *\<^sub>R y"
  1.4592 -  have "?z \<in> s"
  1.4593 -    using convexD_alt[OF assms(1,3,4), of u] using u by auto
  1.4594 -  then show False
  1.4595 -    using assms(5)[THEN bspec[where x="?z"]] and u(3)
  1.4596 -    by (auto simp add: dist_commute algebra_simps)
  1.4597 -qed
  1.4598 -
  1.4599 -lemma any_closest_point_unique:
  1.4600 -  fixes x :: "'a::real_inner"
  1.4601 -  assumes "convex s" "closed s" "x \<in> s" "y \<in> s"
  1.4602 -    "\<forall>z\<in>s. dist a x \<le> dist a z" "\<forall>z\<in>s. dist a y \<le> dist a z"
  1.4603 -  shows "x = y"
  1.4604 -  using any_closest_point_dot[OF assms(1-4,5)] and any_closest_point_dot[OF assms(1-2,4,3,6)]
  1.4605 -  unfolding norm_pths(1) and norm_le_square
  1.4606 -  by (auto simp add: algebra_simps)
  1.4607 -
  1.4608 -lemma closest_point_unique:
  1.4609 -  assumes "convex s" "closed s" "x \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  1.4610 -  shows "x = closest_point s a"
  1.4611 -  using any_closest_point_unique[OF assms(1-3) _ assms(4), of "closest_point s a"]
  1.4612 -  using closest_point_exists[OF assms(2)] and assms(3) by auto
  1.4613 -
  1.4614 -lemma closest_point_dot:
  1.4615 -  assumes "convex s" "closed s" "x \<in> s"
  1.4616 -  shows "inner (a - closest_point s a) (x - closest_point s a) \<le> 0"
  1.4617 -  apply (rule any_closest_point_dot[OF assms(1,2) _ assms(3)])
  1.4618 -  using closest_point_exists[OF assms(2)] and assms(3)
  1.4619 -  apply auto
  1.4620 -  done
  1.4621 -
  1.4622 -lemma closest_point_lt:
  1.4623 -  assumes "convex s" "closed s" "x \<in> s" "x \<noteq> closest_point s a"
  1.4624 -  shows "dist a (closest_point s a) < dist a x"
  1.4625 -  apply (rule ccontr)
  1.4626 -  apply (rule_tac notE[OF assms(4)])
  1.4627 -  apply (rule closest_point_unique[OF assms(1-3), of a])
  1.4628 -  using closest_point_le[OF assms(2), of _ a]
  1.4629 -  apply fastforce
  1.4630 -  done
  1.4631 -
  1.4632 -lemma closest_point_lipschitz:
  1.4633 -  assumes "convex s"
  1.4634 -    and "closed s" "s \<noteq> {}"
  1.4635 -  shows "dist (closest_point s x) (closest_point s y) \<le> dist x y"
  1.4636 -proof -
  1.4637 -  have "inner (x - closest_point s x) (closest_point s y - closest_point s x) \<le> 0"
  1.4638 -    and "inner (y - closest_point s y) (closest_point s x - closest_point s y) \<le> 0"
  1.4639 -    apply (rule_tac[!] any_closest_point_dot[OF assms(1-2)])
  1.4640 -    using closest_point_exists[OF assms(2-3)]
  1.4641 -    apply auto
  1.4642 -    done
  1.4643 -  then show ?thesis unfolding dist_norm and norm_le
  1.4644 -    using inner_ge_zero[of "(x - closest_point s x) - (y - closest_point s y)"]
  1.4645 -    by (simp add: inner_add inner_diff inner_commute)
  1.4646 -qed
  1.4647 -
  1.4648 -lemma continuous_at_closest_point:
  1.4649 -  assumes "convex s"
  1.4650 -    and "closed s"
  1.4651 -    and "s \<noteq> {}"
  1.4652 -  shows "continuous (at x) (closest_point s)"
  1.4653 -  unfolding continuous_at_eps_delta
  1.4654 -  using le_less_trans[OF closest_point_lipschitz[OF assms]] by auto
  1.4655 -
  1.4656 -lemma continuous_on_closest_point:
  1.4657 -  assumes "convex s"
  1.4658 -    and "closed s"
  1.4659 -    and "s \<noteq> {}"
  1.4660 -  shows "continuous_on t (closest_point s)"
  1.4661 -  by (metis continuous_at_imp_continuous_on continuous_at_closest_point[OF assms])
  1.4662 -
  1.4663 -
  1.4664 -subsubsection \<open>Various point-to-set separating/supporting hyperplane theorems.\<close>
  1.4665 -
  1.4666 -lemma supporting_hyperplane_closed_point:
  1.4667 -  fixes z :: "'a::{real_inner,heine_borel}"
  1.4668 -  assumes "convex s"
  1.4669 -    and "closed s"
  1.4670 -    and "s \<noteq> {}"
  1.4671 -    and "z \<notin> s"
  1.4672 -  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)"
  1.4673 -proof -
  1.4674 -  obtain y where "y \<in> s" and y: "\<forall>x\<in>s. dist z y \<le> dist z x"
  1.4675 -    by (metis distance_attains_inf[OF assms(2-3)])
  1.4676 -  show ?thesis
  1.4677 -    apply (rule_tac x="y - z" in exI)
  1.4678 -    apply (rule_tac x="inner (y - z) y" in exI)
  1.4679 -    apply (rule_tac x=y in bexI)
  1.4680 -    apply rule
  1.4681 -    defer
  1.4682 -    apply rule
  1.4683 -    defer
  1.4684 -    apply rule
  1.4685 -    apply (rule ccontr)
  1.4686 -    using \<open>y \<in> s\<close>
  1.4687 -  proof -
  1.4688 -    show "inner (y - z) z < inner (y - z) y"
  1.4689 -      apply (subst diff_gt_0_iff_gt [symmetric])
  1.4690 -      unfolding inner_diff_right[symmetric] and inner_gt_zero_iff
  1.4691 -      using \<open>y\<in>s\<close> \<open>z\<notin>s\<close>
  1.4692 -      apply auto
  1.4693 -      done
  1.4694 -  next
  1.4695 -    fix x
  1.4696 -    assume "x \<in> s"
  1.4697 -    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)"
  1.4698 -      using assms(1)[unfolded convex_alt] and y and \<open>x\<in>s\<close> and \<open>y\<in>s\<close> by auto
  1.4699 -    assume "\<not> inner (y - z) y \<le> inner (y - z) x"
  1.4700 -    then obtain v where "v > 0" "v \<le> 1" "dist (y + v *\<^sub>R (x - y)) z < dist y z"
  1.4701 -      using closer_point_lemma[of z y x] by (auto simp add: inner_diff)
  1.4702 -    then show False
  1.4703 -      using *[THEN spec[where x=v]] by (auto simp add: dist_commute algebra_simps)
  1.4704 -  qed auto
  1.4705 -qed
  1.4706 -
  1.4707 -lemma separating_hyperplane_closed_point:
  1.4708 -  fixes z :: "'a::{real_inner,heine_borel}"
  1.4709 -  assumes "convex s"
  1.4710 -    and "closed s"
  1.4711 -    and "z \<notin> s"
  1.4712 -  shows "\<exists>a b. inner a z < b \<and> (\<forall>x\<in>s. inner a x > b)"
  1.4713 -proof (cases "s = {}")
  1.4714 -  case True
  1.4715 -  then show ?thesis
  1.4716 -    apply (rule_tac x="-z" in exI)
  1.4717 -    apply (rule_tac x=1 in exI)
  1.4718 -    using less_le_trans[OF _ inner_ge_zero[of z]]
  1.4719 -    apply auto
  1.4720 -    done
  1.4721 -next
  1.4722 -  case False
  1.4723 -  obtain y where "y \<in> s" and y: "\<forall>x\<in>s. dist z y \<le> dist z x"
  1.4724 -    by (metis distance_attains_inf[OF assms(2) False])
  1.4725 -  show ?thesis
  1.4726 -    apply (rule_tac x="y - z" in exI)
  1.4727 -    apply (rule_tac x="inner (y - z) z + (norm (y - z))\<^sup>2 / 2" in exI)
  1.4728 -    apply rule
  1.4729 -    defer
  1.4730 -    apply rule
  1.4731 -  proof -
  1.4732 -    fix x
  1.4733 -    assume "x \<in> s"
  1.4734 -    have "\<not> 0 < inner (z - y) (x - y)"
  1.4735 -      apply (rule notI)
  1.4736 -      apply (drule closer_point_lemma)
  1.4737 -    proof -
  1.4738 -      assume "\<exists>u>0. u \<le> 1 \<and> dist (y + u *\<^sub>R (x - y)) z < dist y z"
  1.4739 -      then obtain u where "u > 0" "u \<le> 1" "dist (y + u *\<^sub>R (x - y)) z < dist y z"
  1.4740 -        by auto
  1.4741 -      then show False using y[THEN bspec[where x="y + u *\<^sub>R (x - y)"]]
  1.4742 -        using assms(1)[unfolded convex_alt, THEN bspec[where x=y]]
  1.4743 -        using \<open>x\<in>s\<close> \<open>y\<in>s\<close> by (auto simp add: dist_commute algebra_simps)
  1.4744 -    qed
  1.4745 -    moreover have "0 < (norm (y - z))\<^sup>2"
  1.4746 -      using \<open>y\<in>s\<close> \<open>z\<notin>s\<close> by auto
  1.4747 -    then have "0 < inner (y - z) (y - z)"
  1.4748 -      unfolding power2_norm_eq_inner by simp
  1.4749 -    ultimately show "inner (y - z) z + (norm (y - z))\<^sup>2 / 2 < inner (y - z) x"
  1.4750 -      unfolding power2_norm_eq_inner and not_less
  1.4751 -      by (auto simp add: field_simps inner_commute inner_diff)
  1.4752 -  qed (insert \<open>y\<in>s\<close> \<open>z\<notin>s\<close>, auto)
  1.4753 -qed
  1.4754 -
  1.4755 -lemma separating_hyperplane_closed_0:
  1.4756 -  assumes "convex (s::('a::euclidean_space) set)"
  1.4757 -    and "closed s"
  1.4758 -    and "0 \<notin> s"
  1.4759 -  shows "\<exists>a b. a \<noteq> 0 \<and> 0 < b \<and> (\<forall>x\<in>s. inner a x > b)"
  1.4760 -proof (cases "s = {}")
  1.4761 -  case True
  1.4762 -  have "norm ((SOME i. i\<in>Basis)::'a) = 1" "(SOME i. i\<in>Basis) \<noteq> (0::'a)"
  1.4763 -    defer
  1.4764 -    apply (subst norm_le_zero_iff[symmetric])
  1.4765 -    apply (auto simp: SOME_Basis)
  1.4766 -    done
  1.4767 -  then show ?thesis
  1.4768 -    apply (rule_tac x="SOME i. i\<in>Basis" in exI)
  1.4769 -    apply (rule_tac x=1 in exI)
  1.4770 -    using True using DIM_positive[where 'a='a]
  1.4771 -    apply auto
  1.4772 -    done
  1.4773 -next
  1.4774 -  case False
  1.4775 -  then show ?thesis
  1.4776 -    using False using separating_hyperplane_closed_point[OF assms]
  1.4777 -    apply (elim exE)
  1.4778 -    unfolding inner_zero_right
  1.4779 -    apply (rule_tac x=a in exI)
  1.4780 -    apply (rule_tac x=b in exI)
  1.4781 -    apply auto
  1.4782 -    done
  1.4783 -qed
  1.4784 -
  1.4785 -
  1.4786 -subsubsection \<open>Now set-to-set for closed/compact sets\<close>
  1.4787 -
  1.4788 -lemma separating_hyperplane_closed_compact:
  1.4789 -  fixes s :: "'a::euclidean_space set"
  1.4790 -  assumes "convex s"
  1.4791 -    and "closed s"
  1.4792 -    and "convex t"
  1.4793 -    and "compact t"
  1.4794 -    and "t \<noteq> {}"
  1.4795 -    and "s \<inter> t = {}"
  1.4796 -  shows "\<exists>a b. (\<forall>x\<in>s. inner a x < b) \<and> (\<forall>x\<in>t. inner a x > b)"
  1.4797 -proof (cases "s = {}")
  1.4798 -  case True
  1.4799 -  obtain b where b: "b > 0" "\<forall>x\<in>t. norm x \<le> b"
  1.4800 -    using compact_imp_bounded[OF assms(4)] unfolding bounded_pos by auto
  1.4801 -  obtain z :: 'a where z: "norm z = b + 1"
  1.4802 -    using vector_choose_size[of "b + 1"] and b(1) by auto
  1.4803 -  then have "z \<notin> t" using b(2)[THEN bspec[where x=z]] by auto
  1.4804 -  then obtain a b where ab: "inner a z < b" "\<forall>x\<in>t. b < inner a x"
  1.4805 -    using separating_hyperplane_closed_point[OF assms(3) compact_imp_closed[OF assms(4)], of z]
  1.4806 -    by auto
  1.4807 -  then show ?thesis
  1.4808 -    using True by auto
  1.4809 -next
  1.4810 -  case False
  1.4811 -  then obtain y where "y \<in> s" by auto
  1.4812 -  obtain a b where "0 < b" "\<forall>x\<in>{x - y |x y. x \<in> s \<and> y \<in> t}. b < inner a x"
  1.4813 -    using separating_hyperplane_closed_point[OF convex_differences[OF assms(1,3)], of 0]
  1.4814 -    using closed_compact_differences[OF assms(2,4)]
  1.4815 -    using assms(6) by auto blast
  1.4816 -  then have ab: "\<forall>x\<in>s. \<forall>y\<in>t. b + inner a y < inner a x"
  1.4817 -    apply -
  1.4818 -    apply rule
  1.4819 -    apply rule
  1.4820 -    apply (erule_tac x="x - y" in ballE)
  1.4821 -    apply (auto simp add: inner_diff)
  1.4822 -    done
  1.4823 -  define k where "k = (SUP x:t. a \<bullet> x)"
  1.4824 -  show ?thesis
  1.4825 -    apply (rule_tac x="-a" in exI)
  1.4826 -    apply (rule_tac x="-(k + b / 2)" in exI)
  1.4827 -    apply (intro conjI ballI)
  1.4828 -    unfolding inner_minus_left and neg_less_iff_less
  1.4829 -  proof -
  1.4830 -    fix x assume "x \<in> t"
  1.4831 -    then have "inner a x - b / 2 < k"
  1.4832 -      unfolding k_def
  1.4833 -    proof (subst less_cSUP_iff)
  1.4834 -      show "t \<noteq> {}" by fact
  1.4835 -      show "bdd_above (op \<bullet> a ` t)"
  1.4836 -        using ab[rule_format, of y] \<open>y \<in> s\<close>
  1.4837 -        by (intro bdd_aboveI2[where M="inner a y - b"]) (auto simp: field_simps intro: less_imp_le)
  1.4838 -    qed (auto intro!: bexI[of _ x] \<open>0<b\<close>)
  1.4839 -    then show "inner a x < k + b / 2"
  1.4840 -      by auto
  1.4841 -  next
  1.4842 -    fix x
  1.4843 -    assume "x \<in> s"
  1.4844 -    then have "k \<le> inner a x - b"
  1.4845 -      unfolding k_def
  1.4846 -      apply (rule_tac cSUP_least)
  1.4847 -      using assms(5)
  1.4848 -      using ab[THEN bspec[where x=x]]
  1.4849 -      apply auto
  1.4850 -      done
  1.4851 -    then show "k + b / 2 < inner a x"
  1.4852 -      using \<open>0 < b\<close> by auto
  1.4853 -  qed
  1.4854 -qed
  1.4855 -
  1.4856 -lemma separating_hyperplane_compact_closed:
  1.4857 -  fixes s :: "'a::euclidean_space set"
  1.4858 -  assumes "convex s"
  1.4859 -    and "compact s"
  1.4860 -    and "s \<noteq> {}"
  1.4861 -    and "convex t"
  1.4862 -    and "closed t"
  1.4863 -    and "s \<inter> t = {}"
  1.4864 -  shows "\<exists>a b. (\<forall>x\<in>s. inner a x < b) \<and> (\<forall>x\<in>t. inner a x > b)"
  1.4865 -proof -
  1.4866 -  obtain a b where "(\<forall>x\<in>t. inner a x < b) \<and> (\<forall>x\<in>s. b < inner a x)"
  1.4867 -    using separating_hyperplane_closed_compact[OF assms(4-5,1-2,3)] and assms(6)
  1.4868 -    by auto
  1.4869 -  then show ?thesis
  1.4870 -    apply (rule_tac x="-a" in exI)
  1.4871 -    apply (rule_tac x="-b" in exI)
  1.4872 -    apply auto
  1.4873 -    done
  1.4874 -qed
  1.4875 -
  1.4876 -
  1.4877 -subsubsection \<open>General case without assuming closure and getting non-strict separation\<close>
  1.4878 -
  1.4879 -lemma separating_hyperplane_set_0:
  1.4880 -  assumes "convex s" "(0::'a::euclidean_space) \<notin> s"
  1.4881 -  shows "\<exists>a. a \<noteq> 0 \<and> (\<forall>x\<in>s. 0 \<le> inner a x)"
  1.4882 -proof -
  1.4883 -  let ?k = "\<lambda>c. {x::'a. 0 \<le> inner c x}"
  1.4884 -  have *: "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}" if as: "f \<subseteq> ?k ` s" "finite f" for f
  1.4885 -  proof -
  1.4886 -    obtain c where c: "f = ?k ` c" "c \<subseteq> s" "finite c"
  1.4887 -      using finite_subset_image[OF as(2,1)] by auto
  1.4888 -    then obtain a b where ab: "a \<noteq> 0" "0 < b" "\<forall>x\<in>convex hull c. b < inner a x"
  1.4889 -      using separating_hyperplane_closed_0[OF convex_convex_hull, of c]
  1.4890 -      using finite_imp_compact_convex_hull[OF c(3), THEN compact_imp_closed] and assms(2)
  1.4891 -      using subset_hull[of convex, OF assms(1), symmetric, of c]
  1.4892 -      by force
  1.4893 -    then have "\<exists>x. norm x = 1 \<and> (\<forall>y\<in>c. 0 \<le> inner y x)"
  1.4894 -      apply (rule_tac x = "inverse(norm a) *\<^sub>R a" in exI)
  1.4895 -      using hull_subset[of c convex]
  1.4896 -      unfolding subset_eq and inner_scaleR
  1.4897 -      by (auto simp add: inner_commute del: ballE elim!: ballE)
  1.4898 -    then show "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}"
  1.4899 -      unfolding c(1) frontier_cball sphere_def dist_norm by auto
  1.4900 -  qed
  1.4901 -  have "frontier (cball 0 1) \<inter> (\<Inter>(?k ` s)) \<noteq> {}"
  1.4902 -    apply (rule compact_imp_fip)
  1.4903 -    apply (rule compact_frontier[OF compact_cball])
  1.4904 -    using * closed_halfspace_ge
  1.4905 -    by auto
  1.4906 -  then obtain x where "norm x = 1" "\<forall>y\<in>s. x\<in>?k y"
  1.4907 -    unfolding frontier_cball dist_norm sphere_def by auto
  1.4908 -  then show ?thesis
  1.4909 -    by (metis inner_commute mem_Collect_eq norm_eq_zero zero_neq_one)
  1.4910 -qed
  1.4911 -
  1.4912 -lemma separating_hyperplane_sets:
  1.4913 -  fixes s t :: "'a::euclidean_space set"
  1.4914 -  assumes "convex s"
  1.4915 -    and "convex t"
  1.4916 -    and "s \<noteq> {}"
  1.4917 -    and "t \<noteq> {}"
  1.4918 -    and "s \<inter> t = {}"
  1.4919 -  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)"
  1.4920 -proof -
  1.4921 -  from separating_hyperplane_set_0[OF convex_differences[OF assms(2,1)]]
  1.4922 -  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"
  1.4923 -    using assms(3-5) by fastforce
  1.4924 -  then have *: "\<And>x y. x \<in> t \<Longrightarrow> y \<in> s \<Longrightarrow> inner a y \<le> inner a x"
  1.4925 -    by (force simp add: inner_diff)
  1.4926 -  then have bdd: "bdd_above ((op \<bullet> a)`s)"
  1.4927 -    using \<open>t \<noteq> {}\<close> by (auto intro: bdd_aboveI2[OF *])
  1.4928 -  show ?thesis
  1.4929 -    using \<open>a\<noteq>0\<close>
  1.4930 -    by (intro exI[of _ a] exI[of _ "SUP x:s. a \<bullet> x"])
  1.4931 -       (auto intro!: cSUP_upper bdd cSUP_least \<open>a \<noteq> 0\<close> \<open>s \<noteq> {}\<close> *)
  1.4932 -qed
  1.4933 -
  1.4934 -
  1.4935 -subsection \<open>More convexity generalities\<close>
  1.4936 -
  1.4937 -lemma convex_closure [intro,simp]:
  1.4938 -  fixes s :: "'a::real_normed_vector set"
  1.4939 -  assumes "convex s"
  1.4940 -  shows "convex (closure s)"
  1.4941 -  apply (rule convexI)
  1.4942 -  apply (unfold closure_sequential, elim exE)
  1.4943 -  apply (rule_tac x="\<lambda>n. u *\<^sub>R xa n + v *\<^sub>R xb n" in exI)
  1.4944 -  apply (rule,rule)
  1.4945 -  apply (rule convexD [OF assms])
  1.4946 -  apply (auto del: tendsto_const intro!: tendsto_intros)
  1.4947 -  done
  1.4948 -
  1.4949 -lemma convex_interior [intro,simp]:
  1.4950 -  fixes s :: "'a::real_normed_vector set"
  1.4951 -  assumes "convex s"
  1.4952 -  shows "convex (interior s)"
  1.4953 -  unfolding convex_alt Ball_def mem_interior
  1.4954 -  apply (rule,rule,rule,rule,rule,rule)
  1.4955 -  apply (elim exE conjE)
  1.4956 -proof -
  1.4957 -  fix x y u
  1.4958 -  assume u: "0 \<le> u" "u \<le> (1::real)"
  1.4959 -  fix e d
  1.4960 -  assume ed: "ball x e \<subseteq> s" "ball y d \<subseteq> s" "0<d" "0<e"
  1.4961 -  show "\<exists>e>0. ball ((1 - u) *\<^sub>R x + u *\<^sub>R y) e \<subseteq> s"
  1.4962 -    apply (rule_tac x="min d e" in exI)
  1.4963 -    apply rule
  1.4964 -    unfolding subset_eq
  1.4965 -    defer
  1.4966 -    apply rule
  1.4967 -  proof -
  1.4968 -    fix z
  1.4969 -    assume "z \<in> ball ((1 - u) *\<^sub>R x + u *\<^sub>R y) (min d e)"
  1.4970 -    then have "(1- u) *\<^sub>R (z - u *\<^sub>R (y - x)) + u *\<^sub>R (z + (1 - u) *\<^sub>R (y - x)) \<in> s"
  1.4971 -      apply (rule_tac assms[unfolded convex_alt, rule_format])
  1.4972 -      using ed(1,2) and u
  1.4973 -      unfolding subset_eq mem_ball Ball_def dist_norm
  1.4974 -      apply (auto simp add: algebra_simps)
  1.4975 -      done
  1.4976 -    then show "z \<in> s"
  1.4977 -      using u by (auto simp add: algebra_simps)
  1.4978 -  qed(insert u ed(3-4), auto)
  1.4979 -qed
  1.4980 -
  1.4981 -lemma convex_hull_eq_empty[simp]: "convex hull s = {} \<longleftrightarrow> s = {}"
  1.4982 -  using hull_subset[of s convex] convex_hull_empty by auto
  1.4983 -
  1.4984 -
  1.4985 -subsection \<open>Moving and scaling convex hulls.\<close>
  1.4986 -
  1.4987 -lemma convex_hull_set_plus:
  1.4988 -  "convex hull (s + t) = convex hull s + convex hull t"
  1.4989 -  unfolding set_plus_image
  1.4990 -  apply (subst convex_hull_linear_image [symmetric])
  1.4991 -  apply (simp add: linear_iff scaleR_right_distrib)
  1.4992 -  apply (simp add: convex_hull_Times)
  1.4993 -  done
  1.4994 -
  1.4995 -lemma translation_eq_singleton_plus: "(\<lambda>x. a + x) ` t = {a} + t"
  1.4996 -  unfolding set_plus_def by auto
  1.4997 -
  1.4998 -lemma convex_hull_translation:
  1.4999 -  "convex hull ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (convex hull s)"
  1.5000 -  unfolding translation_eq_singleton_plus
  1.5001 -  by (simp only: convex_hull_set_plus convex_hull_singleton)
  1.5002 -
  1.5003 -lemma convex_hull_scaling:
  1.5004 -  "convex hull ((\<lambda>x. c *\<^sub>R x) ` s) = (\<lambda>x. c *\<^sub>R x) ` (convex hull s)"
  1.5005 -  using linear_scaleR by (rule convex_hull_linear_image [symmetric])
  1.5006 -
  1.5007 -lemma convex_hull_affinity:
  1.5008 -  "convex hull ((\<lambda>x. a + c *\<^sub>R x) ` s) = (\<lambda>x. a + c *\<^sub>R x) ` (convex hull s)"
  1.5009 -  by(simp only: image_image[symmetric] convex_hull_scaling convex_hull_translation)
  1.5010 -
  1.5011 -
  1.5012 -subsection \<open>Convexity of cone hulls\<close>
  1.5013 -
  1.5014 -lemma convex_cone_hull:
  1.5015 -  assumes "convex S"
  1.5016 -  shows "convex (cone hull S)"
  1.5017 -proof (rule convexI)
  1.5018 -  fix x y
  1.5019 -  assume xy: "x \<in> cone hull S" "y \<in> cone hull S"
  1.5020 -  then have "S \<noteq> {}"
  1.5021 -    using cone_hull_empty_iff[of S] by auto
  1.5022 -  fix u v :: real
  1.5023 -  assume uv: "u \<ge> 0" "v \<ge> 0" "u + v = 1"
  1.5024 -  then have *: "u *\<^sub>R x \<in> cone hull S" "v *\<^sub>R y \<in> cone hull S"
  1.5025 -    using cone_cone_hull[of S] xy cone_def[of "cone hull S"] by auto
  1.5026 -  from * obtain cx :: real and xx where x: "u *\<^sub>R x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.5027 -    using cone_hull_expl[of S] by auto
  1.5028 -  from * obtain cy :: real and yy where y: "v *\<^sub>R y = cy *\<^sub>R yy" "cy \<ge> 0" "yy \<in> S"
  1.5029 -    using cone_hull_expl[of S] by auto
  1.5030 -  {
  1.5031 -    assume "cx + cy \<le> 0"
  1.5032 -    then have "u *\<^sub>R x = 0" and "v *\<^sub>R y = 0"
  1.5033 -      using x y by auto
  1.5034 -    then have "u *\<^sub>R x + v *\<^sub>R y = 0"
  1.5035 -      by auto
  1.5036 -    then have "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S"
  1.5037 -      using cone_hull_contains_0[of S] \<open>S \<noteq> {}\<close> by auto
  1.5038 -  }
  1.5039 -  moreover
  1.5040 -  {
  1.5041 -    assume "cx + cy > 0"
  1.5042 -    then have "(cx / (cx + cy)) *\<^sub>R xx + (cy / (cx + cy)) *\<^sub>R yy \<in> S"
  1.5043 -      using assms mem_convex_alt[of S xx yy cx cy] x y by auto
  1.5044 -    then have "cx *\<^sub>R xx + cy *\<^sub>R yy \<in> cone hull S"
  1.5045 -      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>
  1.5046 -      by (auto simp add: scaleR_right_distrib)
  1.5047 -    then have "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S"
  1.5048 -      using x y by auto
  1.5049 -  }
  1.5050 -  moreover have "cx + cy \<le> 0 \<or> cx + cy > 0" by auto
  1.5051 -  ultimately show "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S" by blast
  1.5052 -qed
  1.5053 -
  1.5054 -lemma cone_convex_hull:
  1.5055 -  assumes "cone S"
  1.5056 -  shows "cone (convex hull S)"
  1.5057 -proof (cases "S = {}")
  1.5058 -  case True
  1.5059 -  then show ?thesis by auto
  1.5060 -next
  1.5061 -  case False
  1.5062 -  then have *: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> op *\<^sub>R c ` S = S)"
  1.5063 -    using cone_iff[of S] assms by auto
  1.5064 -  {
  1.5065 -    fix c :: real
  1.5066 -    assume "c > 0"
  1.5067 -    then have "op *\<^sub>R c ` (convex hull S) = convex hull (op *\<^sub>R c ` S)"
  1.5068 -      using convex_hull_scaling[of _ S] by auto
  1.5069 -    also have "\<dots> = convex hull S"
  1.5070 -      using * \<open>c > 0\<close> by auto
  1.5071 -    finally have "op *\<^sub>R c ` (convex hull S) = convex hull S"
  1.5072 -      by auto
  1.5073 -  }
  1.5074 -  then have "0 \<in> convex hull S" "\<And>c. c > 0 \<Longrightarrow> (op *\<^sub>R c ` (convex hull S)) = (convex hull S)"
  1.5075 -    using * hull_subset[of S convex] by auto
  1.5076 -  then show ?thesis
  1.5077 -    using \<open>S \<noteq> {}\<close> cone_iff[of "convex hull S"] by auto
  1.5078 -qed
  1.5079 -
  1.5080 -subsection \<open>Convex set as intersection of halfspaces\<close>
  1.5081 -
  1.5082 -lemma convex_halfspace_intersection:
  1.5083 -  fixes s :: "('a::euclidean_space) set"
  1.5084 -  assumes "closed s" "convex s"
  1.5085 -  shows "s = \<Inter>{h. s \<subseteq> h \<and> (\<exists>a b. h = {x. inner a x \<le> b})}"
  1.5086 -  apply (rule set_eqI)
  1.5087 -  apply rule
  1.5088 -  unfolding Inter_iff Ball_def mem_Collect_eq
  1.5089 -  apply (rule,rule,erule conjE)
  1.5090 -proof -
  1.5091 -  fix x
  1.5092 -  assume "\<forall>xa. s \<subseteq> xa \<and> (\<exists>a b. xa = {x. inner a x \<le> b}) \<longrightarrow> x \<in> xa"
  1.5093 -  then have "\<forall>a b. s \<subseteq> {x. inner a x \<le> b} \<longrightarrow> x \<in> {x. inner a x \<le> b}"
  1.5094 -    by blast
  1.5095 -  then show "x \<in> s"
  1.5096 -    apply (rule_tac ccontr)
  1.5097 -    apply (drule separating_hyperplane_closed_point[OF assms(2,1)])
  1.5098 -    apply (erule exE)+
  1.5099 -    apply (erule_tac x="-a" in allE)
  1.5100 -    apply (erule_tac x="-b" in allE)
  1.5101 -    apply auto
  1.5102 -    done
  1.5103 -qed auto
  1.5104 -
  1.5105 -
  1.5106 -subsection \<open>Radon's theorem (from Lars Schewe)\<close>
  1.5107 -
  1.5108 -lemma radon_ex_lemma:
  1.5109 -  assumes "finite c" "affine_dependent c"
  1.5110 -  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"
  1.5111 -proof -
  1.5112 -  from assms(2)[unfolded affine_dependent_explicit]
  1.5113 -  obtain s u where
  1.5114 -      "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"
  1.5115 -    by blast
  1.5116 -  then show ?thesis
  1.5117 -    apply (rule_tac x="\<lambda>v. if v\<in>s then u v else 0" in exI)
  1.5118 -    unfolding if_smult scaleR_zero_left and setsum.inter_restrict[OF assms(1), symmetric]
  1.5119 -    apply (auto simp add: Int_absorb1)
  1.5120 -    done
  1.5121 -qed
  1.5122 -
  1.5123 -lemma radon_s_lemma:
  1.5124 -  assumes "finite s"
  1.5125 -    and "setsum f s = (0::real)"
  1.5126 -  shows "setsum f {x\<in>s. 0 < f x} = - setsum f {x\<in>s. f x < 0}"
  1.5127 -proof -
  1.5128 -  have *: "\<And>x. (if f x < 0 then f x else 0) + (if 0 < f x then f x else 0) = f x"
  1.5129 -    by auto
  1.5130 -  show ?thesis
  1.5131 -    unfolding add_eq_0_iff[symmetric] and setsum.inter_filter[OF assms(1)]
  1.5132 -      and setsum.distrib[symmetric] and *
  1.5133 -    using assms(2)
  1.5134 -    by assumption
  1.5135 -qed
  1.5136 -
  1.5137 -lemma radon_v_lemma:
  1.5138 -  assumes "finite s"
  1.5139 -    and "setsum f s = 0"
  1.5140 -    and "\<forall>x. g x = (0::real) \<longrightarrow> f x = (0::'a::euclidean_space)"
  1.5141 -  shows "(setsum f {x\<in>s. 0 < g x}) = - setsum f {x\<in>s. g x < 0}"
  1.5142 -proof -
  1.5143 -  have *: "\<And>x. (if 0 < g x then f x else 0) + (if g x < 0 then f x else 0) = f x"
  1.5144 -    using assms(3) by auto
  1.5145 -  show ?thesis
  1.5146 -    unfolding eq_neg_iff_add_eq_0 and setsum.inter_filter[OF assms(1)]
  1.5147 -      and setsum.distrib[symmetric] and *
  1.5148 -    using assms(2)
  1.5149 -    apply assumption
  1.5150 -    done
  1.5151 -qed
  1.5152 -
  1.5153 -lemma radon_partition:
  1.5154 -  assumes "finite c" "affine_dependent c"
  1.5155 -  shows "\<exists>m p. m \<inter> p = {} \<and> m \<union> p = c \<and> (convex hull m) \<inter> (convex hull p) \<noteq> {}"
  1.5156 -proof -
  1.5157 -  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"
  1.5158 -    using radon_ex_lemma[OF assms] by auto
  1.5159 -  have fin: "finite {x \<in> c. 0 < u x}" "finite {x \<in> c. 0 > u x}"
  1.5160 -    using assms(1) by auto
  1.5161 -  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}"
  1.5162 -  have "setsum u {x \<in> c. 0 < u x} \<noteq> 0"
  1.5163 -  proof (cases "u v \<ge> 0")
  1.5164 -    case False
  1.5165 -    then have "u v < 0" by auto
  1.5166 -    then show ?thesis
  1.5167 -    proof (cases "\<exists>w\<in>{x \<in> c. 0 < u x}. u w > 0")
  1.5168 -      case True
  1.5169 -      then show ?thesis
  1.5170 -        using setsum_nonneg_eq_0_iff[of _ u, OF fin(1)] by auto
  1.5171 -    next
  1.5172 -      case False
  1.5173 -      then have "setsum u c \<le> setsum (\<lambda>x. if x=v then u v else 0) c"
  1.5174 -        apply (rule_tac setsum_mono)
  1.5175 -        apply auto
  1.5176 -        done
  1.5177 -      then show ?thesis
  1.5178 -        unfolding setsum.delta[OF assms(1)] using uv(2) and \<open>u v < 0\<close> and uv(1) by auto
  1.5179 -    qed
  1.5180 -  qed (insert setsum_nonneg_eq_0_iff[of _ u, OF fin(1)] uv(2-3), auto)
  1.5181 -
  1.5182 -  then have *: "setsum u {x\<in>c. u x > 0} > 0"
  1.5183 -    unfolding less_le
  1.5184 -    apply (rule_tac conjI)
  1.5185 -    apply (rule_tac setsum_nonneg)
  1.5186 -    apply auto
  1.5187 -    done
  1.5188 -  moreover have "setsum u ({x \<in> c. 0 < u x} \<union> {x \<in> c. u x < 0}) = setsum u c"
  1.5189 -    "(\<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)"
  1.5190 -    using assms(1)
  1.5191 -    apply (rule_tac[!] setsum.mono_neutral_left)
  1.5192 -    apply auto
  1.5193 -    done
  1.5194 -  then have "setsum u {x \<in> c. 0 < u x} = - setsum u {x \<in> c. 0 > u x}"
  1.5195 -    "(\<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)"
  1.5196 -    unfolding eq_neg_iff_add_eq_0
  1.5197 -    using uv(1,4)
  1.5198 -    by (auto simp add: setsum.union_inter_neutral[OF fin, symmetric])
  1.5199 -  moreover have "\<forall>x\<in>{v \<in> c. u v < 0}. 0 \<le> inverse (setsum u {x \<in> c. 0 < u x}) * - u x"
  1.5200 -    apply rule
  1.5201 -    apply (rule mult_nonneg_nonneg)
  1.5202 -    using *
  1.5203 -    apply auto
  1.5204 -    done
  1.5205 -  ultimately have "z \<in> convex hull {v \<in> c. u v \<le> 0}"
  1.5206 -    unfolding convex_hull_explicit mem_Collect_eq
  1.5207 -    apply (rule_tac x="{v \<in> c. u v < 0}" in exI)
  1.5208 -    apply (rule_tac x="\<lambda>y. inverse (setsum u {x\<in>c. u x > 0}) * - u y" in exI)
  1.5209 -    using assms(1) unfolding scaleR_scaleR[symmetric] scaleR_right.setsum [symmetric] and z_def
  1.5210 -    apply (auto simp add: setsum_negf setsum_right_distrib[symmetric])
  1.5211 -    done
  1.5212 -  moreover have "\<forall>x\<in>{v \<in> c. 0 < u v}. 0 \<le> inverse (setsum u {x \<in> c. 0 < u x}) * u x"
  1.5213 -    apply rule
  1.5214 -    apply (rule mult_nonneg_nonneg)
  1.5215 -    using *
  1.5216 -    apply auto
  1.5217 -    done
  1.5218 -  then have "z \<in> convex hull {v \<in> c. u v > 0}"
  1.5219 -    unfolding convex_hull_explicit mem_Collect_eq
  1.5220 -    apply (rule_tac x="{v \<in> c. 0 < u v}" in exI)
  1.5221 -    apply (rule_tac x="\<lambda>y. inverse (setsum u {x\<in>c. u x > 0}) * u y" in exI)
  1.5222 -    using assms(1)
  1.5223 -    unfolding scaleR_scaleR[symmetric] scaleR_right.setsum [symmetric] and z_def
  1.5224 -    using *
  1.5225 -    apply (auto simp add: setsum_negf setsum_right_distrib[symmetric])
  1.5226 -    done
  1.5227 -  ultimately show ?thesis
  1.5228 -    apply (rule_tac x="{v\<in>c. u v \<le> 0}" in exI)
  1.5229 -    apply (rule_tac x="{v\<in>c. u v > 0}" in exI)
  1.5230 -    apply auto
  1.5231 -    done
  1.5232 -qed
  1.5233 -
  1.5234 -lemma radon:
  1.5235 -  assumes "affine_dependent c"
  1.5236 -  obtains m p where "m \<subseteq> c" "p \<subseteq> c" "m \<inter> p = {}" "(convex hull m) \<inter> (convex hull p) \<noteq> {}"
  1.5237 -proof -
  1.5238 -  from assms[unfolded affine_dependent_explicit]
  1.5239 -  obtain s u where
  1.5240 -      "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"
  1.5241 -    by blast
  1.5242 -  then have *: "finite s" "affine_dependent s" and s: "s \<subseteq> c"
  1.5243 -    unfolding affine_dependent_explicit by auto
  1.5244 -  from radon_partition[OF *]
  1.5245 -  obtain m p where "m \<inter> p = {}" "m \<union> p = s" "convex hull m \<inter> convex hull p \<noteq> {}"
  1.5246 -    by blast
  1.5247 -  then show ?thesis
  1.5248 -    apply (rule_tac that[of p m])
  1.5249 -    using s
  1.5250 -    apply auto
  1.5251 -    done
  1.5252 -qed
  1.5253 -
  1.5254 -
  1.5255 -subsection \<open>Helly's theorem\<close>
  1.5256 -
  1.5257 -lemma helly_induct:
  1.5258 -  fixes f :: "'a::euclidean_space set set"
  1.5259 -  assumes "card f = n"
  1.5260 -    and "n \<ge> DIM('a) + 1"
  1.5261 -    and "\<forall>s\<in>f. convex s" "\<forall>t\<subseteq>f. card t = DIM('a) + 1 \<longrightarrow> \<Inter>t \<noteq> {}"
  1.5262 -  shows "\<Inter>f \<noteq> {}"
  1.5263 -  using assms
  1.5264 -proof (induct n arbitrary: f)
  1.5265 -  case 0
  1.5266 -  then show ?case by auto
  1.5267 -next
  1.5268 -  case (Suc n)
  1.5269 -  have "finite f"
  1.5270 -    using \<open>card f = Suc n\<close> by (auto intro: card_ge_0_finite)
  1.5271 -  show "\<Inter>f \<noteq> {}"
  1.5272 -    apply (cases "n = DIM('a)")
  1.5273 -    apply (rule Suc(5)[rule_format])
  1.5274 -    unfolding \<open>card f = Suc n\<close>
  1.5275 -  proof -
  1.5276 -    assume ng: "n \<noteq> DIM('a)"
  1.5277 -    then have "\<exists>X. \<forall>s\<in>f. X s \<in> \<Inter>(f - {s})"
  1.5278 -      apply (rule_tac bchoice)
  1.5279 -      unfolding ex_in_conv
  1.5280 -      apply (rule, rule Suc(1)[rule_format])
  1.5281 -      unfolding card_Diff_singleton_if[OF \<open>finite f\<close>] \<open>card f = Suc n\<close>
  1.5282 -      defer
  1.5283 -      defer
  1.5284 -      apply (rule Suc(4)[rule_format])
  1.5285 -      defer
  1.5286 -      apply (rule Suc(5)[rule_format])
  1.5287 -      using Suc(3) \<open>finite f\<close>
  1.5288 -      apply auto
  1.5289 -      done
  1.5290 -    then obtain X where X: "\<forall>s\<in>f. X s \<in> \<Inter>(f - {s})" by auto
  1.5291 -    show ?thesis
  1.5292 -    proof (cases "inj_on X f")
  1.5293 -      case False
  1.5294 -      then obtain s t where st: "s\<noteq>t" "s\<in>f" "t\<in>f" "X s = X t"
  1.5295 -        unfolding inj_on_def by auto
  1.5296 -      then have *: "\<Inter>f = \<Inter>(f - {s}) \<inter> \<Inter>(f - {t})" by auto
  1.5297 -      show ?thesis
  1.5298 -        unfolding *
  1.5299 -        unfolding ex_in_conv[symmetric]
  1.5300 -        apply (rule_tac x="X s" in exI)
  1.5301 -        apply rule
  1.5302 -        apply (rule X[rule_format])
  1.5303 -        using X st
  1.5304 -        apply auto
  1.5305 -        done
  1.5306 -    next
  1.5307 -      case True
  1.5308 -      then obtain m p where mp: "m \<inter> p = {}" "m \<union> p = X ` f" "convex hull m \<inter> convex hull p \<noteq> {}"
  1.5309 -        using radon_partition[of "X ` f"] and affine_dependent_biggerset[of "X ` f"]
  1.5310 -        unfolding card_image[OF True] and \<open>card f = Suc n\<close>
  1.5311 -        using Suc(3) \<open>finite f\<close> and ng
  1.5312 -        by auto
  1.5313 -      have "m \<subseteq> X ` f" "p \<subseteq> X ` f"
  1.5314 -        using mp(2) by auto
  1.5315 -      then obtain g h where gh:"m = X ` g" "p = X ` h" "g \<subseteq> f" "h \<subseteq> f"
  1.5316 -        unfolding subset_image_iff by auto
  1.5317 -      then have "f \<union> (g \<union> h) = f" by auto
  1.5318 -      then have f: "f = g \<union> h"
  1.5319 -        using inj_on_Un_image_eq_iff[of X f "g \<union> h"] and True
  1.5320 -        unfolding mp(2)[unfolded image_Un[symmetric] gh]
  1.5321 -        by auto
  1.5322 -      have *: "g \<inter> h = {}"
  1.5323 -        using mp(1)
  1.5324 -        unfolding gh
  1.5325 -        using inj_on_image_Int[OF True gh(3,4)]
  1.5326 -        by auto
  1.5327 -      have "convex hull (X ` h) \<subseteq> \<Inter>g" "convex hull (X ` g) \<subseteq> \<Inter>h"
  1.5328 -        apply (rule_tac [!] hull_minimal)
  1.5329 -        using Suc gh(3-4)
  1.5330 -        unfolding subset_eq
  1.5331 -        apply (rule_tac [2] convex_Inter, rule_tac [4] convex_Inter)
  1.5332 -        apply rule
  1.5333 -        prefer 3
  1.5334 -        apply rule
  1.5335 -      proof -
  1.5336 -        fix x
  1.5337 -        assume "x \<in> X ` g"
  1.5338 -        then obtain y where "y \<in> g" "x = X y"
  1.5339 -          unfolding image_iff ..
  1.5340 -        then show "x \<in> \<Inter>h"
  1.5341 -          using X[THEN bspec[where x=y]] using * f by auto
  1.5342 -      next
  1.5343 -        fix x
  1.5344 -        assume "x \<in> X ` h"
  1.5345 -        then obtain y where "y \<in> h" "x = X y"
  1.5346 -          unfolding image_iff ..
  1.5347 -        then show "x \<in> \<Inter>g"
  1.5348 -          using X[THEN bspec[where x=y]] using * f by auto
  1.5349 -      qed auto
  1.5350 -      then show ?thesis
  1.5351 -        unfolding f using mp(3)[unfolded gh] by blast
  1.5352 -    qed
  1.5353 -  qed auto
  1.5354 -qed
  1.5355 -
  1.5356 -lemma helly:
  1.5357 -  fixes f :: "'a::euclidean_space set set"
  1.5358 -  assumes "card f \<ge> DIM('a) + 1" "\<forall>s\<in>f. convex s"
  1.5359 -    and "\<forall>t\<subseteq>f. card t = DIM('a) + 1 \<longrightarrow> \<Inter>t \<noteq> {}"
  1.5360 -  shows "\<Inter>f \<noteq> {}"
  1.5361 -  apply (rule helly_induct)
  1.5362 -  using assms
  1.5363 -  apply auto
  1.5364 -  done
  1.5365 -
  1.5366 -
  1.5367 -subsection \<open>Epigraphs of convex functions\<close>
  1.5368 -
  1.5369 -definition "epigraph s (f :: _ \<Rightarrow> real) = {xy. fst xy \<in> s \<and> f (fst xy) \<le> snd xy}"
  1.5370 -
  1.5371 -lemma mem_epigraph: "(x, y) \<in> epigraph s f \<longleftrightarrow> x \<in> s \<and> f x \<le> y"
  1.5372 -  unfolding epigraph_def by auto
  1.5373 -
  1.5374 -lemma convex_epigraph: "convex (epigraph s f) \<longleftrightarrow> convex_on s f \<and> convex s"
  1.5375 -  unfolding convex_def convex_on_def
  1.5376 -  unfolding Ball_def split_paired_All epigraph_def
  1.5377 -  unfolding mem_Collect_eq fst_conv snd_conv fst_add snd_add fst_scaleR snd_scaleR Ball_def[symmetric]
  1.5378 -  apply safe
  1.5379 -  defer
  1.5380 -  apply (erule_tac x=x in allE)
  1.5381 -  apply (erule_tac x="f x" in allE)
  1.5382 -  apply safe
  1.5383 -  apply (erule_tac x=xa in allE)
  1.5384 -  apply (erule_tac x="f xa" in allE)
  1.5385 -  prefer 3
  1.5386 -  apply (rule_tac y="u * f a + v * f aa" in order_trans)
  1.5387 -  defer
  1.5388 -  apply (auto intro!:mult_left_mono add_mono)
  1.5389 -  done
  1.5390 -
  1.5391 -lemma convex_epigraphI: "convex_on s f \<Longrightarrow> convex s \<Longrightarrow> convex (epigraph s f)"
  1.5392 -  unfolding convex_epigraph by auto
  1.5393 -
  1.5394 -lemma convex_epigraph_convex: "convex s \<Longrightarrow> convex_on s f \<longleftrightarrow> convex(epigraph s f)"
  1.5395 -  by (simp add: convex_epigraph)
  1.5396 -
  1.5397 -
  1.5398 -subsubsection \<open>Use this to derive general bound property of convex function\<close>
  1.5399 -
  1.5400 -lemma convex_on:
  1.5401 -  assumes "convex s"
  1.5402 -  shows "convex_on s f \<longleftrightarrow>
  1.5403 -    (\<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>
  1.5404 -      f (setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} ) \<le> setsum (\<lambda>i. u i * f(x i)) {1..k})"
  1.5405 -  unfolding convex_epigraph_convex[OF assms] convex epigraph_def Ball_def mem_Collect_eq
  1.5406 -  unfolding fst_setsum snd_setsum fst_scaleR snd_scaleR
  1.5407 -  apply safe
  1.5408 -  apply (drule_tac x=k in spec)
  1.5409 -  apply (drule_tac x=u in spec)
  1.5410 -  apply (drule_tac x="\<lambda>i. (x i, f (x i))" in spec)
  1.5411 -  apply simp
  1.5412 -  using assms[unfolded convex]
  1.5413 -  apply simp
  1.5414 -  apply (rule_tac y="\<Sum>i = 1..k. u i * f (fst (x i))" in order_trans)
  1.5415 -  defer
  1.5416 -  apply (rule setsum_mono)
  1.5417 -  apply (erule_tac x=i in allE)
  1.5418 -  unfolding real_scaleR_def
  1.5419 -  apply (rule mult_left_mono)
  1.5420 -  using assms[unfolded convex]
  1.5421 -  apply auto
  1.5422 -  done
  1.5423 -
  1.5424 -
  1.5425 -subsection \<open>Convexity of general and special intervals\<close>
  1.5426 -
  1.5427 -lemma is_interval_convex:
  1.5428 -  fixes s :: "'a::euclidean_space set"
  1.5429 -  assumes "is_interval s"
  1.5430 -  shows "convex s"
  1.5431 -proof (rule convexI)
  1.5432 -  fix x y and u v :: real
  1.5433 -  assume as: "x \<in> s" "y \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
  1.5434 -  then have *: "u = 1 - v" "1 - v \<ge> 0" and **: "v = 1 - u" "1 - u \<ge> 0"
  1.5435 -    by auto
  1.5436 -  {
  1.5437 -    fix a b
  1.5438 -    assume "\<not> b \<le> u * a + v * b"
  1.5439 -    then have "u * a < (1 - v) * b"
  1.5440 -      unfolding not_le using as(4) by (auto simp add: field_simps)
  1.5441 -    then have "a < b"
  1.5442 -      unfolding * using as(4) *(2)
  1.5443 -      apply (rule_tac mult_left_less_imp_less[of "1 - v"])
  1.5444 -      apply (auto simp add: field_simps)
  1.5445 -      done
  1.5446 -    then have "a \<le> u * a + v * b"
  1.5447 -      unfolding * using as(4)
  1.5448 -      by (auto simp add: field_simps intro!:mult_right_mono)
  1.5449 -  }
  1.5450 -  moreover
  1.5451 -  {
  1.5452 -    fix a b
  1.5453 -    assume "\<not> u * a + v * b \<le> a"
  1.5454 -    then have "v * b > (1 - u) * a"
  1.5455 -      unfolding not_le using as(4) by (auto simp add: field_simps)
  1.5456 -    then have "a < b"
  1.5457 -      unfolding * using as(4)
  1.5458 -      apply (rule_tac mult_left_less_imp_less)
  1.5459 -      apply (auto simp add: field_simps)
  1.5460 -      done
  1.5461 -    then have "u * a + v * b \<le> b"
  1.5462 -      unfolding **
  1.5463 -      using **(2) as(3)
  1.5464 -      by (auto simp add: field_simps intro!:mult_right_mono)
  1.5465 -  }
  1.5466 -  ultimately show "u *\<^sub>R x + v *\<^sub>R y \<in> s"
  1.5467 -    apply -
  1.5468 -    apply (rule assms[unfolded is_interval_def, rule_format, OF as(1,2)])
  1.5469 -    using as(3-) DIM_positive[where 'a='a]
  1.5470 -    apply (auto simp: inner_simps)
  1.5471 -    done
  1.5472 -qed
  1.5473 -
  1.5474 -lemma is_interval_connected:
  1.5475 -  fixes s :: "'a::euclidean_space set"
  1.5476 -  shows "is_interval s \<Longrightarrow> connected s"
  1.5477 -  using is_interval_convex convex_connected by auto
  1.5478 -
  1.5479 -lemma convex_box [simp]: "convex (cbox a b)" "convex (box a (b::'a::euclidean_space))"
  1.5480 -  apply (rule_tac[!] is_interval_convex)+
  1.5481 -  using is_interval_box is_interval_cbox
  1.5482 -  apply auto
  1.5483 -  done
  1.5484 -
  1.5485 -subsection \<open>On \<open>real\<close>, \<open>is_interval\<close>, \<open>convex\<close> and \<open>connected\<close> are all equivalent.\<close>
  1.5486 -
  1.5487 -lemma is_interval_1:
  1.5488 -  "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)"
  1.5489 -  unfolding is_interval_def by auto
  1.5490 -
  1.5491 -lemma is_interval_connected_1:
  1.5492 -  fixes s :: "real set"
  1.5493 -  shows "is_interval s \<longleftrightarrow> connected s"
  1.5494 -  apply rule
  1.5495 -  apply (rule is_interval_connected, assumption)
  1.5496 -  unfolding is_interval_1
  1.5497 -  apply rule
  1.5498 -  apply rule
  1.5499 -  apply rule
  1.5500 -  apply rule
  1.5501 -  apply (erule conjE)
  1.5502 -  apply (rule ccontr)
  1.5503 -proof -
  1.5504 -  fix a b x
  1.5505 -  assume as: "connected s" "a \<in> s" "b \<in> s" "a \<le> x" "x \<le> b" "x \<notin> s"
  1.5506 -  then have *: "a < x" "x < b"
  1.5507 -    unfolding not_le [symmetric] by auto
  1.5508 -  let ?halfl = "{..<x} "
  1.5509 -  let ?halfr = "{x<..}"
  1.5510 -  {
  1.5511 -    fix y
  1.5512 -    assume "y \<in> s"
  1.5513 -    with \<open>x \<notin> s\<close> have "x \<noteq> y" by auto
  1.5514 -    then have "y \<in> ?halfr \<union> ?halfl" by auto
  1.5515 -  }
  1.5516 -  moreover have "a \<in> ?halfl" "b \<in> ?halfr" using * by auto
  1.5517 -  then have "?halfl \<inter> s \<noteq> {}" "?halfr \<inter> s \<noteq> {}"
  1.5518 -    using as(2-3) by auto
  1.5519 -  ultimately show False
  1.5520 -    apply (rule_tac notE[OF as(1)[unfolded connected_def]])
  1.5521 -    apply (rule_tac x = ?halfl in exI)
  1.5522 -    apply (rule_tac x = ?halfr in exI)
  1.5523 -    apply rule
  1.5524 -    apply (rule open_lessThan)
  1.5525 -    apply rule
  1.5526 -    apply (rule open_greaterThan)
  1.5527 -    apply auto
  1.5528 -    done
  1.5529 -qed
  1.5530 -
  1.5531 -lemma is_interval_convex_1:
  1.5532 -  fixes s :: "real set"
  1.5533 -  shows "is_interval s \<longleftrightarrow> convex s"
  1.5534 -  by (metis is_interval_convex convex_connected is_interval_connected_1)
  1.5535 -
  1.5536 -lemma connected_convex_1:
  1.5537 -  fixes s :: "real set"
  1.5538 -  shows "connected s \<longleftrightarrow> convex s"
  1.5539 -  by (metis is_interval_convex convex_connected is_interval_connected_1)
  1.5540 -
  1.5541 -lemma connected_convex_1_gen:
  1.5542 -  fixes s :: "'a :: euclidean_space set"
  1.5543 -  assumes "DIM('a) = 1"
  1.5544 -  shows "connected s \<longleftrightarrow> convex s"
  1.5545 -proof -
  1.5546 -  obtain f:: "'a \<Rightarrow> real" where linf: "linear f" and "inj f"
  1.5547 -    using subspace_isomorphism [where 'a = 'a and 'b = real]
  1.5548 -    by (metis DIM_real dim_UNIV subspace_UNIV assms)
  1.5549 -  then have "f -` (f ` s) = s"
  1.5550 -    by (simp add: inj_vimage_image_eq)
  1.5551 -  then show ?thesis
  1.5552 -    by (metis connected_convex_1 convex_linear_vimage linf convex_connected connected_linear_image)
  1.5553 -qed
  1.5554 -
  1.5555 -subsection \<open>Another intermediate value theorem formulation\<close>
  1.5556 -
  1.5557 -lemma ivt_increasing_component_on_1:
  1.5558 -  fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1.5559 -  assumes "a \<le> b"
  1.5560 -    and "continuous_on {a..b} f"
  1.5561 -    and "(f a)\<bullet>k \<le> y" "y \<le> (f b)\<bullet>k"
  1.5562 -  shows "\<exists>x\<in>{a..b}. (f x)\<bullet>k = y"
  1.5563 -proof -
  1.5564 -  have "f a \<in> f ` cbox a b" "f b \<in> f ` cbox a b"
  1.5565 -    apply (rule_tac[!] imageI)
  1.5566 -    using assms(1)
  1.5567 -    apply auto
  1.5568 -    done
  1.5569 -  then show ?thesis
  1.5570 -    using connected_ivt_component[of "f ` cbox a b" "f a" "f b" k y]
  1.5571 -    by (simp add: Topology_Euclidean_Space.connected_continuous_image assms)
  1.5572 -qed
  1.5573 -
  1.5574 -lemma ivt_increasing_component_1:
  1.5575 -  fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1.5576 -  shows "a \<le> b \<Longrightarrow> \<forall>x\<in>{a..b}. continuous (at x) f \<Longrightarrow>
  1.5577 -    f a\<bullet>k \<le> y \<Longrightarrow> y \<le> f b\<bullet>k \<Longrightarrow> \<exists>x\<in>{a..b}. (f x)\<bullet>k = y"
  1.5578 -  by (rule ivt_increasing_component_on_1) (auto simp add: continuous_at_imp_continuous_on)
  1.5579 -
  1.5580 -lemma ivt_decreasing_component_on_1:
  1.5581 -  fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1.5582 -  assumes "a \<le> b"
  1.5583 -    and "continuous_on {a..b} f"
  1.5584 -    and "(f b)\<bullet>k \<le> y"
  1.5585 -    and "y \<le> (f a)\<bullet>k"
  1.5586 -  shows "\<exists>x\<in>{a..b}. (f x)\<bullet>k = y"
  1.5587 -  apply (subst neg_equal_iff_equal[symmetric])
  1.5588 -  using ivt_increasing_component_on_1[of a b "\<lambda>x. - f x" k "- y"]
  1.5589 -  using assms using continuous_on_minus
  1.5590 -  apply auto
  1.5591 -  done
  1.5592 -
  1.5593 -lemma ivt_decreasing_component_1:
  1.5594 -  fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1.5595 -  shows "a \<le> b \<Longrightarrow> \<forall>x\<in>{a..b}. continuous (at x) f \<Longrightarrow>
  1.5596 -    f b\<bullet>k \<le> y \<Longrightarrow> y \<le> f a\<bullet>k \<Longrightarrow> \<exists>x\<in>{a..b}. (f x)\<bullet>k = y"
  1.5597 -  by (rule ivt_decreasing_component_on_1) (auto simp: continuous_at_imp_continuous_on)
  1.5598 -
  1.5599 -
  1.5600 -subsection \<open>A bound within a convex hull, and so an interval\<close>
  1.5601 -
  1.5602 -lemma convex_on_convex_hull_bound:
  1.5603 -  assumes "convex_on (convex hull s) f"
  1.5604 -    and "\<forall>x\<in>s. f x \<le> b"
  1.5605 -  shows "\<forall>x\<in> convex hull s. f x \<le> b"
  1.5606 -proof
  1.5607 -  fix x
  1.5608 -  assume "x \<in> convex hull s"
  1.5609 -  then obtain k u v where
  1.5610 -    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"
  1.5611 -    unfolding convex_hull_indexed mem_Collect_eq by auto
  1.5612 -  have "(\<Sum>i = 1..k. u i * f (v i)) \<le> b"
  1.5613 -    using setsum_mono[of "{1..k}" "\<lambda>i. u i * f (v i)" "\<lambda>i. u i * b"]
  1.5614 -    unfolding setsum_left_distrib[symmetric] obt(2) mult_1
  1.5615 -    apply (drule_tac meta_mp)
  1.5616 -    apply (rule mult_left_mono)
  1.5617 -    using assms(2) obt(1)
  1.5618 -    apply auto
  1.5619 -    done
  1.5620 -  then show "f x \<le> b"
  1.5621 -    using assms(1)[unfolded convex_on[OF convex_convex_hull], rule_format, of k u v]
  1.5622 -    unfolding obt(2-3)
  1.5623 -    using obt(1) and hull_subset[unfolded subset_eq, rule_format, of _ s]
  1.5624 -    by auto
  1.5625 -qed
  1.5626 -
  1.5627 -lemma inner_setsum_Basis[simp]: "i \<in> Basis \<Longrightarrow> (\<Sum>Basis) \<bullet> i = 1"
  1.5628 -  by (simp add: inner_setsum_left setsum.If_cases inner_Basis)
  1.5629 -
  1.5630 -lemma convex_set_plus:
  1.5631 -  assumes "convex s" and "convex t" shows "convex (s + t)"
  1.5632 -proof -
  1.5633 -  have "convex {x + y |x y. x \<in> s \<and> y \<in> t}"
  1.5634 -    using assms by (rule convex_sums)
  1.5635 -  moreover have "{x + y |x y. x \<in> s \<and> y \<in> t} = s + t"
  1.5636 -    unfolding set_plus_def by auto
  1.5637 -  finally show "convex (s + t)" .
  1.5638 -qed
  1.5639 -
  1.5640 -lemma convex_set_setsum:
  1.5641 -  assumes "\<And>i. i \<in> A \<Longrightarrow> convex (B i)"
  1.5642 -  shows "convex (\<Sum>i\<in>A. B i)"
  1.5643 -proof (cases "finite A")
  1.5644 -  case True then show ?thesis using assms
  1.5645 -    by induct (auto simp: convex_set_plus)
  1.5646 -qed auto
  1.5647 -
  1.5648 -lemma finite_set_setsum:
  1.5649 -  assumes "finite A" and "\<forall>i\<in>A. finite (B i)" shows "finite (\<Sum>i\<in>A. B i)"
  1.5650 -  using assms by (induct set: finite, simp, simp add: finite_set_plus)
  1.5651 -
  1.5652 -lemma set_setsum_eq:
  1.5653 -  "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}"
  1.5654 -  apply (induct set: finite)
  1.5655 -  apply simp
  1.5656 -  apply simp
  1.5657 -  apply (safe elim!: set_plus_elim)
  1.5658 -  apply (rule_tac x="fun_upd f x a" in exI)
  1.5659 -  apply simp
  1.5660 -  apply (rule_tac f="\<lambda>x. a + x" in arg_cong)
  1.5661 -  apply (rule setsum.cong [OF refl])
  1.5662 -  apply clarsimp
  1.5663 -  apply fast
  1.5664 -  done
  1.5665 -
  1.5666 -lemma box_eq_set_setsum_Basis:
  1.5667 -  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))"
  1.5668 -  apply (subst set_setsum_eq [OF finite_Basis])
  1.5669 -  apply safe
  1.5670 -  apply (fast intro: euclidean_representation [symmetric])
  1.5671 -  apply (subst inner_setsum_left)
  1.5672 -  apply (subgoal_tac "(\<Sum>x\<in>Basis. f x \<bullet> i) = f i \<bullet> i")
  1.5673 -  apply (drule (1) bspec)
  1.5674 -  apply clarsimp
  1.5675 -  apply (frule setsum.remove [OF finite_Basis])
  1.5676 -  apply (erule trans)
  1.5677 -  apply simp
  1.5678 -  apply (rule setsum.neutral)
  1.5679 -  apply clarsimp
  1.5680 -  apply (frule_tac x=i in bspec, assumption)
  1.5681 -  apply (drule_tac x=x in bspec, assumption)
  1.5682 -  apply clarsimp
  1.5683 -  apply (cut_tac u=x and v=i in inner_Basis, assumption+)
  1.5684 -  apply (rule ccontr)
  1.5685 -  apply simp
  1.5686 -  done
  1.5687 -
  1.5688 -lemma convex_hull_set_setsum:
  1.5689 -  "convex hull (\<Sum>i\<in>A. B i) = (\<Sum>i\<in>A. convex hull (B i))"
  1.5690 -proof (cases "finite A")
  1.5691 -  assume "finite A" then show ?thesis
  1.5692 -    by (induct set: finite, simp, simp add: convex_hull_set_plus)
  1.5693 -qed simp
  1.5694 -
  1.5695 -lemma convex_hull_eq_real_cbox:
  1.5696 -  fixes x y :: real assumes "x \<le> y"
  1.5697 -  shows "convex hull {x, y} = cbox x y"
  1.5698 -proof (rule hull_unique)
  1.5699 -  show "{x, y} \<subseteq> cbox x y" using \<open>x \<le> y\<close> by auto
  1.5700 -  show "convex (cbox x y)"
  1.5701 -    by (rule convex_box)
  1.5702 -next
  1.5703 -  fix s assume "{x, y} \<subseteq> s" and "convex s"
  1.5704 -  then show "cbox x y \<subseteq> s"
  1.5705 -    unfolding is_interval_convex_1 [symmetric] is_interval_def Basis_real_def
  1.5706 -    by - (clarify, simp (no_asm_use), fast)
  1.5707 -qed
  1.5708 -
  1.5709 -lemma unit_interval_convex_hull:
  1.5710 -  "cbox (0::'a::euclidean_space) One = convex hull {x. \<forall>i\<in>Basis. (x\<bullet>i = 0) \<or> (x\<bullet>i = 1)}"
  1.5711 -  (is "?int = convex hull ?points")
  1.5712 -proof -
  1.5713 -  have One[simp]: "\<And>i. i \<in> Basis \<Longrightarrow> One \<bullet> i = 1"
  1.5714 -    by (simp add: inner_setsum_left setsum.If_cases inner_Basis)
  1.5715 -  have "?int = {x. \<forall>i\<in>Basis. x \<bullet> i \<in> cbox 0 1}"
  1.5716 -    by (auto simp: cbox_def)
  1.5717 -  also have "\<dots> = (\<Sum>i\<in>Basis. (\<lambda>x. x *\<^sub>R i) ` cbox 0 1)"
  1.5718 -    by (simp only: box_eq_set_setsum_Basis)
  1.5719 -  also have "\<dots> = (\<Sum>i\<in>Basis. (\<lambda>x. x *\<^sub>R i) ` (convex hull {0, 1}))"
  1.5720 -    by (simp only: convex_hull_eq_real_cbox zero_le_one)
  1.5721 -  also have "\<dots> = (\<Sum>i\<in>Basis. convex hull ((\<lambda>x. x *\<^sub>R i) ` {0, 1}))"
  1.5722 -    by (simp only: convex_hull_linear_image linear_scaleR_left)
  1.5723 -  also have "\<dots> = convex hull (\<Sum>i\<in>Basis. (\<lambda>x. x *\<^sub>R i) ` {0, 1})"
  1.5724 -    by (simp only: convex_hull_set_setsum)
  1.5725 -  also have "\<dots> = convex hull {x. \<forall>i\<in>Basis. x\<bullet>i \<in> {0, 1}}"
  1.5726 -    by (simp only: box_eq_set_setsum_Basis)
  1.5727 -  also have "convex hull {x. \<forall>i\<in>Basis. x\<bullet>i \<in> {0, 1}} = convex hull ?points"
  1.5728 -    by simp
  1.5729 -  finally show ?thesis .
  1.5730 -qed
  1.5731 -
  1.5732 -text \<open>And this is a finite set of vertices.\<close>
  1.5733 -
  1.5734 -lemma unit_cube_convex_hull:
  1.5735 -  obtains s :: "'a::euclidean_space set"
  1.5736 -    where "finite s" and "cbox 0 (\<Sum>Basis) = convex hull s"
  1.5737 -  apply (rule that[of "{x::'a. \<forall>i\<in>Basis. x\<bullet>i=0 \<or> x\<bullet>i=1}"])
  1.5738 -  apply (rule finite_subset[of _ "(\<lambda>s. (\<Sum>i\<in>Basis. (if i\<in>s then 1 else 0) *\<^sub>R i)::'a) ` Pow Basis"])
  1.5739 -  prefer 3
  1.5740 -  apply (rule unit_interval_convex_hull)
  1.5741 -  apply rule
  1.5742 -  unfolding mem_Collect_eq
  1.5743 -proof -
  1.5744 -  fix x :: 'a
  1.5745 -  assume as: "\<forall>i\<in>Basis. x \<bullet> i = 0 \<or> x \<bullet> i = 1"
  1.5746 -  show "x \<in> (\<lambda>s. \<Sum>i\<in>Basis. (if i\<in>s then 1 else 0) *\<^sub>R i) ` Pow Basis"
  1.5747 -    apply (rule image_eqI[where x="{i. i\<in>Basis \<and> x\<bullet>i = 1}"])
  1.5748 -    using as
  1.5749 -    apply (subst euclidean_eq_iff)
  1.5750 -    apply auto
  1.5751 -    done
  1.5752 -qed auto
  1.5753 -
  1.5754 -text \<open>Hence any cube (could do any nonempty interval).\<close>
  1.5755 -
  1.5756 -lemma cube_convex_hull:
  1.5757 -  assumes "d > 0"
  1.5758 -  obtains s :: "'a::euclidean_space set" where
  1.5759 -    "finite s" and "cbox (x - (\<Sum>i\<in>Basis. d*\<^sub>Ri)) (x + (\<Sum>i\<in>Basis. d*\<^sub>Ri)) = convex hull s"
  1.5760 -proof -
  1.5761 -  let ?d = "(\<Sum>i\<in>Basis. d*\<^sub>Ri)::'a"
  1.5762 -  have *: "cbox (x - ?d) (x + ?d) = (\<lambda>y. x - ?d + (2 * d) *\<^sub>R y) ` cbox 0 (\<Sum>Basis)"
  1.5763 -    apply (rule set_eqI, rule)
  1.5764 -    unfolding image_iff
  1.5765 -    defer
  1.5766 -    apply (erule bexE)
  1.5767 -  proof -
  1.5768 -    fix y
  1.5769 -    assume as: "y\<in>cbox (x - ?d) (x + ?d)"
  1.5770 -    then have "inverse (2 * d) *\<^sub>R (y - (x - ?d)) \<in> cbox 0 (\<Sum>Basis)"
  1.5771 -      using assms by (simp add: mem_box field_simps inner_simps)
  1.5772 -    with \<open>0 < d\<close> show "\<exists>z\<in>cbox 0 (\<Sum>Basis). y = x - ?d + (2 * d) *\<^sub>R z"
  1.5773 -      by (intro bexI[of _ "inverse (2 * d) *\<^sub>R (y - (x - ?d))"]) auto
  1.5774 -  next
  1.5775 -    fix y z
  1.5776 -    assume as: "z\<in>cbox 0 (\<Sum>Basis)" "y = x - ?d + (2*d) *\<^sub>R z"
  1.5777 -    have "\<And>i. i\<in>Basis \<Longrightarrow> 0 \<le> d * (z \<bullet> i) \<and> d * (z \<bullet> i) \<le> d"
  1.5778 -      using assms as(1)[unfolded mem_box]
  1.5779 -      apply (erule_tac x=i in ballE)
  1.5780 -      apply rule
  1.5781 -      prefer 2
  1.5782 -      apply (rule mult_right_le_one_le)
  1.5783 -      using assms
  1.5784 -      apply auto
  1.5785 -      done
  1.5786 -    then show "y \<in> cbox (x - ?d) (x + ?d)"
  1.5787 -      unfolding as(2) mem_box
  1.5788 -      apply -
  1.5789 -      apply rule
  1.5790 -      using as(1)[unfolded mem_box]
  1.5791 -      apply (erule_tac x=i in ballE)
  1.5792 -      using assms
  1.5793 -      apply (auto simp: inner_simps)
  1.5794 -      done
  1.5795 -  qed
  1.5796 -  obtain s where "finite s" "cbox 0 (\<Sum>Basis::'a) = convex hull s"
  1.5797 -    using unit_cube_convex_hull by auto
  1.5798 -  then show ?thesis
  1.5799 -    apply (rule_tac that[of "(\<lambda>y. x - ?d + (2 * d) *\<^sub>R y)` s"])
  1.5800 -    unfolding * and convex_hull_affinity
  1.5801 -    apply auto
  1.5802 -    done
  1.5803 -qed
  1.5804 -
  1.5805 -subsubsection\<open>Representation of any interval as a finite convex hull\<close>
  1.5806 -
  1.5807 -lemma image_stretch_interval:
  1.5808 -  "(\<lambda>x. \<Sum>k\<in>Basis. (m k * (x\<bullet>k)) *\<^sub>R k) ` cbox a (b::'a::euclidean_space) =
  1.5809 -  (if (cbox a b) = {} then {} else
  1.5810 -    cbox (\<Sum>k\<in>Basis. (min (m k * (a\<bullet>k)) (m k * (b\<bullet>k))) *\<^sub>R k::'a)
  1.5811 -     (\<Sum>k\<in>Basis. (max (m k * (a\<bullet>k)) (m k * (b\<bullet>k))) *\<^sub>R k))"
  1.5812 -proof cases
  1.5813 -  assume *: "cbox a b \<noteq> {}"
  1.5814 -  show ?thesis
  1.5815 -    unfolding box_ne_empty if_not_P[OF *]
  1.5816 -    apply (simp add: cbox_def image_Collect set_eq_iff euclidean_eq_iff[where 'a='a] ball_conj_distrib[symmetric])
  1.5817 -    apply (subst choice_Basis_iff[symmetric])
  1.5818 -  proof (intro allI ball_cong refl)
  1.5819 -    fix x i :: 'a assume "i \<in> Basis"
  1.5820 -    with * have a_le_b: "a \<bullet> i \<le> b \<bullet> i"
  1.5821 -      unfolding box_ne_empty by auto
  1.5822 -    show "(\<exists>xa. x \<bullet> i = m i * xa \<and> a \<bullet> i \<le> xa \<and> xa \<le> b \<bullet> i) \<longleftrightarrow>
  1.5823 -        min (m i * (a \<bullet> i)) (m i * (b \<bullet> i)) \<le> x \<bullet> i \<and> x \<bullet> i \<le> max (m i * (a \<bullet> i)) (m i * (b \<bullet> i))"
  1.5824 -    proof (cases "m i = 0")
  1.5825 -      case True
  1.5826 -      with a_le_b show ?thesis by auto
  1.5827 -    next
  1.5828 -      case False
  1.5829 -      then have *: "\<And>a b. a = m i * b \<longleftrightarrow> b = a / m i"
  1.5830 -        by (auto simp add: field_simps)
  1.5831 -      from False have
  1.5832 -          "min (m i * (a \<bullet> i)) (m i * (b \<bullet> i)) = (if 0 < m i then m i * (a \<bullet> i) else m i * (b \<bullet> i))"
  1.5833 -          "max (m i * (a \<bullet> i)) (m i * (b \<bullet> i)) = (if 0 < m i then m i * (b \<bullet> i) else m i * (a \<bullet> i))"
  1.5834 -        using a_le_b by (auto simp: min_def max_def mult_le_cancel_left)
  1.5835 -      with False show ?thesis using a_le_b
  1.5836 -        unfolding * by (auto simp add: le_divide_eq divide_le_eq ac_simps)
  1.5837 -    qed
  1.5838 -  qed
  1.5839 -qed simp
  1.5840 -
  1.5841 -lemma interval_image_stretch_interval:
  1.5842 -  "\<exists>u v. (\<lambda>x. \<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k) ` cbox a (b::'a::euclidean_space) = cbox u (v::'a::euclidean_space)"
  1.5843 -  unfolding image_stretch_interval by auto
  1.5844 -
  1.5845 -lemma cbox_translation: "cbox (c + a) (c + b) = image (\<lambda>x. c + x) (cbox a b)"
  1.5846 -  using image_affinity_cbox [of 1 c a b]
  1.5847 -  using box_ne_empty [of "a+c" "b+c"]  box_ne_empty [of a b]
  1.5848 -  by (auto simp add: inner_left_distrib add.commute)
  1.5849 -
  1.5850 -lemma cbox_image_unit_interval:
  1.5851 -  fixes a :: "'a::euclidean_space"
  1.5852 -  assumes "cbox a b \<noteq> {}"
  1.5853 -    shows "cbox a b =
  1.5854 -           op + a ` (\<lambda>x. \<Sum>k\<in>Basis. ((b \<bullet> k - a \<bullet> k) * (x \<bullet> k)) *\<^sub>R k) ` cbox 0 One"
  1.5855 -using assms
  1.5856 -apply (simp add: box_ne_empty image_stretch_interval cbox_translation [symmetric])
  1.5857 -apply (simp add: min_def max_def algebra_simps setsum_subtractf euclidean_representation)
  1.5858 -done
  1.5859 -
  1.5860 -lemma closed_interval_as_convex_hull:
  1.5861 -  fixes a :: "'a::euclidean_space"
  1.5862 -  obtains s where "finite s" "cbox a b = convex hull s"
  1.5863 -proof (cases "cbox a b = {}")
  1.5864 -  case True with convex_hull_empty that show ?thesis
  1.5865 -    by blast
  1.5866 -next
  1.5867 -  case False
  1.5868 -  obtain s::"'a set" where "finite s" and eq: "cbox 0 One = convex hull s"
  1.5869 -    by (blast intro: unit_cube_convex_hull)
  1.5870 -  have lin: "linear (\<lambda>x. \<Sum>k\<in>Basis. ((b \<bullet> k - a \<bullet> k) * (x \<bullet> k)) *\<^sub>R k)"
  1.5871 -    by (rule linear_compose_setsum) (auto simp: algebra_simps linearI)
  1.5872 -  have "finite (op + a ` (\<lambda>x. \<Sum>k\<in>Basis. ((b \<bullet> k - a \<bullet> k) * (x \<bullet> k)) *\<^sub>R k) ` s)"
  1.5873 -    by (rule finite_imageI \<open>finite s\<close>)+
  1.5874 -  then show ?thesis
  1.5875 -    apply (rule that)
  1.5876 -    apply (simp add: convex_hull_translation convex_hull_linear_image [OF lin, symmetric])
  1.5877 -    apply (simp add: eq [symmetric] cbox_image_unit_interval [OF False])
  1.5878 -    done
  1.5879 -qed
  1.5880 -
  1.5881 -
  1.5882 -subsection \<open>Bounded convex function on open set is continuous\<close>
  1.5883 -
  1.5884 -lemma convex_on_bounded_continuous:
  1.5885 -  fixes s :: "('a::real_normed_vector) set"
  1.5886 -  assumes "open s"
  1.5887 -    and "convex_on s f"
  1.5888 -    and "\<forall>x\<in>s. \<bar>f x\<bar> \<le> b"
  1.5889 -  shows "continuous_on s f"
  1.5890 -  apply (rule continuous_at_imp_continuous_on)
  1.5891 -  unfolding continuous_at_real_range
  1.5892 -proof (rule,rule,rule)
  1.5893 -  fix x and e :: real
  1.5894 -  assume "x \<in> s" "e > 0"
  1.5895 -  define B where "B = \<bar>b\<bar> + 1"
  1.5896 -  have B: "0 < B" "\<And>x. x\<in>s \<Longrightarrow> \<bar>f x\<bar> \<le> B"
  1.5897 -    unfolding B_def
  1.5898 -    defer
  1.5899 -    apply (drule assms(3)[rule_format])
  1.5900 -    apply auto
  1.5901 -    done
  1.5902 -  obtain k where "k > 0" and k: "cball x k \<subseteq> s"
  1.5903 -    using assms(1)[unfolded open_contains_cball, THEN bspec[where x=x]]
  1.5904 -    using \<open>x\<in>s\<close> by auto
  1.5905 -  show "\<exists>d>0. \<forall>x'. norm (x' - x) < d \<longrightarrow> \<bar>f x' - f x\<bar> < e"
  1.5906 -    apply (rule_tac x="min (k / 2) (e / (2 * B) * k)" in exI)
  1.5907 -    apply rule
  1.5908 -    defer
  1.5909 -  proof (rule, rule)
  1.5910 -    fix y
  1.5911 -    assume as: "norm (y - x) < min (k / 2) (e / (2 * B) * k)"
  1.5912 -    show "\<bar>f y - f x\<bar> < e"
  1.5913 -    proof (cases "y = x")
  1.5914 -      case False
  1.5915 -      define t where "t = k / norm (y - x)"
  1.5916 -      have "2 < t" "0<t"
  1.5917 -        unfolding t_def using as False and \<open>k>0\<close>
  1.5918 -        by (auto simp add:field_simps)
  1.5919 -      have "y \<in> s"
  1.5920 -        apply (rule k[unfolded subset_eq,rule_format])
  1.5921 -        unfolding mem_cball dist_norm
  1.5922 -        apply (rule order_trans[of _ "2 * norm (x - y)"])
  1.5923 -        using as
  1.5924 -        by (auto simp add: field_simps norm_minus_commute)
  1.5925 -      {
  1.5926 -        define w where "w = x + t *\<^sub>R (y - x)"
  1.5927 -        have "w \<in> s"
  1.5928 -          unfolding w_def
  1.5929 -          apply (rule k[unfolded subset_eq,rule_format])
  1.5930 -          unfolding mem_cball dist_norm
  1.5931 -          unfolding t_def
  1.5932 -          using \<open>k>0\<close>
  1.5933 -          apply auto
  1.5934 -          done
  1.5935 -        have "(1 / t) *\<^sub>R x + - x + ((t - 1) / t) *\<^sub>R x = (1 / t - 1 + (t - 1) / t) *\<^sub>R x"
  1.5936 -          by (auto simp add: algebra_simps)
  1.5937 -        also have "\<dots> = 0"
  1.5938 -          using \<open>t > 0\<close> by (auto simp add:field_simps)
  1.5939 -        finally have w: "(1 / t) *\<^sub>R w + ((t - 1) / t) *\<^sub>R x = y"
  1.5940 -          unfolding w_def using False and \<open>t > 0\<close>
  1.5941 -          by (auto simp add: algebra_simps)
  1.5942 -        have  "2 * B < e * t"
  1.5943 -          unfolding t_def using \<open>0 < e\<close> \<open>0 < k\<close> \<open>B > 0\<close> and as and False
  1.5944 -          by (auto simp add:field_simps)
  1.5945 -        then have "(f w - f x) / t < e"
  1.5946 -          using B(2)[OF \<open>w\<in>s\<close>] and B(2)[OF \<open>x\<in>s\<close>]
  1.5947 -          using \<open>t > 0\<close> by (auto simp add:field_simps)
  1.5948 -        then have th1: "f y - f x < e"
  1.5949 -          apply -
  1.5950 -          apply (rule le_less_trans)
  1.5951 -          defer
  1.5952 -          apply assumption
  1.5953 -          using assms(2)[unfolded convex_on_def,rule_format,of w x "1/t" "(t - 1)/t", unfolded w]
  1.5954 -          using \<open>0 < t\<close> \<open>2 < t\<close> and \<open>x \<in> s\<close> \<open>w \<in> s\<close>
  1.5955 -          by (auto simp add:field_simps)
  1.5956 -      }
  1.5957 -      moreover
  1.5958 -      {
  1.5959 -        define w where "w = x - t *\<^sub>R (y - x)"
  1.5960 -        have "w \<in> s"
  1.5961 -          unfolding w_def
  1.5962 -          apply (rule k[unfolded subset_eq,rule_format])
  1.5963 -          unfolding mem_cball dist_norm
  1.5964 -          unfolding t_def
  1.5965 -          using \<open>k > 0\<close>
  1.5966 -          apply auto
  1.5967 -          done
  1.5968 -        have "(1 / (1 + t)) *\<^sub>R x + (t / (1 + t)) *\<^sub>R x = (1 / (1 + t) + t / (1 + t)) *\<^sub>R x"
  1.5969 -          by (auto simp add: algebra_simps)
  1.5970 -        also have "\<dots> = x"
  1.5971 -          using \<open>t > 0\<close> by (auto simp add:field_simps)
  1.5972 -        finally have w: "(1 / (1+t)) *\<^sub>R w + (t / (1 + t)) *\<^sub>R y = x"
  1.5973 -          unfolding w_def using False and \<open>t > 0\<close>
  1.5974 -          by (auto simp add: algebra_simps)
  1.5975 -        have "2 * B < e * t"
  1.5976 -          unfolding t_def
  1.5977 -          using \<open>0 < e\<close> \<open>0 < k\<close> \<open>B > 0\<close> and as and False
  1.5978 -          by (auto simp add:field_simps)
  1.5979 -        then have *: "(f w - f y) / t < e"
  1.5980 -          using B(2)[OF \<open>w\<in>s\<close>] and B(2)[OF \<open>y\<in>s\<close>]
  1.5981 -          using \<open>t > 0\<close>
  1.5982 -          by (auto simp add:field_simps)
  1.5983 -        have "f x \<le> 1 / (1 + t) * f w + (t / (1 + t)) * f y"
  1.5984 -          using assms(2)[unfolded convex_on_def,rule_format,of w y "1/(1+t)" "t / (1+t)",unfolded w]
  1.5985 -          using \<open>0 < t\<close> \<open>2 < t\<close> and \<open>y \<in> s\<close> \<open>w \<in> s\<close>
  1.5986 -          by (auto simp add:field_simps)
  1.5987 -        also have "\<dots> = (f w + t * f y) / (1 + t)"
  1.5988 -          using \<open>t > 0\<close> by (auto simp add: divide_simps)
  1.5989 -        also have "\<dots> < e + f y"
  1.5990 -          using \<open>t > 0\<close> * \<open>e > 0\<close> by (auto simp add: field_simps)
  1.5991 -        finally have "f x - f y < e" by auto
  1.5992 -      }
  1.5993 -      ultimately show ?thesis by auto
  1.5994 -    qed (insert \<open>0<e\<close>, auto)
  1.5995 -  qed (insert \<open>0<e\<close> \<open>0<k\<close> \<open>0<B\<close>, auto simp: field_simps)
  1.5996 -qed
  1.5997 -
  1.5998 -
  1.5999 -subsection \<open>Upper bound on a ball implies upper and lower bounds\<close>
  1.6000 -
  1.6001 -lemma convex_bounds_lemma:
  1.6002 -  fixes x :: "'a::real_normed_vector"
  1.6003 -  assumes "convex_on (cball x e) f"
  1.6004 -    and "\<forall>y \<in> cball x e. f y \<le> b"
  1.6005 -  shows "\<forall>y \<in> cball x e. \<bar>f y\<bar> \<le> b + 2 * \<bar>f x\<bar>"
  1.6006 -  apply rule
  1.6007 -proof (cases "0 \<le> e")
  1.6008 -  case True
  1.6009 -  fix y
  1.6010 -  assume y: "y \<in> cball x e"
  1.6011 -  define z where "z = 2 *\<^sub>R x - y"
  1.6012 -  have *: "x - (2 *\<^sub>R x - y) = y - x"
  1.6013 -    by (simp add: scaleR_2)
  1.6014 -  have z: "z \<in> cball x e"
  1.6015 -    using y unfolding z_def mem_cball dist_norm * by (auto simp add: norm_minus_commute)
  1.6016 -  have "(1 / 2) *\<^sub>R y + (1 / 2) *\<^sub>R z = x"
  1.6017 -    unfolding z_def by (auto simp add: algebra_simps)
  1.6018 -  then show "\<bar>f y\<bar> \<le> b + 2 * \<bar>f x\<bar>"
  1.6019 -    using assms(1)[unfolded convex_on_def,rule_format, OF y z, of "1/2" "1/2"]
  1.6020 -    using assms(2)[rule_format,OF y] assms(2)[rule_format,OF z]
  1.6021 -    by (auto simp add:field_simps)
  1.6022 -next
  1.6023 -  case False
  1.6024 -  fix y
  1.6025 -  assume "y \<in> cball x e"
  1.6026 -  then have "dist x y < 0"
  1.6027 -    using False unfolding mem_cball not_le by (auto simp del: dist_not_less_zero)
  1.6028 -  then show "\<bar>f y\<bar> \<le> b + 2 * \<bar>f x\<bar>"
  1.6029 -    using zero_le_dist[of x y] by auto
  1.6030 -qed
  1.6031 -
  1.6032 -
  1.6033 -subsubsection \<open>Hence a convex function on an open set is continuous\<close>
  1.6034 -
  1.6035 -lemma real_of_nat_ge_one_iff: "1 \<le> real (n::nat) \<longleftrightarrow> 1 \<le> n"
  1.6036 -  by auto
  1.6037 -
  1.6038 -lemma convex_on_continuous:
  1.6039 -  assumes "open (s::('a::euclidean_space) set)" "convex_on s f"
  1.6040 -  shows "continuous_on s f"
  1.6041 -  unfolding continuous_on_eq_continuous_at[OF assms(1)]
  1.6042 -proof
  1.6043 -  note dimge1 = DIM_positive[where 'a='a]
  1.6044 -  fix x
  1.6045 -  assume "x \<in> s"
  1.6046 -  then obtain e where e: "cball x e \<subseteq> s" "e > 0"
  1.6047 -    using assms(1) unfolding open_contains_cball by auto
  1.6048 -  define d where "d = e / real DIM('a)"
  1.6049 -  have "0 < d"
  1.6050 -    unfolding d_def using \<open>e > 0\<close> dimge1 by auto
  1.6051 -  let ?d = "(\<Sum>i\<in>Basis. d *\<^sub>R i)::'a"
  1.6052 -  obtain c
  1.6053 -    where c: "finite c"
  1.6054 -    and c1: "convex hull c \<subseteq> cball x e"
  1.6055 -    and c2: "cball x d \<subseteq> convex hull c"
  1.6056 -  proof
  1.6057 -    define c where "c = (\<Sum>i\<in>Basis. (\<lambda>a. a *\<^sub>R i) ` {x\<bullet>i - d, x\<bullet>i + d})"
  1.6058 -    show "finite c"
  1.6059 -      unfolding c_def by (simp add: finite_set_setsum)
  1.6060 -    have 1: "convex hull c = {a. \<forall>i\<in>Basis. a \<bullet> i \<in> cbox (x \<bullet> i - d) (x \<bullet> i + d)}"
  1.6061 -      unfolding box_eq_set_setsum_Basis
  1.6062 -      unfolding c_def convex_hull_set_setsum
  1.6063 -      apply (subst convex_hull_linear_image [symmetric])
  1.6064 -      apply (simp add: linear_iff scaleR_add_left)
  1.6065 -      apply (rule setsum.cong [OF refl])
  1.6066 -      apply (rule image_cong [OF _ refl])
  1.6067 -      apply (rule convex_hull_eq_real_cbox)
  1.6068 -      apply (cut_tac \<open>0 < d\<close>, simp)
  1.6069 -      done
  1.6070 -    then have 2: "convex hull c = {a. \<forall>i\<in>Basis. a \<bullet> i \<in> cball (x \<bullet> i) d}"
  1.6071 -      by (simp add: dist_norm abs_le_iff algebra_simps)
  1.6072 -    show "cball x d \<subseteq> convex hull c"
  1.6073 -      unfolding 2
  1.6074 -      apply clarsimp
  1.6075 -      apply (simp only: dist_norm)
  1.6076 -      apply (subst inner_diff_left [symmetric])
  1.6077 -      apply simp
  1.6078 -      apply (erule (1) order_trans [OF Basis_le_norm])
  1.6079 -      done
  1.6080 -    have e': "e = (\<Sum>(i::'a)\<in>Basis. d)"
  1.6081 -      by (simp add: d_def DIM_positive)
  1.6082 -    show "convex hull c \<subseteq> cball x e"
  1.6083 -      unfolding 2
  1.6084 -      apply clarsimp
  1.6085 -      apply (subst euclidean_dist_l2)
  1.6086 -      apply (rule order_trans [OF setL2_le_setsum])
  1.6087 -      apply (rule zero_le_dist)
  1.6088 -      unfolding e'
  1.6089 -      apply (rule setsum_mono)
  1.6090 -      apply simp
  1.6091 -      done
  1.6092 -  qed
  1.6093 -  define k where "k = Max (f ` c)"
  1.6094 -  have "convex_on (convex hull c) f"
  1.6095 -    apply(rule convex_on_subset[OF assms(2)])
  1.6096 -    apply(rule subset_trans[OF _ e(1)])
  1.6097 -    apply(rule c1)
  1.6098 -    done
  1.6099 -  then have k: "\<forall>y\<in>convex hull c. f y \<le> k"
  1.6100 -    apply (rule_tac convex_on_convex_hull_bound)
  1.6101 -    apply assumption
  1.6102 -    unfolding k_def
  1.6103 -    apply (rule, rule Max_ge)
  1.6104 -    using c(1)
  1.6105 -    apply auto
  1.6106 -    done
  1.6107 -  have "d \<le> e"
  1.6108 -    unfolding d_def
  1.6109 -    apply (rule mult_imp_div_pos_le)
  1.6110 -    using \<open>e > 0\<close>
  1.6111 -    unfolding mult_le_cancel_left1
  1.6112 -    apply (auto simp: real_of_nat_ge_one_iff Suc_le_eq DIM_positive)
  1.6113 -    done
  1.6114 -  then have dsube: "cball x d \<subseteq> cball x e"
  1.6115 -    by (rule subset_cball)
  1.6116 -  have conv: "convex_on (cball x d) f"
  1.6117 -    apply (rule convex_on_subset)
  1.6118 -    apply (rule convex_on_subset[OF assms(2)])
  1.6119 -    apply (rule e(1))
  1.6120 -    apply (rule dsube)
  1.6121 -    done
  1.6122 -  then have "\<forall>y\<in>cball x d. \<bar>f y\<bar> \<le> k + 2 * \<bar>f x\<bar>"
  1.6123 -    apply (rule convex_bounds_lemma)
  1.6124 -    apply (rule ballI)
  1.6125 -    apply (rule k [rule_format])
  1.6126 -    apply (erule rev_subsetD)
  1.6127 -    apply (rule c2)
  1.6128 -    done
  1.6129 -  then have "continuous_on (ball x d) f"
  1.6130 -    apply (rule_tac convex_on_bounded_continuous)
  1.6131 -    apply (rule open_ball, rule convex_on_subset[OF conv])
  1.6132 -    apply (rule ball_subset_cball)
  1.6133 -    apply force
  1.6134 -    done
  1.6135 -  then show "continuous (at x) f"
  1.6136 -    unfolding continuous_on_eq_continuous_at[OF open_ball]
  1.6137 -    using \<open>d > 0\<close> by auto
  1.6138 -qed
  1.6139 -
  1.6140 -
  1.6141 -subsection \<open>Line segments, Starlike Sets, etc.\<close>
  1.6142 -
  1.6143 -definition midpoint :: "'a::real_vector \<Rightarrow> 'a \<Rightarrow> 'a"
  1.6144 -  where "midpoint a b = (inverse (2::real)) *\<^sub>R (a + b)"
  1.6145 -
  1.6146 -definition closed_segment :: "'a::real_vector \<Rightarrow> 'a \<Rightarrow> 'a set"
  1.6147 -  where "closed_segment a b = {(1 - u) *\<^sub>R a + u *\<^sub>R b | u::real. 0 \<le> u \<and> u \<le> 1}"
  1.6148 -
  1.6149 -definition open_segment :: "'a::real_vector \<Rightarrow> 'a \<Rightarrow> 'a set" where
  1.6150 -  "open_segment a b \<equiv> closed_segment a b - {a,b}"
  1.6151 -
  1.6152 -lemmas segment = open_segment_def closed_segment_def
  1.6153 -
  1.6154 -lemma in_segment:
  1.6155 -    "x \<in> closed_segment a b \<longleftrightarrow> (\<exists>u. 0 \<le> u \<and> u \<le> 1 \<and> x = (1 - u) *\<^sub>R a + u *\<^sub>R b)"
  1.6156 -    "x \<in> open_segment a b \<longleftrightarrow> a \<noteq> b \<and> (\<exists>u. 0 < u \<and> u < 1 \<and> x = (1 - u) *\<^sub>R a + u *\<^sub>R b)"
  1.6157 -  using less_eq_real_def by (auto simp: segment algebra_simps)
  1.6158 -
  1.6159 -lemma closed_segment_linear_image:
  1.6160 -    "linear f \<Longrightarrow> closed_segment (f a) (f b) = f ` (closed_segment a b)"
  1.6161 -  by (force simp add: in_segment linear_add_cmul)
  1.6162 -
  1.6163 -lemma open_segment_linear_image:
  1.6164 -    "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> open_segment (f a) (f b) = f ` (open_segment a b)"
  1.6165 -  by (force simp: open_segment_def closed_segment_linear_image inj_on_def)
  1.6166 -
  1.6167 -lemma closed_segment_translation:
  1.6168 -    "closed_segment (c + a) (c + b) = image (\<lambda>x. c + x) (closed_segment a b)"
  1.6169 -apply safe
  1.6170 -apply (rule_tac x="x-c" in image_eqI)
  1.6171 -apply (auto simp: in_segment algebra_simps)
  1.6172 -done
  1.6173 -
  1.6174 -lemma open_segment_translation:
  1.6175 -    "open_segment (c + a) (c + b) = image (\<lambda>x. c + x) (open_segment a b)"
  1.6176 -by (simp add: open_segment_def closed_segment_translation translation_diff)
  1.6177 -
  1.6178 -lemma open_segment_PairD:
  1.6179 -    "(x, x') \<in> open_segment (a, a') (b, b')
  1.6180 -     \<Longrightarrow> (x \<in> open_segment a b \<or> a = b) \<and> (x' \<in> open_segment a' b' \<or> a' = b')"
  1.6181 -  by (auto simp: in_segment)
  1.6182 -
  1.6183 -lemma closed_segment_PairD:
  1.6184 -  "(x, x') \<in> closed_segment (a, a') (b, b') \<Longrightarrow> x \<in> closed_segment a b \<and> x' \<in> closed_segment a' b'"
  1.6185 -  by (auto simp: closed_segment_def)
  1.6186 -
  1.6187 -lemma closed_segment_translation_eq [simp]:
  1.6188 -    "d + x \<in> closed_segment (d + a) (d + b) \<longleftrightarrow> x \<in> closed_segment a b"
  1.6189 -proof -
  1.6190 -  have *: "\<And>d x a b. x \<in> closed_segment a b \<Longrightarrow> d + x \<in> closed_segment (d + a) (d + b)"
  1.6191 -    apply (simp add: closed_segment_def)
  1.6192 -    apply (erule ex_forward)
  1.6193 -    apply (simp add: algebra_simps)
  1.6194 -    done
  1.6195 -  show ?thesis
  1.6196 -  using * [where d = "-d"] *
  1.6197 -  by (fastforce simp add:)
  1.6198 -qed
  1.6199 -
  1.6200 -lemma open_segment_translation_eq [simp]:
  1.6201 -    "d + x \<in> open_segment (d + a) (d + b) \<longleftrightarrow> x \<in> open_segment a b"
  1.6202 -  by (simp add: open_segment_def)
  1.6203 -
  1.6204 -definition "between = (\<lambda>(a,b) x. x \<in> closed_segment a b)"
  1.6205 -
  1.6206 -definition "starlike s \<longleftrightarrow> (\<exists>a\<in>s. \<forall>x\<in>s. closed_segment a x \<subseteq> s)"
  1.6207 -
  1.6208 -lemma starlike_UNIV [simp]: "starlike UNIV"
  1.6209 -  by (simp add: starlike_def)
  1.6210 -
  1.6211 -lemma midpoint_refl: "midpoint x x = x"
  1.6212 -  unfolding midpoint_def
  1.6213 -  unfolding scaleR_right_distrib
  1.6214 -  unfolding scaleR_left_distrib[symmetric]
  1.6215 -  by auto
  1.6216 -
  1.6217 -lemma midpoint_sym: "midpoint a b = midpoint b a"
  1.6218 -  unfolding midpoint_def by (auto simp add: scaleR_right_distrib)
  1.6219 -
  1.6220 -lemma midpoint_eq_iff: "midpoint a b = c \<longleftrightarrow> a + b = c + c"
  1.6221 -proof -
  1.6222 -  have "midpoint a b = c \<longleftrightarrow> scaleR 2 (midpoint a b) = scaleR 2 c"
  1.6223 -    by simp
  1.6224 -  then show ?thesis
  1.6225 -    unfolding midpoint_def scaleR_2 [symmetric] by simp
  1.6226 -qed
  1.6227 -
  1.6228 -lemma dist_midpoint:
  1.6229 -  fixes a b :: "'a::real_normed_vector" shows
  1.6230 -  "dist a (midpoint a b) = (dist a b) / 2" (is ?t1)
  1.6231 -  "dist b (midpoint a b) = (dist a b) / 2" (is ?t2)
  1.6232 -  "dist (midpoint a b) a = (dist a b) / 2" (is ?t3)
  1.6233 -  "dist (midpoint a b) b = (dist a b) / 2" (is ?t4)
  1.6234 -proof -
  1.6235 -  have *: "\<And>x y::'a. 2 *\<^sub>R x = - y \<Longrightarrow> norm x = (norm y) / 2"
  1.6236 -    unfolding equation_minus_iff by auto
  1.6237 -  have **: "\<And>x y::'a. 2 *\<^sub>R x =   y \<Longrightarrow> norm x = (norm y) / 2"
  1.6238 -    by auto
  1.6239 -  note scaleR_right_distrib [simp]
  1.6240 -  show ?t1
  1.6241 -    unfolding midpoint_def dist_norm
  1.6242 -    apply (rule **)
  1.6243 -    apply (simp add: scaleR_right_diff_distrib)
  1.6244 -    apply (simp add: scaleR_2)
  1.6245 -    done
  1.6246 -  show ?t2
  1.6247 -    unfolding midpoint_def dist_norm
  1.6248 -    apply (rule *)
  1.6249 -    apply (simp add: scaleR_right_diff_distrib)
  1.6250 -    apply (simp add: scaleR_2)
  1.6251 -    done
  1.6252 -  show ?t3
  1.6253 -    unfolding midpoint_def dist_norm
  1.6254 -    apply (rule *)
  1.6255 -    apply (simp add: scaleR_right_diff_distrib)
  1.6256 -    apply (simp add: scaleR_2)
  1.6257 -    done
  1.6258 -  show ?t4
  1.6259 -    unfolding midpoint_def dist_norm
  1.6260 -    apply (rule **)
  1.6261 -    apply (simp add: scaleR_right_diff_distrib)
  1.6262 -    apply (simp add: scaleR_2)
  1.6263 -    done
  1.6264 -qed
  1.6265 -
  1.6266 -lemma midpoint_eq_endpoint [simp]:
  1.6267 -  "midpoint a b = a \<longleftrightarrow> a = b"
  1.6268 -  "midpoint a b = b \<longleftrightarrow> a = b"
  1.6269 -  unfolding midpoint_eq_iff by auto
  1.6270 -
  1.6271 -lemma convex_contains_segment:
  1.6272 -  "convex s \<longleftrightarrow> (\<forall>a\<in>s. \<forall>b\<in>s. closed_segment a b \<subseteq> s)"
  1.6273 -  unfolding convex_alt closed_segment_def by auto
  1.6274 -
  1.6275 -lemma closed_segment_subset: "\<lbrakk>x \<in> s; y \<in> s; convex s\<rbrakk> \<Longrightarrow> closed_segment x y \<subseteq> s"
  1.6276 -  by (simp add: convex_contains_segment)
  1.6277 -
  1.6278 -lemma closed_segment_subset_convex_hull:
  1.6279 -    "\<lbrakk>x \<in> convex hull s; y \<in> convex hull s\<rbrakk> \<Longrightarrow> closed_segment x y \<subseteq> convex hull s"
  1.6280 -  using convex_contains_segment by blast
  1.6281 -
  1.6282 -lemma convex_imp_starlike:
  1.6283 -  "convex s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> starlike s"
  1.6284 -  unfolding convex_contains_segment starlike_def by auto
  1.6285 -
  1.6286 -lemma segment_convex_hull:
  1.6287 -  "closed_segment a b = convex hull {a,b}"
  1.6288 -proof -
  1.6289 -  have *: "\<And>x. {x} \<noteq> {}" by auto
  1.6290 -  show ?thesis
  1.6291 -    unfolding segment convex_hull_insert[OF *] convex_hull_singleton
  1.6292 -    by (safe; rule_tac x="1 - u" in exI; force)
  1.6293 -qed
  1.6294 -
  1.6295 -lemma open_closed_segment: "u \<in> open_segment w z \<Longrightarrow> u \<in> closed_segment w z"
  1.6296 -  by (auto simp add: closed_segment_def open_segment_def)
  1.6297 -
  1.6298 -lemma segment_open_subset_closed:
  1.6299 -   "open_segment a b \<subseteq> closed_segment a b"
  1.6300 -  by (auto simp: closed_segment_def open_segment_def)
  1.6301 -
  1.6302 -lemma bounded_closed_segment:
  1.6303 -    fixes a :: "'a::euclidean_space" shows "bounded (closed_segment a b)"
  1.6304 -  by (simp add: segment_convex_hull compact_convex_hull compact_imp_bounded)
  1.6305 -
  1.6306 -lemma bounded_open_segment:
  1.6307 -    fixes a :: "'a::euclidean_space" shows "bounded (open_segment a b)"
  1.6308 -  by (rule bounded_subset [OF bounded_closed_segment segment_open_subset_closed])
  1.6309 -
  1.6310 -lemmas bounded_segment = bounded_closed_segment open_closed_segment
  1.6311 -
  1.6312 -lemma ends_in_segment [iff]: "a \<in> closed_segment a b" "b \<in> closed_segment a b"
  1.6313 -  unfolding segment_convex_hull
  1.6314 -  by (auto intro!: hull_subset[unfolded subset_eq, rule_format])
  1.6315 -
  1.6316 -lemma segment_furthest_le:
  1.6317 -  fixes a b x y :: "'a::euclidean_space"
  1.6318 -  assumes "x \<in> closed_segment a b"
  1.6319 -  shows "norm (y - x) \<le> norm (y - a) \<or>  norm (y - x) \<le> norm (y - b)"
  1.6320 -proof -
  1.6321 -  obtain z where "z \<in> {a, b}" "norm (x - y) \<le> norm (z - y)"
  1.6322 -    using simplex_furthest_le[of "{a, b}" y]
  1.6323 -    using assms[unfolded segment_convex_hull]
  1.6324 -    by auto
  1.6325 -  then show ?thesis
  1.6326 -    by (auto simp add:norm_minus_commute)
  1.6327 -qed
  1.6328 -
  1.6329 -lemma closed_segment_commute: "closed_segment a b = closed_segment b a"
  1.6330 -proof -
  1.6331 -  have "{a, b} = {b, a}" by auto
  1.6332 -  thus ?thesis
  1.6333 -    by (simp add: segment_convex_hull)
  1.6334 -qed
  1.6335 -
  1.6336 -lemma segment_bound1:
  1.6337 -  assumes "x \<in> closed_segment a b"
  1.6338 -  shows "norm (x - a) \<le> norm (b - a)"
  1.6339 -proof -
  1.6340 -  obtain u where "x = (1 - u) *\<^sub>R a + u *\<^sub>R b" "0 \<le> u" "u \<le> 1"
  1.6341 -    using assms by (auto simp add: closed_segment_def)
  1.6342 -  then show "norm (x - a) \<le> norm (b - a)"
  1.6343 -    apply clarify
  1.6344 -    apply (auto simp: algebra_simps)
  1.6345 -    apply (simp add: scaleR_diff_right [symmetric] mult_left_le_one_le)
  1.6346 -    done
  1.6347 -qed
  1.6348 -
  1.6349 -lemma segment_bound:
  1.6350 -  assumes "x \<in> closed_segment a b"
  1.6351 -  shows "norm (x - a) \<le> norm (b - a)" "norm (x - b) \<le> norm (b - a)"
  1.6352 -apply (simp add: assms segment_bound1)
  1.6353 -by (metis assms closed_segment_commute dist_commute dist_norm segment_bound1)
  1.6354 -
  1.6355 -lemma open_segment_commute: "open_segment a b = open_segment b a"
  1.6356 -proof -
  1.6357 -  have "{a, b} = {b, a}" by auto
  1.6358 -  thus ?thesis
  1.6359 -    by (simp add: closed_segment_commute open_segment_def)
  1.6360 -qed
  1.6361 -
  1.6362 -lemma closed_segment_idem [simp]: "closed_segment a a = {a}"
  1.6363 -  unfolding segment by (auto simp add: algebra_simps)
  1.6364 -
  1.6365 -lemma open_segment_idem [simp]: "open_segment a a = {}"
  1.6366 -  by (simp add: open_segment_def)
  1.6367 -
  1.6368 -lemma closed_segment_eq_open: "closed_segment a b = open_segment a b \<union> {a,b}"
  1.6369 -  using open_segment_def by auto
  1.6370 -
  1.6371 -lemma convex_contains_open_segment:
  1.6372 -  "convex s \<longleftrightarrow> (\<forall>a\<in>s. \<forall>b\<in>s. open_segment a b \<subseteq> s)"
  1.6373 -  by (simp add: convex_contains_segment closed_segment_eq_open)
  1.6374 -
  1.6375 -lemma closed_segment_eq_real_ivl:
  1.6376 -  fixes a b::real
  1.6377 -  shows "closed_segment a b = (if a \<le> b then {a .. b} else {b .. a})"
  1.6378 -proof -
  1.6379 -  have "b \<le> a \<Longrightarrow> closed_segment b a = {b .. a}"
  1.6380 -    and "a \<le> b \<Longrightarrow> closed_segment a b = {a .. b}"
  1.6381 -    by (auto simp: convex_hull_eq_real_cbox segment_convex_hull)
  1.6382 -  thus ?thesis
  1.6383 -    by (auto simp: closed_segment_commute)
  1.6384 -qed
  1.6385 -
  1.6386 -lemma closed_segment_real_eq:
  1.6387 -  fixes u::real shows "closed_segment u v = (\<lambda>x. (v - u) * x + u) ` {0..1}"
  1.6388 -  by (simp add: add.commute [of u] image_affinity_atLeastAtMost [where c=u] closed_segment_eq_real_ivl)
  1.6389 -
  1.6390 -lemma dist_in_closed_segment:
  1.6391 -  fixes a :: "'a :: euclidean_space"
  1.6392 -  assumes "x \<in> closed_segment a b"
  1.6393 -    shows "dist x a \<le> dist a b \<and> dist x b \<le> dist a b"
  1.6394 -proof (intro conjI)
  1.6395 -  obtain u where u: "0 \<le> u" "u \<le> 1" and x: "x = (1 - u) *\<^sub>R a + u *\<^sub>R b"
  1.6396 -    using assms by (force simp: in_segment algebra_simps)
  1.6397 -  have "dist x a = u * dist a b"
  1.6398 -    apply (simp add: dist_norm algebra_simps x)
  1.6399 -    by (metis \<open>0 \<le> u\<close> abs_of_nonneg norm_minus_commute norm_scaleR real_vector.scale_right_diff_distrib)
  1.6400 -  also have "...  \<le> dist a b"
  1.6401 -    by (simp add: mult_left_le_one_le u)
  1.6402 -  finally show "dist x a \<le> dist a b" .
  1.6403 -  have "dist x b = norm ((1-u) *\<^sub>R a - (1-u) *\<^sub>R b)"
  1.6404 -    by (simp add: dist_norm algebra_simps x)
  1.6405 -  also have "... = (1-u) * dist a b"
  1.6406 -  proof -
  1.6407 -    have "norm ((1 - 1 * u) *\<^sub>R (a - b)) = (1 - 1 * u) * norm (a - b)"
  1.6408 -      using \<open>u \<le> 1\<close> by force
  1.6409 -    then show ?thesis
  1.6410 -      by (simp add: dist_norm real_vector.scale_right_diff_distrib)
  1.6411 -  qed
  1.6412 -  also have "... \<le> dist a b"
  1.6413 -    by (simp add: mult_left_le_one_le u)
  1.6414 -  finally show "dist x b \<le> dist a b" .
  1.6415 -qed
  1.6416 -
  1.6417 -lemma dist_in_open_segment:
  1.6418 -  fixes a :: "'a :: euclidean_space"
  1.6419 -  assumes "x \<in> open_segment a b"
  1.6420 -    shows "dist x a < dist a b \<and> dist x b < dist a b"
  1.6421 -proof (intro conjI)
  1.6422 -  obtain u where u: "0 < u" "u < 1" and x: "x = (1 - u) *\<^sub>R a + u *\<^sub>R b"
  1.6423 -    using assms by (force simp: in_segment algebra_simps)
  1.6424 -  have "dist x a = u * dist a b"
  1.6425 -    apply (simp add: dist_norm algebra_simps x)
  1.6426 -    by (metis abs_of_nonneg less_eq_real_def norm_minus_commute norm_scaleR real_vector.scale_right_diff_distrib \<open>0 < u\<close>)
  1.6427 -  also have *: "...  < dist a b"
  1.6428 -    by (metis (no_types) assms dist_eq_0_iff dist_not_less_zero in_segment(2) linorder_neqE_linordered_idom mult.left_neutral real_mult_less_iff1 \<open>u < 1\<close>)
  1.6429 -  finally show "dist x a < dist a b" .
  1.6430 -  have ab_ne0: "dist a b \<noteq> 0"
  1.6431 -    using * by fastforce
  1.6432 -  have "dist x b = norm ((1-u) *\<^sub>R a - (1-u) *\<^sub>R b)"
  1.6433 -    by (simp add: dist_norm algebra_simps x)
  1.6434 -  also have "... = (1-u) * dist a b"
  1.6435 -  proof -
  1.6436 -    have "norm ((1 - 1 * u) *\<^sub>R (a - b)) = (1 - 1 * u) * norm (a - b)"
  1.6437 -      using \<open>u < 1\<close> by force
  1.6438 -    then show ?thesis
  1.6439 -      by (simp add: dist_norm real_vector.scale_right_diff_distrib)
  1.6440 -  qed
  1.6441 -  also have "... < dist a b"
  1.6442 -    using ab_ne0 \<open>0 < u\<close> by simp
  1.6443 -  finally show "dist x b < dist a b" .
  1.6444 -qed
  1.6445 -
  1.6446 -lemma dist_decreases_open_segment_0:
  1.6447 -  fixes x :: "'a :: euclidean_space"
  1.6448 -  assumes "x \<in> open_segment 0 b"
  1.6449 -    shows "dist c x < dist c 0 \<or> dist c x < dist c b"
  1.6450 -proof (rule ccontr, clarsimp simp: not_less)
  1.6451 -  obtain u where u: "0 \<noteq> b" "0 < u" "u < 1" and x: "x = u *\<^sub>R b"
  1.6452 -    using assms by (auto simp: in_segment)
  1.6453 -  have xb: "x \<bullet> b < b \<bullet> b"
  1.6454 -    using u x by auto
  1.6455 -  assume "norm c \<le> dist c x"
  1.6456 -  then have "c \<bullet> c \<le> (c - x) \<bullet> (c - x)"
  1.6457 -    by (simp add: dist_norm norm_le)
  1.6458 -  moreover have "0 < x \<bullet> b"
  1.6459 -    using u x by auto
  1.6460 -  ultimately have less: "c \<bullet> b < x \<bullet> b"
  1.6461 -    by (simp add: x algebra_simps inner_commute u)
  1.6462 -  assume "dist c b \<le> dist c x"
  1.6463 -  then have "(c - b) \<bullet> (c - b) \<le> (c - x) \<bullet> (c - x)"
  1.6464 -    by (simp add: dist_norm norm_le)
  1.6465 -  then have "(b \<bullet> b) * (1 - u*u) \<le> 2 * (b \<bullet> c) * (1-u)"
  1.6466 -    by (simp add: x algebra_simps inner_commute)
  1.6467 -  then have "(1+u) * (b \<bullet> b) * (1-u) \<le> 2 * (b \<bullet> c) * (1-u)"
  1.6468 -    by (simp add: algebra_simps)
  1.6469 -  then have "(1+u) * (b \<bullet> b) \<le> 2 * (b \<bullet> c)"
  1.6470 -    using \<open>u < 1\<close> by auto
  1.6471 -  with xb have "c \<bullet> b \<ge> x \<bullet> b"
  1.6472 -    by (auto simp: x algebra_simps inner_commute)
  1.6473 -  with less show False by auto
  1.6474 -qed
  1.6475 -
  1.6476 -proposition dist_decreases_open_segment:
  1.6477 -  fixes a :: "'a :: euclidean_space"
  1.6478 -  assumes "x \<in> open_segment a b"
  1.6479 -    shows "dist c x < dist c a \<or> dist c x < dist c b"
  1.6480 -proof -
  1.6481 -  have *: "x - a \<in> open_segment 0 (b - a)" using assms
  1.6482 -    by (metis diff_self open_segment_translation_eq uminus_add_conv_diff)
  1.6483 -  show ?thesis
  1.6484 -    using dist_decreases_open_segment_0 [OF *, of "c-a"] assms
  1.6485 -    by (simp add: dist_norm)
  1.6486 -qed
  1.6487 -
  1.6488 -lemma dist_decreases_closed_segment:
  1.6489 -  fixes a :: "'a :: euclidean_space"
  1.6490 -  assumes "x \<in> closed_segment a b"
  1.6491 -    shows "dist c x \<le> dist c a \<or> dist c x \<le> dist c b"
  1.6492 -apply (cases "x \<in> open_segment a b")
  1.6493 - using dist_decreases_open_segment less_eq_real_def apply blast
  1.6494 -by (metis DiffI assms empty_iff insertE open_segment_def order_refl)
  1.6495 -
  1.6496 -lemma convex_intermediate_ball:
  1.6497 -  fixes a :: "'a :: euclidean_space"
  1.6498 -  shows "\<lbrakk>ball a r \<subseteq> T; T \<subseteq> cball a r\<rbrakk> \<Longrightarrow> convex T"
  1.6499 -apply (simp add: convex_contains_open_segment, clarify)
  1.6500 -by (metis (no_types, hide_lams) less_le_trans mem_ball mem_cball subsetCE dist_decreases_open_segment)
  1.6501 -
  1.6502 -lemma csegment_midpoint_subset: "closed_segment (midpoint a b) b \<subseteq> closed_segment a b"
  1.6503 -  apply (clarsimp simp: midpoint_def in_segment)
  1.6504 -  apply (rule_tac x="(1 + u) / 2" in exI)
  1.6505 -  apply (auto simp: algebra_simps add_divide_distrib diff_divide_distrib)
  1.6506 -  by (metis real_sum_of_halves scaleR_left.add)
  1.6507 -
  1.6508 -lemma notin_segment_midpoint:
  1.6509 -  fixes a :: "'a :: euclidean_space"
  1.6510 -  shows "a \<noteq> b \<Longrightarrow> a \<notin> closed_segment (midpoint a b) b"
  1.6511 -by (auto simp: dist_midpoint dest!: dist_in_closed_segment)
  1.6512 -
  1.6513 -subsubsection\<open>More lemmas, especially for working with the underlying formula\<close>
  1.6514 -
  1.6515 -lemma segment_eq_compose:
  1.6516 -  fixes a :: "'a :: real_vector"
  1.6517 -  shows "(\<lambda>u. (1 - u) *\<^sub>R a + u *\<^sub>R b) = (\<lambda>x. a + x) o (\<lambda>u. u *\<^sub>R (b - a))"
  1.6518 -    by (simp add: o_def algebra_simps)
  1.6519 -
  1.6520 -lemma segment_degen_1:
  1.6521 -  fixes a :: "'a :: real_vector"
  1.6522 -  shows "(1 - u) *\<^sub>R a + u *\<^sub>R b = b \<longleftrightarrow> a=b \<or> u=1"
  1.6523 -proof -
  1.6524 -  { assume "(1 - u) *\<^sub>R a + u *\<^sub>R b = b"
  1.6525 -    then have "(1 - u) *\<^sub>R a = (1 - u) *\<^sub>R b"
  1.6526 -      by (simp add: algebra_simps)
  1.6527 -    then have "a=b \<or> u=1"
  1.6528 -      by simp
  1.6529 -  } then show ?thesis
  1.6530 -      by (auto simp: algebra_simps)
  1.6531 -qed
  1.6532 -
  1.6533 -lemma segment_degen_0:
  1.6534 -    fixes a :: "'a :: real_vector"
  1.6535 -    shows "(1 - u) *\<^sub>R a + u *\<^sub>R b = a \<longleftrightarrow> a=b \<or> u=0"
  1.6536 -  using segment_degen_1 [of "1-u" b a]
  1.6537 -  by (auto simp: algebra_simps)
  1.6538 -
  1.6539 -lemma closed_segment_image_interval:
  1.6540 -     "closed_segment a b = (\<lambda>u. (1 - u) *\<^sub>R a + u *\<^sub>R b) ` {0..1}"
  1.6541 -  by (auto simp: set_eq_iff image_iff closed_segment_def)
  1.6542 -
  1.6543 -lemma open_segment_image_interval:
  1.6544 -     "open_segment a b = (if a=b then {} else (\<lambda>u. (1 - u) *\<^sub>R a + u *\<^sub>R b) ` {0<..<1})"
  1.6545 -  by (auto simp:  open_segment_def closed_segment_def segment_degen_0 segment_degen_1)
  1.6546 -
  1.6547 -lemmas segment_image_interval = closed_segment_image_interval open_segment_image_interval
  1.6548 -
  1.6549 -lemma open_segment_bound1:
  1.6550 -  assumes "x \<in> open_segment a b"
  1.6551 -  shows "norm (x - a) < norm (b - a)"
  1.6552 -proof -
  1.6553 -  obtain u where "x = (1 - u) *\<^sub>R a + u *\<^sub>R b" "0 < u" "u < 1" "a \<noteq> b"
  1.6554 -    using assms by (auto simp add: open_segment_image_interval split: if_split_asm)
  1.6555 -  then show "norm (x - a) < norm (b - a)"
  1.6556 -    apply clarify
  1.6557 -    apply (auto simp: algebra_simps)
  1.6558 -    apply (simp add: scaleR_diff_right [symmetric])
  1.6559 -    done
  1.6560 -qed
  1.6561 -
  1.6562 -lemma compact_segment [simp]:
  1.6563 -  fixes a :: "'a::real_normed_vector"
  1.6564 -  shows "compact (closed_segment a b)"
  1.6565 -  by (auto simp: segment_image_interval intro!: compact_continuous_image continuous_intros)
  1.6566 -
  1.6567 -lemma closed_segment [simp]:
  1.6568 -  fixes a :: "'a::real_normed_vector"
  1.6569 -  shows "closed (closed_segment a b)"
  1.6570 -  by (simp add: compact_imp_closed)
  1.6571 -
  1.6572 -lemma closure_closed_segment [simp]:
  1.6573 -  fixes a :: "'a::real_normed_vector"
  1.6574 -  shows "closure(closed_segment a b) = closed_segment a b"
  1.6575 -  by simp
  1.6576 -
  1.6577 -lemma open_segment_bound:
  1.6578 -  assumes "x \<in> open_segment a b"
  1.6579 -  shows "norm (x - a) < norm (b - a)" "norm (x - b) < norm (b - a)"
  1.6580 -apply (simp add: assms open_segment_bound1)
  1.6581 -by (metis assms norm_minus_commute open_segment_bound1 open_segment_commute)
  1.6582 -
  1.6583 -lemma closure_open_segment [simp]:
  1.6584 -    fixes a :: "'a::euclidean_space"
  1.6585 -    shows "closure(open_segment a b) = (if a = b then {} else closed_segment a b)"
  1.6586 -proof -
  1.6587 -  have "closure ((\<lambda>u. u *\<^sub>R (b - a)) ` {0<..<1}) = (\<lambda>u. u *\<^sub>R (b - a)) ` closure {0<..<1}" if "a \<noteq> b"
  1.6588 -    apply (rule closure_injective_linear_image [symmetric])
  1.6589 -    apply (simp add:)
  1.6590 -    using that by (simp add: inj_on_def)
  1.6591 -  then show ?thesis
  1.6592 -    by (simp add: segment_image_interval segment_eq_compose closure_greaterThanLessThan [symmetric]
  1.6593 -         closure_translation image_comp [symmetric] del: closure_greaterThanLessThan)
  1.6594 -qed
  1.6595 -
  1.6596 -lemma closed_open_segment_iff [simp]:
  1.6597 -    fixes a :: "'a::euclidean_space"  shows "closed(open_segment a b) \<longleftrightarrow> a = b"
  1.6598 -  by (metis open_segment_def DiffE closure_eq closure_open_segment ends_in_segment(1) insert_iff segment_image_interval(2))
  1.6599 -
  1.6600 -lemma compact_open_segment_iff [simp]:
  1.6601 -    fixes a :: "'a::euclidean_space"  shows "compact(open_segment a b) \<longleftrightarrow> a = b"
  1.6602 -  by (simp add: bounded_open_segment compact_eq_bounded_closed)
  1.6603 -
  1.6604 -lemma convex_closed_segment [iff]: "convex (closed_segment a b)"
  1.6605 -  unfolding segment_convex_hull by(rule convex_convex_hull)
  1.6606 -
  1.6607 -lemma convex_open_segment [iff]: "convex(open_segment a b)"
  1.6608 -proof -
  1.6609 -  have "convex ((\<lambda>u. u *\<^sub>R (b-a)) ` {0<..<1})"
  1.6610 -    by (rule convex_linear_image) auto
  1.6611 -  then show ?thesis
  1.6612 -    apply (simp add: open_segment_image_interval segment_eq_compose)
  1.6613 -    by (metis image_comp convex_translation)
  1.6614 -qed
  1.6615 -
  1.6616 -lemmas convex_segment = convex_closed_segment convex_open_segment
  1.6617 -
  1.6618 -lemma connected_segment [iff]:
  1.6619 -  fixes x :: "'a :: real_normed_vector"
  1.6620 -  shows "connected (closed_segment x y)"
  1.6621 -  by (simp add: convex_connected)
  1.6622 -
  1.6623 -lemma affine_hull_closed_segment [simp]:
  1.6624 -     "affine hull (closed_segment a b) = affine hull {a,b}"
  1.6625 -  by (simp add: segment_convex_hull)
  1.6626 -
  1.6627 -lemma affine_hull_open_segment [simp]:
  1.6628 -    fixes a :: "'a::euclidean_space"
  1.6629 -    shows "affine hull (open_segment a b) = (if a = b then {} else affine hull {a,b})"
  1.6630 -by (metis affine_hull_convex_hull affine_hull_empty closure_open_segment closure_same_affine_hull segment_convex_hull)
  1.6631 -
  1.6632 -lemma rel_interior_closure_convex_segment:
  1.6633 -  fixes S :: "_::euclidean_space set"
  1.6634 -  assumes "convex S" "a \<in> rel_interior S" "b \<in> closure S"
  1.6635 -    shows "open_segment a b \<subseteq> rel_interior S"
  1.6636 -proof
  1.6637 -  fix x
  1.6638 -  have [simp]: "(1 - u) *\<^sub>R a + u *\<^sub>R b = b - (1 - u) *\<^sub>R (b - a)" for u
  1.6639 -    by (simp add: algebra_simps)
  1.6640 -  assume "x \<in> open_segment a b"
  1.6641 -  then show "x \<in> rel_interior S"
  1.6642 -    unfolding closed_segment_def open_segment_def  using assms
  1.6643 -    by (auto intro: rel_interior_closure_convex_shrink)
  1.6644 -qed
  1.6645 -
  1.6646 -subsection\<open>More results about segments\<close>
  1.6647 -
  1.6648 -lemma dist_half_times2:
  1.6649 -  fixes a :: "'a :: real_normed_vector"
  1.6650 -  shows "dist ((1 / 2) *\<^sub>R (a + b)) x * 2 = dist (a+b) (2 *\<^sub>R x)"
  1.6651 -proof -
  1.6652 -  have "norm ((1 / 2) *\<^sub>R (a + b) - x) * 2 = norm (2 *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x))"
  1.6653 -    by simp
  1.6654 -  also have "... = norm ((a + b) - 2 *\<^sub>R x)"
  1.6655 -    by (simp add: real_vector.scale_right_diff_distrib)
  1.6656 -  finally show ?thesis
  1.6657 -    by (simp only: dist_norm)
  1.6658 -qed
  1.6659 -
  1.6660 -lemma closed_segment_as_ball:
  1.6661 -    "closed_segment a b = affine hull {a,b} \<inter> cball(inverse 2 *\<^sub>R (a + b))(norm(b - a) / 2)"
  1.6662 -proof (cases "b = a")
  1.6663 -  case True then show ?thesis by (auto simp: hull_inc)
  1.6664 -next
  1.6665 -  case False
  1.6666 -  then have *: "((\<exists>u v. x = u *\<^sub>R a + v *\<^sub>R b \<and> u + v = 1) \<and>
  1.6667 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 \<le> norm (b - a)) =
  1.6668 -                 (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> 0 \<le> u \<and> u \<le> 1)" for x
  1.6669 -  proof -
  1.6670 -    have "((\<exists>u v. x = u *\<^sub>R a + v *\<^sub>R b \<and> u + v = 1) \<and>
  1.6671 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 \<le> norm (b - a)) =
  1.6672 -          ((\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b) \<and>
  1.6673 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 \<le> norm (b - a))"
  1.6674 -      unfolding eq_diff_eq [symmetric] by simp
  1.6675 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6676 -                          norm ((a+b) - (2 *\<^sub>R x)) \<le> norm (b - a))"
  1.6677 -      by (simp add: dist_half_times2) (simp add: dist_norm)
  1.6678 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6679 -            norm ((a+b) - (2 *\<^sub>R ((1 - u) *\<^sub>R a + u *\<^sub>R b))) \<le> norm (b - a))"
  1.6680 -      by auto
  1.6681 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6682 -                norm ((1 - u * 2) *\<^sub>R (b - a)) \<le> norm (b - a))"
  1.6683 -      by (simp add: algebra_simps scaleR_2)
  1.6684 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6685 -                          \<bar>1 - u * 2\<bar> * norm (b - a) \<le> norm (b - a))"
  1.6686 -      by simp
  1.6687 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> \<bar>1 - u * 2\<bar> \<le> 1)"
  1.6688 -      by (simp add: mult_le_cancel_right2 False)
  1.6689 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> 0 \<le> u \<and> u \<le> 1)"
  1.6690 -      by auto
  1.6691 -    finally show ?thesis .
  1.6692 -  qed
  1.6693 -  show ?thesis
  1.6694 -    by (simp add: affine_hull_2 Set.set_eq_iff closed_segment_def *)
  1.6695 -qed
  1.6696 -
  1.6697 -lemma open_segment_as_ball:
  1.6698 -    "open_segment a b =
  1.6699 -     affine hull {a,b} \<inter> ball(inverse 2 *\<^sub>R (a + b))(norm(b - a) / 2)"
  1.6700 -proof (cases "b = a")
  1.6701 -  case True then show ?thesis by (auto simp: hull_inc)
  1.6702 -next
  1.6703 -  case False
  1.6704 -  then have *: "((\<exists>u v. x = u *\<^sub>R a + v *\<^sub>R b \<and> u + v = 1) \<and>
  1.6705 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 < norm (b - a)) =
  1.6706 -                 (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> 0 < u \<and> u < 1)" for x
  1.6707 -  proof -
  1.6708 -    have "((\<exists>u v. x = u *\<^sub>R a + v *\<^sub>R b \<and> u + v = 1) \<and>
  1.6709 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 < norm (b - a)) =
  1.6710 -          ((\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b) \<and>
  1.6711 -                  dist ((1 / 2) *\<^sub>R (a + b)) x * 2 < norm (b - a))"
  1.6712 -      unfolding eq_diff_eq [symmetric] by simp
  1.6713 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6714 -                          norm ((a+b) - (2 *\<^sub>R x)) < norm (b - a))"
  1.6715 -      by (simp add: dist_half_times2) (simp add: dist_norm)
  1.6716 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6717 -            norm ((a+b) - (2 *\<^sub>R ((1 - u) *\<^sub>R a + u *\<^sub>R b))) < norm (b - a))"
  1.6718 -      by auto
  1.6719 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6720 -                norm ((1 - u * 2) *\<^sub>R (b - a)) < norm (b - a))"
  1.6721 -      by (simp add: algebra_simps scaleR_2)
  1.6722 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and>
  1.6723 -                          \<bar>1 - u * 2\<bar> * norm (b - a) < norm (b - a))"
  1.6724 -      by simp
  1.6725 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> \<bar>1 - u * 2\<bar> < 1)"
  1.6726 -      by (simp add: mult_le_cancel_right2 False)
  1.6727 -    also have "... = (\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> 0 < u \<and> u < 1)"
  1.6728 -      by auto
  1.6729 -    finally show ?thesis .
  1.6730 -  qed
  1.6731 -  show ?thesis
  1.6732 -    using False by (force simp: affine_hull_2 Set.set_eq_iff open_segment_image_interval *)
  1.6733 -qed
  1.6734 -
  1.6735 -lemmas segment_as_ball = closed_segment_as_ball open_segment_as_ball
  1.6736 -
  1.6737 -lemma closed_segment_neq_empty [simp]: "closed_segment a b \<noteq> {}"
  1.6738 -  by auto
  1.6739 -
  1.6740 -lemma open_segment_eq_empty [simp]: "open_segment a b = {} \<longleftrightarrow> a = b"
  1.6741 -proof -
  1.6742 -  { assume a1: "open_segment a b = {}"
  1.6743 -    have "{} \<noteq> {0::real<..<1}"
  1.6744 -      by simp
  1.6745 -    then have "a = b"
  1.6746 -      using a1 open_segment_image_interval by fastforce
  1.6747 -  } then show ?thesis by auto
  1.6748 -qed
  1.6749 -
  1.6750 -lemma open_segment_eq_empty' [simp]: "{} = open_segment a b \<longleftrightarrow> a = b"
  1.6751 -  using open_segment_eq_empty by blast
  1.6752 -
  1.6753 -lemmas segment_eq_empty = closed_segment_neq_empty open_segment_eq_empty
  1.6754 -
  1.6755 -lemma inj_segment:
  1.6756 -  fixes a :: "'a :: real_vector"
  1.6757 -  assumes "a \<noteq> b"
  1.6758 -    shows "inj_on (\<lambda>u. (1 - u) *\<^sub>R a + u *\<^sub>R b) I"
  1.6759 -proof
  1.6760 -  fix x y
  1.6761 -  assume "(1 - x) *\<^sub>R a + x *\<^sub>R b = (1 - y) *\<^sub>R a + y *\<^sub>R b"
  1.6762 -  then have "x *\<^sub>R (b - a) = y *\<^sub>R (b - a)"
  1.6763 -    by (simp add: algebra_simps)
  1.6764 -  with assms show "x = y"
  1.6765 -    by (simp add: real_vector.scale_right_imp_eq)
  1.6766 -qed
  1.6767 -
  1.6768 -lemma finite_closed_segment [simp]: "finite(closed_segment a b) \<longleftrightarrow> a = b"
  1.6769 -  apply auto
  1.6770 -  apply (rule ccontr)
  1.6771 -  apply (simp add: segment_image_interval)
  1.6772 -  using infinite_Icc [OF zero_less_one] finite_imageD [OF _ inj_segment] apply blast
  1.6773 -  done
  1.6774 -
  1.6775 -lemma finite_open_segment [simp]: "finite(open_segment a b) \<longleftrightarrow> a = b"
  1.6776 -  by (auto simp: open_segment_def)
  1.6777 -
  1.6778 -lemmas finite_segment = finite_closed_segment finite_open_segment
  1.6779 -
  1.6780 -lemma closed_segment_eq_sing: "closed_segment a b = {c} \<longleftrightarrow> a = c \<and> b = c"
  1.6781 -  by auto
  1.6782 -
  1.6783 -lemma open_segment_eq_sing: "open_segment a b \<noteq> {c}"
  1.6784 -  by (metis finite_insert finite_open_segment insert_not_empty open_segment_image_interval)
  1.6785 -
  1.6786 -lemmas segment_eq_sing = closed_segment_eq_sing open_segment_eq_sing
  1.6787 -
  1.6788 -lemma subset_closed_segment:
  1.6789 -    "closed_segment a b \<subseteq> closed_segment c d \<longleftrightarrow>
  1.6790 -     a \<in> closed_segment c d \<and> b \<in> closed_segment c d"
  1.6791 -  by auto (meson contra_subsetD convex_closed_segment convex_contains_segment)
  1.6792 -
  1.6793 -lemma subset_co_segment:
  1.6794 -    "closed_segment a b \<subseteq> open_segment c d \<longleftrightarrow>
  1.6795 -     a \<in> open_segment c d \<and> b \<in> open_segment c d"
  1.6796 -using closed_segment_subset by blast
  1.6797 -
  1.6798 -lemma subset_open_segment:
  1.6799 -  fixes a :: "'a::euclidean_space"
  1.6800 -  shows "open_segment a b \<subseteq> open_segment c d \<longleftrightarrow>
  1.6801 -         a = b \<or> a \<in> closed_segment c d \<and> b \<in> closed_segment c d"
  1.6802 -        (is "?lhs = ?rhs")
  1.6803 -proof (cases "a = b")
  1.6804 -  case True then show ?thesis by simp
  1.6805 -next
  1.6806 -  case False show ?thesis
  1.6807 -  proof
  1.6808 -    assume rhs: ?rhs
  1.6809 -    with \<open>a \<noteq> b\<close> have "c \<noteq> d"
  1.6810 -      using closed_segment_idem singleton_iff by auto
  1.6811 -    have "\<exists>uc. (1 - u) *\<^sub>R ((1 - ua) *\<^sub>R c + ua *\<^sub>R d) + u *\<^sub>R ((1 - ub) *\<^sub>R c + ub *\<^sub>R d) =
  1.6812 -               (1 - uc) *\<^sub>R c + uc *\<^sub>R d \<and> 0 < uc \<and> uc < 1"
  1.6813 -        if neq: "(1 - ua) *\<^sub>R c + ua *\<^sub>R d \<noteq> (1 - ub) *\<^sub>R c + ub *\<^sub>R d" "c \<noteq> d"
  1.6814 -           and "a = (1 - ua) *\<^sub>R c + ua *\<^sub>R d" "b = (1 - ub) *\<^sub>R c + ub *\<^sub>R d"
  1.6815 -           and u: "0 < u" "u < 1" and uab: "0 \<le> ua" "ua \<le> 1" "0 \<le> ub" "ub \<le> 1"
  1.6816 -        for u ua ub
  1.6817 -    proof -
  1.6818 -      have "ua \<noteq> ub"
  1.6819 -        using neq by auto
  1.6820 -      moreover have "(u - 1) * ua \<le> 0" using u uab
  1.6821 -        by (simp add: mult_nonpos_nonneg)
  1.6822 -      ultimately have lt: "(u - 1) * ua < u * ub" using u uab
  1.6823 -        by (metis antisym_conv diff_ge_0_iff_ge le_less_trans mult_eq_0_iff mult_le_0_iff not_less)
  1.6824 -      have "p * ua + q * ub < p+q" if p: "0 < p" and  q: "0 < q" for p q
  1.6825 -      proof -
  1.6826 -        have "\<not> p \<le> 0" "\<not> q \<le> 0"
  1.6827 -          using p q not_less by blast+
  1.6828 -        then show ?thesis
  1.6829 -          by (metis \<open>ua \<noteq> ub\<close> add_less_cancel_left add_less_cancel_right add_mono_thms_linordered_field(5)
  1.6830 -                    less_eq_real_def mult_cancel_left1 mult_less_cancel_left2 uab(2) uab(4))
  1.6831 -      qed
  1.6832 -      then have "(1 - u) * ua + u * ub < 1" using u \<open>ua \<noteq> ub\<close>
  1.6833 -        by (metis diff_add_cancel diff_gt_0_iff_gt)
  1.6834 -      with lt show ?thesis
  1.6835 -        by (rule_tac x="ua + u*(ub-ua)" in exI) (simp add: algebra_simps)
  1.6836 -    qed
  1.6837 -    with rhs \<open>a \<noteq> b\<close> \<open>c \<noteq> d\<close> show ?lhs
  1.6838 -      unfolding open_segment_image_interval closed_segment_def
  1.6839 -      by (fastforce simp add:)
  1.6840 -  next
  1.6841 -    assume lhs: ?lhs
  1.6842 -    with \<open>a \<noteq> b\<close> have "c \<noteq> d"
  1.6843 -      by (meson finite_open_segment rev_finite_subset)
  1.6844 -    have "closure (open_segment a b) \<subseteq> closure (open_segment c d)"
  1.6845 -      using lhs closure_mono by blast
  1.6846 -    then have "closed_segment a b \<subseteq> closed_segment c d"
  1.6847 -      by (simp add: \<open>a \<noteq> b\<close> \<open>c \<noteq> d\<close>)
  1.6848 -    then show ?rhs
  1.6849 -      by (force simp: \<open>a \<noteq> b\<close>)
  1.6850 -  qed
  1.6851 -qed
  1.6852 -
  1.6853 -lemma subset_oc_segment:
  1.6854 -  fixes a :: "'a::euclidean_space"
  1.6855 -  shows "open_segment a b \<subseteq> closed_segment c d \<longleftrightarrow>
  1.6856 -         a = b \<or> a \<in> closed_segment c d \<and> b \<in> closed_segment c d"
  1.6857 -apply (simp add: subset_open_segment [symmetric])
  1.6858 -apply (rule iffI)
  1.6859 - apply (metis closure_closed_segment closure_mono closure_open_segment subset_closed_segment subset_open_segment)
  1.6860 -apply (meson dual_order.trans segment_open_subset_closed)
  1.6861 -done
  1.6862 -
  1.6863 -lemmas subset_segment = subset_closed_segment subset_co_segment subset_oc_segment subset_open_segment
  1.6864 -
  1.6865 -
  1.6866 -subsection\<open>Betweenness\<close>
  1.6867 -
  1.6868 -lemma between_mem_segment: "between (a,b) x \<longleftrightarrow> x \<in> closed_segment a b"
  1.6869 -  unfolding between_def by auto
  1.6870 -
  1.6871 -lemma between: "between (a, b) (x::'a::euclidean_space) \<longleftrightarrow> dist a b = (dist a x) + (dist x b)"
  1.6872 -proof (cases "a = b")
  1.6873 -  case True
  1.6874 -  then show ?thesis
  1.6875 -    unfolding between_def split_conv
  1.6876 -    by (auto simp add: dist_commute)
  1.6877 -next
  1.6878 -  case False
  1.6879 -  then have Fal: "norm (a - b) \<noteq> 0" and Fal2: "norm (a - b) > 0"
  1.6880 -    by auto
  1.6881 -  have *: "\<And>u. a - ((1 - u) *\<^sub>R a + u *\<^sub>R b) = u *\<^sub>R (a - b)"
  1.6882 -    by (auto simp add: algebra_simps)
  1.6883 -  show ?thesis
  1.6884 -    unfolding between_def split_conv closed_segment_def mem_Collect_eq
  1.6885 -    apply rule
  1.6886 -    apply (elim exE conjE)
  1.6887 -    apply (subst dist_triangle_eq)
  1.6888 -  proof -
  1.6889 -    fix u
  1.6890 -    assume as: "x = (1 - u) *\<^sub>R a + u *\<^sub>R b" "0 \<le> u" "u \<le> 1"
  1.6891 -    then have *: "a - x = u *\<^sub>R (a - b)" "x - b = (1 - u) *\<^sub>R (a - b)"
  1.6892 -      unfolding as(1) by (auto simp add:algebra_simps)
  1.6893 -    show "norm (a - x) *\<^sub>R (x - b) = norm (x - b) *\<^sub>R (a - x)"
  1.6894 -      unfolding norm_minus_commute[of x a] * using as(2,3)
  1.6895 -      by (auto simp add: field_simps)
  1.6896 -  next
  1.6897 -    assume as: "dist a b = dist a x + dist x b"
  1.6898 -    have "norm (a - x) / norm (a - b) \<le> 1"
  1.6899 -      using Fal2 unfolding as[unfolded dist_norm] norm_ge_zero by auto
  1.6900 -    then show "\<exists>u. x = (1 - u) *\<^sub>R a + u *\<^sub>R b \<and> 0 \<le> u \<and> u \<le> 1"
  1.6901 -      apply (rule_tac x="dist a x / dist a b" in exI)
  1.6902 -      unfolding dist_norm
  1.6903 -      apply (subst euclidean_eq_iff)
  1.6904 -      apply rule
  1.6905 -      defer
  1.6906 -      apply rule
  1.6907 -      prefer 3
  1.6908 -      apply rule
  1.6909 -    proof -
  1.6910 -      fix i :: 'a
  1.6911 -      assume i: "i \<in> Basis"
  1.6912 -      have "((1 - norm (a - x) / norm (a - b)) *\<^sub>R a + (norm (a - x) / norm (a - b)) *\<^sub>R b) \<bullet> i =
  1.6913 -        ((norm (a - b) - norm (a - x)) * (a \<bullet> i) + norm (a - x) * (b \<bullet> i)) / norm (a - b)"
  1.6914 -        using Fal by (auto simp add: field_simps inner_simps)
  1.6915 -      also have "\<dots> = x\<bullet>i"
  1.6916 -        apply (rule divide_eq_imp[OF Fal])
  1.6917 -        unfolding as[unfolded dist_norm]
  1.6918 -        using as[unfolded dist_triangle_eq]
  1.6919 -        apply -
  1.6920 -        apply (subst (asm) euclidean_eq_iff)
  1.6921 -        using i
  1.6922 -        apply (erule_tac x=i in ballE)
  1.6923 -        apply (auto simp add: field_simps inner_simps)
  1.6924 -        done
  1.6925 -      finally show "x \<bullet> i =
  1.6926 -        ((1 - norm (a - x) / norm (a - b)) *\<^sub>R a + (norm (a - x) / norm (a - b)) *\<^sub>R b) \<bullet> i"
  1.6927 -        by auto
  1.6928 -    qed (insert Fal2, auto)
  1.6929 -  qed
  1.6930 -qed
  1.6931 -
  1.6932 -lemma between_midpoint:
  1.6933 -  fixes a :: "'a::euclidean_space"
  1.6934 -  shows "between (a,b) (midpoint a b)" (is ?t1)
  1.6935 -    and "between (b,a) (midpoint a b)" (is ?t2)
  1.6936 -proof -
  1.6937 -  have *: "\<And>x y z. x = (1/2::real) *\<^sub>R z \<Longrightarrow> y = (1/2) *\<^sub>R z \<Longrightarrow> norm z = norm x + norm y"
  1.6938 -    by auto
  1.6939 -  show ?t1 ?t2
  1.6940 -    unfolding between midpoint_def dist_norm
  1.6941 -    apply(rule_tac[!] *)
  1.6942 -    unfolding euclidean_eq_iff[where 'a='a]
  1.6943 -    apply (auto simp add: field_simps inner_simps)
  1.6944 -    done
  1.6945 -qed
  1.6946 -
  1.6947 -lemma between_mem_convex_hull:
  1.6948 -  "between (a,b) x \<longleftrightarrow> x \<in> convex hull {a,b}"
  1.6949 -  unfolding between_mem_segment segment_convex_hull ..
  1.6950 -
  1.6951 -
  1.6952 -subsection \<open>Shrinking towards the interior of a convex set\<close>
  1.6953 -
  1.6954 -lemma mem_interior_convex_shrink:
  1.6955 -  fixes s :: "'a::euclidean_space set"
  1.6956 -  assumes "convex s"
  1.6957 -    and "c \<in> interior s"
  1.6958 -    and "x \<in> s"
  1.6959 -    and "0 < e"
  1.6960 -    and "e \<le> 1"
  1.6961 -  shows "x - e *\<^sub>R (x - c) \<in> interior s"
  1.6962 -proof -
  1.6963 -  obtain d where "d > 0" and d: "ball c d \<subseteq> s"
  1.6964 -    using assms(2) unfolding mem_interior by auto
  1.6965 -  show ?thesis
  1.6966 -    unfolding mem_interior
  1.6967 -    apply (rule_tac x="e*d" in exI)
  1.6968 -    apply rule
  1.6969 -    defer
  1.6970 -    unfolding subset_eq Ball_def mem_ball
  1.6971 -  proof (rule, rule)
  1.6972 -    fix y
  1.6973 -    assume as: "dist (x - e *\<^sub>R (x - c)) y < e * d"
  1.6974 -    have *: "y = (1 - (1 - e)) *\<^sub>R ((1 / e) *\<^sub>R y - ((1 - e) / e) *\<^sub>R x) + (1 - e) *\<^sub>R x"
  1.6975 -      using \<open>e > 0\<close> by (auto simp add: scaleR_left_diff_distrib scaleR_right_diff_distrib)
  1.6976 -    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)"
  1.6977 -      unfolding dist_norm
  1.6978 -      unfolding norm_scaleR[symmetric]
  1.6979 -      apply (rule arg_cong[where f=norm])
  1.6980 -      using \<open>e > 0\<close>
  1.6981 -      by (auto simp add: euclidean_eq_iff[where 'a='a] field_simps inner_simps)
  1.6982 -    also have "\<dots> = \<bar>1/e\<bar> * norm (x - e *\<^sub>R (x - c) - y)"
  1.6983 -      by (auto intro!:arg_cong[where f=norm] simp add: algebra_simps)
  1.6984 -    also have "\<dots> < d"
  1.6985 -      using as[unfolded dist_norm] and \<open>e > 0\<close>
  1.6986 -      by (auto simp add:pos_divide_less_eq[OF \<open>e > 0\<close>] mult.commute)
  1.6987 -    finally show "y \<in> s"
  1.6988 -      apply (subst *)
  1.6989 -      apply (rule assms(1)[unfolded convex_alt,rule_format])
  1.6990 -      apply (rule d[unfolded subset_eq,rule_format])
  1.6991 -      unfolding mem_ball
  1.6992 -      using assms(3-5)
  1.6993 -      apply auto
  1.6994 -      done
  1.6995 -  qed (insert \<open>e>0\<close> \<open>d>0\<close>, auto)
  1.6996 -qed
  1.6997 -
  1.6998 -lemma mem_interior_closure_convex_shrink:
  1.6999 -  fixes s :: "'a::euclidean_space set"
  1.7000 -  assumes "convex s"
  1.7001 -    and "c \<in> interior s"
  1.7002 -    and "x \<in> closure s"
  1.7003 -    and "0 < e"
  1.7004 -    and "e \<le> 1"
  1.7005 -  shows "x - e *\<^sub>R (x - c) \<in> interior s"
  1.7006 -proof -
  1.7007 -  obtain d where "d > 0" and d: "ball c d \<subseteq> s"
  1.7008 -    using assms(2) unfolding mem_interior by auto
  1.7009 -  have "\<exists>y\<in>s. norm (y - x) * (1 - e) < e * d"
  1.7010 -  proof (cases "x \<in> s")
  1.7011 -    case True
  1.7012 -    then show ?thesis
  1.7013 -      using \<open>e > 0\<close> \<open>d > 0\<close>
  1.7014 -      apply (rule_tac bexI[where x=x])
  1.7015 -      apply (auto)
  1.7016 -      done
  1.7017 -  next
  1.7018 -    case False
  1.7019 -    then have x: "x islimpt s"
  1.7020 -      using assms(3)[unfolded closure_def] by auto
  1.7021 -    show ?thesis
  1.7022 -    proof (cases "e = 1")
  1.7023 -      case True
  1.7024 -      obtain y where "y \<in> s" "y \<noteq> x" "dist y x < 1"
  1.7025 -        using x[unfolded islimpt_approachable,THEN spec[where x=1]] by auto
  1.7026 -      then show ?thesis
  1.7027 -        apply (rule_tac x=y in bexI)
  1.7028 -        unfolding True
  1.7029 -        using \<open>d > 0\<close>
  1.7030 -        apply auto
  1.7031 -        done
  1.7032 -    next
  1.7033 -      case False
  1.7034 -      then have "0 < e * d / (1 - e)" and *: "1 - e > 0"
  1.7035 -        using \<open>e \<le> 1\<close> \<open>e > 0\<close> \<open>d > 0\<close> by auto
  1.7036 -      then obtain y where "y \<in> s" "y \<noteq> x" "dist y x < e * d / (1 - e)"
  1.7037 -        using x[unfolded islimpt_approachable,THEN spec[where x="e*d / (1 - e)"]] by auto
  1.7038 -      then show ?thesis
  1.7039 -        apply (rule_tac x=y in bexI)
  1.7040 -        unfolding dist_norm
  1.7041 -        using pos_less_divide_eq[OF *]
  1.7042 -        apply auto
  1.7043 -        done
  1.7044 -    qed
  1.7045 -  qed
  1.7046 -  then obtain y where "y \<in> s" and y: "norm (y - x) * (1 - e) < e * d"
  1.7047 -    by auto
  1.7048 -  define z where "z = c + ((1 - e) / e) *\<^sub>R (x - y)"
  1.7049 -  have *: "x - e *\<^sub>R (x - c) = y - e *\<^sub>R (y - z)"
  1.7050 -    unfolding z_def using \<open>e > 0\<close>
  1.7051 -    by (auto simp add: scaleR_right_diff_distrib scaleR_right_distrib scaleR_left_diff_distrib)
  1.7052 -  have "z \<in> interior s"
  1.7053 -    apply (rule interior_mono[OF d,unfolded subset_eq,rule_format])
  1.7054 -    unfolding interior_open[OF open_ball] mem_ball z_def dist_norm using y and assms(4,5)
  1.7055 -    apply (auto simp add:field_simps norm_minus_commute)
  1.7056 -    done
  1.7057 -  then show ?thesis
  1.7058 -    unfolding *
  1.7059 -    apply -
  1.7060 -    apply (rule mem_interior_convex_shrink)
  1.7061 -    using assms(1,4-5) \<open>y\<in>s\<close>
  1.7062 -    apply auto
  1.7063 -    done
  1.7064 -qed
  1.7065 -
  1.7066 -lemma in_interior_closure_convex_segment:
  1.7067 -  fixes S :: "'a::euclidean_space set"
  1.7068 -  assumes "convex S" and a: "a \<in> interior S" and b: "b \<in> closure S"
  1.7069 -    shows "open_segment a b \<subseteq> interior S"
  1.7070 -proof (clarsimp simp: in_segment)
  1.7071 -  fix u::real
  1.7072 -  assume u: "0 < u" "u < 1"
  1.7073 -  have "(1 - u) *\<^sub>R a + u *\<^sub>R b = b - (1 - u) *\<^sub>R (b - a)"
  1.7074 -    by (simp add: algebra_simps)
  1.7075 -  also have "... \<in> interior S" using mem_interior_closure_convex_shrink [OF assms] u
  1.7076 -    by simp
  1.7077 -  finally show "(1 - u) *\<^sub>R a + u *\<^sub>R b \<in> interior S" .
  1.7078 -qed
  1.7079 -
  1.7080 -
  1.7081 -subsection \<open>Some obvious but surprisingly hard simplex lemmas\<close>
  1.7082 -
  1.7083 -lemma simplex:
  1.7084 -  assumes "finite s"
  1.7085 -    and "0 \<notin> s"
  1.7086 -  shows "convex hull (insert 0 s) =
  1.7087 -    {y. (\<exists>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s \<le> 1 \<and> setsum (\<lambda>x. u x *\<^sub>R x) s = y)}"
  1.7088 -  unfolding convex_hull_finite[OF finite.insertI[OF assms(1)]]
  1.7089 -  apply (rule set_eqI, rule)
  1.7090 -  unfolding mem_Collect_eq
  1.7091 -  apply (erule_tac[!] exE)
  1.7092 -  apply (erule_tac[!] conjE)+
  1.7093 -  unfolding setsum_clauses(2)[OF \<open>finite s\<close>]
  1.7094 -  apply (rule_tac x=u in exI)
  1.7095 -  defer
  1.7096 -  apply (rule_tac x="\<lambda>x. if x = 0 then 1 - setsum u s else u x" in exI)
  1.7097 -  using assms(2)
  1.7098 -  unfolding if_smult and setsum_delta_notmem[OF assms(2)]
  1.7099 -  apply auto
  1.7100 -  done
  1.7101 -
  1.7102 -lemma substd_simplex:
  1.7103 -  assumes d: "d \<subseteq> Basis"
  1.7104 -  shows "convex hull (insert 0 d) =
  1.7105 -    {x. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i) \<and> (\<Sum>i\<in>d. x\<bullet>i) \<le> 1 \<and> (\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0)}"
  1.7106 -  (is "convex hull (insert 0 ?p) = ?s")
  1.7107 -proof -
  1.7108 -  let ?D = d
  1.7109 -  have "0 \<notin> ?p"
  1.7110 -    using assms by (auto simp: image_def)
  1.7111 -  from d have "finite d"
  1.7112 -    by (blast intro: finite_subset finite_Basis)
  1.7113 -  show ?thesis
  1.7114 -    unfolding simplex[OF \<open>finite d\<close> \<open>0 \<notin> ?p\<close>]
  1.7115 -    apply (rule set_eqI)
  1.7116 -    unfolding mem_Collect_eq
  1.7117 -    apply rule
  1.7118 -    apply (elim exE conjE)
  1.7119 -    apply (erule_tac[2] conjE)+
  1.7120 -  proof -
  1.7121 -    fix x :: "'a::euclidean_space"
  1.7122 -    fix u
  1.7123 -    assume as: "\<forall>x\<in>?D. 0 \<le> u x" "setsum u ?D \<le> 1" "(\<Sum>x\<in>?D. u x *\<^sub>R x) = x"
  1.7124 -    have *: "\<forall>i\<in>Basis. i:d \<longrightarrow> u i = x\<bullet>i"
  1.7125 -      and "(\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0)"
  1.7126 -      using as(3)
  1.7127 -      unfolding substdbasis_expansion_unique[OF assms]
  1.7128 -      by auto
  1.7129 -    then have **: "setsum u ?D = setsum (op \<bullet> x) ?D"
  1.7130 -      apply -
  1.7131 -      apply (rule setsum.cong)
  1.7132 -      using assms
  1.7133 -      apply auto
  1.7134 -      done
  1.7135 -    have "(\<forall>i\<in>Basis. 0 \<le> x\<bullet>i) \<and> setsum (op \<bullet> x) ?D \<le> 1"
  1.7136 -    proof (rule,rule)
  1.7137 -      fix i :: 'a
  1.7138 -      assume i: "i \<in> Basis"
  1.7139 -      have "i \<in> d \<Longrightarrow> 0 \<le> x\<bullet>i"
  1.7140 -        unfolding *[rule_format,OF i,symmetric]
  1.7141 -         apply (rule_tac as(1)[rule_format])
  1.7142 -         apply auto
  1.7143 -         done
  1.7144 -      moreover have "i \<notin> d \<Longrightarrow> 0 \<le> x\<bullet>i"
  1.7145 -        using \<open>(\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0)\<close>[rule_format, OF i] by auto
  1.7146 -      ultimately show "0 \<le> x\<bullet>i" by auto
  1.7147 -    qed (insert as(2)[unfolded **], auto)
  1.7148 -    then show "(\<forall>i\<in>Basis. 0 \<le> x\<bullet>i) \<and> setsum (op \<bullet> x) ?D \<le> 1 \<and> (\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0)"
  1.7149 -      using \<open>(\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0)\<close> by auto
  1.7150 -  next
  1.7151 -    fix x :: "'a::euclidean_space"
  1.7152 -    assume as: "\<forall>i\<in>Basis. 0 \<le> x \<bullet> i" "setsum (op \<bullet> x) ?D \<le> 1" "(\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0)"
  1.7153 -    show "\<exists>u. (\<forall>x\<in>?D. 0 \<le> u x) \<and> setsum u ?D \<le> 1 \<and> (\<Sum>x\<in>?D. u x *\<^sub>R x) = x"
  1.7154 -      using as d
  1.7155 -      unfolding substdbasis_expansion_unique[OF assms]
  1.7156 -      apply (rule_tac x="inner x" in exI)
  1.7157 -      apply auto
  1.7158 -      done
  1.7159 -  qed
  1.7160 -qed
  1.7161 -
  1.7162 -lemma std_simplex:
  1.7163 -  "convex hull (insert 0 Basis) =
  1.7164 -    {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i) \<and> setsum (\<lambda>i. x\<bullet>i) Basis \<le> 1}"
  1.7165 -  using substd_simplex[of Basis] by auto
  1.7166 -
  1.7167 -lemma interior_std_simplex:
  1.7168 -  "interior (convex hull (insert 0 Basis)) =
  1.7169 -    {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 < x\<bullet>i) \<and> setsum (\<lambda>i. x\<bullet>i) Basis < 1}"
  1.7170 -  apply (rule set_eqI)
  1.7171 -  unfolding mem_interior std_simplex
  1.7172 -  unfolding subset_eq mem_Collect_eq Ball_def mem_ball
  1.7173 -  unfolding Ball_def[symmetric]
  1.7174 -  apply rule
  1.7175 -  apply (elim exE conjE)
  1.7176 -  defer
  1.7177 -  apply (erule conjE)
  1.7178 -proof -
  1.7179 -  fix x :: 'a
  1.7180 -  fix e
  1.7181 -  assume "e > 0" and as: "\<forall>xa. dist x xa < e \<longrightarrow> (\<forall>x\<in>Basis. 0 \<le> xa \<bullet> x) \<and> setsum (op \<bullet> xa) Basis \<le> 1"
  1.7182 -  show "(\<forall>xa\<in>Basis. 0 < x \<bullet> xa) \<and> setsum (op \<bullet> x) Basis < 1"
  1.7183 -    apply safe
  1.7184 -  proof -
  1.7185 -    fix i :: 'a
  1.7186 -    assume i: "i \<in> Basis"
  1.7187 -    then show "0 < x \<bullet> i"
  1.7188 -      using as[THEN spec[where x="x - (e / 2) *\<^sub>R i"]] and \<open>e > 0\<close>
  1.7189 -      unfolding dist_norm
  1.7190 -      by (auto elim!: ballE[where x=i] simp: inner_simps)
  1.7191 -  next
  1.7192 -    have **: "dist x (x + (e / 2) *\<^sub>R (SOME i. i\<in>Basis)) < e" using \<open>e > 0\<close>
  1.7193 -      unfolding dist_norm
  1.7194 -      by (auto intro!: mult_strict_left_mono simp: SOME_Basis)
  1.7195 -    have "\<And>i. i \<in> Basis \<Longrightarrow> (x + (e / 2) *\<^sub>R (SOME i. i\<in>Basis)) \<bullet> i =
  1.7196 -      x\<bullet>i + (if i = (SOME i. i\<in>Basis) then e/2 else 0)"
  1.7197 -      by (auto simp: SOME_Basis inner_Basis inner_simps)
  1.7198 -    then have *: "setsum (op \<bullet> (x + (e / 2) *\<^sub>R (SOME i. i\<in>Basis))) Basis =
  1.7199 -      setsum (\<lambda>i. x\<bullet>i + (if (SOME i. i\<in>Basis) = i then e/2 else 0)) Basis"
  1.7200 -      apply (rule_tac setsum.cong)
  1.7201 -      apply auto
  1.7202 -      done
  1.7203 -    have "setsum (op \<bullet> x) Basis < setsum (op \<bullet> (x + (e / 2) *\<^sub>R (SOME i. i\<in>Basis))) Basis"
  1.7204 -      unfolding * setsum.distrib
  1.7205 -      using \<open>e > 0\<close> DIM_positive[where 'a='a]
  1.7206 -      apply (subst setsum.delta')
  1.7207 -      apply (auto simp: SOME_Basis)
  1.7208 -      done
  1.7209 -    also have "\<dots> \<le> 1"
  1.7210 -      using **
  1.7211 -      apply (drule_tac as[rule_format])
  1.7212 -      apply auto
  1.7213 -      done
  1.7214 -    finally show "setsum (op \<bullet> x) Basis < 1" by auto
  1.7215 -  qed
  1.7216 -next
  1.7217 -  fix x :: 'a
  1.7218 -  assume as: "\<forall>i\<in>Basis. 0 < x \<bullet> i" "setsum (op \<bullet> x) Basis < 1"
  1.7219 -  obtain a :: 'b where "a \<in> UNIV" using UNIV_witness ..
  1.7220 -  let ?d = "(1 - setsum (op \<bullet> x) Basis) / real (DIM('a))"
  1.7221 -  have "Min ((op \<bullet> x) ` Basis) > 0"
  1.7222 -    apply (rule Min_grI)
  1.7223 -    using as(1)
  1.7224 -    apply auto
  1.7225 -    done
  1.7226 -  moreover have "?d > 0"
  1.7227 -    using as(2) by (auto simp: Suc_le_eq DIM_positive)
  1.7228 -  ultimately show "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> (\<forall>i\<in>Basis. 0 \<le> y \<bullet> i) \<and> setsum (op \<bullet> y) Basis \<le> 1"
  1.7229 -    apply (rule_tac x="min (Min ((op \<bullet> x) ` Basis)) D" for D in exI)
  1.7230 -    apply rule
  1.7231 -    defer
  1.7232 -    apply (rule, rule)
  1.7233 -  proof -
  1.7234 -    fix y
  1.7235 -    assume y: "dist x y < min (Min (op \<bullet> x ` Basis)) ?d"
  1.7236 -    have "setsum (op \<bullet> y) Basis \<le> setsum (\<lambda>i. x\<bullet>i + ?d) Basis"
  1.7237 -    proof (rule setsum_mono)
  1.7238 -      fix i :: 'a
  1.7239 -      assume i: "i \<in> Basis"
  1.7240 -      then have "\<bar>y\<bullet>i - x\<bullet>i\<bar> < ?d"
  1.7241 -        apply -
  1.7242 -        apply (rule le_less_trans)
  1.7243 -        using Basis_le_norm[OF i, of "y - x"]
  1.7244 -        using y[unfolded min_less_iff_conj dist_norm, THEN conjunct2]
  1.7245 -        apply (auto simp add: norm_minus_commute inner_diff_left)
  1.7246 -        done
  1.7247 -      then show "y \<bullet> i \<le> x \<bullet> i + ?d" by auto
  1.7248 -    qed
  1.7249 -    also have "\<dots> \<le> 1"
  1.7250 -      unfolding setsum.distrib set