split off Convex.thy: material that does not require Topology_Euclidean_Space
authorimmler
Mon Jan 07 14:06:54 2019 +0100 (4 months ago)
changeset 696193f7d8e05e0f2
parent 69618 2be1baf40351
child 69620 19d8a59481db
split off Convex.thy: material that does not require Topology_Euclidean_Space
src/HOL/Analysis/Convex.thy
src/HOL/Analysis/Convex_Euclidean_Space.thy
src/HOL/Analysis/Further_Topology.thy
src/HOL/Analysis/Linear_Algebra.thy
src/HOL/Analysis/Topology_Euclidean_Space.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Convex.thy	Mon Jan 07 14:06:54 2019 +0100
     1.3 @@ -0,0 +1,4195 @@
     1.4 +(* Title:      HOL/Analysis/Convex_Euclidean_Space.thy
     1.5 +   Author:     L C Paulson, University of Cambridge
     1.6 +   Author:     Robert Himmelmann, TU Muenchen
     1.7 +   Author:     Bogdan Grechuk, University of Edinburgh
     1.8 +   Author:     Armin Heller, TU Muenchen
     1.9 +   Author:     Johannes Hoelzl, TU Muenchen
    1.10 +*)
    1.11 +
    1.12 +section \<open>Convex Sets and Functions\<close>
    1.13 +
    1.14 +theory Convex
    1.15 +imports
    1.16 +  Linear_Algebra
    1.17 +  "HOL-Library.Set_Algebras"
    1.18 +begin
    1.19 +
    1.20 +lemma substdbasis_expansion_unique:
    1.21 +  assumes d: "d \<subseteq> Basis"
    1.22 +  shows "(\<Sum>i\<in>d. f i *\<^sub>R i) = (x::'a::euclidean_space) \<longleftrightarrow>
    1.23 +    (\<forall>i\<in>Basis. (i \<in> d \<longrightarrow> f i = x \<bullet> i) \<and> (i \<notin> d \<longrightarrow> x \<bullet> i = 0))"
    1.24 +proof -
    1.25 +  have *: "\<And>x a b P. x * (if P then a else b) = (if P then x * a else x * b)"
    1.26 +    by auto
    1.27 +  have **: "finite d"
    1.28 +    by (auto intro: finite_subset[OF assms])
    1.29 +  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.30 +    using d
    1.31 +    by (auto intro!: sum.cong simp: inner_Basis inner_sum_left)
    1.32 +  show ?thesis
    1.33 +    unfolding euclidean_eq_iff[where 'a='a] by (auto simp: sum.delta[OF **] ***)
    1.34 +qed
    1.35 +
    1.36 +lemma independent_substdbasis: "d \<subseteq> Basis \<Longrightarrow> independent d"
    1.37 +  by (rule independent_mono[OF independent_Basis])
    1.38 +
    1.39 +lemma sum_not_0: "sum f A \<noteq> 0 \<Longrightarrow> \<exists>a \<in> A. f a \<noteq> 0"
    1.40 +  by (rule ccontr) auto
    1.41 +
    1.42 +lemma subset_translation_eq [simp]:
    1.43 +    fixes a :: "'a::real_vector" shows "(+) a ` s \<subseteq> (+) a ` t \<longleftrightarrow> s \<subseteq> t"
    1.44 +  by auto
    1.45 +
    1.46 +lemma translate_inj_on:
    1.47 +  fixes A :: "'a::ab_group_add set"
    1.48 +  shows "inj_on (\<lambda>x. a + x) A"
    1.49 +  unfolding inj_on_def by auto
    1.50 +
    1.51 +lemma translation_assoc:
    1.52 +  fixes a b :: "'a::ab_group_add"
    1.53 +  shows "(\<lambda>x. b + x) ` ((\<lambda>x. a + x) ` S) = (\<lambda>x. (a + b) + x) ` S"
    1.54 +  by auto
    1.55 +
    1.56 +lemma translation_invert:
    1.57 +  fixes a :: "'a::ab_group_add"
    1.58 +  assumes "(\<lambda>x. a + x) ` A = (\<lambda>x. a + x) ` B"
    1.59 +  shows "A = B"
    1.60 +proof -
    1.61 +  have "(\<lambda>x. -a + x) ` ((\<lambda>x. a + x) ` A) = (\<lambda>x. - a + x) ` ((\<lambda>x. a + x) ` B)"
    1.62 +    using assms by auto
    1.63 +  then show ?thesis
    1.64 +    using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto
    1.65 +qed
    1.66 +
    1.67 +lemma translation_galois:
    1.68 +  fixes a :: "'a::ab_group_add"
    1.69 +  shows "T = ((\<lambda>x. a + x) ` S) \<longleftrightarrow> S = ((\<lambda>x. (- a) + x) ` T)"
    1.70 +  using translation_assoc[of "-a" a S]
    1.71 +  apply auto
    1.72 +  using translation_assoc[of a "-a" T]
    1.73 +  apply auto
    1.74 +  done
    1.75 +
    1.76 +lemma translation_inverse_subset:
    1.77 +  assumes "((\<lambda>x. - a + x) ` V) \<le> (S :: 'n::ab_group_add set)"
    1.78 +  shows "V \<le> ((\<lambda>x. a + x) ` S)"
    1.79 +proof -
    1.80 +  {
    1.81 +    fix x
    1.82 +    assume "x \<in> V"
    1.83 +    then have "x-a \<in> S" using assms by auto
    1.84 +    then have "x \<in> {a + v |v. v \<in> S}"
    1.85 +      apply auto
    1.86 +      apply (rule exI[of _ "x-a"], simp)
    1.87 +      done
    1.88 +    then have "x \<in> ((\<lambda>x. a+x) ` S)" by auto
    1.89 +  }
    1.90 +  then show ?thesis by auto
    1.91 +qed
    1.92 +
    1.93 +subsection \<open>Convexity\<close>
    1.94 +
    1.95 +definition%important convex :: "'a::real_vector set \<Rightarrow> bool"
    1.96 +  where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    1.97 +
    1.98 +lemma convexI:
    1.99 +  assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.100 +  shows "convex s"
   1.101 +  using assms unfolding convex_def by fast
   1.102 +
   1.103 +lemma convexD:
   1.104 +  assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
   1.105 +  shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.106 +  using assms unfolding convex_def by fast
   1.107 +
   1.108 +lemma convex_alt: "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
   1.109 +  (is "_ \<longleftrightarrow> ?alt")
   1.110 +proof
   1.111 +  show "convex s" if alt: ?alt
   1.112 +  proof -
   1.113 +    {
   1.114 +      fix x y and u v :: real
   1.115 +      assume mem: "x \<in> s" "y \<in> s"
   1.116 +      assume "0 \<le> u" "0 \<le> v"
   1.117 +      moreover
   1.118 +      assume "u + v = 1"
   1.119 +      then have "u = 1 - v" by auto
   1.120 +      ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.121 +        using alt [rule_format, OF mem] by auto
   1.122 +    }
   1.123 +    then show ?thesis
   1.124 +      unfolding convex_def by auto
   1.125 +  qed
   1.126 +  show ?alt if "convex s"
   1.127 +    using that by (auto simp: convex_def)
   1.128 +qed
   1.129 +
   1.130 +lemma convexD_alt:
   1.131 +  assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
   1.132 +  shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
   1.133 +  using assms unfolding convex_alt by auto
   1.134 +
   1.135 +lemma mem_convex_alt:
   1.136 +  assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
   1.137 +  shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
   1.138 +  apply (rule convexD)
   1.139 +  using assms
   1.140 +       apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
   1.141 +  done
   1.142 +
   1.143 +lemma convex_empty[intro,simp]: "convex {}"
   1.144 +  unfolding convex_def by simp
   1.145 +
   1.146 +lemma convex_singleton[intro,simp]: "convex {a}"
   1.147 +  unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
   1.148 +
   1.149 +lemma convex_UNIV[intro,simp]: "convex UNIV"
   1.150 +  unfolding convex_def by auto
   1.151 +
   1.152 +lemma convex_Inter: "(\<And>s. s\<in>f \<Longrightarrow> convex s) \<Longrightarrow> convex(\<Inter>f)"
   1.153 +  unfolding convex_def by auto
   1.154 +
   1.155 +lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
   1.156 +  unfolding convex_def by auto
   1.157 +
   1.158 +lemma convex_INT: "(\<And>i. i \<in> A \<Longrightarrow> convex (B i)) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
   1.159 +  unfolding convex_def by auto
   1.160 +
   1.161 +lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
   1.162 +  unfolding convex_def by auto
   1.163 +
   1.164 +lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
   1.165 +  unfolding convex_def
   1.166 +  by (auto simp: inner_add intro!: convex_bound_le)
   1.167 +
   1.168 +lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
   1.169 +proof -
   1.170 +  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
   1.171 +    by auto
   1.172 +  show ?thesis
   1.173 +    unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
   1.174 +qed
   1.175 +
   1.176 +lemma convex_halfspace_abs_le: "convex {x. \<bar>inner a x\<bar> \<le> b}"
   1.177 +proof -
   1.178 +  have *: "{x. \<bar>inner a x\<bar> \<le> b} = {x. inner a x \<le> b} \<inter> {x. -b \<le> inner a x}"
   1.179 +    by auto
   1.180 +  show ?thesis
   1.181 +    unfolding * by (simp add: convex_Int convex_halfspace_ge convex_halfspace_le)
   1.182 +qed
   1.183 +
   1.184 +lemma convex_hyperplane: "convex {x. inner a x = b}"
   1.185 +proof -
   1.186 +  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
   1.187 +    by auto
   1.188 +  show ?thesis using convex_halfspace_le convex_halfspace_ge
   1.189 +    by (auto intro!: convex_Int simp: *)
   1.190 +qed
   1.191 +
   1.192 +lemma convex_halfspace_lt: "convex {x. inner a x < b}"
   1.193 +  unfolding convex_def
   1.194 +  by (auto simp: convex_bound_lt inner_add)
   1.195 +
   1.196 +lemma convex_halfspace_gt: "convex {x. inner a x > b}"
   1.197 +  using convex_halfspace_lt[of "-a" "-b"] by auto
   1.198 +
   1.199 +lemma convex_halfspace_Re_ge: "convex {x. Re x \<ge> b}"
   1.200 +  using convex_halfspace_ge[of b "1::complex"] by simp
   1.201 +
   1.202 +lemma convex_halfspace_Re_le: "convex {x. Re x \<le> b}"
   1.203 +  using convex_halfspace_le[of "1::complex" b] by simp
   1.204 +
   1.205 +lemma convex_halfspace_Im_ge: "convex {x. Im x \<ge> b}"
   1.206 +  using convex_halfspace_ge[of b \<i>] by simp
   1.207 +
   1.208 +lemma convex_halfspace_Im_le: "convex {x. Im x \<le> b}"
   1.209 +  using convex_halfspace_le[of \<i> b] by simp
   1.210 +
   1.211 +lemma convex_halfspace_Re_gt: "convex {x. Re x > b}"
   1.212 +  using convex_halfspace_gt[of b "1::complex"] by simp
   1.213 +
   1.214 +lemma convex_halfspace_Re_lt: "convex {x. Re x < b}"
   1.215 +  using convex_halfspace_lt[of "1::complex" b] by simp
   1.216 +
   1.217 +lemma convex_halfspace_Im_gt: "convex {x. Im x > b}"
   1.218 +  using convex_halfspace_gt[of b \<i>] by simp
   1.219 +
   1.220 +lemma convex_halfspace_Im_lt: "convex {x. Im x < b}"
   1.221 +  using convex_halfspace_lt[of \<i> b] by simp
   1.222 +
   1.223 +lemma convex_real_interval [iff]:
   1.224 +  fixes a b :: "real"
   1.225 +  shows "convex {a..}" and "convex {..b}"
   1.226 +    and "convex {a<..}" and "convex {..<b}"
   1.227 +    and "convex {a..b}" and "convex {a<..b}"
   1.228 +    and "convex {a..<b}" and "convex {a<..<b}"
   1.229 +proof -
   1.230 +  have "{a..} = {x. a \<le> inner 1 x}"
   1.231 +    by auto
   1.232 +  then show 1: "convex {a..}"
   1.233 +    by (simp only: convex_halfspace_ge)
   1.234 +  have "{..b} = {x. inner 1 x \<le> b}"
   1.235 +    by auto
   1.236 +  then show 2: "convex {..b}"
   1.237 +    by (simp only: convex_halfspace_le)
   1.238 +  have "{a<..} = {x. a < inner 1 x}"
   1.239 +    by auto
   1.240 +  then show 3: "convex {a<..}"
   1.241 +    by (simp only: convex_halfspace_gt)
   1.242 +  have "{..<b} = {x. inner 1 x < b}"
   1.243 +    by auto
   1.244 +  then show 4: "convex {..<b}"
   1.245 +    by (simp only: convex_halfspace_lt)
   1.246 +  have "{a..b} = {a..} \<inter> {..b}"
   1.247 +    by auto
   1.248 +  then show "convex {a..b}"
   1.249 +    by (simp only: convex_Int 1 2)
   1.250 +  have "{a<..b} = {a<..} \<inter> {..b}"
   1.251 +    by auto
   1.252 +  then show "convex {a<..b}"
   1.253 +    by (simp only: convex_Int 3 2)
   1.254 +  have "{a..<b} = {a..} \<inter> {..<b}"
   1.255 +    by auto
   1.256 +  then show "convex {a..<b}"
   1.257 +    by (simp only: convex_Int 1 4)
   1.258 +  have "{a<..<b} = {a<..} \<inter> {..<b}"
   1.259 +    by auto
   1.260 +  then show "convex {a<..<b}"
   1.261 +    by (simp only: convex_Int 3 4)
   1.262 +qed
   1.263 +
   1.264 +lemma convex_Reals: "convex \<real>"
   1.265 +  by (simp add: convex_def scaleR_conv_of_real)
   1.266 +
   1.267 +
   1.268 +subsection%unimportant \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
   1.269 +
   1.270 +lemma convex_sum:
   1.271 +  fixes C :: "'a::real_vector set"
   1.272 +  assumes "finite s"
   1.273 +    and "convex C"
   1.274 +    and "(\<Sum> i \<in> s. a i) = 1"
   1.275 +  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   1.276 +    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.277 +  shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   1.278 +  using assms(1,3,4,5)
   1.279 +proof (induct arbitrary: a set: finite)
   1.280 +  case empty
   1.281 +  then show ?case by simp
   1.282 +next
   1.283 +  case (insert i s) note IH = this(3)
   1.284 +  have "a i + sum a s = 1"
   1.285 +    and "0 \<le> a i"
   1.286 +    and "\<forall>j\<in>s. 0 \<le> a j"
   1.287 +    and "y i \<in> C"
   1.288 +    and "\<forall>j\<in>s. y j \<in> C"
   1.289 +    using insert.hyps(1,2) insert.prems by simp_all
   1.290 +  then have "0 \<le> sum a s"
   1.291 +    by (simp add: sum_nonneg)
   1.292 +  have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
   1.293 +  proof (cases "sum a s = 0")
   1.294 +    case True
   1.295 +    with \<open>a i + sum a s = 1\<close> have "a i = 1"
   1.296 +      by simp
   1.297 +    from sum_nonneg_0 [OF \<open>finite s\<close> _ True] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
   1.298 +      by simp
   1.299 +    show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
   1.300 +      by simp
   1.301 +  next
   1.302 +    case False
   1.303 +    with \<open>0 \<le> sum a s\<close> have "0 < sum a s"
   1.304 +      by simp
   1.305 +    then have "(\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   1.306 +      using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
   1.307 +      by (simp add: IH sum_divide_distrib [symmetric])
   1.308 +    from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
   1.309 +      and \<open>0 \<le> sum a s\<close> and \<open>a i + sum a s = 1\<close>
   1.310 +    have "a i *\<^sub>R y i + sum a s *\<^sub>R (\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   1.311 +      by (rule convexD)
   1.312 +    then show ?thesis
   1.313 +      by (simp add: scaleR_sum_right False)
   1.314 +  qed
   1.315 +  then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
   1.316 +    by simp
   1.317 +qed
   1.318 +
   1.319 +lemma convex:
   1.320 +  "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (sum u {1..k} = 1)
   1.321 +      \<longrightarrow> sum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   1.322 +proof safe
   1.323 +  fix k :: nat
   1.324 +  fix u :: "nat \<Rightarrow> real"
   1.325 +  fix x
   1.326 +  assume "convex s"
   1.327 +    "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   1.328 +    "sum u {1..k} = 1"
   1.329 +  with convex_sum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
   1.330 +    by auto
   1.331 +next
   1.332 +  assume *: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> sum u {1..k} = 1
   1.333 +    \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   1.334 +  {
   1.335 +    fix \<mu> :: real
   1.336 +    fix x y :: 'a
   1.337 +    assume xy: "x \<in> s" "y \<in> s"
   1.338 +    assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.339 +    let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   1.340 +    let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   1.341 +    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
   1.342 +      by auto
   1.343 +    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
   1.344 +      by simp
   1.345 +    then have "sum ?u {1 .. 2} = 1"
   1.346 +      using sum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   1.347 +      by auto
   1.348 +    with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   1.349 +      using mu xy by auto
   1.350 +    have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   1.351 +      using sum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   1.352 +    from sum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   1.353 +    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.354 +      by auto
   1.355 +    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
   1.356 +      using s by (auto simp: add.commute)
   1.357 +  }
   1.358 +  then show "convex s"
   1.359 +    unfolding convex_alt by auto
   1.360 +qed
   1.361 +
   1.362 +
   1.363 +lemma convex_explicit:
   1.364 +  fixes s :: "'a::real_vector set"
   1.365 +  shows "convex s \<longleftrightarrow>
   1.366 +    (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> sum u t = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   1.367 +proof safe
   1.368 +  fix t
   1.369 +  fix u :: "'a \<Rightarrow> real"
   1.370 +  assume "convex s"
   1.371 +    and "finite t"
   1.372 +    and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   1.373 +  then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.374 +    using convex_sum[of t s u "\<lambda> x. x"] by auto
   1.375 +next
   1.376 +  assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
   1.377 +    sum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.378 +  show "convex s"
   1.379 +    unfolding convex_alt
   1.380 +  proof safe
   1.381 +    fix x y
   1.382 +    fix \<mu> :: real
   1.383 +    assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.384 +    show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.385 +    proof (cases "x = y")
   1.386 +      case False
   1.387 +      then show ?thesis
   1.388 +        using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
   1.389 +        by auto
   1.390 +    next
   1.391 +      case True
   1.392 +      then show ?thesis
   1.393 +        using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
   1.394 +        by (auto simp: field_simps real_vector.scale_left_diff_distrib)
   1.395 +    qed
   1.396 +  qed
   1.397 +qed
   1.398 +
   1.399 +lemma convex_finite:
   1.400 +  assumes "finite s"
   1.401 +  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   1.402 +  unfolding convex_explicit
   1.403 +  apply safe
   1.404 +  subgoal for u by (erule allE [where x=s], erule allE [where x=u]) auto
   1.405 +  subgoal for t u
   1.406 +  proof -
   1.407 +    have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   1.408 +      by simp
   1.409 +    assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   1.410 +    assume *: "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   1.411 +    assume "t \<subseteq> s"
   1.412 +    then have "s \<inter> t = t" by auto
   1.413 +    with sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] * show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.414 +      by (auto simp: assms sum.If_cases if_distrib if_distrib_arg)
   1.415 +  qed
   1.416 +  done
   1.417 +
   1.418 +
   1.419 +subsection \<open>Functions that are convex on a set\<close>
   1.420 +
   1.421 +definition%important convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   1.422 +  where "convex_on s f \<longleftrightarrow>
   1.423 +    (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   1.424 +
   1.425 +lemma convex_onI [intro?]:
   1.426 +  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   1.427 +    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   1.428 +  shows "convex_on A f"
   1.429 +  unfolding convex_on_def
   1.430 +proof clarify
   1.431 +  fix x y
   1.432 +  fix u v :: real
   1.433 +  assume A: "x \<in> A" "y \<in> A" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   1.434 +  from A(5) have [simp]: "v = 1 - u"
   1.435 +    by (simp add: algebra_simps)
   1.436 +  from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   1.437 +    using assms[of u y x]
   1.438 +    by (cases "u = 0 \<or> u = 1") (auto simp: algebra_simps)
   1.439 +qed
   1.440 +
   1.441 +lemma convex_on_linorderI [intro?]:
   1.442 +  fixes A :: "('a::{linorder,real_vector}) set"
   1.443 +  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x < y \<Longrightarrow>
   1.444 +    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   1.445 +  shows "convex_on A f"
   1.446 +proof
   1.447 +  fix x y
   1.448 +  fix t :: real
   1.449 +  assume A: "x \<in> A" "y \<in> A" "t > 0" "t < 1"
   1.450 +  with assms [of t x y] assms [of "1 - t" y x]
   1.451 +  show "f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   1.452 +    by (cases x y rule: linorder_cases) (auto simp: algebra_simps)
   1.453 +qed
   1.454 +
   1.455 +lemma convex_onD:
   1.456 +  assumes "convex_on A f"
   1.457 +  shows "\<And>t x y. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   1.458 +    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   1.459 +  using assms by (auto simp: convex_on_def)
   1.460 +
   1.461 +lemma convex_onD_Icc:
   1.462 +  assumes "convex_on {x..y} f" "x \<le> (y :: _ :: {real_vector,preorder})"
   1.463 +  shows "\<And>t. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow>
   1.464 +    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   1.465 +  using assms(2) by (intro convex_onD [OF assms(1)]) simp_all
   1.466 +
   1.467 +lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   1.468 +  unfolding convex_on_def by auto
   1.469 +
   1.470 +lemma convex_on_add [intro]:
   1.471 +  assumes "convex_on s f"
   1.472 +    and "convex_on s g"
   1.473 +  shows "convex_on s (\<lambda>x. f x + g x)"
   1.474 +proof -
   1.475 +  {
   1.476 +    fix x y
   1.477 +    assume "x \<in> s" "y \<in> s"
   1.478 +    moreover
   1.479 +    fix u v :: real
   1.480 +    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.481 +    ultimately
   1.482 +    have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   1.483 +      using assms unfolding convex_on_def by (auto simp: add_mono)
   1.484 +    then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   1.485 +      by (simp add: field_simps)
   1.486 +  }
   1.487 +  then show ?thesis
   1.488 +    unfolding convex_on_def by auto
   1.489 +qed
   1.490 +
   1.491 +lemma convex_on_cmul [intro]:
   1.492 +  fixes c :: real
   1.493 +  assumes "0 \<le> c"
   1.494 +    and "convex_on s f"
   1.495 +  shows "convex_on s (\<lambda>x. c * f x)"
   1.496 +proof -
   1.497 +  have *: "u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   1.498 +    for u c fx v fy :: real
   1.499 +    by (simp add: field_simps)
   1.500 +  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   1.501 +    unfolding convex_on_def and * by auto
   1.502 +qed
   1.503 +
   1.504 +lemma convex_lower:
   1.505 +  assumes "convex_on s f"
   1.506 +    and "x \<in> s"
   1.507 +    and "y \<in> s"
   1.508 +    and "0 \<le> u"
   1.509 +    and "0 \<le> v"
   1.510 +    and "u + v = 1"
   1.511 +  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   1.512 +proof -
   1.513 +  let ?m = "max (f x) (f y)"
   1.514 +  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   1.515 +    using assms(4,5) by (auto simp: mult_left_mono add_mono)
   1.516 +  also have "\<dots> = max (f x) (f y)"
   1.517 +    using assms(6) by (simp add: distrib_right [symmetric])
   1.518 +  finally show ?thesis
   1.519 +    using assms unfolding convex_on_def by fastforce
   1.520 +qed
   1.521 +
   1.522 +lemma convex_on_dist [intro]:
   1.523 +  fixes s :: "'a::real_normed_vector set"
   1.524 +  shows "convex_on s (\<lambda>x. dist a x)"
   1.525 +proof (auto simp: convex_on_def dist_norm)
   1.526 +  fix x y
   1.527 +  assume "x \<in> s" "y \<in> s"
   1.528 +  fix u v :: real
   1.529 +  assume "0 \<le> u"
   1.530 +  assume "0 \<le> v"
   1.531 +  assume "u + v = 1"
   1.532 +  have "a = u *\<^sub>R a + v *\<^sub>R a"
   1.533 +    unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
   1.534 +  then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   1.535 +    by (auto simp: algebra_simps)
   1.536 +  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   1.537 +    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   1.538 +    using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
   1.539 +qed
   1.540 +
   1.541 +
   1.542 +subsection%unimportant \<open>Arithmetic operations on sets preserve convexity\<close>
   1.543 +
   1.544 +lemma convex_linear_image:
   1.545 +  assumes "linear f"
   1.546 +    and "convex s"
   1.547 +  shows "convex (f ` s)"
   1.548 +proof -
   1.549 +  interpret f: linear f by fact
   1.550 +  from \<open>convex s\<close> show "convex (f ` s)"
   1.551 +    by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
   1.552 +qed
   1.553 +
   1.554 +lemma convex_linear_vimage:
   1.555 +  assumes "linear f"
   1.556 +    and "convex s"
   1.557 +  shows "convex (f -` s)"
   1.558 +proof -
   1.559 +  interpret f: linear f by fact
   1.560 +  from \<open>convex s\<close> show "convex (f -` s)"
   1.561 +    by (simp add: convex_def f.add f.scaleR)
   1.562 +qed
   1.563 +
   1.564 +lemma convex_scaling:
   1.565 +  assumes "convex s"
   1.566 +  shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   1.567 +proof -
   1.568 +  have "linear (\<lambda>x. c *\<^sub>R x)"
   1.569 +    by (simp add: linearI scaleR_add_right)
   1.570 +  then show ?thesis
   1.571 +    using \<open>convex s\<close> by (rule convex_linear_image)
   1.572 +qed
   1.573 +
   1.574 +lemma convex_scaled:
   1.575 +  assumes "convex S"
   1.576 +  shows "convex ((\<lambda>x. x *\<^sub>R c) ` S)"
   1.577 +proof -
   1.578 +  have "linear (\<lambda>x. x *\<^sub>R c)"
   1.579 +    by (simp add: linearI scaleR_add_left)
   1.580 +  then show ?thesis
   1.581 +    using \<open>convex S\<close> by (rule convex_linear_image)
   1.582 +qed
   1.583 +
   1.584 +lemma convex_negations:
   1.585 +  assumes "convex S"
   1.586 +  shows "convex ((\<lambda>x. - x) ` S)"
   1.587 +proof -
   1.588 +  have "linear (\<lambda>x. - x)"
   1.589 +    by (simp add: linearI)
   1.590 +  then show ?thesis
   1.591 +    using \<open>convex S\<close> by (rule convex_linear_image)
   1.592 +qed
   1.593 +
   1.594 +lemma convex_sums:
   1.595 +  assumes "convex S"
   1.596 +    and "convex T"
   1.597 +  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   1.598 +proof -
   1.599 +  have "linear (\<lambda>(x, y). x + y)"
   1.600 +    by (auto intro: linearI simp: scaleR_add_right)
   1.601 +  with assms have "convex ((\<lambda>(x, y). x + y) ` (S \<times> T))"
   1.602 +    by (intro convex_linear_image convex_Times)
   1.603 +  also have "((\<lambda>(x, y). x + y) ` (S \<times> T)) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   1.604 +    by auto
   1.605 +  finally show ?thesis .
   1.606 +qed
   1.607 +
   1.608 +lemma convex_differences:
   1.609 +  assumes "convex S" "convex T"
   1.610 +  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
   1.611 +proof -
   1.612 +  have "{x - y| x y. x \<in> S \<and> y \<in> T} = {x + y |x y. x \<in> S \<and> y \<in> uminus ` T}"
   1.613 +    by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
   1.614 +  then show ?thesis
   1.615 +    using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   1.616 +qed
   1.617 +
   1.618 +lemma convex_translation:
   1.619 +  assumes "convex S"
   1.620 +  shows "convex ((\<lambda>x. a + x) ` S)"
   1.621 +proof -
   1.622 +  have "(\<Union> x\<in> {a}. \<Union>y \<in> S. {x + y}) = (\<lambda>x. a + x) ` S"
   1.623 +    by auto
   1.624 +  then show ?thesis
   1.625 +    using convex_sums[OF convex_singleton[of a] assms] by auto
   1.626 +qed
   1.627 +
   1.628 +lemma convex_affinity:
   1.629 +  assumes "convex S"
   1.630 +  shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` S)"
   1.631 +proof -
   1.632 +  have "(\<lambda>x. a + c *\<^sub>R x) ` S = (+) a ` (*\<^sub>R) c ` S"
   1.633 +    by auto
   1.634 +  then show ?thesis
   1.635 +    using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   1.636 +qed
   1.637 +
   1.638 +lemma pos_is_convex: "convex {0 :: real <..}"
   1.639 +  unfolding convex_alt
   1.640 +proof safe
   1.641 +  fix y x \<mu> :: real
   1.642 +  assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.643 +  {
   1.644 +    assume "\<mu> = 0"
   1.645 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y"
   1.646 +      by simp
   1.647 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   1.648 +      using * by simp
   1.649 +  }
   1.650 +  moreover
   1.651 +  {
   1.652 +    assume "\<mu> = 1"
   1.653 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   1.654 +      using * by simp
   1.655 +  }
   1.656 +  moreover
   1.657 +  {
   1.658 +    assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   1.659 +    then have "\<mu> > 0" "(1 - \<mu>) > 0"
   1.660 +      using * by auto
   1.661 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   1.662 +      using * by (auto simp: add_pos_pos)
   1.663 +  }
   1.664 +  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
   1.665 +    by fastforce
   1.666 +qed
   1.667 +
   1.668 +lemma convex_on_sum:
   1.669 +  fixes a :: "'a \<Rightarrow> real"
   1.670 +    and y :: "'a \<Rightarrow> 'b::real_vector"
   1.671 +    and f :: "'b \<Rightarrow> real"
   1.672 +  assumes "finite s" "s \<noteq> {}"
   1.673 +    and "convex_on C f"
   1.674 +    and "convex C"
   1.675 +    and "(\<Sum> i \<in> s. a i) = 1"
   1.676 +    and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   1.677 +    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.678 +  shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   1.679 +  using assms
   1.680 +proof (induct s arbitrary: a rule: finite_ne_induct)
   1.681 +  case (singleton i)
   1.682 +  then have ai: "a i = 1"
   1.683 +    by auto
   1.684 +  then show ?case
   1.685 +    by auto
   1.686 +next
   1.687 +  case (insert i s)
   1.688 +  then have "convex_on C f"
   1.689 +    by simp
   1.690 +  from this[unfolded convex_on_def, rule_format]
   1.691 +  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
   1.692 +      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.693 +    by simp
   1.694 +  show ?case
   1.695 +  proof (cases "a i = 1")
   1.696 +    case True
   1.697 +    then have "(\<Sum> j \<in> s. a j) = 0"
   1.698 +      using insert by auto
   1.699 +    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   1.700 +      using insert by (fastforce simp: sum_nonneg_eq_0_iff)
   1.701 +    then show ?thesis
   1.702 +      using insert by auto
   1.703 +  next
   1.704 +    case False
   1.705 +    from insert have yai: "y i \<in> C" "a i \<ge> 0"
   1.706 +      by auto
   1.707 +    have fis: "finite (insert i s)"
   1.708 +      using insert by auto
   1.709 +    then have ai1: "a i \<le> 1"
   1.710 +      using sum_nonneg_leq_bound[of "insert i s" a] insert by simp
   1.711 +    then have "a i < 1"
   1.712 +      using False by auto
   1.713 +    then have i0: "1 - a i > 0"
   1.714 +      by auto
   1.715 +    let ?a = "\<lambda>j. a j / (1 - a i)"
   1.716 +    have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
   1.717 +      using i0 insert that by fastforce
   1.718 +    have "(\<Sum> j \<in> insert i s. a j) = 1"
   1.719 +      using insert by auto
   1.720 +    then have "(\<Sum> j \<in> s. a j) = 1 - a i"
   1.721 +      using sum.insert insert by fastforce
   1.722 +    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
   1.723 +      using i0 by auto
   1.724 +    then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
   1.725 +      unfolding sum_divide_distrib by simp
   1.726 +    have "convex C" using insert by auto
   1.727 +    then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.728 +      using insert convex_sum [OF \<open>finite s\<close> \<open>convex C\<close> a1 a_nonneg] by auto
   1.729 +    have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   1.730 +      using a_nonneg a1 insert by blast
   1.731 +    have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) = f ((\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.732 +      using sum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
   1.733 +      by (auto simp only: add.commute)
   1.734 +    also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.735 +      using i0 by auto
   1.736 +    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.737 +      using scaleR_right.sum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   1.738 +      by (auto simp: algebra_simps)
   1.739 +    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.740 +      by (auto simp: divide_inverse)
   1.741 +    also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   1.742 +      using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   1.743 +      by (auto simp: add.commute)
   1.744 +    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   1.745 +      using add_right_mono [OF mult_left_mono [of _ _ "1 - a i",
   1.746 +            OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"]
   1.747 +      by simp
   1.748 +    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   1.749 +      unfolding sum_distrib_left[of "1 - a i" "\<lambda> j. ?a j * f (y j)"]
   1.750 +      using i0 by auto
   1.751 +    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
   1.752 +      using i0 by auto
   1.753 +    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
   1.754 +      using insert by auto
   1.755 +    finally show ?thesis
   1.756 +      by simp
   1.757 +  qed
   1.758 +qed
   1.759 +
   1.760 +lemma convex_on_alt:
   1.761 +  fixes C :: "'a::real_vector set"
   1.762 +  assumes "convex C"
   1.763 +  shows "convex_on C f \<longleftrightarrow>
   1.764 +    (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
   1.765 +      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   1.766 +proof safe
   1.767 +  fix x y
   1.768 +  fix \<mu> :: real
   1.769 +  assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.770 +  from this[unfolded convex_on_def, rule_format]
   1.771 +  have "0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" for u v
   1.772 +    by auto
   1.773 +  from this [of "\<mu>" "1 - \<mu>", simplified] *
   1.774 +  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.775 +    by auto
   1.776 +next
   1.777 +  assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
   1.778 +    f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.779 +  {
   1.780 +    fix x y
   1.781 +    fix u v :: real
   1.782 +    assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   1.783 +    then have[simp]: "1 - u = v" by auto
   1.784 +    from *[rule_format, of x y u]
   1.785 +    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   1.786 +      using ** by auto
   1.787 +  }
   1.788 +  then show "convex_on C f"
   1.789 +    unfolding convex_on_def by auto
   1.790 +qed
   1.791 +
   1.792 +lemma convex_on_diff:
   1.793 +  fixes f :: "real \<Rightarrow> real"
   1.794 +  assumes f: "convex_on I f"
   1.795 +    and I: "x \<in> I" "y \<in> I"
   1.796 +    and t: "x < t" "t < y"
   1.797 +  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   1.798 +    and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.799 +proof -
   1.800 +  define a where "a \<equiv> (t - y) / (x - y)"
   1.801 +  with t have "0 \<le> a" "0 \<le> 1 - a"
   1.802 +    by (auto simp: field_simps)
   1.803 +  with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   1.804 +    by (auto simp: convex_on_def)
   1.805 +  have "a * x + (1 - a) * y = a * (x - y) + y"
   1.806 +    by (simp add: field_simps)
   1.807 +  also have "\<dots> = t"
   1.808 +    unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
   1.809 +  finally have "f t \<le> a * f x + (1 - a) * f y"
   1.810 +    using cvx by simp
   1.811 +  also have "\<dots> = a * (f x - f y) + f y"
   1.812 +    by (simp add: field_simps)
   1.813 +  finally have "f t - f y \<le> a * (f x - f y)"
   1.814 +    by simp
   1.815 +  with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   1.816 +    by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   1.817 +  with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.818 +    by (simp add: le_divide_eq divide_le_eq field_simps)
   1.819 +qed
   1.820 +
   1.821 +lemma pos_convex_function:
   1.822 +  fixes f :: "real \<Rightarrow> real"
   1.823 +  assumes "convex C"
   1.824 +    and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.825 +  shows "convex_on C f"
   1.826 +  unfolding convex_on_alt[OF assms(1)]
   1.827 +  using assms
   1.828 +proof safe
   1.829 +  fix x y \<mu> :: real
   1.830 +  let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.831 +  assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.832 +  then have "1 - \<mu> \<ge> 0" by auto
   1.833 +  then have xpos: "?x \<in> C"
   1.834 +    using * unfolding convex_alt by fastforce
   1.835 +  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
   1.836 +      \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   1.837 +    using add_mono [OF mult_left_mono [OF leq [OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
   1.838 +        mult_left_mono [OF leq [OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
   1.839 +    by auto
   1.840 +  then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   1.841 +    by (auto simp: field_simps)
   1.842 +  then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.843 +    using convex_on_alt by auto
   1.844 +qed
   1.845 +
   1.846 +lemma atMostAtLeast_subset_convex:
   1.847 +  fixes C :: "real set"
   1.848 +  assumes "convex C"
   1.849 +    and "x \<in> C" "y \<in> C" "x < y"
   1.850 +  shows "{x .. y} \<subseteq> C"
   1.851 +proof safe
   1.852 +  fix z assume z: "z \<in> {x .. y}"
   1.853 +  have less: "z \<in> C" if *: "x < z" "z < y"
   1.854 +  proof -
   1.855 +    let ?\<mu> = "(y - z) / (y - x)"
   1.856 +    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
   1.857 +      using assms * by (auto simp: field_simps)
   1.858 +    then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   1.859 +      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   1.860 +      by (simp add: algebra_simps)
   1.861 +    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   1.862 +      by (auto simp: field_simps)
   1.863 +    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   1.864 +      using assms by (simp only: add_divide_distrib) (auto simp: field_simps)
   1.865 +    also have "\<dots> = z"
   1.866 +      using assms by (auto simp: field_simps)
   1.867 +    finally show ?thesis
   1.868 +      using comb by auto
   1.869 +  qed
   1.870 +  show "z \<in> C"
   1.871 +    using z less assms by (auto simp: le_less)
   1.872 +qed
   1.873 +
   1.874 +lemma f''_imp_f':
   1.875 +  fixes f :: "real \<Rightarrow> real"
   1.876 +  assumes "convex C"
   1.877 +    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.878 +    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.879 +    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.880 +    and x: "x \<in> C"
   1.881 +    and y: "y \<in> C"
   1.882 +  shows "f' x * (y - x) \<le> f y - f x"
   1.883 +  using assms
   1.884 +proof -
   1.885 +  have less_imp: "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.886 +    if *: "x \<in> C" "y \<in> C" "y > x" for x y :: real
   1.887 +  proof -
   1.888 +    from * have ge: "y - x > 0" "y - x \<ge> 0"
   1.889 +      by auto
   1.890 +    from * have le: "x - y < 0" "x - y \<le> 0"
   1.891 +      by auto
   1.892 +    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   1.893 +      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
   1.894 +          THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   1.895 +      by auto
   1.896 +    then have "z1 \<in> C"
   1.897 +      using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
   1.898 +      by fastforce
   1.899 +    from z1 have z1': "f x - f y = (x - y) * f' z1"
   1.900 +      by (simp add: field_simps)
   1.901 +    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   1.902 +      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
   1.903 +          THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   1.904 +      by auto
   1.905 +    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   1.906 +      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
   1.907 +          THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   1.908 +      by auto
   1.909 +    have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   1.910 +      using * z1' by auto
   1.911 +    also have "\<dots> = (y - z1) * f'' z3"
   1.912 +      using z3 by auto
   1.913 +    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
   1.914 +      by simp
   1.915 +    have A': "y - z1 \<ge> 0"
   1.916 +      using z1 by auto
   1.917 +    have "z3 \<in> C"
   1.918 +      using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
   1.919 +      by fastforce
   1.920 +    then have B': "f'' z3 \<ge> 0"
   1.921 +      using assms by auto
   1.922 +    from A' B' have "(y - z1) * f'' z3 \<ge> 0"
   1.923 +      by auto
   1.924 +    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
   1.925 +      by auto
   1.926 +    from mult_right_mono_neg[OF this le(2)]
   1.927 +    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   1.928 +      by (simp add: algebra_simps)
   1.929 +    then have "f' y * (x - y) - (f x - f y) \<le> 0"
   1.930 +      using le by auto
   1.931 +    then have res: "f' y * (x - y) \<le> f x - f y"
   1.932 +      by auto
   1.933 +    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   1.934 +      using * z1 by auto
   1.935 +    also have "\<dots> = (z1 - x) * f'' z2"
   1.936 +      using z2 by auto
   1.937 +    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
   1.938 +      by simp
   1.939 +    have A: "z1 - x \<ge> 0"
   1.940 +      using z1 by auto
   1.941 +    have "z2 \<in> C"
   1.942 +      using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
   1.943 +      by fastforce
   1.944 +    then have B: "f'' z2 \<ge> 0"
   1.945 +      using assms by auto
   1.946 +    from A B have "(z1 - x) * f'' z2 \<ge> 0"
   1.947 +      by auto
   1.948 +    with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
   1.949 +      by auto
   1.950 +    from mult_right_mono[OF this ge(2)]
   1.951 +    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   1.952 +      by (simp add: algebra_simps)
   1.953 +    then have "f y - f x - f' x * (y - x) \<ge> 0"
   1.954 +      using ge by auto
   1.955 +    then show "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.956 +      using res by auto
   1.957 +  qed
   1.958 +  show ?thesis
   1.959 +  proof (cases "x = y")
   1.960 +    case True
   1.961 +    with x y show ?thesis by auto
   1.962 +  next
   1.963 +    case False
   1.964 +    with less_imp x y show ?thesis
   1.965 +      by (auto simp: neq_iff)
   1.966 +  qed
   1.967 +qed
   1.968 +
   1.969 +lemma f''_ge0_imp_convex:
   1.970 +  fixes f :: "real \<Rightarrow> real"
   1.971 +  assumes conv: "convex C"
   1.972 +    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.973 +    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.974 +    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.975 +  shows "convex_on C f"
   1.976 +  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
   1.977 +  by fastforce
   1.978 +
   1.979 +lemma minus_log_convex:
   1.980 +  fixes b :: real
   1.981 +  assumes "b > 1"
   1.982 +  shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   1.983 +proof -
   1.984 +  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
   1.985 +    using DERIV_log by auto
   1.986 +  then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   1.987 +    by (auto simp: DERIV_minus)
   1.988 +  have "\<And>z::real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   1.989 +    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   1.990 +  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   1.991 +  have "\<And>z::real. z > 0 \<Longrightarrow>
   1.992 +    DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   1.993 +    by auto
   1.994 +  then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
   1.995 +    DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.996 +    unfolding inverse_eq_divide by (auto simp: mult.assoc)
   1.997 +  have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.998 +    using \<open>b > 1\<close> by (auto intro!: less_imp_le)
   1.999 +  from f''_ge0_imp_convex[OF pos_is_convex, unfolded greaterThan_iff, OF f' f''0 f''_ge0]
  1.1000 +  show ?thesis
  1.1001 +    by auto
  1.1002 +qed
  1.1003 +
  1.1004 +
  1.1005 +subsection%unimportant \<open>Convexity of real functions\<close>
  1.1006 +
  1.1007 +lemma convex_on_realI:
  1.1008 +  assumes "connected A"
  1.1009 +    and "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
  1.1010 +    and "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f' x \<le> f' y"
  1.1011 +  shows "convex_on A f"
  1.1012 +proof (rule convex_on_linorderI)
  1.1013 +  fix t x y :: real
  1.1014 +  assume t: "t > 0" "t < 1"
  1.1015 +  assume xy: "x \<in> A" "y \<in> A" "x < y"
  1.1016 +  define z where "z = (1 - t) * x + t * y"
  1.1017 +  with \<open>connected A\<close> and xy have ivl: "{x..y} \<subseteq> A"
  1.1018 +    using connected_contains_Icc by blast
  1.1019 +
  1.1020 +  from xy t have xz: "z > x"
  1.1021 +    by (simp add: z_def algebra_simps)
  1.1022 +  have "y - z = (1 - t) * (y - x)"
  1.1023 +    by (simp add: z_def algebra_simps)
  1.1024 +  also from xy t have "\<dots> > 0"
  1.1025 +    by (intro mult_pos_pos) simp_all
  1.1026 +  finally have yz: "z < y"
  1.1027 +    by simp
  1.1028 +
  1.1029 +  from assms xz yz ivl t have "\<exists>\<xi>. \<xi> > x \<and> \<xi> < z \<and> f z - f x = (z - x) * f' \<xi>"
  1.1030 +    by (intro MVT2) (auto intro!: assms(2))
  1.1031 +  then obtain \<xi> where \<xi>: "\<xi> > x" "\<xi> < z" "f' \<xi> = (f z - f x) / (z - x)"
  1.1032 +    by auto
  1.1033 +  from assms xz yz ivl t have "\<exists>\<eta>. \<eta> > z \<and> \<eta> < y \<and> f y - f z = (y - z) * f' \<eta>"
  1.1034 +    by (intro MVT2) (auto intro!: assms(2))
  1.1035 +  then obtain \<eta> where \<eta>: "\<eta> > z" "\<eta> < y" "f' \<eta> = (f y - f z) / (y - z)"
  1.1036 +    by auto
  1.1037 +
  1.1038 +  from \<eta>(3) have "(f y - f z) / (y - z) = f' \<eta>" ..
  1.1039 +  also from \<xi> \<eta> ivl have "\<xi> \<in> A" "\<eta> \<in> A"
  1.1040 +    by auto
  1.1041 +  with \<xi> \<eta> have "f' \<eta> \<ge> f' \<xi>"
  1.1042 +    by (intro assms(3)) auto
  1.1043 +  also from \<xi>(3) have "f' \<xi> = (f z - f x) / (z - x)" .
  1.1044 +  finally have "(f y - f z) * (z - x) \<ge> (f z - f x) * (y - z)"
  1.1045 +    using xz yz by (simp add: field_simps)
  1.1046 +  also have "z - x = t * (y - x)"
  1.1047 +    by (simp add: z_def algebra_simps)
  1.1048 +  also have "y - z = (1 - t) * (y - x)"
  1.1049 +    by (simp add: z_def algebra_simps)
  1.1050 +  finally have "(f y - f z) * t \<ge> (f z - f x) * (1 - t)"
  1.1051 +    using xy by simp
  1.1052 +  then show "(1 - t) * f x + t * f y \<ge> f ((1 - t) *\<^sub>R x + t *\<^sub>R y)"
  1.1053 +    by (simp add: z_def algebra_simps)
  1.1054 +qed
  1.1055 +
  1.1056 +lemma convex_on_inverse:
  1.1057 +  assumes "A \<subseteq> {0<..}"
  1.1058 +  shows "convex_on A (inverse :: real \<Rightarrow> real)"
  1.1059 +proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\<lambda>x. -inverse (x^2)"])
  1.1060 +  fix u v :: real
  1.1061 +  assume "u \<in> {0<..}" "v \<in> {0<..}" "u \<le> v"
  1.1062 +  with assms show "-inverse (u^2) \<le> -inverse (v^2)"
  1.1063 +    by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all)
  1.1064 +qed (insert assms, auto intro!: derivative_eq_intros simp: divide_simps power2_eq_square)
  1.1065 +
  1.1066 +lemma convex_onD_Icc':
  1.1067 +  assumes "convex_on {x..y} f" "c \<in> {x..y}"
  1.1068 +  defines "d \<equiv> y - x"
  1.1069 +  shows "f c \<le> (f y - f x) / d * (c - x) + f x"
  1.1070 +proof (cases x y rule: linorder_cases)
  1.1071 +  case less
  1.1072 +  then have d: "d > 0"
  1.1073 +    by (simp add: d_def)
  1.1074 +  from assms(2) less have A: "0 \<le> (c - x) / d" "(c - x) / d \<le> 1"
  1.1075 +    by (simp_all add: d_def divide_simps)
  1.1076 +  have "f c = f (x + (c - x) * 1)"
  1.1077 +    by simp
  1.1078 +  also from less have "1 = ((y - x) / d)"
  1.1079 +    by (simp add: d_def)
  1.1080 +  also from d have "x + (c - x) * \<dots> = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y"
  1.1081 +    by (simp add: field_simps)
  1.1082 +  also have "f \<dots> \<le> (1 - (c - x) / d) * f x + (c - x) / d * f y"
  1.1083 +    using assms less by (intro convex_onD_Icc) simp_all
  1.1084 +  also from d have "\<dots> = (f y - f x) / d * (c - x) + f x"
  1.1085 +    by (simp add: field_simps)
  1.1086 +  finally show ?thesis .
  1.1087 +qed (insert assms(2), simp_all)
  1.1088 +
  1.1089 +lemma convex_onD_Icc'':
  1.1090 +  assumes "convex_on {x..y} f" "c \<in> {x..y}"
  1.1091 +  defines "d \<equiv> y - x"
  1.1092 +  shows "f c \<le> (f x - f y) / d * (y - c) + f y"
  1.1093 +proof (cases x y rule: linorder_cases)
  1.1094 +  case less
  1.1095 +  then have d: "d > 0"
  1.1096 +    by (simp add: d_def)
  1.1097 +  from assms(2) less have A: "0 \<le> (y - c) / d" "(y - c) / d \<le> 1"
  1.1098 +    by (simp_all add: d_def divide_simps)
  1.1099 +  have "f c = f (y - (y - c) * 1)"
  1.1100 +    by simp
  1.1101 +  also from less have "1 = ((y - x) / d)"
  1.1102 +    by (simp add: d_def)
  1.1103 +  also from d have "y - (y - c) * \<dots> = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y"
  1.1104 +    by (simp add: field_simps)
  1.1105 +  also have "f \<dots> \<le> (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y"
  1.1106 +    using assms less by (intro convex_onD_Icc) (simp_all add: field_simps)
  1.1107 +  also from d have "\<dots> = (f x - f y) / d * (y - c) + f y"
  1.1108 +    by (simp add: field_simps)
  1.1109 +  finally show ?thesis .
  1.1110 +qed (insert assms(2), simp_all)
  1.1111 +
  1.1112 +lemma convex_translation_eq [simp]: "convex ((\<lambda>x. a + x) ` s) \<longleftrightarrow> convex s"
  1.1113 +  by (metis convex_translation translation_galois)
  1.1114 +
  1.1115 +lemma convex_linear_image_eq [simp]:
  1.1116 +    fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
  1.1117 +    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> convex (f ` s) \<longleftrightarrow> convex s"
  1.1118 +    by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq)
  1.1119 +
  1.1120 +lemma fst_linear: "linear fst"
  1.1121 +  unfolding linear_iff by (simp add: algebra_simps)
  1.1122 +
  1.1123 +lemma snd_linear: "linear snd"
  1.1124 +  unfolding linear_iff by (simp add: algebra_simps)
  1.1125 +
  1.1126 +lemma fst_snd_linear: "linear (\<lambda>(x,y). x + y)"
  1.1127 +  unfolding linear_iff by (simp add: algebra_simps)
  1.1128 +
  1.1129 +lemma vector_choose_size:
  1.1130 +  assumes "0 \<le> c"
  1.1131 +  obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c"
  1.1132 +proof -
  1.1133 +  obtain a::'a where "a \<noteq> 0"
  1.1134 +    using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce
  1.1135 +  then show ?thesis
  1.1136 +    by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms)
  1.1137 +qed
  1.1138 +
  1.1139 +lemma vector_choose_dist:
  1.1140 +  assumes "0 \<le> c"
  1.1141 +  obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c"
  1.1142 +by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size)
  1.1143 +
  1.1144 +lemma sum_delta_notmem:
  1.1145 +  assumes "x \<notin> s"
  1.1146 +  shows "sum (\<lambda>y. if (y = x) then P x else Q y) s = sum Q s"
  1.1147 +    and "sum (\<lambda>y. if (x = y) then P x else Q y) s = sum Q s"
  1.1148 +    and "sum (\<lambda>y. if (y = x) then P y else Q y) s = sum Q s"
  1.1149 +    and "sum (\<lambda>y. if (x = y) then P y else Q y) s = sum Q s"
  1.1150 +  apply (rule_tac [!] sum.cong)
  1.1151 +  using assms
  1.1152 +  apply auto
  1.1153 +  done
  1.1154 +
  1.1155 +lemma sum_delta'':
  1.1156 +  fixes s::"'a::real_vector set"
  1.1157 +  assumes "finite s"
  1.1158 +  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.1159 +proof -
  1.1160 +  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.1161 +    by auto
  1.1162 +  show ?thesis
  1.1163 +    unfolding * using sum.delta[OF assms, of y "\<lambda>x. f x *\<^sub>R x"] by auto
  1.1164 +qed
  1.1165 +
  1.1166 +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.1167 +  by (fact if_distrib)
  1.1168 +
  1.1169 +lemma dist_triangle_eq:
  1.1170 +  fixes x y z :: "'a::real_inner"
  1.1171 +  shows "dist x z = dist x y + dist y z \<longleftrightarrow>
  1.1172 +    norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)"
  1.1173 +proof -
  1.1174 +  have *: "x - y + (y - z) = x - z" by auto
  1.1175 +  show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *]
  1.1176 +    by (auto simp:norm_minus_commute)
  1.1177 +qed
  1.1178 +
  1.1179 +
  1.1180 +subsection \<open>Affine set and affine hull\<close>
  1.1181 +
  1.1182 +definition%important affine :: "'a::real_vector set \<Rightarrow> bool"
  1.1183 +  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.1184 +
  1.1185 +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.1186 +  unfolding affine_def by (metis eq_diff_eq')
  1.1187 +
  1.1188 +lemma affine_empty [iff]: "affine {}"
  1.1189 +  unfolding affine_def by auto
  1.1190 +
  1.1191 +lemma affine_sing [iff]: "affine {x}"
  1.1192 +  unfolding affine_alt by (auto simp: scaleR_left_distrib [symmetric])
  1.1193 +
  1.1194 +lemma affine_UNIV [iff]: "affine UNIV"
  1.1195 +  unfolding affine_def by auto
  1.1196 +
  1.1197 +lemma affine_Inter [intro]: "(\<And>s. s\<in>f \<Longrightarrow> affine s) \<Longrightarrow> affine (\<Inter>f)"
  1.1198 +  unfolding affine_def by auto
  1.1199 +
  1.1200 +lemma affine_Int[intro]: "affine s \<Longrightarrow> affine t \<Longrightarrow> affine (s \<inter> t)"
  1.1201 +  unfolding affine_def by auto
  1.1202 +
  1.1203 +lemma affine_scaling: "affine s \<Longrightarrow> affine (image (\<lambda>x. c *\<^sub>R x) s)"
  1.1204 +  apply (clarsimp simp add: affine_def)
  1.1205 +  apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI)
  1.1206 +  apply (auto simp: algebra_simps)
  1.1207 +  done
  1.1208 +
  1.1209 +lemma affine_affine_hull [simp]: "affine(affine hull s)"
  1.1210 +  unfolding hull_def
  1.1211 +  using affine_Inter[of "{t. affine t \<and> s \<subseteq> t}"] by auto
  1.1212 +
  1.1213 +lemma affine_hull_eq[simp]: "(affine hull s = s) \<longleftrightarrow> affine s"
  1.1214 +  by (metis affine_affine_hull hull_same)
  1.1215 +
  1.1216 +lemma affine_hyperplane: "affine {x. a \<bullet> x = b}"
  1.1217 +  by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
  1.1218 +
  1.1219 +
  1.1220 +subsubsection%unimportant \<open>Some explicit formulations\<close>
  1.1221 +
  1.1222 +text "Formalized by Lars Schewe."
  1.1223 +
  1.1224 +lemma affine:
  1.1225 +  fixes V::"'a::real_vector set"
  1.1226 +  shows "affine V \<longleftrightarrow>
  1.1227 +         (\<forall>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> V \<and> sum u S = 1 \<longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V)"
  1.1228 +proof -
  1.1229 +  have "u *\<^sub>R x + v *\<^sub>R y \<in> V" if "x \<in> V" "y \<in> V" "u + v = (1::real)"
  1.1230 +    and *: "\<And>S u. \<lbrakk>finite S; S \<noteq> {}; S \<subseteq> V; sum u S = 1\<rbrakk> \<Longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V" for x y u v
  1.1231 +  proof (cases "x = y")
  1.1232 +    case True
  1.1233 +    then show ?thesis
  1.1234 +      using that by (metis scaleR_add_left scaleR_one)
  1.1235 +  next
  1.1236 +    case False
  1.1237 +    then show ?thesis
  1.1238 +      using that *[of "{x,y}" "\<lambda>w. if w = x then u else v"] by auto
  1.1239 +  qed
  1.1240 +  moreover have "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  1.1241 +                if *: "\<And>x y u v. \<lbrakk>x\<in>V; y\<in>V; u + v = 1\<rbrakk> \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V"
  1.1242 +                  and "finite S" "S \<noteq> {}" "S \<subseteq> V" "sum u S = 1" for S u
  1.1243 +  proof -
  1.1244 +    define n where "n = card S"
  1.1245 +    consider "card S = 0" | "card S = 1" | "card S = 2" | "card S > 2" by linarith
  1.1246 +    then show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  1.1247 +    proof cases
  1.1248 +      assume "card S = 1"
  1.1249 +      then obtain a where "S={a}"
  1.1250 +        by (auto simp: card_Suc_eq)
  1.1251 +      then show ?thesis
  1.1252 +        using that by simp
  1.1253 +    next
  1.1254 +      assume "card S = 2"
  1.1255 +      then obtain a b where "S = {a, b}"
  1.1256 +        by (metis Suc_1 card_1_singletonE card_Suc_eq)
  1.1257 +      then show ?thesis
  1.1258 +        using *[of a b] that
  1.1259 +        by (auto simp: sum_clauses(2))
  1.1260 +    next
  1.1261 +      assume "card S > 2"
  1.1262 +      then show ?thesis using that n_def
  1.1263 +      proof (induct n arbitrary: u S)
  1.1264 +        case 0
  1.1265 +        then show ?case by auto
  1.1266 +      next
  1.1267 +        case (Suc n u S)
  1.1268 +        have "sum u S = card S" if "\<not> (\<exists>x\<in>S. u x \<noteq> 1)"
  1.1269 +          using that unfolding card_eq_sum by auto
  1.1270 +        with Suc.prems obtain x where "x \<in> S" and x: "u x \<noteq> 1" by force
  1.1271 +        have c: "card (S - {x}) = card S - 1"
  1.1272 +          by (simp add: Suc.prems(3) \<open>x \<in> S\<close>)
  1.1273 +        have "sum u (S - {x}) = 1 - u x"
  1.1274 +          by (simp add: Suc.prems sum_diff1_ring \<open>x \<in> S\<close>)
  1.1275 +        with x have eq1: "inverse (1 - u x) * sum u (S - {x}) = 1"
  1.1276 +          by auto
  1.1277 +        have inV: "(\<Sum>y\<in>S - {x}. (inverse (1 - u x) * u y) *\<^sub>R y) \<in> V"
  1.1278 +        proof (cases "card (S - {x}) > 2")
  1.1279 +          case True
  1.1280 +          then have S: "S - {x} \<noteq> {}" "card (S - {x}) = n"
  1.1281 +            using Suc.prems c by force+
  1.1282 +          show ?thesis
  1.1283 +          proof (rule Suc.hyps)
  1.1284 +            show "(\<Sum>a\<in>S - {x}. inverse (1 - u x) * u a) = 1"
  1.1285 +              by (auto simp: eq1 sum_distrib_left[symmetric])
  1.1286 +          qed (use S Suc.prems True in auto)
  1.1287 +        next
  1.1288 +          case False
  1.1289 +          then have "card (S - {x}) = Suc (Suc 0)"
  1.1290 +            using Suc.prems c by auto
  1.1291 +          then obtain a b where ab: "(S - {x}) = {a, b}" "a\<noteq>b"
  1.1292 +            unfolding card_Suc_eq by auto
  1.1293 +          then show ?thesis
  1.1294 +            using eq1 \<open>S \<subseteq> V\<close>
  1.1295 +            by (auto simp: sum_distrib_left distrib_left intro!: Suc.prems(2)[of a b])
  1.1296 +        qed
  1.1297 +        have "u x + (1 - u x) = 1 \<Longrightarrow>
  1.1298 +          u x *\<^sub>R x + (1 - u x) *\<^sub>R ((\<Sum>y\<in>S - {x}. u y *\<^sub>R y) /\<^sub>R (1 - u x)) \<in> V"
  1.1299 +          by (rule Suc.prems) (use \<open>x \<in> S\<close> Suc.prems inV in \<open>auto simp: scaleR_right.sum\<close>)
  1.1300 +        moreover have "(\<Sum>a\<in>S. u a *\<^sub>R a) = u x *\<^sub>R x + (\<Sum>a\<in>S - {x}. u a *\<^sub>R a)"
  1.1301 +          by (meson Suc.prems(3) sum.remove \<open>x \<in> S\<close>)
  1.1302 +        ultimately show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  1.1303 +          by (simp add: x)
  1.1304 +      qed
  1.1305 +    qed (use \<open>S\<noteq>{}\<close> \<open>finite S\<close> in auto)
  1.1306 +  qed
  1.1307 +  ultimately show ?thesis
  1.1308 +    unfolding affine_def by meson
  1.1309 +qed
  1.1310 +
  1.1311 +
  1.1312 +lemma affine_hull_explicit:
  1.1313 +  "affine hull p = {y. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and> sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1.1314 +  (is "_ = ?rhs")
  1.1315 +proof (rule hull_unique)
  1.1316 +  show "p \<subseteq> ?rhs"
  1.1317 +  proof (intro subsetI CollectI exI conjI)
  1.1318 +    show "\<And>x. sum (\<lambda>z. 1) {x} = 1"
  1.1319 +      by auto
  1.1320 +  qed auto
  1.1321 +  show "?rhs \<subseteq> T" if "p \<subseteq> T" "affine T" for T
  1.1322 +    using that unfolding affine by blast
  1.1323 +  show "affine ?rhs"
  1.1324 +    unfolding affine_def
  1.1325 +  proof clarify
  1.1326 +    fix u v :: real and sx ux sy uy
  1.1327 +    assume uv: "u + v = 1"
  1.1328 +      and x: "finite sx" "sx \<noteq> {}" "sx \<subseteq> p" "sum ux sx = (1::real)"
  1.1329 +      and y: "finite sy" "sy \<noteq> {}" "sy \<subseteq> p" "sum uy sy = (1::real)" 
  1.1330 +    have **: "(sx \<union> sy) \<inter> sx = sx" "(sx \<union> sy) \<inter> sy = sy"
  1.1331 +      by auto
  1.1332 +    show "\<exists>S w. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and>
  1.1333 +        sum w S = 1 \<and> (\<Sum>v\<in>S. w v *\<^sub>R v) = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
  1.1334 +    proof (intro exI conjI)
  1.1335 +      show "finite (sx \<union> sy)"
  1.1336 +        using x y by auto
  1.1337 +      show "sum (\<lambda>i. (if i\<in>sx then u * ux i else 0) + (if i\<in>sy then v * uy i else 0)) (sx \<union> sy) = 1"
  1.1338 +        using x y uv
  1.1339 +        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric] sum_distrib_left [symmetric] **)
  1.1340 +      have "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i)
  1.1341 +          = (\<Sum>i\<in>sx. (u * ux i) *\<^sub>R i) + (\<Sum>i\<in>sy. (v * uy i) *\<^sub>R i)"
  1.1342 +        using x y
  1.1343 +        unfolding scaleR_left_distrib scaleR_zero_left if_smult
  1.1344 +        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric]  **)
  1.1345 +      also have "\<dots> = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
  1.1346 +        unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] by blast
  1.1347 +      finally show "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i) 
  1.1348 +                  = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)" .
  1.1349 +    qed (use x y in auto)
  1.1350 +  qed
  1.1351 +qed
  1.1352 +
  1.1353 +lemma affine_hull_finite:
  1.1354 +  assumes "finite S"
  1.1355 +  shows "affine hull S = {y. \<exists>u. sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1.1356 +proof -
  1.1357 +  have *: "\<exists>h. sum h S = 1 \<and> (\<Sum>v\<in>S. h v *\<^sub>R v) = x" 
  1.1358 +    if "F \<subseteq> S" "finite F" "F \<noteq> {}" and sum: "sum u F = 1" and x: "(\<Sum>v\<in>F. u v *\<^sub>R v) = x" for x F u
  1.1359 +  proof -
  1.1360 +    have "S \<inter> F = F"
  1.1361 +      using that by auto
  1.1362 +    show ?thesis
  1.1363 +    proof (intro exI conjI)
  1.1364 +      show "(\<Sum>x\<in>S. if x \<in> F then u x else 0) = 1"
  1.1365 +        by (metis (mono_tags, lifting) \<open>S \<inter> F = F\<close> assms sum.inter_restrict sum)
  1.1366 +      show "(\<Sum>v\<in>S. (if v \<in> F then u v else 0) *\<^sub>R v) = x"
  1.1367 +        by (simp add: if_smult cong: if_cong) (metis (no_types) \<open>S \<inter> F = F\<close> assms sum.inter_restrict x)
  1.1368 +    qed
  1.1369 +  qed
  1.1370 +  show ?thesis
  1.1371 +    unfolding affine_hull_explicit using assms
  1.1372 +    by (fastforce dest: *)
  1.1373 +qed
  1.1374 +
  1.1375 +
  1.1376 +subsubsection%unimportant \<open>Stepping theorems and hence small special cases\<close>
  1.1377 +
  1.1378 +lemma affine_hull_empty[simp]: "affine hull {} = {}"
  1.1379 +  by simp
  1.1380 +
  1.1381 +lemma affine_hull_finite_step:
  1.1382 +  fixes y :: "'a::real_vector"
  1.1383 +  shows "finite S \<Longrightarrow>
  1.1384 +      (\<exists>u. sum u (insert a S) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a S) = y) \<longleftrightarrow>
  1.1385 +      (\<exists>v u. sum u S = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y - v *\<^sub>R a)" (is "_ \<Longrightarrow> ?lhs = ?rhs")
  1.1386 +proof -
  1.1387 +  assume fin: "finite S"
  1.1388 +  show "?lhs = ?rhs"
  1.1389 +  proof
  1.1390 +    assume ?lhs
  1.1391 +    then obtain u where u: "sum u (insert a S) = w \<and> (\<Sum>x\<in>insert a S. u x *\<^sub>R x) = y"
  1.1392 +      by auto
  1.1393 +    show ?rhs
  1.1394 +    proof (cases "a \<in> S")
  1.1395 +      case True
  1.1396 +      then show ?thesis
  1.1397 +        using u by (simp add: insert_absorb) (metis diff_zero real_vector.scale_zero_left)
  1.1398 +    next
  1.1399 +      case False
  1.1400 +      show ?thesis
  1.1401 +        by (rule exI [where x="u a"]) (use u fin False in auto)
  1.1402 +    qed
  1.1403 +  next
  1.1404 +    assume ?rhs
  1.1405 +    then obtain v u where vu: "sum u S = w - v"  "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  1.1406 +      by auto
  1.1407 +    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.1408 +      by auto
  1.1409 +    show ?lhs
  1.1410 +    proof (cases "a \<in> S")
  1.1411 +      case True
  1.1412 +      show ?thesis
  1.1413 +        by (rule exI [where x="\<lambda>x. (if x=a then v else 0) + u x"])
  1.1414 +           (simp add: True scaleR_left_distrib sum.distrib sum_clauses fin vu * cong: if_cong)
  1.1415 +    next
  1.1416 +      case False
  1.1417 +      then show ?thesis
  1.1418 +        apply (rule_tac x="\<lambda>x. if x=a then v else u x" in exI) 
  1.1419 +        apply (simp add: vu sum_clauses(2)[OF fin] *)
  1.1420 +        by (simp add: sum_delta_notmem(3) vu)
  1.1421 +    qed
  1.1422 +  qed
  1.1423 +qed
  1.1424 +
  1.1425 +lemma affine_hull_2:
  1.1426 +  fixes a b :: "'a::real_vector"
  1.1427 +  shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}"
  1.1428 +  (is "?lhs = ?rhs")
  1.1429 +proof -
  1.1430 +  have *:
  1.1431 +    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  1.1432 +    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  1.1433 +  have "?lhs = {y. \<exists>u. sum u {a, b} = 1 \<and> (\<Sum>v\<in>{a, b}. u v *\<^sub>R v) = y}"
  1.1434 +    using affine_hull_finite[of "{a,b}"] by auto
  1.1435 +  also have "\<dots> = {y. \<exists>v u. u b = 1 - v \<and> u b *\<^sub>R b = y - v *\<^sub>R a}"
  1.1436 +    by (simp add: affine_hull_finite_step[of "{b}" a])
  1.1437 +  also have "\<dots> = ?rhs" unfolding * by auto
  1.1438 +  finally show ?thesis by auto
  1.1439 +qed
  1.1440 +
  1.1441 +lemma affine_hull_3:
  1.1442 +  fixes a b c :: "'a::real_vector"
  1.1443 +  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.1444 +proof -
  1.1445 +  have *:
  1.1446 +    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  1.1447 +    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  1.1448 +  show ?thesis
  1.1449 +    apply (simp add: affine_hull_finite affine_hull_finite_step)
  1.1450 +    unfolding *
  1.1451 +    apply safe
  1.1452 +     apply (metis add.assoc)
  1.1453 +    apply (rule_tac x=u in exI, force)
  1.1454 +    done
  1.1455 +qed
  1.1456 +
  1.1457 +lemma mem_affine:
  1.1458 +  assumes "affine S" "x \<in> S" "y \<in> S" "u + v = 1"
  1.1459 +  shows "u *\<^sub>R x + v *\<^sub>R y \<in> S"
  1.1460 +  using assms affine_def[of S] by auto
  1.1461 +
  1.1462 +lemma mem_affine_3:
  1.1463 +  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S" "u + v + w = 1"
  1.1464 +  shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> S"
  1.1465 +proof -
  1.1466 +  have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> affine hull {x, y, z}"
  1.1467 +    using affine_hull_3[of x y z] assms by auto
  1.1468 +  moreover
  1.1469 +  have "affine hull {x, y, z} \<subseteq> affine hull S"
  1.1470 +    using hull_mono[of "{x, y, z}" "S"] assms by auto
  1.1471 +  moreover
  1.1472 +  have "affine hull S = S"
  1.1473 +    using assms affine_hull_eq[of S] by auto
  1.1474 +  ultimately show ?thesis by auto
  1.1475 +qed
  1.1476 +
  1.1477 +lemma mem_affine_3_minus:
  1.1478 +  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S"
  1.1479 +  shows "x + v *\<^sub>R (y-z) \<in> S"
  1.1480 +  using mem_affine_3[of S x y z 1 v "-v"] assms
  1.1481 +  by (simp add: algebra_simps)
  1.1482 +
  1.1483 +corollary mem_affine_3_minus2:
  1.1484 +    "\<lbrakk>affine S; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> x - v *\<^sub>R (y-z) \<in> S"
  1.1485 +  by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left)
  1.1486 +
  1.1487 +
  1.1488 +subsubsection%unimportant \<open>Some relations between affine hull and subspaces\<close>
  1.1489 +
  1.1490 +lemma affine_hull_insert_subset_span:
  1.1491 +  "affine hull (insert a S) \<subseteq> {a + v| v . v \<in> span {x - a | x . x \<in> S}}"
  1.1492 +proof -
  1.1493 +  have "\<exists>v T u. x = a + v \<and> (finite T \<and> T \<subseteq> {x - a |x. x \<in> S} \<and> (\<Sum>v\<in>T. u v *\<^sub>R v) = v)"
  1.1494 +    if "finite F" "F \<noteq> {}" "F \<subseteq> insert a S" "sum u F = 1" "(\<Sum>v\<in>F. u v *\<^sub>R v) = x"
  1.1495 +    for x F u
  1.1496 +  proof -
  1.1497 +    have *: "(\<lambda>x. x - a) ` (F - {a}) \<subseteq> {x - a |x. x \<in> S}"
  1.1498 +      using that by auto
  1.1499 +    show ?thesis
  1.1500 +    proof (intro exI conjI)
  1.1501 +      show "finite ((\<lambda>x. x - a) ` (F - {a}))"
  1.1502 +        by (simp add: that(1))
  1.1503 +      show "(\<Sum>v\<in>(\<lambda>x. x - a) ` (F - {a}). u(v+a) *\<^sub>R v) = x-a"
  1.1504 +        by (simp add: sum.reindex[unfolded inj_on_def] algebra_simps
  1.1505 +            sum_subtractf scaleR_left.sum[symmetric] sum_diff1 that)
  1.1506 +    qed (use \<open>F \<subseteq> insert a S\<close> in auto)
  1.1507 +  qed
  1.1508 +  then show ?thesis
  1.1509 +    unfolding affine_hull_explicit span_explicit by blast
  1.1510 +qed
  1.1511 +
  1.1512 +lemma affine_hull_insert_span:
  1.1513 +  assumes "a \<notin> S"
  1.1514 +  shows "affine hull (insert a S) = {a + v | v . v \<in> span {x - a | x.  x \<in> S}}"
  1.1515 +proof -
  1.1516 +  have *: "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
  1.1517 +    if "v \<in> span {x - a |x. x \<in> S}" "y = a + v" for y v
  1.1518 +  proof -
  1.1519 +    from that
  1.1520 +    obtain T u where u: "finite T" "T \<subseteq> {x - a |x. x \<in> S}" "a + (\<Sum>v\<in>T. u v *\<^sub>R v) = y"
  1.1521 +      unfolding span_explicit by auto
  1.1522 +    define F where "F = (\<lambda>x. x + a) ` T"
  1.1523 +    have F: "finite F" "F \<subseteq> S" "(\<Sum>v\<in>F. u (v - a) *\<^sub>R (v - a)) = y - a"
  1.1524 +      unfolding F_def using u by (auto simp: sum.reindex[unfolded inj_on_def])
  1.1525 +    have *: "F \<inter> {a} = {}" "F \<inter> - {a} = F"
  1.1526 +      using F assms by auto
  1.1527 +    show "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
  1.1528 +      apply (rule_tac x = "insert a F" in exI)
  1.1529 +      apply (rule_tac x = "\<lambda>x. if x=a then 1 - sum (\<lambda>x. u (x - a)) F else u (x - a)" in exI)
  1.1530 +      using assms F
  1.1531 +      apply (auto simp:  sum_clauses sum.If_cases if_smult sum_subtractf scaleR_left.sum algebra_simps *)
  1.1532 +      done
  1.1533 +  qed
  1.1534 +  show ?thesis
  1.1535 +    by (intro subset_antisym affine_hull_insert_subset_span) (auto simp: affine_hull_explicit dest!: *)
  1.1536 +qed
  1.1537 +
  1.1538 +lemma affine_hull_span:
  1.1539 +  assumes "a \<in> S"
  1.1540 +  shows "affine hull S = {a + v | v. v \<in> span {x - a | x. x \<in> S - {a}}}"
  1.1541 +  using affine_hull_insert_span[of a "S - {a}", unfolded insert_Diff[OF assms]] by auto
  1.1542 +
  1.1543 +
  1.1544 +subsubsection%unimportant \<open>Parallel affine sets\<close>
  1.1545 +
  1.1546 +definition affine_parallel :: "'a::real_vector set \<Rightarrow> 'a::real_vector set \<Rightarrow> bool"
  1.1547 +  where "affine_parallel S T \<longleftrightarrow> (\<exists>a. T = (\<lambda>x. a + x) ` S)"
  1.1548 +
  1.1549 +lemma affine_parallel_expl_aux:
  1.1550 +  fixes S T :: "'a::real_vector set"
  1.1551 +  assumes "\<And>x. x \<in> S \<longleftrightarrow> a + x \<in> T"
  1.1552 +  shows "T = (\<lambda>x. a + x) ` S"
  1.1553 +proof -
  1.1554 +  have "x \<in> ((\<lambda>x. a + x) ` S)" if "x \<in> T" for x
  1.1555 +    using that
  1.1556 +    by (simp add: image_iff) (metis add.commute diff_add_cancel assms)
  1.1557 +  moreover have "T \<ge> (\<lambda>x. a + x) ` S"
  1.1558 +    using assms by auto
  1.1559 +  ultimately show ?thesis by auto
  1.1560 +qed
  1.1561 +
  1.1562 +lemma affine_parallel_expl: "affine_parallel S T \<longleftrightarrow> (\<exists>a. \<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T)"
  1.1563 +  unfolding affine_parallel_def
  1.1564 +  using affine_parallel_expl_aux[of S _ T] by auto
  1.1565 +
  1.1566 +lemma affine_parallel_reflex: "affine_parallel S S"
  1.1567 +  unfolding affine_parallel_def
  1.1568 +  using image_add_0 by blast
  1.1569 +
  1.1570 +lemma affine_parallel_commut:
  1.1571 +  assumes "affine_parallel A B"
  1.1572 +  shows "affine_parallel B A"
  1.1573 +proof -
  1.1574 +  from assms obtain a where B: "B = (\<lambda>x. a + x) ` A"
  1.1575 +    unfolding affine_parallel_def by auto
  1.1576 +  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  1.1577 +  from B show ?thesis
  1.1578 +    using translation_galois [of B a A]
  1.1579 +    unfolding affine_parallel_def by auto
  1.1580 +qed
  1.1581 +
  1.1582 +lemma affine_parallel_assoc:
  1.1583 +  assumes "affine_parallel A B"
  1.1584 +    and "affine_parallel B C"
  1.1585 +  shows "affine_parallel A C"
  1.1586 +proof -
  1.1587 +  from assms obtain ab where "B = (\<lambda>x. ab + x) ` A"
  1.1588 +    unfolding affine_parallel_def by auto
  1.1589 +  moreover
  1.1590 +  from assms obtain bc where "C = (\<lambda>x. bc + x) ` B"
  1.1591 +    unfolding affine_parallel_def by auto
  1.1592 +  ultimately show ?thesis
  1.1593 +    using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto
  1.1594 +qed
  1.1595 +
  1.1596 +lemma affine_translation_aux:
  1.1597 +  fixes a :: "'a::real_vector"
  1.1598 +  assumes "affine ((\<lambda>x. a + x) ` S)"
  1.1599 +  shows "affine S"
  1.1600 +proof -
  1.1601 +  {
  1.1602 +    fix x y u v
  1.1603 +    assume xy: "x \<in> S" "y \<in> S" "(u :: real) + v = 1"
  1.1604 +    then have "(a + x) \<in> ((\<lambda>x. a + x) ` S)" "(a + y) \<in> ((\<lambda>x. a + x) ` S)"
  1.1605 +      by auto
  1.1606 +    then have h1: "u *\<^sub>R  (a + x) + v *\<^sub>R (a + y) \<in> (\<lambda>x. a + x) ` S"
  1.1607 +      using xy assms unfolding affine_def by auto
  1.1608 +    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.1609 +      by (simp add: algebra_simps)
  1.1610 +    also have "\<dots> = a + (u *\<^sub>R x + v *\<^sub>R y)"
  1.1611 +      using \<open>u + v = 1\<close> by auto
  1.1612 +    ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \<in> (\<lambda>x. a + x) ` S"
  1.1613 +      using h1 by auto
  1.1614 +    then have "u *\<^sub>R x + v *\<^sub>R y \<in> S" by auto
  1.1615 +  }
  1.1616 +  then show ?thesis unfolding affine_def by auto
  1.1617 +qed
  1.1618 +
  1.1619 +lemma affine_translation:
  1.1620 +  fixes a :: "'a::real_vector"
  1.1621 +  shows "affine S \<longleftrightarrow> affine ((\<lambda>x. a + x) ` S)"
  1.1622 +proof -
  1.1623 +  have "affine S \<Longrightarrow> affine ((\<lambda>x. a + x) ` S)"
  1.1624 +    using affine_translation_aux[of "-a" "((\<lambda>x. a + x) ` S)"]
  1.1625 +    using translation_assoc[of "-a" a S] by auto
  1.1626 +  then show ?thesis using affine_translation_aux by auto
  1.1627 +qed
  1.1628 +
  1.1629 +lemma parallel_is_affine:
  1.1630 +  fixes S T :: "'a::real_vector set"
  1.1631 +  assumes "affine S" "affine_parallel S T"
  1.1632 +  shows "affine T"
  1.1633 +proof -
  1.1634 +  from assms obtain a where "T = (\<lambda>x. a + x) ` S"
  1.1635 +    unfolding affine_parallel_def by auto
  1.1636 +  then show ?thesis
  1.1637 +    using affine_translation assms by auto
  1.1638 +qed
  1.1639 +
  1.1640 +lemma subspace_imp_affine: "subspace s \<Longrightarrow> affine s"
  1.1641 +  unfolding subspace_def affine_def by auto
  1.1642 +
  1.1643 +
  1.1644 +subsubsection%unimportant \<open>Subspace parallel to an affine set\<close>
  1.1645 +
  1.1646 +lemma subspace_affine: "subspace S \<longleftrightarrow> affine S \<and> 0 \<in> S"
  1.1647 +proof -
  1.1648 +  have h0: "subspace S \<Longrightarrow> affine S \<and> 0 \<in> S"
  1.1649 +    using subspace_imp_affine[of S] subspace_0 by auto
  1.1650 +  {
  1.1651 +    assume assm: "affine S \<and> 0 \<in> S"
  1.1652 +    {
  1.1653 +      fix c :: real
  1.1654 +      fix x
  1.1655 +      assume x: "x \<in> S"
  1.1656 +      have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto
  1.1657 +      moreover
  1.1658 +      have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \<in> S"
  1.1659 +        using affine_alt[of S] assm x by auto
  1.1660 +      ultimately have "c *\<^sub>R x \<in> S" by auto
  1.1661 +    }
  1.1662 +    then have h1: "\<forall>c. \<forall>x \<in> S. c *\<^sub>R x \<in> S" by auto
  1.1663 +
  1.1664 +    {
  1.1665 +      fix x y
  1.1666 +      assume xy: "x \<in> S" "y \<in> S"
  1.1667 +      define u where "u = (1 :: real)/2"
  1.1668 +      have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)"
  1.1669 +        by auto
  1.1670 +      moreover
  1.1671 +      have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y"
  1.1672 +        by (simp add: algebra_simps)
  1.1673 +      moreover
  1.1674 +      have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> S"
  1.1675 +        using affine_alt[of S] assm xy by auto
  1.1676 +      ultimately
  1.1677 +      have "(1/2) *\<^sub>R (x+y) \<in> S"
  1.1678 +        using u_def by auto
  1.1679 +      moreover
  1.1680 +      have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))"
  1.1681 +        by auto
  1.1682 +      ultimately
  1.1683 +      have "x + y \<in> S"
  1.1684 +        using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto
  1.1685 +    }
  1.1686 +    then have "\<forall>x \<in> S. \<forall>y \<in> S. x + y \<in> S"
  1.1687 +      by auto
  1.1688 +    then have "subspace S"
  1.1689 +      using h1 assm unfolding subspace_def by auto
  1.1690 +  }
  1.1691 +  then show ?thesis using h0 by metis
  1.1692 +qed
  1.1693 +
  1.1694 +lemma affine_diffs_subspace:
  1.1695 +  assumes "affine S" "a \<in> S"
  1.1696 +  shows "subspace ((\<lambda>x. (-a)+x) ` S)"
  1.1697 +proof -
  1.1698 +  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  1.1699 +  have "affine ((\<lambda>x. (-a)+x) ` S)"
  1.1700 +    using  affine_translation assms by auto
  1.1701 +  moreover have "0 \<in> ((\<lambda>x. (-a)+x) ` S)"
  1.1702 +    using assms exI[of "(\<lambda>x. x\<in>S \<and> -a+x = 0)" a] by auto
  1.1703 +  ultimately show ?thesis using subspace_affine by auto
  1.1704 +qed
  1.1705 +
  1.1706 +lemma parallel_subspace_explicit:
  1.1707 +  assumes "affine S"
  1.1708 +    and "a \<in> S"
  1.1709 +  assumes "L \<equiv> {y. \<exists>x \<in> S. (-a) + x = y}"
  1.1710 +  shows "subspace L \<and> affine_parallel S L"
  1.1711 +proof -
  1.1712 +  from assms have "L = plus (- a) ` S" by auto
  1.1713 +  then have par: "affine_parallel S L"
  1.1714 +    unfolding affine_parallel_def ..
  1.1715 +  then have "affine L" using assms parallel_is_affine by auto
  1.1716 +  moreover have "0 \<in> L"
  1.1717 +    using assms by auto
  1.1718 +  ultimately show ?thesis
  1.1719 +    using subspace_affine par by auto
  1.1720 +qed
  1.1721 +
  1.1722 +lemma parallel_subspace_aux:
  1.1723 +  assumes "subspace A"
  1.1724 +    and "subspace B"
  1.1725 +    and "affine_parallel A B"
  1.1726 +  shows "A \<supseteq> B"
  1.1727 +proof -
  1.1728 +  from assms obtain a where a: "\<forall>x. x \<in> A \<longleftrightarrow> a + x \<in> B"
  1.1729 +    using affine_parallel_expl[of A B] by auto
  1.1730 +  then have "-a \<in> A"
  1.1731 +    using assms subspace_0[of B] by auto
  1.1732 +  then have "a \<in> A"
  1.1733 +    using assms subspace_neg[of A "-a"] by auto
  1.1734 +  then show ?thesis
  1.1735 +    using assms a unfolding subspace_def by auto
  1.1736 +qed
  1.1737 +
  1.1738 +lemma parallel_subspace:
  1.1739 +  assumes "subspace A"
  1.1740 +    and "subspace B"
  1.1741 +    and "affine_parallel A B"
  1.1742 +  shows "A = B"
  1.1743 +proof
  1.1744 +  show "A \<supseteq> B"
  1.1745 +    using assms parallel_subspace_aux by auto
  1.1746 +  show "A \<subseteq> B"
  1.1747 +    using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto
  1.1748 +qed
  1.1749 +
  1.1750 +lemma affine_parallel_subspace:
  1.1751 +  assumes "affine S" "S \<noteq> {}"
  1.1752 +  shows "\<exists>!L. subspace L \<and> affine_parallel S L"
  1.1753 +proof -
  1.1754 +  have ex: "\<exists>L. subspace L \<and> affine_parallel S L"
  1.1755 +    using assms parallel_subspace_explicit by auto
  1.1756 +  {
  1.1757 +    fix L1 L2
  1.1758 +    assume ass: "subspace L1 \<and> affine_parallel S L1" "subspace L2 \<and> affine_parallel S L2"
  1.1759 +    then have "affine_parallel L1 L2"
  1.1760 +      using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto
  1.1761 +    then have "L1 = L2"
  1.1762 +      using ass parallel_subspace by auto
  1.1763 +  }
  1.1764 +  then show ?thesis using ex by auto
  1.1765 +qed
  1.1766 +
  1.1767 +
  1.1768 +subsection \<open>Cones\<close>
  1.1769 +
  1.1770 +definition%important cone :: "'a::real_vector set \<Rightarrow> bool"
  1.1771 +  where "cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>c\<ge>0. c *\<^sub>R x \<in> s)"
  1.1772 +
  1.1773 +lemma cone_empty[intro, simp]: "cone {}"
  1.1774 +  unfolding cone_def by auto
  1.1775 +
  1.1776 +lemma cone_univ[intro, simp]: "cone UNIV"
  1.1777 +  unfolding cone_def by auto
  1.1778 +
  1.1779 +lemma cone_Inter[intro]: "\<forall>s\<in>f. cone s \<Longrightarrow> cone (\<Inter>f)"
  1.1780 +  unfolding cone_def by auto
  1.1781 +
  1.1782 +lemma subspace_imp_cone: "subspace S \<Longrightarrow> cone S"
  1.1783 +  by (simp add: cone_def subspace_scale)
  1.1784 +
  1.1785 +
  1.1786 +subsubsection \<open>Conic hull\<close>
  1.1787 +
  1.1788 +lemma cone_cone_hull: "cone (cone hull s)"
  1.1789 +  unfolding hull_def by auto
  1.1790 +
  1.1791 +lemma cone_hull_eq: "cone hull s = s \<longleftrightarrow> cone s"
  1.1792 +  apply (rule hull_eq)
  1.1793 +  using cone_Inter
  1.1794 +  unfolding subset_eq
  1.1795 +  apply auto
  1.1796 +  done
  1.1797 +
  1.1798 +lemma mem_cone:
  1.1799 +  assumes "cone S" "x \<in> S" "c \<ge> 0"
  1.1800 +  shows "c *\<^sub>R x \<in> S"
  1.1801 +  using assms cone_def[of S] by auto
  1.1802 +
  1.1803 +lemma cone_contains_0:
  1.1804 +  assumes "cone S"
  1.1805 +  shows "S \<noteq> {} \<longleftrightarrow> 0 \<in> S"
  1.1806 +proof -
  1.1807 +  {
  1.1808 +    assume "S \<noteq> {}"
  1.1809 +    then obtain a where "a \<in> S" by auto
  1.1810 +    then have "0 \<in> S"
  1.1811 +      using assms mem_cone[of S a 0] by auto
  1.1812 +  }
  1.1813 +  then show ?thesis by auto
  1.1814 +qed
  1.1815 +
  1.1816 +lemma cone_0: "cone {0}"
  1.1817 +  unfolding cone_def by auto
  1.1818 +
  1.1819 +lemma cone_Union[intro]: "(\<forall>s\<in>f. cone s) \<longrightarrow> cone (\<Union>f)"
  1.1820 +  unfolding cone_def by blast
  1.1821 +
  1.1822 +lemma cone_iff:
  1.1823 +  assumes "S \<noteq> {}"
  1.1824 +  shows "cone S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  1.1825 +proof -
  1.1826 +  {
  1.1827 +    assume "cone S"
  1.1828 +    {
  1.1829 +      fix c :: real
  1.1830 +      assume "c > 0"
  1.1831 +      {
  1.1832 +        fix x
  1.1833 +        assume "x \<in> S"
  1.1834 +        then have "x \<in> ((*\<^sub>R) c) ` S"
  1.1835 +          unfolding image_def
  1.1836 +          using \<open>cone S\<close> \<open>c>0\<close> mem_cone[of S x "1/c"]
  1.1837 +            exI[of "(\<lambda>t. t \<in> S \<and> x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"]
  1.1838 +          by auto
  1.1839 +      }
  1.1840 +      moreover
  1.1841 +      {
  1.1842 +        fix x
  1.1843 +        assume "x \<in> ((*\<^sub>R) c) ` S"
  1.1844 +        then have "x \<in> S"
  1.1845 +          using \<open>cone S\<close> \<open>c > 0\<close>
  1.1846 +          unfolding cone_def image_def \<open>c > 0\<close> by auto
  1.1847 +      }
  1.1848 +      ultimately have "((*\<^sub>R) c) ` S = S" by auto
  1.1849 +    }
  1.1850 +    then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  1.1851 +      using \<open>cone S\<close> cone_contains_0[of S] assms by auto
  1.1852 +  }
  1.1853 +  moreover
  1.1854 +  {
  1.1855 +    assume a: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  1.1856 +    {
  1.1857 +      fix x
  1.1858 +      assume "x \<in> S"
  1.1859 +      fix c1 :: real
  1.1860 +      assume "c1 \<ge> 0"
  1.1861 +      then have "c1 = 0 \<or> c1 > 0" by auto
  1.1862 +      then have "c1 *\<^sub>R x \<in> S" using a \<open>x \<in> S\<close> by auto
  1.1863 +    }
  1.1864 +    then have "cone S" unfolding cone_def by auto
  1.1865 +  }
  1.1866 +  ultimately show ?thesis by blast
  1.1867 +qed
  1.1868 +
  1.1869 +lemma cone_hull_empty: "cone hull {} = {}"
  1.1870 +  by (metis cone_empty cone_hull_eq)
  1.1871 +
  1.1872 +lemma cone_hull_empty_iff: "S = {} \<longleftrightarrow> cone hull S = {}"
  1.1873 +  by (metis bot_least cone_hull_empty hull_subset xtrans(5))
  1.1874 +
  1.1875 +lemma cone_hull_contains_0: "S \<noteq> {} \<longleftrightarrow> 0 \<in> cone hull S"
  1.1876 +  using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S]
  1.1877 +  by auto
  1.1878 +
  1.1879 +lemma mem_cone_hull:
  1.1880 +  assumes "x \<in> S" "c \<ge> 0"
  1.1881 +  shows "c *\<^sub>R x \<in> cone hull S"
  1.1882 +  by (metis assms cone_cone_hull hull_inc mem_cone)
  1.1883 +
  1.1884 +proposition cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \<ge> 0 \<and> x \<in> S}"
  1.1885 +  (is "?lhs = ?rhs")
  1.1886 +proof -
  1.1887 +  {
  1.1888 +    fix x
  1.1889 +    assume "x \<in> ?rhs"
  1.1890 +    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.1891 +      by auto
  1.1892 +    fix c :: real
  1.1893 +    assume c: "c \<ge> 0"
  1.1894 +    then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx"
  1.1895 +      using x by (simp add: algebra_simps)
  1.1896 +    moreover
  1.1897 +    have "c * cx \<ge> 0" using c x by auto
  1.1898 +    ultimately
  1.1899 +    have "c *\<^sub>R x \<in> ?rhs" using x by auto
  1.1900 +  }
  1.1901 +  then have "cone ?rhs"
  1.1902 +    unfolding cone_def by auto
  1.1903 +  then have "?rhs \<in> Collect cone"
  1.1904 +    unfolding mem_Collect_eq by auto
  1.1905 +  {
  1.1906 +    fix x
  1.1907 +    assume "x \<in> S"
  1.1908 +    then have "1 *\<^sub>R x \<in> ?rhs"
  1.1909 +      apply auto
  1.1910 +      apply (rule_tac x = 1 in exI, auto)
  1.1911 +      done
  1.1912 +    then have "x \<in> ?rhs" by auto
  1.1913 +  }
  1.1914 +  then have "S \<subseteq> ?rhs" by auto
  1.1915 +  then have "?lhs \<subseteq> ?rhs"
  1.1916 +    using \<open>?rhs \<in> Collect cone\<close> hull_minimal[of S "?rhs" "cone"] by auto
  1.1917 +  moreover
  1.1918 +  {
  1.1919 +    fix x
  1.1920 +    assume "x \<in> ?rhs"
  1.1921 +    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.1922 +      by auto
  1.1923 +    then have "xx \<in> cone hull S"
  1.1924 +      using hull_subset[of S] by auto
  1.1925 +    then have "x \<in> ?lhs"
  1.1926 +      using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto
  1.1927 +  }
  1.1928 +  ultimately show ?thesis by auto
  1.1929 +qed
  1.1930 +
  1.1931 +
  1.1932 +subsection \<open>Affine dependence and consequential theorems\<close>
  1.1933 +
  1.1934 +text "Formalized by Lars Schewe."
  1.1935 +
  1.1936 +definition%important affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
  1.1937 +  where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
  1.1938 +
  1.1939 +lemma affine_dependent_subset:
  1.1940 +   "\<lbrakk>affine_dependent s; s \<subseteq> t\<rbrakk> \<Longrightarrow> affine_dependent t"
  1.1941 +apply (simp add: affine_dependent_def Bex_def)
  1.1942 +apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]])
  1.1943 +done
  1.1944 +
  1.1945 +lemma affine_independent_subset:
  1.1946 +  shows "\<lbrakk>\<not> affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> \<not> affine_dependent s"
  1.1947 +by (metis affine_dependent_subset)
  1.1948 +
  1.1949 +lemma affine_independent_Diff:
  1.1950 +   "\<not> affine_dependent s \<Longrightarrow> \<not> affine_dependent(s - t)"
  1.1951 +by (meson Diff_subset affine_dependent_subset)
  1.1952 +
  1.1953 +proposition affine_dependent_explicit:
  1.1954 +  "affine_dependent p \<longleftrightarrow>
  1.1955 +    (\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
  1.1956 +proof -
  1.1957 +  have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> (\<Sum>w\<in>S. u w *\<^sub>R w) = 0"
  1.1958 +    if "(\<Sum>w\<in>S. u w *\<^sub>R w) = x" "x \<in> p" "finite S" "S \<noteq> {}" "S \<subseteq> p - {x}" "sum u S = 1" for x S u
  1.1959 +  proof (intro exI conjI)
  1.1960 +    have "x \<notin> S" 
  1.1961 +      using that by auto
  1.1962 +    then show "(\<Sum>v \<in> insert x S. if v = x then - 1 else u v) = 0"
  1.1963 +      using that by (simp add: sum_delta_notmem)
  1.1964 +    show "(\<Sum>w \<in> insert x S. (if w = x then - 1 else u w) *\<^sub>R w) = 0"
  1.1965 +      using that \<open>x \<notin> S\<close> by (simp add: if_smult sum_delta_notmem cong: if_cong)
  1.1966 +  qed (use that in auto)
  1.1967 +  moreover have "\<exists>x\<in>p. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p - {x} \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
  1.1968 +    if "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0" "finite S" "S \<subseteq> p" "sum u S = 0" "v \<in> S" "u v \<noteq> 0" for S u v
  1.1969 +  proof (intro bexI exI conjI)
  1.1970 +    have "S \<noteq> {v}"
  1.1971 +      using that by auto
  1.1972 +    then show "S - {v} \<noteq> {}"
  1.1973 +      using that by auto
  1.1974 +    show "(\<Sum>x \<in> S - {v}. - (1 / u v) * u x) = 1"
  1.1975 +      unfolding sum_distrib_left[symmetric] sum_diff1[OF \<open>finite S\<close>] by (simp add: that)
  1.1976 +    show "(\<Sum>x\<in>S - {v}. (- (1 / u v) * u x) *\<^sub>R x) = v"
  1.1977 +      unfolding sum_distrib_left [symmetric] scaleR_scaleR[symmetric]
  1.1978 +                scaleR_right.sum [symmetric] sum_diff1[OF \<open>finite S\<close>] 
  1.1979 +      using that by auto
  1.1980 +    show "S - {v} \<subseteq> p - {v}"
  1.1981 +      using that by auto
  1.1982 +  qed (use that in auto)
  1.1983 +  ultimately show ?thesis
  1.1984 +    unfolding affine_dependent_def affine_hull_explicit by auto
  1.1985 +qed
  1.1986 +
  1.1987 +lemma affine_dependent_explicit_finite:
  1.1988 +  fixes S :: "'a::real_vector set"
  1.1989 +  assumes "finite S"
  1.1990 +  shows "affine_dependent S \<longleftrightarrow>
  1.1991 +    (\<exists>u. sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
  1.1992 +  (is "?lhs = ?rhs")
  1.1993 +proof
  1.1994 +  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.1995 +    by auto
  1.1996 +  assume ?lhs
  1.1997 +  then obtain t u v where
  1.1998 +    "finite t" "t \<subseteq> S" "sum u t = 0" "v\<in>t" "u v \<noteq> 0"  "(\<Sum>v\<in>t. u v *\<^sub>R v) = 0"
  1.1999 +    unfolding affine_dependent_explicit by auto
  1.2000 +  then show ?rhs
  1.2001 +    apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
  1.2002 +    apply (auto simp: * sum.inter_restrict[OF assms, symmetric] Int_absorb1[OF \<open>t\<subseteq>S\<close>])
  1.2003 +    done
  1.2004 +next
  1.2005 +  assume ?rhs
  1.2006 +  then obtain u v where "sum u S = 0"  "v\<in>S" "u v \<noteq> 0" "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0"
  1.2007 +    by auto
  1.2008 +  then show ?lhs unfolding affine_dependent_explicit
  1.2009 +    using assms by auto
  1.2010 +qed
  1.2011 +
  1.2012 +
  1.2013 +subsection%unimportant \<open>Connectedness of convex sets\<close>
  1.2014 +
  1.2015 +lemma connectedD:
  1.2016 +  "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.2017 +  by (rule Topological_Spaces.topological_space_class.connectedD)
  1.2018 +
  1.2019 +lemma convex_connected:
  1.2020 +  fixes S :: "'a::real_normed_vector set"
  1.2021 +  assumes "convex S"
  1.2022 +  shows "connected S"
  1.2023 +proof (rule connectedI)
  1.2024 +  fix A B
  1.2025 +  assume "open A" "open B" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
  1.2026 +  moreover
  1.2027 +  assume "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}"
  1.2028 +  then obtain a b where a: "a \<in> A" "a \<in> S" and b: "b \<in> B" "b \<in> S" by auto
  1.2029 +  define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u
  1.2030 +  then have "continuous_on {0 .. 1} f"
  1.2031 +    by (auto intro!: continuous_intros)
  1.2032 +  then have "connected (f ` {0 .. 1})"
  1.2033 +    by (auto intro!: connected_continuous_image)
  1.2034 +  note connectedD[OF this, of A B]
  1.2035 +  moreover have "a \<in> A \<inter> f ` {0 .. 1}"
  1.2036 +    using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def)
  1.2037 +  moreover have "b \<in> B \<inter> f ` {0 .. 1}"
  1.2038 +    using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def)
  1.2039 +  moreover have "f ` {0 .. 1} \<subseteq> S"
  1.2040 +    using \<open>convex S\<close> a b unfolding convex_def f_def by auto
  1.2041 +  ultimately show False by auto
  1.2042 +qed
  1.2043 +
  1.2044 +corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
  1.2045 +  by (simp add: convex_connected)
  1.2046 +
  1.2047 +lemma convex_prod:
  1.2048 +  assumes "\<And>i. i \<in> Basis \<Longrightarrow> convex {x. P i x}"
  1.2049 +  shows "convex {x. \<forall>i\<in>Basis. P i (x\<bullet>i)}"
  1.2050 +  using assms unfolding convex_def
  1.2051 +  by (auto simp: inner_add_left)
  1.2052 +
  1.2053 +lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i)}"
  1.2054 +  by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval)
  1.2055 +
  1.2056 +subsection \<open>Convex hull\<close>
  1.2057 +
  1.2058 +lemma convex_convex_hull [iff]: "convex (convex hull s)"
  1.2059 +  unfolding hull_def
  1.2060 +  using convex_Inter[of "{t. convex t \<and> s \<subseteq> t}"]
  1.2061 +  by auto
  1.2062 +
  1.2063 +lemma convex_hull_subset:
  1.2064 +    "s \<subseteq> convex hull t \<Longrightarrow> convex hull s \<subseteq> convex hull t"
  1.2065 +  by (simp add: convex_convex_hull subset_hull)
  1.2066 +
  1.2067 +lemma convex_hull_eq: "convex hull s = s \<longleftrightarrow> convex s"
  1.2068 +  by (metis convex_convex_hull hull_same)
  1.2069 +
  1.2070 +subsubsection%unimportant \<open>Convex hull is "preserved" by a linear function\<close>
  1.2071 +
  1.2072 +lemma convex_hull_linear_image:
  1.2073 +  assumes f: "linear f"
  1.2074 +  shows "f ` (convex hull s) = convex hull (f ` s)"
  1.2075 +proof
  1.2076 +  show "convex hull (f ` s) \<subseteq> f ` (convex hull s)"
  1.2077 +    by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull)
  1.2078 +  show "f ` (convex hull s) \<subseteq> convex hull (f ` s)"
  1.2079 +  proof (unfold image_subset_iff_subset_vimage, rule hull_minimal)
  1.2080 +    show "s \<subseteq> f -` (convex hull (f ` s))"
  1.2081 +      by (fast intro: hull_inc)
  1.2082 +    show "convex (f -` (convex hull (f ` s)))"
  1.2083 +      by (intro convex_linear_vimage [OF f] convex_convex_hull)
  1.2084 +  qed
  1.2085 +qed
  1.2086 +
  1.2087 +lemma in_convex_hull_linear_image:
  1.2088 +  assumes "linear f"
  1.2089 +    and "x \<in> convex hull s"
  1.2090 +  shows "f x \<in> convex hull (f ` s)"
  1.2091 +  using convex_hull_linear_image[OF assms(1)] assms(2) by auto
  1.2092 +
  1.2093 +lemma convex_hull_Times:
  1.2094 +  "convex hull (s \<times> t) = (convex hull s) \<times> (convex hull t)"
  1.2095 +proof
  1.2096 +  show "convex hull (s \<times> t) \<subseteq> (convex hull s) \<times> (convex hull t)"
  1.2097 +    by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull)
  1.2098 +  have "(x, y) \<in> convex hull (s \<times> t)" if x: "x \<in> convex hull s" and y: "y \<in> convex hull t" for x y
  1.2099 +  proof (rule hull_induct [OF x], rule hull_induct [OF y])
  1.2100 +    fix x y assume "x \<in> s" and "y \<in> t"
  1.2101 +    then show "(x, y) \<in> convex hull (s \<times> t)"
  1.2102 +      by (simp add: hull_inc)
  1.2103 +  next
  1.2104 +    fix x let ?S = "((\<lambda>y. (0, y)) -` (\<lambda>p. (- x, 0) + p) ` (convex hull s \<times> t))"
  1.2105 +    have "convex ?S"
  1.2106 +      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  1.2107 +        simp add: linear_iff)
  1.2108 +    also have "?S = {y. (x, y) \<in> convex hull (s \<times> t)}"
  1.2109 +      by (auto simp: image_def Bex_def)
  1.2110 +    finally show "convex {y. (x, y) \<in> convex hull (s \<times> t)}" .
  1.2111 +  next
  1.2112 +    show "convex {x. (x, y) \<in> convex hull s \<times> t}"
  1.2113 +    proof -
  1.2114 +      fix y let ?S = "((\<lambda>x. (x, 0)) -` (\<lambda>p. (0, - y) + p) ` (convex hull s \<times> t))"
  1.2115 +      have "convex ?S"
  1.2116 +      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  1.2117 +        simp add: linear_iff)
  1.2118 +      also have "?S = {x. (x, y) \<in> convex hull (s \<times> t)}"
  1.2119 +        by (auto simp: image_def Bex_def)
  1.2120 +      finally show "convex {x. (x, y) \<in> convex hull (s \<times> t)}" .
  1.2121 +    qed
  1.2122 +  qed
  1.2123 +  then show "(convex hull s) \<times> (convex hull t) \<subseteq> convex hull (s \<times> t)"
  1.2124 +    unfolding subset_eq split_paired_Ball_Sigma by blast
  1.2125 +qed
  1.2126 +
  1.2127 +
  1.2128 +subsubsection%unimportant \<open>Stepping theorems for convex hulls of finite sets\<close>
  1.2129 +
  1.2130 +lemma convex_hull_empty[simp]: "convex hull {} = {}"
  1.2131 +  by (rule hull_unique) auto
  1.2132 +
  1.2133 +lemma convex_hull_singleton[simp]: "convex hull {a} = {a}"
  1.2134 +  by (rule hull_unique) auto
  1.2135 +
  1.2136 +lemma convex_hull_insert:
  1.2137 +  fixes S :: "'a::real_vector set"
  1.2138 +  assumes "S \<noteq> {}"
  1.2139 +  shows "convex hull (insert a S) =
  1.2140 +         {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.2141 +  (is "_ = ?hull")
  1.2142 +proof (intro equalityI hull_minimal subsetI)
  1.2143 +  fix x
  1.2144 +  assume "x \<in> insert a S"
  1.2145 +  then have "\<exists>u\<ge>0. \<exists>v\<ge>0. u + v = 1 \<and> (\<exists>b. b \<in> convex hull S \<and> x = u *\<^sub>R a + v *\<^sub>R b)"
  1.2146 +  unfolding insert_iff
  1.2147 +  proof
  1.2148 +    assume "x = a"
  1.2149 +    then show ?thesis
  1.2150 +      by (rule_tac x=1 in exI) (use assms hull_subset in fastforce)
  1.2151 +  next
  1.2152 +    assume "x \<in> S"
  1.2153 +    with hull_subset[of S convex] show ?thesis
  1.2154 +      by force
  1.2155 +  qed
  1.2156 +  then show "x \<in> ?hull"
  1.2157 +    by simp
  1.2158 +next
  1.2159 +  fix x
  1.2160 +  assume "x \<in> ?hull"
  1.2161 +  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.2162 +    by auto
  1.2163 +  have "a \<in> convex hull insert a S" "b \<in> convex hull insert a S"
  1.2164 +    using hull_mono[of S "insert a S" convex] hull_mono[of "{a}" "insert a S" convex] and obt(4)
  1.2165 +    by auto
  1.2166 +  then show "x \<in> convex hull insert a S"
  1.2167 +    unfolding obt(5) using obt(1-3)
  1.2168 +    by (rule convexD [OF convex_convex_hull])
  1.2169 +next
  1.2170 +  show "convex ?hull"
  1.2171 +  proof (rule convexI)
  1.2172 +    fix x y u v
  1.2173 +    assume as: "(0::real) \<le> u" "0 \<le> v" "u + v = 1" and x: "x \<in> ?hull" and y: "y \<in> ?hull"
  1.2174 +    from x obtain u1 v1 b1 where
  1.2175 +      obt1: "u1\<ge>0" "v1\<ge>0" "u1 + v1 = 1" "b1 \<in> convex hull S" and xeq: "x = u1 *\<^sub>R a + v1 *\<^sub>R b1"
  1.2176 +      by auto
  1.2177 +    from y obtain u2 v2 b2 where
  1.2178 +      obt2: "u2\<ge>0" "v2\<ge>0" "u2 + v2 = 1" "b2 \<in> convex hull S" and yeq: "y = u2 *\<^sub>R a + v2 *\<^sub>R b2"
  1.2179 +      by auto
  1.2180 +    have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  1.2181 +      by (auto simp: algebra_simps)
  1.2182 +    have "\<exists>b \<in> convex hull S. u *\<^sub>R x + v *\<^sub>R y =
  1.2183 +      (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)"
  1.2184 +    proof (cases "u * v1 + v * v2 = 0")
  1.2185 +      case True
  1.2186 +      have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  1.2187 +        by (auto simp: algebra_simps)
  1.2188 +      have eq0: "u * v1 = 0" "v * v2 = 0"
  1.2189 +        using True mult_nonneg_nonneg[OF \<open>u\<ge>0\<close> \<open>v1\<ge>0\<close>] mult_nonneg_nonneg[OF \<open>v\<ge>0\<close> \<open>v2\<ge>0\<close>]
  1.2190 +        by arith+
  1.2191 +      then have "u * u1 + v * u2 = 1"
  1.2192 +        using as(3) obt1(3) obt2(3) by auto
  1.2193 +      then show ?thesis
  1.2194 +        using "*" eq0 as obt1(4) xeq yeq by auto
  1.2195 +    next
  1.2196 +      case False
  1.2197 +      have "1 - (u * u1 + v * u2) = (u + v) - (u * u1 + v * u2)"
  1.2198 +        using as(3) obt1(3) obt2(3) by (auto simp: field_simps)
  1.2199 +      also have "\<dots> = u * (v1 + u1 - u1) + v * (v2 + u2 - u2)"
  1.2200 +        using as(3) obt1(3) obt2(3) by (auto simp: field_simps)
  1.2201 +      also have "\<dots> = u * v1 + v * v2"
  1.2202 +        by simp
  1.2203 +      finally have **:"1 - (u * u1 + v * u2) = u * v1 + v * v2" by auto
  1.2204 +      let ?b = "((u * v1) / (u * v1 + v * v2)) *\<^sub>R b1 + ((v * v2) / (u * v1 + v * v2)) *\<^sub>R b2"
  1.2205 +      have zeroes: "0 \<le> u * v1 + v * v2" "0 \<le> u * v1" "0 \<le> u * v1 + v * v2" "0 \<le> v * v2"
  1.2206 +        using as(1,2) obt1(1,2) obt2(1,2) by auto
  1.2207 +      show ?thesis
  1.2208 +      proof
  1.2209 +        show "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (?b - (u * u1) *\<^sub>R ?b - (v * u2) *\<^sub>R ?b)"
  1.2210 +          unfolding xeq yeq * **
  1.2211 +          using False by (auto simp: scaleR_left_distrib scaleR_right_distrib)
  1.2212 +        show "?b \<in> convex hull S"
  1.2213 +          using False zeroes obt1(4) obt2(4)
  1.2214 +          by (auto simp: convexD [OF convex_convex_hull] scaleR_left_distrib scaleR_right_distrib  add_divide_distrib[symmetric]  zero_le_divide_iff)
  1.2215 +      qed
  1.2216 +    qed
  1.2217 +    then obtain b where b: "b \<in> convex hull S" 
  1.2218 +       "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)" ..
  1.2219 +
  1.2220 +    have u1: "u1 \<le> 1"
  1.2221 +      unfolding obt1(3)[symmetric] and not_le using obt1(2) by auto
  1.2222 +    have u2: "u2 \<le> 1"
  1.2223 +      unfolding obt2(3)[symmetric] and not_le using obt2(2) by auto
  1.2224 +    have "u1 * u + u2 * v \<le> max u1 u2 * u + max u1 u2 * v"
  1.2225 +    proof (rule add_mono)
  1.2226 +      show "u1 * u \<le> max u1 u2 * u" "u2 * v \<le> max u1 u2 * v"
  1.2227 +        by (simp_all add: as mult_right_mono)
  1.2228 +    qed
  1.2229 +    also have "\<dots> \<le> 1"
  1.2230 +      unfolding distrib_left[symmetric] and as(3) using u1 u2 by auto
  1.2231 +    finally have le1: "u1 * u + u2 * v \<le> 1" .    
  1.2232 +    show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  1.2233 +    proof (intro CollectI exI conjI)
  1.2234 +      show "0 \<le> u * u1 + v * u2"
  1.2235 +        by (simp add: as(1) as(2) obt1(1) obt2(1))
  1.2236 +      show "0 \<le> 1 - u * u1 - v * u2"
  1.2237 +        by (simp add: le1 diff_diff_add mult.commute)
  1.2238 +    qed (use b in \<open>auto simp: algebra_simps\<close>)
  1.2239 +  qed
  1.2240 +qed
  1.2241 +
  1.2242 +lemma convex_hull_insert_alt:
  1.2243 +   "convex hull (insert a S) =
  1.2244 +     (if S = {} then {a}
  1.2245 +      else {(1 - u) *\<^sub>R a + u *\<^sub>R x |x u. 0 \<le> u \<and> u \<le> 1 \<and> x \<in> convex hull S})"
  1.2246 +  apply (auto simp: convex_hull_insert)
  1.2247 +  using diff_eq_eq apply fastforce
  1.2248 +  by (metis add.group_left_neutral add_le_imp_le_diff diff_add_cancel)
  1.2249 +
  1.2250 +subsubsection%unimportant \<open>Explicit expression for convex hull\<close>
  1.2251 +
  1.2252 +proposition convex_hull_indexed:
  1.2253 +  fixes S :: "'a::real_vector set"
  1.2254 +  shows "convex hull S =
  1.2255 +    {y. \<exists>k u x. (\<forall>i\<in>{1::nat .. k}. 0 \<le> u i \<and> x i \<in> S) \<and>
  1.2256 +                (sum u {1..k} = 1) \<and> (\<Sum>i = 1..k. u i *\<^sub>R x i) = y}"
  1.2257 +    (is "?xyz = ?hull")
  1.2258 +proof (rule hull_unique [OF _ convexI])
  1.2259 +  show "S \<subseteq> ?hull" 
  1.2260 +    by (clarsimp, rule_tac x=1 in exI, rule_tac x="\<lambda>x. 1" in exI, auto)
  1.2261 +next
  1.2262 +  fix T
  1.2263 +  assume "S \<subseteq> T" "convex T"
  1.2264 +  then show "?hull \<subseteq> T"
  1.2265 +    by (blast intro: convex_sum)
  1.2266 +next
  1.2267 +  fix x y u v
  1.2268 +  assume uv: "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
  1.2269 +  assume xy: "x \<in> ?hull" "y \<in> ?hull"
  1.2270 +  from xy obtain k1 u1 x1 where
  1.2271 +    x [rule_format]: "\<forall>i\<in>{1::nat..k1}. 0\<le>u1 i \<and> x1 i \<in> S" 
  1.2272 +                      "sum u1 {Suc 0..k1} = 1" "(\<Sum>i = Suc 0..k1. u1 i *\<^sub>R x1 i) = x"
  1.2273 +    by auto
  1.2274 +  from xy obtain k2 u2 x2 where
  1.2275 +    y [rule_format]: "\<forall>i\<in>{1::nat..k2}. 0\<le>u2 i \<and> x2 i \<in> S" 
  1.2276 +                     "sum u2 {Suc 0..k2} = 1" "(\<Sum>i = Suc 0..k2. u2 i *\<^sub>R x2 i) = y"
  1.2277 +    by auto
  1.2278 +  have *: "\<And>P (x::'a) y s t i. (if P i then s else t) *\<^sub>R (if P i then x else y) = (if P i then s *\<^sub>R x else t *\<^sub>R y)"
  1.2279 +          "{1..k1 + k2} \<inter> {1..k1} = {1..k1}" "{1..k1 + k2} \<inter> - {1..k1} = (\<lambda>i. i + k1) ` {1..k2}"
  1.2280 +    by auto
  1.2281 +  have inj: "inj_on (\<lambda>i. i + k1) {1..k2}"
  1.2282 +    unfolding inj_on_def by auto
  1.2283 +  let ?uu = "\<lambda>i. if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)"
  1.2284 +  let ?xx = "\<lambda>i. if i \<in> {1..k1} then x1 i else x2 (i - k1)"
  1.2285 +  show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  1.2286 +  proof (intro CollectI exI conjI ballI)
  1.2287 +    show "0 \<le> ?uu i" "?xx i \<in> S" if "i \<in> {1..k1+k2}" for i
  1.2288 +      using that by (auto simp add: le_diff_conv uv(1) x(1) uv(2) y(1))
  1.2289 +    show "(\<Sum>i = 1..k1 + k2. ?uu i) = 1"  "(\<Sum>i = 1..k1 + k2. ?uu i *\<^sub>R ?xx i) = u *\<^sub>R x + v *\<^sub>R y"
  1.2290 +      unfolding * sum.If_cases[OF finite_atLeastAtMost[of 1 "k1 + k2"]]
  1.2291 +        sum.reindex[OF inj] Collect_mem_eq o_def
  1.2292 +      unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] sum_distrib_left[symmetric]
  1.2293 +      by (simp_all add: sum_distrib_left[symmetric]  x(2,3) y(2,3) uv(3))
  1.2294 +  qed 
  1.2295 +qed
  1.2296 +
  1.2297 +lemma convex_hull_finite:
  1.2298 +  fixes S :: "'a::real_vector set"
  1.2299 +  assumes "finite S"
  1.2300 +  shows "convex hull S = {y. \<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y}"
  1.2301 +  (is "?HULL = _")
  1.2302 +proof (rule hull_unique [OF _ convexI]; clarify)
  1.2303 +  fix x
  1.2304 +  assume "x \<in> S"
  1.2305 +  then show "\<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> (\<Sum>x\<in>S. u x *\<^sub>R x) = x"
  1.2306 +    by (rule_tac x="\<lambda>y. if x=y then 1 else 0" in exI) (auto simp: sum.delta'[OF assms] sum_delta''[OF assms])
  1.2307 +next
  1.2308 +  fix u v :: real
  1.2309 +  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  1.2310 +  fix ux assume ux [rule_format]: "\<forall>x\<in>S. 0 \<le> ux x" "sum ux S = (1::real)"
  1.2311 +  fix uy assume uy [rule_format]: "\<forall>x\<in>S. 0 \<le> uy x" "sum uy S = (1::real)"
  1.2312 +  have "0 \<le> u * ux x + v * uy x" if "x\<in>S" for x
  1.2313 +    by (simp add: that uv ux(1) uy(1))
  1.2314 +  moreover
  1.2315 +  have "(\<Sum>x\<in>S. u * ux x + v * uy x) = 1"
  1.2316 +    unfolding sum.distrib and sum_distrib_left[symmetric] ux(2) uy(2)
  1.2317 +    using uv(3) by auto
  1.2318 +  moreover
  1.2319 +  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.2320 +    unfolding scaleR_left_distrib sum.distrib scaleR_scaleR[symmetric] scaleR_right.sum [symmetric]
  1.2321 +    by auto
  1.2322 +  ultimately
  1.2323 +  show "\<exists>uc. (\<forall>x\<in>S. 0 \<le> uc x) \<and> sum uc S = 1 \<and>
  1.2324 +             (\<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.2325 +    by (rule_tac x="\<lambda>x. u * ux x + v * uy x" in exI, auto)
  1.2326 +qed (use assms in \<open>auto simp: convex_explicit\<close>)
  1.2327 +
  1.2328 +
  1.2329 +subsubsection%unimportant \<open>Another formulation\<close>
  1.2330 +
  1.2331 +text "Formalized by Lars Schewe."
  1.2332 +
  1.2333 +lemma convex_hull_explicit:
  1.2334 +  fixes p :: "'a::real_vector set"
  1.2335 +  shows "convex hull p =
  1.2336 +    {y. \<exists>S u. finite S \<and> S \<subseteq> p \<and> (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1.2337 +  (is "?lhs = ?rhs")
  1.2338 +proof -
  1.2339 +  {
  1.2340 +    fix x
  1.2341 +    assume "x\<in>?lhs"
  1.2342 +    then obtain k u y where
  1.2343 +        obt: "\<forall>i\<in>{1::nat..k}. 0 \<le> u i \<and> y i \<in> p" "sum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R y i) = x"
  1.2344 +      unfolding convex_hull_indexed by auto
  1.2345 +
  1.2346 +    have fin: "finite {1..k}" by auto
  1.2347 +    have fin': "\<And>v. finite {i \<in> {1..k}. y i = v}" by auto
  1.2348 +    {
  1.2349 +      fix j
  1.2350 +      assume "j\<in>{1..k}"
  1.2351 +      then have "y j \<in> p" "0 \<le> sum u {i. Suc 0 \<le> i \<and> i \<le> k \<and> y i = y j}"
  1.2352 +        using obt(1)[THEN bspec[where x=j]] and obt(2)
  1.2353 +        apply simp
  1.2354 +        apply (rule sum_nonneg)
  1.2355 +        using obt(1)
  1.2356 +        apply auto
  1.2357 +        done
  1.2358 +    }
  1.2359 +    moreover
  1.2360 +    have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v}) = 1"
  1.2361 +      unfolding sum.image_gen[OF fin, symmetric] using obt(2) by auto
  1.2362 +    moreover have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v} *\<^sub>R v) = x"
  1.2363 +      using sum.image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
  1.2364 +      unfolding scaleR_left.sum using obt(3) by auto
  1.2365 +    ultimately
  1.2366 +    have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
  1.2367 +      apply (rule_tac x="y ` {1..k}" in exI)
  1.2368 +      apply (rule_tac x="\<lambda>v. sum u {i\<in>{1..k}. y i = v}" in exI, auto)
  1.2369 +      done
  1.2370 +    then have "x\<in>?rhs" by auto
  1.2371 +  }
  1.2372 +  moreover
  1.2373 +  {
  1.2374 +    fix y
  1.2375 +    assume "y\<in>?rhs"
  1.2376 +    then obtain S u where
  1.2377 +      obt: "finite S" "S \<subseteq> p" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = 1" "(\<Sum>v\<in>S. u v *\<^sub>R v) = y"
  1.2378 +      by auto
  1.2379 +
  1.2380 +    obtain f where f: "inj_on f {1..card S}" "f ` {1..card S} = S"
  1.2381 +      using ex_bij_betw_nat_finite_1[OF obt(1)] unfolding bij_betw_def by auto
  1.2382 +
  1.2383 +    {
  1.2384 +      fix i :: nat
  1.2385 +      assume "i\<in>{1..card S}"
  1.2386 +      then have "f i \<in> S"
  1.2387 +        using f(2) by blast
  1.2388 +      then have "0 \<le> u (f i)" "f i \<in> p" using obt(2,3) by auto
  1.2389 +    }
  1.2390 +    moreover have *: "finite {1..card S}" by auto
  1.2391 +    {
  1.2392 +      fix y
  1.2393 +      assume "y\<in>S"
  1.2394 +      then obtain i where "i\<in>{1..card S}" "f i = y"
  1.2395 +        using f using image_iff[of y f "{1..card S}"]
  1.2396 +        by auto
  1.2397 +      then have "{x. Suc 0 \<le> x \<and> x \<le> card S \<and> f x = y} = {i}"
  1.2398 +        apply auto
  1.2399 +        using f(1)[unfolded inj_on_def]
  1.2400 +        by (metis One_nat_def atLeastAtMost_iff)
  1.2401 +      then have "card {x. Suc 0 \<le> x \<and> x \<le> card S \<and> f x = y} = 1" by auto
  1.2402 +      then have "(\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x)) = u y"
  1.2403 +          "(\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x) *\<^sub>R f x) = u y *\<^sub>R y"
  1.2404 +        by (auto simp: sum_constant_scaleR)
  1.2405 +    }
  1.2406 +    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.2407 +      unfolding sum.image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
  1.2408 +        and sum.image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
  1.2409 +      unfolding f
  1.2410 +      using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x) *\<^sub>R f x)" "\<lambda>v. u v *\<^sub>R v"]
  1.2411 +      using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x))" u]
  1.2412 +      unfolding obt(4,5)
  1.2413 +      by auto
  1.2414 +    ultimately
  1.2415 +    have "\<exists>k u x. (\<forall>i\<in>{1..k}. 0 \<le> u i \<and> x i \<in> p) \<and> sum u {1..k} = 1 \<and>
  1.2416 +        (\<Sum>i::nat = 1..k. u i *\<^sub>R x i) = y"
  1.2417 +      apply (rule_tac x="card S" in exI)
  1.2418 +      apply (rule_tac x="u \<circ> f" in exI)
  1.2419 +      apply (rule_tac x=f in exI, fastforce)
  1.2420 +      done
  1.2421 +    then have "y \<in> ?lhs"
  1.2422 +      unfolding convex_hull_indexed by auto
  1.2423 +  }
  1.2424 +  ultimately show ?thesis
  1.2425 +    unfolding set_eq_iff by blast
  1.2426 +qed
  1.2427 +
  1.2428 +
  1.2429 +subsubsection%unimportant \<open>A stepping theorem for that expansion\<close>
  1.2430 +
  1.2431 +lemma convex_hull_finite_step:
  1.2432 +  fixes S :: "'a::real_vector set"
  1.2433 +  assumes "finite S"
  1.2434 +  shows
  1.2435 +    "(\<exists>u. (\<forall>x\<in>insert a S. 0 \<le> u x) \<and> sum u (insert a S) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a S) = y)
  1.2436 +      \<longleftrightarrow> (\<exists>v\<ge>0. \<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y - v *\<^sub>R a)"
  1.2437 +  (is "?lhs = ?rhs")
  1.2438 +proof (rule, case_tac[!] "a\<in>S")
  1.2439 +  assume "a \<in> S"
  1.2440 +  then have *: "insert a S = S" by auto
  1.2441 +  assume ?lhs
  1.2442 +  then show ?rhs
  1.2443 +    unfolding *  by (rule_tac x=0 in exI, auto)
  1.2444 +next
  1.2445 +  assume ?lhs
  1.2446 +  then obtain u where
  1.2447 +      u: "\<forall>x\<in>insert a S. 0 \<le> u x" "sum u (insert a S) = w" "(\<Sum>x\<in>insert a S. u x *\<^sub>R x) = y"
  1.2448 +    by auto
  1.2449 +  assume "a \<notin> S"
  1.2450 +  then show ?rhs
  1.2451 +    apply (rule_tac x="u a" in exI)
  1.2452 +    using u(1)[THEN bspec[where x=a]]
  1.2453 +    apply simp
  1.2454 +    apply (rule_tac x=u in exI)
  1.2455 +    using u[unfolded sum_clauses(2)[OF assms]] and \<open>a\<notin>S\<close>
  1.2456 +    apply auto
  1.2457 +    done
  1.2458 +next
  1.2459 +  assume "a \<in> S"
  1.2460 +  then have *: "insert a S = S" by auto
  1.2461 +  have fin: "finite (insert a S)" using assms by auto
  1.2462 +  assume ?rhs
  1.2463 +  then obtain v u where uv: "v\<ge>0" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = w - v" "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  1.2464 +    by auto
  1.2465 +  show ?lhs
  1.2466 +    apply (rule_tac x = "\<lambda>x. (if a = x then v else 0) + u x" in exI)
  1.2467 +    unfolding scaleR_left_distrib and sum.distrib and sum_delta''[OF fin] and sum.delta'[OF fin]
  1.2468 +    unfolding sum_clauses(2)[OF assms]
  1.2469 +    using uv and uv(2)[THEN bspec[where x=a]] and \<open>a\<in>S\<close>
  1.2470 +    apply auto
  1.2471 +    done
  1.2472 +next
  1.2473 +  assume ?rhs
  1.2474 +  then obtain v u where uv: "v\<ge>0" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = w - v" "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  1.2475 +    by auto
  1.2476 +  moreover assume "a \<notin> S"
  1.2477 +  moreover
  1.2478 +  have "(\<Sum>x\<in>S. if a = x then v else u x) = sum u S"  "(\<Sum>x\<in>S. (if a = x then v else u x) *\<^sub>R x) = (\<Sum>x\<in>S. u x *\<^sub>R x)"
  1.2479 +    using \<open>a \<notin> S\<close>
  1.2480 +    by (auto simp: intro!: sum.cong)
  1.2481 +  ultimately show ?lhs
  1.2482 +    by (rule_tac x="\<lambda>x. if a = x then v else u x" in exI) (auto simp: sum_clauses(2)[OF assms])
  1.2483 +qed
  1.2484 +
  1.2485 +
  1.2486 +subsubsection%unimportant \<open>Hence some special cases\<close>
  1.2487 +
  1.2488 +lemma convex_hull_2:
  1.2489 +  "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.2490 +proof -
  1.2491 +  have *: "\<And>u. (\<forall>x\<in>{a, b}. 0 \<le> u x) \<longleftrightarrow> 0 \<le> u a \<and> 0 \<le> u b"
  1.2492 +    by auto
  1.2493 +  have **: "finite {b}" by auto
  1.2494 +  show ?thesis
  1.2495 +    apply (simp add: convex_hull_finite)
  1.2496 +    unfolding convex_hull_finite_step[OF **, of a 1, unfolded * conj_assoc]
  1.2497 +    apply auto
  1.2498 +    apply (rule_tac x=v in exI)
  1.2499 +    apply (rule_tac x="1 - v" in exI, simp)
  1.2500 +    apply (rule_tac x=u in exI, simp)
  1.2501 +    apply (rule_tac x="\<lambda>x. v" in exI, simp)
  1.2502 +    done
  1.2503 +qed
  1.2504 +
  1.2505 +lemma convex_hull_2_alt: "convex hull {a,b} = {a + u *\<^sub>R (b - a) | u.  0 \<le> u \<and> u \<le> 1}"
  1.2506 +  unfolding convex_hull_2
  1.2507 +proof (rule Collect_cong)
  1.2508 +  have *: "\<And>x y ::real. x + y = 1 \<longleftrightarrow> x = 1 - y"
  1.2509 +    by auto
  1.2510 +  fix x
  1.2511 +  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.2512 +    (\<exists>u. x = a + u *\<^sub>R (b - a) \<and> 0 \<le> u \<and> u \<le> 1)"
  1.2513 +    unfolding *
  1.2514 +    apply auto
  1.2515 +    apply (rule_tac[!] x=u in exI)
  1.2516 +    apply (auto simp: algebra_simps)
  1.2517 +    done
  1.2518 +qed
  1.2519 +
  1.2520 +lemma convex_hull_3:
  1.2521 +  "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.2522 +proof -
  1.2523 +  have fin: "finite {a,b,c}" "finite {b,c}" "finite {c}"
  1.2524 +    by auto
  1.2525 +  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  1.2526 +    by (auto simp: field_simps)
  1.2527 +  show ?thesis
  1.2528 +    unfolding convex_hull_finite[OF fin(1)] and convex_hull_finite_step[OF fin(2)] and *
  1.2529 +    unfolding convex_hull_finite_step[OF fin(3)]
  1.2530 +    apply (rule Collect_cong, simp)
  1.2531 +    apply auto
  1.2532 +    apply (rule_tac x=va in exI)
  1.2533 +    apply (rule_tac x="u c" in exI, simp)
  1.2534 +    apply (rule_tac x="1 - v - w" in exI, simp)
  1.2535 +    apply (rule_tac x=v in exI, simp)
  1.2536 +    apply (rule_tac x="\<lambda>x. w" in exI, simp)
  1.2537 +    done
  1.2538 +qed
  1.2539 +
  1.2540 +lemma convex_hull_3_alt:
  1.2541 +  "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.2542 +proof -
  1.2543 +  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  1.2544 +    by auto
  1.2545 +  show ?thesis
  1.2546 +    unfolding convex_hull_3
  1.2547 +    apply (auto simp: *)
  1.2548 +    apply (rule_tac x=v in exI)
  1.2549 +    apply (rule_tac x=w in exI)
  1.2550 +    apply (simp add: algebra_simps)
  1.2551 +    apply (rule_tac x=u in exI)
  1.2552 +    apply (rule_tac x=v in exI)
  1.2553 +    apply (simp add: algebra_simps)
  1.2554 +    done
  1.2555 +qed
  1.2556 +
  1.2557 +
  1.2558 +subsection%unimportant \<open>Relations among closure notions and corresponding hulls\<close>
  1.2559 +
  1.2560 +lemma affine_imp_convex: "affine s \<Longrightarrow> convex s"
  1.2561 +  unfolding affine_def convex_def by auto
  1.2562 +
  1.2563 +lemma convex_affine_hull [simp]: "convex (affine hull S)"
  1.2564 +  by (simp add: affine_imp_convex)
  1.2565 +
  1.2566 +lemma subspace_imp_convex: "subspace s \<Longrightarrow> convex s"
  1.2567 +  using subspace_imp_affine affine_imp_convex by auto
  1.2568 +
  1.2569 +lemma affine_hull_subset_span: "(affine hull s) \<subseteq> (span s)"
  1.2570 +  by (metis hull_minimal span_superset subspace_imp_affine subspace_span)
  1.2571 +
  1.2572 +lemma convex_hull_subset_span: "(convex hull s) \<subseteq> (span s)"
  1.2573 +  by (metis hull_minimal span_superset subspace_imp_convex subspace_span)
  1.2574 +
  1.2575 +lemma convex_hull_subset_affine_hull: "(convex hull s) \<subseteq> (affine hull s)"
  1.2576 +  by (metis affine_affine_hull affine_imp_convex hull_minimal hull_subset)
  1.2577 +
  1.2578 +lemma affine_dependent_imp_dependent: "affine_dependent s \<Longrightarrow> dependent s"
  1.2579 +  unfolding affine_dependent_def dependent_def
  1.2580 +  using affine_hull_subset_span by auto
  1.2581 +
  1.2582 +lemma dependent_imp_affine_dependent:
  1.2583 +  assumes "dependent {x - a| x . x \<in> s}"
  1.2584 +    and "a \<notin> s"
  1.2585 +  shows "affine_dependent (insert a s)"
  1.2586 +proof -
  1.2587 +  from assms(1)[unfolded dependent_explicit] obtain S u v
  1.2588 +    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.2589 +    by auto
  1.2590 +  define t where "t = (\<lambda>x. x + a) ` S"
  1.2591 +
  1.2592 +  have inj: "inj_on (\<lambda>x. x + a) S"
  1.2593 +    unfolding inj_on_def by auto
  1.2594 +  have "0 \<notin> S"
  1.2595 +    using obt(2) assms(2) unfolding subset_eq by auto
  1.2596 +  have fin: "finite t" and "t \<subseteq> s"
  1.2597 +    unfolding t_def using obt(1,2) by auto
  1.2598 +  then have "finite (insert a t)" and "insert a t \<subseteq> insert a s"
  1.2599 +    by auto
  1.2600 +  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.2601 +    apply (rule sum.cong)
  1.2602 +    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2603 +    apply auto
  1.2604 +    done
  1.2605 +  have "(\<Sum>x\<in>insert a t. if x = a then - (\<Sum>x\<in>t. u (x - a)) else u (x - a)) = 0"
  1.2606 +    unfolding sum_clauses(2)[OF fin] * using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close> by auto
  1.2607 +  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.2608 +    using obt(3,4) \<open>0\<notin>S\<close>
  1.2609 +    by (rule_tac x="v + a" in bexI) (auto simp: t_def)
  1.2610 +  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.2611 +    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close> by (auto intro!: sum.cong)
  1.2612 +  have "(\<Sum>x\<in>t. u (x - a)) *\<^sub>R a = (\<Sum>v\<in>t. u (v - a) *\<^sub>R v)"
  1.2613 +    unfolding scaleR_left.sum
  1.2614 +    unfolding t_def and sum.reindex[OF inj] and o_def
  1.2615 +    using obt(5)
  1.2616 +    by (auto simp: sum.distrib scaleR_right_distrib)
  1.2617 +  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.2618 +    unfolding sum_clauses(2)[OF fin]
  1.2619 +    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  1.2620 +    by (auto simp: *)
  1.2621 +  ultimately show ?thesis
  1.2622 +    unfolding affine_dependent_explicit
  1.2623 +    apply (rule_tac x="insert a t" in exI, auto)
  1.2624 +    done
  1.2625 +qed
  1.2626 +
  1.2627 +lemma convex_cone:
  1.2628 +  "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.2629 +  (is "?lhs = ?rhs")
  1.2630 +proof -
  1.2631 +  {
  1.2632 +    fix x y
  1.2633 +    assume "x\<in>s" "y\<in>s" and ?lhs
  1.2634 +    then have "2 *\<^sub>R x \<in>s" "2 *\<^sub>R y \<in> s"
  1.2635 +      unfolding cone_def by auto
  1.2636 +    then have "x + y \<in> s"
  1.2637 +      using \<open>?lhs\<close>[unfolded convex_def, THEN conjunct1]
  1.2638 +      apply (erule_tac x="2*\<^sub>R x" in ballE)
  1.2639 +      apply (erule_tac x="2*\<^sub>R y" in ballE)
  1.2640 +      apply (erule_tac x="1/2" in allE, simp)
  1.2641 +      apply (erule_tac x="1/2" in allE, auto)
  1.2642 +      done
  1.2643 +  }
  1.2644 +  then show ?thesis
  1.2645 +    unfolding convex_def cone_def by blast
  1.2646 +qed
  1.2647 +
  1.2648 +lemma affine_dependent_biggerset:
  1.2649 +  fixes s :: "'a::euclidean_space set"
  1.2650 +  assumes "finite s" "card s \<ge> DIM('a) + 2"
  1.2651 +  shows "affine_dependent s"
  1.2652 +proof -
  1.2653 +  have "s \<noteq> {}" using assms by auto
  1.2654 +  then obtain a where "a\<in>s" by auto
  1.2655 +  have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  1.2656 +    by auto
  1.2657 +  have "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  1.2658 +    unfolding * by (simp add: card_image inj_on_def)
  1.2659 +  also have "\<dots> > DIM('a)" using assms(2)
  1.2660 +    unfolding card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>] by auto
  1.2661 +  finally show ?thesis
  1.2662 +    apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  1.2663 +    apply (rule dependent_imp_affine_dependent)
  1.2664 +    apply (rule dependent_biggerset, auto)
  1.2665 +    done
  1.2666 +qed
  1.2667 +
  1.2668 +lemma affine_dependent_biggerset_general:
  1.2669 +  assumes "finite (S :: 'a::euclidean_space set)"
  1.2670 +    and "card S \<ge> dim S + 2"
  1.2671 +  shows "affine_dependent S"
  1.2672 +proof -
  1.2673 +  from assms(2) have "S \<noteq> {}" by auto
  1.2674 +  then obtain a where "a\<in>S" by auto
  1.2675 +  have *: "{x - a |x. x \<in> S - {a}} = (\<lambda>x. x - a) ` (S - {a})"
  1.2676 +    by auto
  1.2677 +  have **: "card {x - a |x. x \<in> S - {a}} = card (S - {a})"
  1.2678 +    by (metis (no_types, lifting) "*" card_image diff_add_cancel inj_on_def)
  1.2679 +  have "dim {x - a |x. x \<in> S - {a}} \<le> dim S"
  1.2680 +    using \<open>a\<in>S\<close> by (auto simp: span_base span_diff intro: subset_le_dim)
  1.2681 +  also have "\<dots> < dim S + 1" by auto
  1.2682 +  also have "\<dots> \<le> card (S - {a})"
  1.2683 +    using assms
  1.2684 +    using card_Diff_singleton[OF assms(1) \<open>a\<in>S\<close>]
  1.2685 +    by auto
  1.2686 +  finally show ?thesis
  1.2687 +    apply (subst insert_Diff[OF \<open>a\<in>S\<close>, symmetric])
  1.2688 +    apply (rule dependent_imp_affine_dependent)
  1.2689 +    apply (rule dependent_biggerset_general)
  1.2690 +    unfolding **
  1.2691 +    apply auto
  1.2692 +    done
  1.2693 +qed
  1.2694 +
  1.2695 +
  1.2696 +subsection%unimportant \<open>Some Properties of Affine Dependent Sets\<close>
  1.2697 +
  1.2698 +lemma affine_independent_0 [simp]: "\<not> affine_dependent {}"
  1.2699 +  by (simp add: affine_dependent_def)
  1.2700 +
  1.2701 +lemma affine_independent_1 [simp]: "\<not> affine_dependent {a}"
  1.2702 +  by (simp add: affine_dependent_def)
  1.2703 +
  1.2704 +lemma affine_independent_2 [simp]: "\<not> affine_dependent {a,b}"
  1.2705 +  by (simp add: affine_dependent_def insert_Diff_if hull_same)
  1.2706 +
  1.2707 +lemma affine_hull_translation: "affine hull ((\<lambda>x. a + x) `  S) = (\<lambda>x. a + x) ` (affine hull S)"
  1.2708 +proof -
  1.2709 +  have "affine ((\<lambda>x. a + x) ` (affine hull S))"
  1.2710 +    using affine_translation affine_affine_hull by blast
  1.2711 +  moreover have "(\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  1.2712 +    using hull_subset[of S] by auto
  1.2713 +  ultimately have h1: "affine hull ((\<lambda>x. a + x) `  S) \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  1.2714 +    by (metis hull_minimal)
  1.2715 +  have "affine((\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)))"
  1.2716 +    using affine_translation affine_affine_hull by blast
  1.2717 +  moreover have "(\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S))"
  1.2718 +    using hull_subset[of "(\<lambda>x. a + x) `  S"] by auto
  1.2719 +  moreover have "S = (\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S"
  1.2720 +    using translation_assoc[of "-a" a] by auto
  1.2721 +  ultimately have "(\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)) >= (affine hull S)"
  1.2722 +    by (metis hull_minimal)
  1.2723 +  then have "affine hull ((\<lambda>x. a + x) ` S) >= (\<lambda>x. a + x) ` (affine hull S)"
  1.2724 +    by auto
  1.2725 +  then show ?thesis using h1 by auto
  1.2726 +qed
  1.2727 +
  1.2728 +lemma affine_dependent_translation:
  1.2729 +  assumes "affine_dependent S"
  1.2730 +  shows "affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2731 +proof -
  1.2732 +  obtain x where x: "x \<in> S \<and> x \<in> affine hull (S - {x})"
  1.2733 +    using assms affine_dependent_def by auto
  1.2734 +  have "(+) a ` (S - {x}) = (+) a ` S - {a + x}"
  1.2735 +    by auto
  1.2736 +  then have "a + x \<in> affine hull ((\<lambda>x. a + x) ` S - {a + x})"
  1.2737 +    using affine_hull_translation[of a "S - {x}"] x by auto
  1.2738 +  moreover have "a + x \<in> (\<lambda>x. a + x) ` S"
  1.2739 +    using x by auto
  1.2740 +  ultimately show ?thesis
  1.2741 +    unfolding affine_dependent_def by auto
  1.2742 +qed
  1.2743 +
  1.2744 +lemma affine_dependent_translation_eq:
  1.2745 +  "affine_dependent S \<longleftrightarrow> affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2746 +proof -
  1.2747 +  {
  1.2748 +    assume "affine_dependent ((\<lambda>x. a + x) ` S)"
  1.2749 +    then have "affine_dependent S"
  1.2750 +      using affine_dependent_translation[of "((\<lambda>x. a + x) ` S)" "-a"] translation_assoc[of "-a" a]
  1.2751 +      by auto
  1.2752 +  }
  1.2753 +  then show ?thesis
  1.2754 +    using affine_dependent_translation by auto
  1.2755 +qed
  1.2756 +
  1.2757 +lemma affine_hull_0_dependent:
  1.2758 +  assumes "0 \<in> affine hull S"
  1.2759 +  shows "dependent S"
  1.2760 +proof -
  1.2761 +  obtain s u where s_u: "finite s \<and> s \<noteq> {} \<and> s \<subseteq> S \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  1.2762 +    using assms affine_hull_explicit[of S] by auto
  1.2763 +  then have "\<exists>v\<in>s. u v \<noteq> 0"
  1.2764 +    using sum_not_0[of "u" "s"] by auto
  1.2765 +  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.2766 +    using s_u by auto
  1.2767 +  then show ?thesis
  1.2768 +    unfolding dependent_explicit[of S] by auto
  1.2769 +qed
  1.2770 +
  1.2771 +lemma affine_dependent_imp_dependent2:
  1.2772 +  assumes "affine_dependent (insert 0 S)"
  1.2773 +  shows "dependent S"
  1.2774 +proof -
  1.2775 +  obtain x where x: "x \<in> insert 0 S \<and> x \<in> affine hull (insert 0 S - {x})"
  1.2776 +    using affine_dependent_def[of "(insert 0 S)"] assms by blast
  1.2777 +  then have "x \<in> span (insert 0 S - {x})"
  1.2778 +    using affine_hull_subset_span by auto
  1.2779 +  moreover have "span (insert 0 S - {x}) = span (S - {x})"
  1.2780 +    using insert_Diff_if[of "0" S "{x}"] span_insert_0[of "S-{x}"] by auto
  1.2781 +  ultimately have "x \<in> span (S - {x})" by auto
  1.2782 +  then have "x \<noteq> 0 \<Longrightarrow> dependent S"
  1.2783 +    using x dependent_def by auto
  1.2784 +  moreover
  1.2785 +  {
  1.2786 +    assume "x = 0"
  1.2787 +    then have "0 \<in> affine hull S"
  1.2788 +      using x hull_mono[of "S - {0}" S] by auto
  1.2789 +    then have "dependent S"
  1.2790 +      using affine_hull_0_dependent by auto
  1.2791 +  }
  1.2792 +  ultimately show ?thesis by auto
  1.2793 +qed
  1.2794 +
  1.2795 +lemma affine_dependent_iff_dependent:
  1.2796 +  assumes "a \<notin> S"
  1.2797 +  shows "affine_dependent (insert a S) \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` S)"
  1.2798 +proof -
  1.2799 +  have "((+) (- a) ` S) = {x - a| x . x \<in> S}" by auto
  1.2800 +  then show ?thesis
  1.2801 +    using affine_dependent_translation_eq[of "(insert a S)" "-a"]
  1.2802 +      affine_dependent_imp_dependent2 assms
  1.2803 +      dependent_imp_affine_dependent[of a S]
  1.2804 +    by (auto simp del: uminus_add_conv_diff)
  1.2805 +qed
  1.2806 +
  1.2807 +lemma affine_dependent_iff_dependent2:
  1.2808 +  assumes "a \<in> S"
  1.2809 +  shows "affine_dependent S \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` (S-{a}))"
  1.2810 +proof -
  1.2811 +  have "insert a (S - {a}) = S"
  1.2812 +    using assms by auto
  1.2813 +  then show ?thesis
  1.2814 +    using assms affine_dependent_iff_dependent[of a "S-{a}"] by auto
  1.2815 +qed
  1.2816 +
  1.2817 +lemma affine_hull_insert_span_gen:
  1.2818 +  "affine hull (insert a s) = (\<lambda>x. a + x) ` span ((\<lambda>x. - a + x) ` s)"
  1.2819 +proof -
  1.2820 +  have h1: "{x - a |x. x \<in> s} = ((\<lambda>x. -a+x) ` s)"
  1.2821 +    by auto
  1.2822 +  {
  1.2823 +    assume "a \<notin> s"
  1.2824 +    then have ?thesis
  1.2825 +      using affine_hull_insert_span[of a s] h1 by auto
  1.2826 +  }
  1.2827 +  moreover
  1.2828 +  {
  1.2829 +    assume a1: "a \<in> s"
  1.2830 +    have "\<exists>x. x \<in> s \<and> -a+x=0"
  1.2831 +      apply (rule exI[of _ a])
  1.2832 +      using a1
  1.2833 +      apply auto
  1.2834 +      done
  1.2835 +    then have "insert 0 ((\<lambda>x. -a+x) ` (s - {a})) = (\<lambda>x. -a+x) ` s"
  1.2836 +      by auto
  1.2837 +    then have "span ((\<lambda>x. -a+x) ` (s - {a}))=span ((\<lambda>x. -a+x) ` s)"
  1.2838 +      using span_insert_0[of "(+) (- a) ` (s - {a})"] by (auto simp del: uminus_add_conv_diff)
  1.2839 +    moreover have "{x - a |x. x \<in> (s - {a})} = ((\<lambda>x. -a+x) ` (s - {a}))"
  1.2840 +      by auto
  1.2841 +    moreover have "insert a (s - {a}) = insert a s"
  1.2842 +      by auto
  1.2843 +    ultimately have ?thesis
  1.2844 +      using affine_hull_insert_span[of "a" "s-{a}"] by auto
  1.2845 +  }
  1.2846 +  ultimately show ?thesis by auto
  1.2847 +qed
  1.2848 +
  1.2849 +lemma affine_hull_span2:
  1.2850 +  assumes "a \<in> s"
  1.2851 +  shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` (s-{a}))"
  1.2852 +  using affine_hull_insert_span_gen[of a "s - {a}", unfolded insert_Diff[OF assms]]
  1.2853 +  by auto
  1.2854 +
  1.2855 +lemma affine_hull_span_gen:
  1.2856 +  assumes "a \<in> affine hull s"
  1.2857 +  shows "affine hull s = (\<lambda>x. a+x) ` span ((\<lambda>x. -a+x) ` s)"
  1.2858 +proof -
  1.2859 +  have "affine hull (insert a s) = affine hull s"
  1.2860 +    using hull_redundant[of a affine s] assms by auto
  1.2861 +  then show ?thesis
  1.2862 +    using affine_hull_insert_span_gen[of a "s"] by auto
  1.2863 +qed
  1.2864 +
  1.2865 +lemma affine_hull_span_0:
  1.2866 +  assumes "0 \<in> affine hull S"
  1.2867 +  shows "affine hull S = span S"
  1.2868 +  using affine_hull_span_gen[of "0" S] assms by auto
  1.2869 +
  1.2870 +lemma extend_to_affine_basis_nonempty:
  1.2871 +  fixes S V :: "'n::euclidean_space set"
  1.2872 +  assumes "\<not> affine_dependent S" "S \<subseteq> V" "S \<noteq> {}"
  1.2873 +  shows "\<exists>T. \<not> affine_dependent T \<and> S \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  1.2874 +proof -
  1.2875 +  obtain a where a: "a \<in> S"
  1.2876 +    using assms by auto
  1.2877 +  then have h0: "independent  ((\<lambda>x. -a + x) ` (S-{a}))"
  1.2878 +    using affine_dependent_iff_dependent2 assms by auto
  1.2879 +  obtain B where B:
  1.2880 +    "(\<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.2881 +    using assms
  1.2882 +    by (blast intro: maximal_independent_subset_extend[OF _ h0, of "(\<lambda>x. -a + x) ` V"])
  1.2883 +  define T where "T = (\<lambda>x. a+x) ` insert 0 B"
  1.2884 +  then have "T = insert a ((\<lambda>x. a+x) ` B)"
  1.2885 +    by auto
  1.2886 +  then have "affine hull T = (\<lambda>x. a+x) ` span B"
  1.2887 +    using affine_hull_insert_span_gen[of a "((\<lambda>x. a+x) ` B)"] translation_assoc[of "-a" a B]
  1.2888 +    by auto
  1.2889 +  then have "V \<subseteq> affine hull T"
  1.2890 +    using B assms translation_inverse_subset[of a V "span B"]
  1.2891 +    by auto
  1.2892 +  moreover have "T \<subseteq> V"
  1.2893 +    using T_def B a assms by auto
  1.2894 +  ultimately have "affine hull T = affine hull V"
  1.2895 +    by (metis Int_absorb1 Int_absorb2 hull_hull hull_mono)
  1.2896 +  moreover have "S \<subseteq> T"
  1.2897 +    using T_def B translation_inverse_subset[of a "S-{a}" B]
  1.2898 +    by auto
  1.2899 +  moreover have "\<not> affine_dependent T"
  1.2900 +    using T_def affine_dependent_translation_eq[of "insert 0 B"]
  1.2901 +      affine_dependent_imp_dependent2 B
  1.2902 +    by auto
  1.2903 +  ultimately show ?thesis using \<open>T \<subseteq> V\<close> by auto
  1.2904 +qed
  1.2905 +
  1.2906 +lemma affine_basis_exists:
  1.2907 +  fixes V :: "'n::euclidean_space set"
  1.2908 +  shows "\<exists>B. B \<subseteq> V \<and> \<not> affine_dependent B \<and> affine hull V = affine hull B"
  1.2909 +proof (cases "V = {}")
  1.2910 +  case True
  1.2911 +  then show ?thesis
  1.2912 +    using affine_independent_0 by auto
  1.2913 +next
  1.2914 +  case False
  1.2915 +  then obtain x where "x \<in> V" by auto
  1.2916 +  then show ?thesis
  1.2917 +    using affine_dependent_def[of "{x}"] extend_to_affine_basis_nonempty[of "{x}" V]
  1.2918 +    by auto
  1.2919 +qed
  1.2920 +
  1.2921 +proposition extend_to_affine_basis:
  1.2922 +  fixes S V :: "'n::euclidean_space set"
  1.2923 +  assumes "\<not> affine_dependent S" "S \<subseteq> V"
  1.2924 +  obtains T where "\<not> affine_dependent T" "S \<subseteq> T" "T \<subseteq> V" "affine hull T = affine hull V"
  1.2925 +proof (cases "S = {}")
  1.2926 +  case True then show ?thesis
  1.2927 +    using affine_basis_exists by (metis empty_subsetI that)
  1.2928 +next
  1.2929 +  case False
  1.2930 +  then show ?thesis by (metis assms extend_to_affine_basis_nonempty that)
  1.2931 +qed
  1.2932 +
  1.2933 +subsection \<open>Affine Dimension of a Set\<close>
  1.2934 +
  1.2935 +definition%important aff_dim :: "('a::euclidean_space) set \<Rightarrow> int"
  1.2936 +  where "aff_dim V =
  1.2937 +  (SOME d :: int.
  1.2938 +    \<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = d + 1)"
  1.2939 +
  1.2940 +lemma aff_dim_basis_exists:
  1.2941 +  fixes V :: "('n::euclidean_space) set"
  1.2942 +  shows "\<exists>B. affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  1.2943 +proof -
  1.2944 +  obtain B where "\<not> affine_dependent B \<and> affine hull B = affine hull V"
  1.2945 +    using affine_basis_exists[of V] by auto
  1.2946 +  then show ?thesis
  1.2947 +    unfolding aff_dim_def
  1.2948 +      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.2949 +    apply auto
  1.2950 +    apply (rule exI[of _ "int (card B) - (1 :: int)"])
  1.2951 +    apply (rule exI[of _ "B"], auto)
  1.2952 +    done
  1.2953 +qed
  1.2954 +
  1.2955 +lemma affine_hull_nonempty: "S \<noteq> {} \<longleftrightarrow> affine hull S \<noteq> {}"
  1.2956 +proof -
  1.2957 +  have "S = {} \<Longrightarrow> affine hull S = {}"
  1.2958 +    using affine_hull_empty by auto
  1.2959 +  moreover have "affine hull S = {} \<Longrightarrow> S = {}"
  1.2960 +    unfolding hull_def by auto
  1.2961 +  ultimately show ?thesis by blast
  1.2962 +qed
  1.2963 +
  1.2964 +lemma aff_dim_parallel_subspace_aux:
  1.2965 +  fixes B :: "'n::euclidean_space set"
  1.2966 +  assumes "\<not> affine_dependent B" "a \<in> B"
  1.2967 +  shows "finite B \<and> ((card B) - 1 = dim (span ((\<lambda>x. -a+x) ` (B-{a}))))"
  1.2968 +proof -
  1.2969 +  have "independent ((\<lambda>x. -a + x) ` (B-{a}))"
  1.2970 +    using affine_dependent_iff_dependent2 assms by auto
  1.2971 +  then have fin: "dim (span ((\<lambda>x. -a+x) ` (B-{a}))) = card ((\<lambda>x. -a + x) ` (B-{a}))"
  1.2972 +    "finite ((\<lambda>x. -a + x) ` (B - {a}))"
  1.2973 +    using indep_card_eq_dim_span[of "(\<lambda>x. -a+x) ` (B-{a})"] by auto
  1.2974 +  show ?thesis
  1.2975 +  proof (cases "(\<lambda>x. -a + x) ` (B - {a}) = {}")
  1.2976 +    case True
  1.2977 +    have "B = insert a ((\<lambda>x. a + x) ` (\<lambda>x. -a + x) ` (B - {a}))"
  1.2978 +      using translation_assoc[of "a" "-a" "(B - {a})"] assms by auto
  1.2979 +    then have "B = {a}" using True by auto
  1.2980 +    then show ?thesis using assms fin by auto
  1.2981 +  next
  1.2982 +    case False
  1.2983 +    then have "card ((\<lambda>x. -a + x) ` (B - {a})) > 0"
  1.2984 +      using fin by auto
  1.2985 +    moreover have h1: "card ((\<lambda>x. -a + x) ` (B-{a})) = card (B-{a})"
  1.2986 +      by (rule card_image) (use translate_inj_on in blast)
  1.2987 +    ultimately have "card (B-{a}) > 0" by auto
  1.2988 +    then have *: "finite (B - {a})"
  1.2989 +      using card_gt_0_iff[of "(B - {a})"] by auto
  1.2990 +    then have "card (B - {a}) = card B - 1"
  1.2991 +      using card_Diff_singleton assms by auto
  1.2992 +    with * show ?thesis using fin h1 by auto
  1.2993 +  qed
  1.2994 +qed
  1.2995 +
  1.2996 +lemma aff_dim_parallel_subspace:
  1.2997 +  fixes V L :: "'n::euclidean_space set"
  1.2998 +  assumes "V \<noteq> {}"
  1.2999 +    and "subspace L"
  1.3000 +    and "affine_parallel (affine hull V) L"
  1.3001 +  shows "aff_dim V = int (dim L)"
  1.3002 +proof -
  1.3003 +  obtain B where
  1.3004 +    B: "affine hull B = affine hull V \<and> \<not> affine_dependent B \<and> int (card B) = aff_dim V + 1"
  1.3005 +    using aff_dim_basis_exists by auto
  1.3006 +  then have "B \<noteq> {}"
  1.3007 +    using assms B affine_hull_nonempty[of V] affine_hull_nonempty[of B]
  1.3008 +    by auto
  1.3009 +  then obtain a where a: "a \<in> B" by auto
  1.3010 +  define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  1.3011 +  moreover have "affine_parallel (affine hull B) Lb"
  1.3012 +    using Lb_def B assms affine_hull_span2[of a B] a
  1.3013 +      affine_parallel_commut[of "Lb" "(affine hull B)"]
  1.3014 +    unfolding affine_parallel_def
  1.3015 +    by auto
  1.3016 +  moreover have "subspace Lb"
  1.3017 +    using Lb_def subspace_span by auto
  1.3018 +  moreover have "affine hull B \<noteq> {}"
  1.3019 +    using assms B affine_hull_nonempty[of V] by auto
  1.3020 +  ultimately have "L = Lb"
  1.3021 +    using assms affine_parallel_subspace[of "affine hull B"] affine_affine_hull[of B] B
  1.3022 +    by auto
  1.3023 +  then have "dim L = dim Lb"
  1.3024 +    by auto
  1.3025 +  moreover have "card B - 1 = dim Lb" and "finite B"
  1.3026 +    using Lb_def aff_dim_parallel_subspace_aux a B by auto
  1.3027 +  ultimately show ?thesis
  1.3028 +    using B \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  1.3029 +qed
  1.3030 +
  1.3031 +lemma aff_independent_finite:
  1.3032 +  fixes B :: "'n::euclidean_space set"
  1.3033 +  assumes "\<not> affine_dependent B"
  1.3034 +  shows "finite B"
  1.3035 +proof -
  1.3036 +  {
  1.3037 +    assume "B \<noteq> {}"
  1.3038 +    then obtain a where "a \<in> B" by auto
  1.3039 +    then have ?thesis
  1.3040 +      using aff_dim_parallel_subspace_aux assms by auto
  1.3041 +  }
  1.3042 +  then show ?thesis by auto
  1.3043 +qed
  1.3044 +
  1.3045 +lemmas independent_finite = independent_imp_finite
  1.3046 +
  1.3047 +lemma span_substd_basis:
  1.3048 +  assumes d: "d \<subseteq> Basis"
  1.3049 +  shows "span d = {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.3050 +  (is "_ = ?B")
  1.3051 +proof -
  1.3052 +  have "d \<subseteq> ?B"
  1.3053 +    using d by (auto simp: inner_Basis)
  1.3054 +  moreover have s: "subspace ?B"
  1.3055 +    using subspace_substandard[of "\<lambda>i. i \<notin> d"] .
  1.3056 +  ultimately have "span d \<subseteq> ?B"
  1.3057 +    using span_mono[of d "?B"] span_eq_iff[of "?B"] by blast
  1.3058 +  moreover have *: "card d \<le> dim (span d)"
  1.3059 +    using independent_card_le_dim[of d "span d"] independent_substdbasis[OF assms]
  1.3060 +      span_superset[of d]
  1.3061 +    by auto
  1.3062 +  moreover from * have "dim ?B \<le> dim (span d)"
  1.3063 +    using dim_substandard[OF assms] by auto
  1.3064 +  ultimately show ?thesis
  1.3065 +    using s subspace_dim_equal[of "span d" "?B"] subspace_span[of d] by auto
  1.3066 +qed
  1.3067 +
  1.3068 +lemma basis_to_substdbasis_subspace_isomorphism:
  1.3069 +  fixes B :: "'a::euclidean_space set"
  1.3070 +  assumes "independent B"
  1.3071 +  shows "\<exists>f d::'a set. card d = card B \<and> linear f \<and> f ` B = d \<and>
  1.3072 +    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.3073 +proof -
  1.3074 +  have B: "card B = dim B"
  1.3075 +    using dim_unique[of B B "card B"] assms span_superset[of B] by auto
  1.3076 +  have "dim B \<le> card (Basis :: 'a set)"
  1.3077 +    using dim_subset_UNIV[of B] by simp
  1.3078 +  from ex_card[OF this] obtain d :: "'a set" where d: "d \<subseteq> Basis" and t: "card d = dim B"
  1.3079 +    by auto
  1.3080 +  let ?t = "{x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.3081 +  have "\<exists>f. linear f \<and> f ` B = d \<and> f ` span B = ?t \<and> inj_on f (span B)"
  1.3082 +  proof (intro basis_to_basis_subspace_isomorphism subspace_span subspace_substandard span_superset)
  1.3083 +    show "d \<subseteq> {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"
  1.3084 +      using d inner_not_same_Basis by blast
  1.3085 +  qed (auto simp: span_substd_basis independent_substdbasis dim_substandard d t B assms)
  1.3086 +  with t \<open>card B = dim B\<close> d show ?thesis by auto
  1.3087 +qed
  1.3088 +
  1.3089 +lemma aff_dim_empty:
  1.3090 +  fixes S :: "'n::euclidean_space set"
  1.3091 +  shows "S = {} \<longleftrightarrow> aff_dim S = -1"
  1.3092 +proof -
  1.3093 +  obtain B where *: "affine hull B = affine hull S"
  1.3094 +    and "\<not> affine_dependent B"
  1.3095 +    and "int (card B) = aff_dim S + 1"
  1.3096 +    using aff_dim_basis_exists by auto
  1.3097 +  moreover
  1.3098 +  from * have "S = {} \<longleftrightarrow> B = {}"
  1.3099 +    using affine_hull_nonempty[of B] affine_hull_nonempty[of S] by auto
  1.3100 +  ultimately show ?thesis
  1.3101 +    using aff_independent_finite[of B] card_gt_0_iff[of B] by auto
  1.3102 +qed
  1.3103 +
  1.3104 +lemma aff_dim_empty_eq [simp]: "aff_dim ({}::'a::euclidean_space set) = -1"
  1.3105 +  by (simp add: aff_dim_empty [symmetric])
  1.3106 +
  1.3107 +lemma aff_dim_affine_hull [simp]: "aff_dim (affine hull S) = aff_dim S"
  1.3108 +  unfolding aff_dim_def using hull_hull[of _ S] by auto
  1.3109 +
  1.3110 +lemma aff_dim_affine_hull2:
  1.3111 +  assumes "affine hull S = affine hull T"
  1.3112 +  shows "aff_dim S = aff_dim T"
  1.3113 +  unfolding aff_dim_def using assms by auto
  1.3114 +
  1.3115 +lemma aff_dim_unique:
  1.3116 +  fixes B V :: "'n::euclidean_space set"
  1.3117 +  assumes "affine hull B = affine hull V \<and> \<not> affine_dependent B"
  1.3118 +  shows "of_nat (card B) = aff_dim V + 1"
  1.3119 +proof (cases "B = {}")
  1.3120 +  case True
  1.3121 +  then have "V = {}"
  1.3122 +    using affine_hull_nonempty[of V] affine_hull_nonempty[of B] assms
  1.3123 +    by auto
  1.3124 +  then have "aff_dim V = (-1::int)"
  1.3125 +    using aff_dim_empty by auto
  1.3126 +  then show ?thesis
  1.3127 +    using \<open>B = {}\<close> by auto
  1.3128 +next
  1.3129 +  case False
  1.3130 +  then obtain a where a: "a \<in> B" by auto
  1.3131 +  define Lb where "Lb = span ((\<lambda>x. -a+x) ` (B-{a}))"
  1.3132 +  have "affine_parallel (affine hull B) Lb"
  1.3133 +    using Lb_def affine_hull_span2[of a B] a
  1.3134 +      affine_parallel_commut[of "Lb" "(affine hull B)"]
  1.3135 +    unfolding affine_parallel_def by auto
  1.3136 +  moreover have "subspace Lb"
  1.3137 +    using Lb_def subspace_span by auto
  1.3138 +  ultimately have "aff_dim B = int(dim Lb)"
  1.3139 +    using aff_dim_parallel_subspace[of B Lb] \<open>B \<noteq> {}\<close> by auto
  1.3140 +  moreover have "(card B) - 1 = dim Lb" "finite B"
  1.3141 +    using Lb_def aff_dim_parallel_subspace_aux a assms by auto
  1.3142 +  ultimately have "of_nat (card B) = aff_dim B + 1"
  1.3143 +    using \<open>B \<noteq> {}\<close> card_gt_0_iff[of B] by auto
  1.3144 +  then show ?thesis
  1.3145 +    using aff_dim_affine_hull2 assms by auto
  1.3146 +qed
  1.3147 +
  1.3148 +lemma aff_dim_affine_independent:
  1.3149 +  fixes B :: "'n::euclidean_space set"
  1.3150 +  assumes "\<not> affine_dependent B"
  1.3151 +  shows "of_nat (card B) = aff_dim B + 1"
  1.3152 +  using aff_dim_unique[of B B] assms by auto
  1.3153 +
  1.3154 +lemma affine_independent_iff_card:
  1.3155 +    fixes s :: "'a::euclidean_space set"
  1.3156 +    shows "\<not> affine_dependent s \<longleftrightarrow> finite s \<and> aff_dim s = int(card s) - 1"
  1.3157 +  apply (rule iffI)
  1.3158 +  apply (simp add: aff_dim_affine_independent aff_independent_finite)
  1.3159 +  by (metis affine_basis_exists [of s] aff_dim_unique card_subset_eq diff_add_cancel of_nat_eq_iff)
  1.3160 +
  1.3161 +lemma aff_dim_sing [simp]:
  1.3162 +  fixes a :: "'n::euclidean_space"
  1.3163 +  shows "aff_dim {a} = 0"
  1.3164 +  using aff_dim_affine_independent[of "{a}"] affine_independent_1 by auto
  1.3165 +
  1.3166 +lemma aff_dim_2 [simp]: "aff_dim {a,b} = (if a = b then 0 else 1)"
  1.3167 +proof (clarsimp)
  1.3168 +  assume "a \<noteq> b"
  1.3169 +  then have "aff_dim{a,b} = card{a,b} - 1"
  1.3170 +    using affine_independent_2 [of a b] aff_dim_affine_independent by fastforce
  1.3171 +  also have "\<dots> = 1"
  1.3172 +    using \<open>a \<noteq> b\<close> by simp
  1.3173 +  finally show "aff_dim {a, b} = 1" .
  1.3174 +qed
  1.3175 +
  1.3176 +lemma aff_dim_inner_basis_exists:
  1.3177 +  fixes V :: "('n::euclidean_space) set"
  1.3178 +  shows "\<exists>B. B \<subseteq> V \<and> affine hull B = affine hull V \<and>
  1.3179 +    \<not> affine_dependent B \<and> of_nat (card B) = aff_dim V + 1"
  1.3180 +proof -
  1.3181 +  obtain B where B: "\<not> affine_dependent B" "B \<subseteq> V" "affine hull B = affine hull V"
  1.3182 +    using affine_basis_exists[of V] by auto
  1.3183 +  then have "of_nat(card B) = aff_dim V+1" using aff_dim_unique by auto
  1.3184 +  with B show ?thesis by auto
  1.3185 +qed
  1.3186 +
  1.3187 +lemma aff_dim_le_card:
  1.3188 +  fixes V :: "'n::euclidean_space set"
  1.3189 +  assumes "finite V"
  1.3190 +  shows "aff_dim V \<le> of_nat (card V) - 1"
  1.3191 +proof -
  1.3192 +  obtain B where B: "B \<subseteq> V" "of_nat (card B) = aff_dim V + 1"
  1.3193 +    using aff_dim_inner_basis_exists[of V] by auto
  1.3194 +  then have "card B \<le> card V"
  1.3195 +    using assms card_mono by auto
  1.3196 +  with B show ?thesis by auto
  1.3197 +qed
  1.3198 +
  1.3199 +lemma aff_dim_parallel_eq:
  1.3200 +  fixes S T :: "'n::euclidean_space set"
  1.3201 +  assumes "affine_parallel (affine hull S) (affine hull T)"
  1.3202 +  shows "aff_dim S = aff_dim T"
  1.3203 +proof -
  1.3204 +  {
  1.3205 +    assume "T \<noteq> {}" "S \<noteq> {}"
  1.3206 +    then obtain L where L: "subspace L \<and> affine_parallel (affine hull T) L"
  1.3207 +      using affine_parallel_subspace[of "affine hull T"]
  1.3208 +        affine_affine_hull[of T] affine_hull_nonempty
  1.3209 +      by auto
  1.3210 +    then have "aff_dim T = int (dim L)"
  1.3211 +      using aff_dim_parallel_subspace \<open>T \<noteq> {}\<close> by auto
  1.3212 +    moreover have *: "subspace L \<and> affine_parallel (affine hull S) L"
  1.3213 +       using L affine_parallel_assoc[of "affine hull S" "affine hull T" L] assms by auto
  1.3214 +    moreover from * have "aff_dim S = int (dim L)"
  1.3215 +      using aff_dim_parallel_subspace \<open>S \<noteq> {}\<close> by auto
  1.3216 +    ultimately have ?thesis by auto
  1.3217 +  }
  1.3218 +  moreover
  1.3219 +  {
  1.3220 +    assume "S = {}"
  1.3221 +    then have "S = {}" and "T = {}"
  1.3222 +      using assms affine_hull_nonempty
  1.3223 +      unfolding affine_parallel_def
  1.3224 +      by auto
  1.3225 +    then have ?thesis using aff_dim_empty by auto
  1.3226 +  }
  1.3227 +  moreover
  1.3228 +  {
  1.3229 +    assume "T = {}"
  1.3230 +    then have "S = {}" and "T = {}"
  1.3231 +      using assms affine_hull_nonempty
  1.3232 +      unfolding affine_parallel_def
  1.3233 +      by auto
  1.3234 +    then have ?thesis
  1.3235 +      using aff_dim_empty by auto
  1.3236 +  }
  1.3237 +  ultimately show ?thesis by blast
  1.3238 +qed
  1.3239 +
  1.3240 +lemma aff_dim_translation_eq:
  1.3241 +  fixes a :: "'n::euclidean_space"
  1.3242 +  shows "aff_dim ((\<lambda>x. a + x) ` S) = aff_dim S"
  1.3243 +proof -
  1.3244 +  have "affine_parallel (affine hull S) (affine hull ((\<lambda>x. a + x) ` S))"
  1.3245 +    unfolding affine_parallel_def
  1.3246 +    apply (rule exI[of _ "a"])
  1.3247 +    using affine_hull_translation[of a S]
  1.3248 +    apply auto
  1.3249 +    done
  1.3250 +  then show ?thesis
  1.3251 +    using aff_dim_parallel_eq[of S "(\<lambda>x. a + x) ` S"] by auto
  1.3252 +qed
  1.3253 +
  1.3254 +lemma aff_dim_affine:
  1.3255 +  fixes S L :: "'n::euclidean_space set"
  1.3256 +  assumes "S \<noteq> {}"
  1.3257 +    and "affine S"
  1.3258 +    and "subspace L"
  1.3259 +    and "affine_parallel S L"
  1.3260 +  shows "aff_dim S = int (dim L)"
  1.3261 +proof -
  1.3262 +  have *: "affine hull S = S"
  1.3263 +    using assms affine_hull_eq[of S] by auto
  1.3264 +  then have "affine_parallel (affine hull S) L"
  1.3265 +    using assms by (simp add: *)
  1.3266 +  then show ?thesis
  1.3267 +    using assms aff_dim_parallel_subspace[of S L] by blast
  1.3268 +qed
  1.3269 +
  1.3270 +lemma dim_affine_hull:
  1.3271 +  fixes S :: "'n::euclidean_space set"
  1.3272 +  shows "dim (affine hull S) = dim S"
  1.3273 +proof -
  1.3274 +  have "dim (affine hull S) \<ge> dim S"
  1.3275 +    using dim_subset by auto
  1.3276 +  moreover have "dim (span S) \<ge> dim (affine hull S)"
  1.3277 +    using dim_subset affine_hull_subset_span by blast
  1.3278 +  moreover have "dim (span S) = dim S"
  1.3279 +    using dim_span by auto
  1.3280 +  ultimately show ?thesis by auto
  1.3281 +qed
  1.3282 +
  1.3283 +lemma aff_dim_subspace:
  1.3284 +  fixes S :: "'n::euclidean_space set"
  1.3285 +  assumes "subspace S"
  1.3286 +  shows "aff_dim S = int (dim S)"
  1.3287 +proof (cases "S={}")
  1.3288 +  case True with assms show ?thesis
  1.3289 +    by (simp add: subspace_affine)
  1.3290 +next
  1.3291 +  case False
  1.3292 +  with aff_dim_affine[of S S] assms subspace_imp_affine[of S] affine_parallel_reflex[of S] subspace_affine
  1.3293 +  show ?thesis by auto
  1.3294 +qed
  1.3295 +
  1.3296 +lemma aff_dim_zero:
  1.3297 +  fixes S :: "'n::euclidean_space set"
  1.3298 +  assumes "0 \<in> affine hull S"
  1.3299 +  shows "aff_dim S = int (dim S)"
  1.3300 +proof -
  1.3301 +  have "subspace (affine hull S)"
  1.3302 +    using subspace_affine[of "affine hull S"] affine_affine_hull assms
  1.3303 +    by auto
  1.3304 +  then have "aff_dim (affine hull S) = int (dim (affine hull S))"
  1.3305 +    using assms aff_dim_subspace[of "affine hull S"] by auto
  1.3306 +  then show ?thesis
  1.3307 +    using aff_dim_affine_hull[of S] dim_affine_hull[of S]
  1.3308 +    by auto
  1.3309 +qed
  1.3310 +
  1.3311 +lemma aff_dim_eq_dim:
  1.3312 +  fixes S :: "'n::euclidean_space set"
  1.3313 +  assumes "a \<in> affine hull S"
  1.3314 +  shows "aff_dim S = int (dim ((\<lambda>x. -a+x) ` S))"
  1.3315 +proof -
  1.3316 +  have "0 \<in> affine hull ((\<lambda>x. -a+x) ` S)"
  1.3317 +    unfolding affine_hull_translation
  1.3318 +    using assms by (simp add: ab_group_add_class.ab_left_minus image_iff)
  1.3319 +  with aff_dim_zero show ?thesis
  1.3320 +    by (metis aff_dim_translation_eq)
  1.3321 +qed
  1.3322 +
  1.3323 +lemma aff_dim_UNIV [simp]: "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  1.3324 +  using aff_dim_subspace[of "(UNIV :: 'n::euclidean_space set)"]
  1.3325 +    dim_UNIV[where 'a="'n::euclidean_space"]
  1.3326 +  by auto
  1.3327 +
  1.3328 +lemma aff_dim_geq:
  1.3329 +  fixes V :: "'n::euclidean_space set"
  1.3330 +  shows "aff_dim V \<ge> -1"
  1.3331 +proof -
  1.3332 +  obtain B where "affine hull B = affine hull V"
  1.3333 +    and "\<not> affine_dependent B"
  1.3334 +    and "int (card B) = aff_dim V + 1"
  1.3335 +    using aff_dim_basis_exists by auto
  1.3336 +  then show ?thesis by auto
  1.3337 +qed
  1.3338 +
  1.3339 +lemma aff_dim_negative_iff [simp]:
  1.3340 +  fixes S :: "'n::euclidean_space set"
  1.3341 +  shows "aff_dim S < 0 \<longleftrightarrow>S = {}"
  1.3342 +by (metis aff_dim_empty aff_dim_geq diff_0 eq_iff zle_diff1_eq)
  1.3343 +
  1.3344 +lemma aff_lowdim_subset_hyperplane:
  1.3345 +  fixes S :: "'a::euclidean_space set"
  1.3346 +  assumes "aff_dim S < DIM('a)"
  1.3347 +  obtains a b where "a \<noteq> 0" "S \<subseteq> {x. a \<bullet> x = b}"
  1.3348 +proof (cases "S={}")
  1.3349 +  case True
  1.3350 +  moreover
  1.3351 +  have "(SOME b. b \<in> Basis) \<noteq> 0"
  1.3352 +    by (metis norm_some_Basis norm_zero zero_neq_one)
  1.3353 +  ultimately show ?thesis
  1.3354 +    using that by blast
  1.3355 +next
  1.3356 +  case False
  1.3357 +  then obtain c S' where "c \<notin> S'" "S = insert c S'"
  1.3358 +    by (meson equals0I mk_disjoint_insert)
  1.3359 +  have "dim ((+) (-c) ` S) < DIM('a)"
  1.3360 +    by (metis \<open>S = insert c S'\<close> aff_dim_eq_dim assms hull_inc insertI1 of_nat_less_imp_less)
  1.3361 +  then obtain a where "a \<noteq> 0" "span ((+) (-c) ` S) \<subseteq> {x. a \<bullet> x = 0}"
  1.3362 +    using lowdim_subset_hyperplane by blast
  1.3363 +  moreover
  1.3364 +  have "a \<bullet> w = a \<bullet> c" if "span ((+) (- c) ` S) \<subseteq> {x. a \<bullet> x = 0}" "w \<in> S" for w
  1.3365 +  proof -
  1.3366 +    have "w-c \<in> span ((+) (- c) ` S)"
  1.3367 +      by (simp add: span_base \<open>w \<in> S\<close>)
  1.3368 +    with that have "w-c \<in> {x. a \<bullet> x = 0}"
  1.3369 +      by blast
  1.3370 +    then show ?thesis
  1.3371 +      by (auto simp: algebra_simps)
  1.3372 +  qed
  1.3373 +  ultimately have "S \<subseteq> {x. a \<bullet> x = a \<bullet> c}"
  1.3374 +    by blast
  1.3375 +  then show ?thesis
  1.3376 +    by (rule that[OF \<open>a \<noteq> 0\<close>])
  1.3377 +qed
  1.3378 +
  1.3379 +lemma affine_independent_card_dim_diffs:
  1.3380 +  fixes S :: "'a :: euclidean_space set"
  1.3381 +  assumes "\<not> affine_dependent S" "a \<in> S"
  1.3382 +    shows "card S = dim {x - a|x. x \<in> S} + 1"
  1.3383 +proof -
  1.3384 +  have 1: "{b - a|b. b \<in> (S - {a})} \<subseteq> {x - a|x. x \<in> S}" by auto
  1.3385 +  have 2: "x - a \<in> span {b - a |b. b \<in> S - {a}}" if "x \<in> S" for x
  1.3386 +  proof (cases "x = a")
  1.3387 +    case True then show ?thesis by (simp add: span_clauses)
  1.3388 +  next
  1.3389 +    case False then show ?thesis
  1.3390 +      using assms by (blast intro: span_base that)
  1.3391 +  qed
  1.3392 +  have "\<not> affine_dependent (insert a S)"
  1.3393 +    by (simp add: assms insert_absorb)
  1.3394 +  then have 3: "independent {b - a |b. b \<in> S - {a}}"
  1.3395 +      using dependent_imp_affine_dependent by fastforce
  1.3396 +  have "{b - a |b. b \<in> S - {a}} = (\<lambda>b. b-a) ` (S - {a})"
  1.3397 +    by blast
  1.3398 +  then have "card {b - a |b. b \<in> S - {a}} = card ((\<lambda>b. b-a) ` (S - {a}))"
  1.3399 +    by simp
  1.3400 +  also have "\<dots> = card (S - {a})"
  1.3401 +    by (metis (no_types, lifting) card_image diff_add_cancel inj_onI)
  1.3402 +  also have "\<dots> = card S - 1"
  1.3403 +    by (simp add: aff_independent_finite assms)
  1.3404 +  finally have 4: "card {b - a |b. b \<in> S - {a}} = card S - 1" .
  1.3405 +  have "finite S"
  1.3406 +    by (meson assms aff_independent_finite)
  1.3407 +  with \<open>a \<in> S\<close> have "card S \<noteq> 0" by auto
  1.3408 +  moreover have "dim {x - a |x. x \<in> S} = card S - 1"
  1.3409 +    using 2 by (blast intro: dim_unique [OF 1 _ 3 4])
  1.3410 +  ultimately show ?thesis
  1.3411 +    by auto
  1.3412 +qed
  1.3413 +
  1.3414 +lemma independent_card_le_aff_dim:
  1.3415 +  fixes B :: "'n::euclidean_space set"
  1.3416 +  assumes "B \<subseteq> V"
  1.3417 +  assumes "\<not> affine_dependent B"
  1.3418 +  shows "int (card B) \<le> aff_dim V + 1"
  1.3419 +proof -
  1.3420 +  obtain T where T: "\<not> affine_dependent T \<and> B \<subseteq> T \<and> T \<subseteq> V \<and> affine hull T = affine hull V"
  1.3421 +    by (metis assms extend_to_affine_basis[of B V])
  1.3422 +  then have "of_nat (card T) = aff_dim V + 1"
  1.3423 +    using aff_dim_unique by auto
  1.3424 +  then show ?thesis
  1.3425 +    using T card_mono[of T B] aff_independent_finite[of T] by auto
  1.3426 +qed
  1.3427 +
  1.3428 +lemma aff_dim_subset:
  1.3429 +  fixes S T :: "'n::euclidean_space set"
  1.3430 +  assumes "S \<subseteq> T"
  1.3431 +  shows "aff_dim S \<le> aff_dim T"
  1.3432 +proof -
  1.3433 +  obtain B where B: "\<not> affine_dependent B" "B \<subseteq> S" "affine hull B = affine hull S"
  1.3434 +    "of_nat (card B) = aff_dim S + 1"
  1.3435 +    using aff_dim_inner_basis_exists[of S] by auto
  1.3436 +  then have "int (card B) \<le> aff_dim T + 1"
  1.3437 +    using assms independent_card_le_aff_dim[of B T] by auto
  1.3438 +  with B show ?thesis by auto
  1.3439 +qed
  1.3440 +
  1.3441 +lemma aff_dim_le_DIM:
  1.3442 +  fixes S :: "'n::euclidean_space set"
  1.3443 +  shows "aff_dim S \<le> int (DIM('n))"
  1.3444 +proof -
  1.3445 +  have "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))"
  1.3446 +    using aff_dim_UNIV by auto
  1.3447 +  then show "aff_dim (S:: 'n::euclidean_space set) \<le> int(DIM('n))"
  1.3448 +    using aff_dim_subset[of S "(UNIV :: ('n::euclidean_space) set)"] subset_UNIV by auto
  1.3449 +qed
  1.3450 +
  1.3451 +lemma affine_dim_equal:
  1.3452 +  fixes S :: "'n::euclidean_space set"
  1.3453 +  assumes "affine S" "affine T" "S \<noteq> {}" "S \<subseteq> T" "aff_dim S = aff_dim T"
  1.3454 +  shows "S = T"
  1.3455 +proof -
  1.3456 +  obtain a where "a \<in> S" using assms by auto
  1.3457 +  then have "a \<in> T" using assms by auto
  1.3458 +  define LS where "LS = {y. \<exists>x \<in> S. (-a) + x = y}"
  1.3459 +  then have ls: "subspace LS" "affine_parallel S LS"
  1.3460 +    using assms parallel_subspace_explicit[of S a LS] \<open>a \<in> S\<close> by auto
  1.3461 +  then have h1: "int(dim LS) = aff_dim S"
  1.3462 +    using assms aff_dim_affine[of S LS] by auto
  1.3463 +  have "T \<noteq> {}" using assms by auto
  1.3464 +  define LT where "LT = {y. \<exists>x \<in> T. (-a) + x = y}"
  1.3465 +  then have lt: "subspace LT \<and> affine_parallel T LT"
  1.3466 +    using assms parallel_subspace_explicit[of T a LT] \<open>a \<in> T\<close> by auto
  1.3467 +  then have "int(dim LT) = aff_dim T"
  1.3468 +    using assms aff_dim_affine[of T LT] \<open>T \<noteq> {}\<close> by auto
  1.3469 +  then have "dim LS = dim LT"
  1.3470 +    using h1 assms by auto
  1.3471 +  moreover have "LS \<le> LT"
  1.3472 +    using LS_def LT_def assms by auto
  1.3473 +  ultimately have "LS = LT"
  1.3474 +    using subspace_dim_equal[of LS LT] ls lt by auto
  1.3475 +  moreover have "S = {x. \<exists>y \<in> LS. a+y=x}"
  1.3476 +    using LS_def by auto
  1.3477 +  moreover have "T = {x. \<exists>y \<in> LT. a+y=x}"
  1.3478 +    using LT_def by auto
  1.3479 +  ultimately show ?thesis by auto
  1.3480 +qed
  1.3481 +
  1.3482 +lemma aff_dim_eq_0:
  1.3483 +  fixes S :: "'a::euclidean_space set"
  1.3484 +  shows "aff_dim S = 0 \<longleftrightarrow> (\<exists>a. S = {a})"
  1.3485 +proof (cases "S = {}")
  1.3486 +  case True
  1.3487 +  then show ?thesis
  1.3488 +    by auto
  1.3489 +next
  1.3490 +  case False
  1.3491 +  then obtain a where "a \<in> S" by auto
  1.3492 +  show ?thesis
  1.3493 +  proof safe
  1.3494 +    assume 0: "aff_dim S = 0"
  1.3495 +    have "\<not> {a,b} \<subseteq> S" if "b \<noteq> a" for b
  1.3496 +      by (metis "0" aff_dim_2 aff_dim_subset not_one_le_zero that)
  1.3497 +    then show "\<exists>a. S = {a}"
  1.3498 +      using \<open>a \<in> S\<close> by blast
  1.3499 +  qed auto
  1.3500 +qed
  1.3501 +
  1.3502 +lemma affine_hull_UNIV:
  1.3503 +  fixes S :: "'n::euclidean_space set"
  1.3504 +  assumes "aff_dim S = int(DIM('n))"
  1.3505 +  shows "affine hull S = (UNIV :: ('n::euclidean_space) set)"
  1.3506 +proof -
  1.3507 +  have "S \<noteq> {}"
  1.3508 +    using assms aff_dim_empty[of S] by auto
  1.3509 +  have h0: "S \<subseteq> affine hull S"
  1.3510 +    using hull_subset[of S _] by auto
  1.3511 +  have h1: "aff_dim (UNIV :: ('n::euclidean_space) set) = aff_dim S"
  1.3512 +    using aff_dim_UNIV assms by auto
  1.3513 +  then have h2: "aff_dim (affine hull S) \<le> aff_dim (UNIV :: ('n::euclidean_space) set)"
  1.3514 +    using aff_dim_le_DIM[of "affine hull S"] assms h0 by auto
  1.3515 +  have h3: "aff_dim S \<le> aff_dim (affine hull S)"
  1.3516 +    using h0 aff_dim_subset[of S "affine hull S"] assms by auto
  1.3517 +  then have h4: "aff_dim (affine hull S) = aff_dim (UNIV :: ('n::euclidean_space) set)"
  1.3518 +    using h0 h1 h2 by auto
  1.3519 +  then show ?thesis
  1.3520 +    using affine_dim_equal[of "affine hull S" "(UNIV :: ('n::euclidean_space) set)"]
  1.3521 +      affine_affine_hull[of S] affine_UNIV assms h4 h0 \<open>S \<noteq> {}\<close>
  1.3522 +    by auto
  1.3523 +qed
  1.3524 +
  1.3525 +lemma disjoint_affine_hull:
  1.3526 +  fixes s :: "'n::euclidean_space set"
  1.3527 +  assumes "\<not> affine_dependent s" "t \<subseteq> s" "u \<subseteq> s" "t \<inter> u = {}"
  1.3528 +    shows "(affine hull t) \<inter> (affine hull u) = {}"
  1.3529 +proof -
  1.3530 +  have "finite s" using assms by (simp add: aff_independent_finite)
  1.3531 +  then have "finite t" "finite u" using assms finite_subset by blast+
  1.3532 +  { fix y
  1.3533 +    assume yt: "y \<in> affine hull t" and yu: "y \<in> affine hull u"
  1.3534 +    then obtain a b
  1.3535 +           where a1 [simp]: "sum a t = 1" and [simp]: "sum (\<lambda>v. a v *\<^sub>R v) t = y"
  1.3536 +             and [simp]: "sum b u = 1" "sum (\<lambda>v. b v *\<^sub>R v) u = y"
  1.3537 +      by (auto simp: affine_hull_finite \<open>finite t\<close> \<open>finite u\<close>)
  1.3538 +    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.3539 +    have [simp]: "s \<inter> t = t" "s \<inter> - t \<inter> u = u" using assms by auto
  1.3540 +    have "sum c s = 0"
  1.3541 +      by (simp add: c_def comm_monoid_add_class.sum.If_cases \<open>finite s\<close> sum_negf)
  1.3542 +    moreover have "\<not> (\<forall>v\<in>s. c v = 0)"
  1.3543 +      by (metis (no_types) IntD1 \<open>s \<inter> t = t\<close> a1 c_def sum_not_0 zero_neq_one)
  1.3544 +    moreover have "(\<Sum>v\<in>s. c v *\<^sub>R v) = 0"
  1.3545 +      by (simp add: c_def if_smult sum_negf
  1.3546 +             comm_monoid_add_class.sum.If_cases \<open>finite s\<close>)
  1.3547 +    ultimately have False
  1.3548 +      using assms \<open>finite s\<close> by (auto simp: affine_dependent_explicit)
  1.3549 +  }
  1.3550 +  then show ?thesis by blast
  1.3551 +qed
  1.3552 +
  1.3553 +lemma aff_dim_convex_hull:
  1.3554 +  fixes S :: "'n::euclidean_space set"
  1.3555 +  shows "aff_dim (convex hull S) = aff_dim S"
  1.3556 +  using aff_dim_affine_hull[of S] convex_hull_subset_affine_hull[of S]
  1.3557 +    hull_subset[of S "convex"] aff_dim_subset[of S "convex hull S"]
  1.3558 +    aff_dim_subset[of "convex hull S" "affine hull S"]
  1.3559 +  by auto
  1.3560 +
  1.3561 +subsection \<open>Caratheodory's theorem\<close>
  1.3562 +
  1.3563 +lemma convex_hull_caratheodory_aff_dim:
  1.3564 +  fixes p :: "('a::euclidean_space) set"
  1.3565 +  shows "convex hull p =
  1.3566 +    {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  1.3567 +      (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  1.3568 +  unfolding convex_hull_explicit set_eq_iff mem_Collect_eq
  1.3569 +proof (intro allI iffI)
  1.3570 +  fix y
  1.3571 +  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.3572 +    sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1.3573 +  assume "\<exists>s u. finite s \<and> s \<subseteq> p \<and> (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1.3574 +  then obtain N where "?P N" by auto
  1.3575 +  then have "\<exists>n\<le>N. (\<forall>k<n. \<not> ?P k) \<and> ?P n"
  1.3576 +    apply (rule_tac ex_least_nat_le, auto)
  1.3577 +    done
  1.3578 +  then obtain n where "?P n" and smallest: "\<forall>k<n. \<not> ?P k"
  1.3579 +    by blast
  1.3580 +  then obtain s u where obt: "finite s" "card s = n" "s\<subseteq>p" "\<forall>x\<in>s. 0 \<le> u x"
  1.3581 +    "sum u s = 1"  "(\<Sum>v\<in>s. u v *\<^sub>R v) = y" by auto
  1.3582 +
  1.3583 +  have "card s \<le> aff_dim p + 1"
  1.3584 +  proof (rule ccontr, simp only: not_le)
  1.3585 +    assume "aff_dim p + 1 < card s"
  1.3586 +    then have "affine_dependent s"
  1.3587 +      using affine_dependent_biggerset[OF obt(1)] independent_card_le_aff_dim not_less obt(3)
  1.3588 +      by blast
  1.3589 +    then obtain w v where wv: "sum w s = 0" "v\<in>s" "w v \<noteq> 0" "(\<Sum>v\<in>s. w v *\<^sub>R v) = 0"
  1.3590 +      using affine_dependent_explicit_finite[OF obt(1)] by auto
  1.3591 +    define i where "i = (\<lambda>v. (u v) / (- w v)) ` {v\<in>s. w v < 0}"
  1.3592 +    define t where "t = Min i"
  1.3593 +    have "\<exists>x\<in>s. w x < 0"
  1.3594 +    proof (rule ccontr, simp add: not_less)
  1.3595 +      assume as:"\<forall>x\<in>s. 0 \<le> w x"
  1.3596 +      then have "sum w (s - {v}) \<ge> 0"
  1.3597 +        apply (rule_tac sum_nonneg, auto)
  1.3598 +        done
  1.3599 +      then have "sum w s > 0"
  1.3600 +        unfolding sum.remove[OF obt(1) \<open>v\<in>s\<close>]
  1.3601 +        using as[THEN bspec[where x=v]]  \<open>v\<in>s\<close>  \<open>w v \<noteq> 0\<close> by auto
  1.3602 +      then show False using wv(1) by auto
  1.3603 +    qed
  1.3604 +    then have "i \<noteq> {}" unfolding i_def by auto
  1.3605 +    then have "t \<ge> 0"
  1.3606 +      using Min_ge_iff[of i 0 ] and obt(1)
  1.3607 +      unfolding t_def i_def
  1.3608 +      using obt(4)[unfolded le_less]
  1.3609 +      by (auto simp: divide_le_0_iff)
  1.3610 +    have t: "\<forall>v\<in>s. u v + t * w v \<ge> 0"
  1.3611 +    proof
  1.3612 +      fix v
  1.3613 +      assume "v \<in> s"
  1.3614 +      then have v: "0 \<le> u v"
  1.3615 +        using obt(4)[THEN bspec[where x=v]] by auto
  1.3616 +      show "0 \<le> u v + t * w v"
  1.3617 +      proof (cases "w v < 0")
  1.3618 +        case False
  1.3619 +        thus ?thesis using v \<open>t\<ge>0\<close> by auto
  1.3620 +      next
  1.3621 +        case True
  1.3622 +        then have "t \<le> u v / (- w v)"
  1.3623 +          using \<open>v\<in>s\<close> unfolding t_def i_def
  1.3624 +          apply (rule_tac Min_le)
  1.3625 +          using obt(1) apply auto
  1.3626 +          done
  1.3627 +        then show ?thesis
  1.3628 +          unfolding real_0_le_add_iff
  1.3629 +          using pos_le_divide_eq[OF True[unfolded neg_0_less_iff_less[symmetric]]]
  1.3630 +          by auto
  1.3631 +      qed
  1.3632 +    qed
  1.3633 +    obtain a where "a \<in> s" and "t = (\<lambda>v. (u v) / (- w v)) a" and "w a < 0"
  1.3634 +      using Min_in[OF _ \<open>i\<noteq>{}\<close>] and obt(1) unfolding i_def t_def by auto
  1.3635 +    then have a: "a \<in> s" "u a + t * w a = 0" by auto
  1.3636 +    have *: "\<And>f. sum f (s - {a}) = sum f s - ((f a)::'b::ab_group_add)"
  1.3637 +      unfolding sum.remove[OF obt(1) \<open>a\<in>s\<close>] by auto
  1.3638 +    have "(\<Sum>v\<in>s. u v + t * w v) = 1"
  1.3639 +      unfolding sum.distrib wv(1) sum_distrib_left[symmetric] obt(5) by auto
  1.3640 +    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.3641 +      unfolding sum.distrib obt(6) scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] wv(4)
  1.3642 +      using a(2) [THEN eq_neg_iff_add_eq_0 [THEN iffD2]] by simp
  1.3643 +    ultimately have "?P (n - 1)"
  1.3644 +      apply (rule_tac x="(s - {a})" in exI)
  1.3645 +      apply (rule_tac x="\<lambda>v. u v + t * w v" in exI)
  1.3646 +      using obt(1-3) and t and a
  1.3647 +      apply (auto simp: * scaleR_left_distrib)
  1.3648 +      done
  1.3649 +    then show False
  1.3650 +      using smallest[THEN spec[where x="n - 1"]] by auto
  1.3651 +  qed
  1.3652 +  then show "\<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> aff_dim p + 1 \<and>
  1.3653 +      (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = y"
  1.3654 +    using obt by auto
  1.3655 +qed auto
  1.3656 +
  1.3657 +lemma caratheodory_aff_dim:
  1.3658 +  fixes p :: "('a::euclidean_space) set"
  1.3659 +  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.3660 +        (is "?lhs = ?rhs")
  1.3661 +proof
  1.3662 +  show "?lhs \<subseteq> ?rhs"
  1.3663 +    apply (subst convex_hull_caratheodory_aff_dim, clarify)
  1.3664 +    apply (rule_tac x=s in exI)
  1.3665 +    apply (simp add: hull_subset convex_explicit [THEN iffD1, OF convex_convex_hull])
  1.3666 +    done
  1.3667 +next
  1.3668 +  show "?rhs \<subseteq> ?lhs"
  1.3669 +    using hull_mono by blast
  1.3670 +qed
  1.3671 +
  1.3672 +lemma convex_hull_caratheodory:
  1.3673 +  fixes p :: "('a::euclidean_space) set"
  1.3674 +  shows "convex hull p =
  1.3675 +            {y. \<exists>s u. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and>
  1.3676 +              (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) s = y}"
  1.3677 +        (is "?lhs = ?rhs")
  1.3678 +proof (intro set_eqI iffI)
  1.3679 +  fix x
  1.3680 +  assume "x \<in> ?lhs" then show "x \<in> ?rhs"
  1.3681 +    apply (simp only: convex_hull_caratheodory_aff_dim Set.mem_Collect_eq)
  1.3682 +    apply (erule ex_forward)+
  1.3683 +    using aff_dim_le_DIM [of p]
  1.3684 +    apply simp
  1.3685 +    done
  1.3686 +next
  1.3687 +  fix x
  1.3688 +  assume "x \<in> ?rhs" then show "x \<in> ?lhs"
  1.3689 +    by (auto simp: convex_hull_explicit)
  1.3690 +qed
  1.3691 +
  1.3692 +theorem caratheodory:
  1.3693 +  "convex hull p =
  1.3694 +    {x::'a::euclidean_space. \<exists>s. finite s \<and> s \<subseteq> p \<and>
  1.3695 +      card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s}"
  1.3696 +proof safe
  1.3697 +  fix x
  1.3698 +  assume "x \<in> convex hull p"
  1.3699 +  then obtain s u where "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1"
  1.3700 +    "\<forall>x\<in>s. 0 \<le> u x" "sum u s = 1" "(\<Sum>v\<in>s. u v *\<^sub>R v) = x"
  1.3701 +    unfolding convex_hull_caratheodory by auto
  1.3702 +  then show "\<exists>s. finite s \<and> s \<subseteq> p \<and> card s \<le> DIM('a) + 1 \<and> x \<in> convex hull s"
  1.3703 +    apply (rule_tac x=s in exI)
  1.3704 +    using hull_subset[of s convex]
  1.3705 +    using convex_convex_hull[simplified convex_explicit, of s,
  1.3706 +      THEN spec[where x=s], THEN spec[where x=u]]
  1.3707 +    apply auto
  1.3708 +    done
  1.3709 +next
  1.3710 +  fix x s
  1.3711 +  assume  "finite s" "s \<subseteq> p" "card s \<le> DIM('a) + 1" "x \<in> convex hull s"
  1.3712 +  then show "x \<in> convex hull p"
  1.3713 +    using hull_mono[OF \<open>s\<subseteq>p\<close>] by auto
  1.3714 +qed
  1.3715 +
  1.3716 +subsection%unimportant\<open>Some Properties of subset of standard basis\<close>
  1.3717 +
  1.3718 +lemma affine_hull_substd_basis:
  1.3719 +  assumes "d \<subseteq> Basis"
  1.3720 +  shows "affine hull (insert 0 d) = {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  1.3721 +  (is "affine hull (insert 0 ?A) = ?B")
  1.3722 +proof -
  1.3723 +  have *: "\<And>A. (+) (0::'a) ` A = A" "\<And>A. (+) (- (0::'a)) ` A = A"
  1.3724 +    by auto
  1.3725 +  show ?thesis
  1.3726 +    unfolding affine_hull_insert_span_gen span_substd_basis[OF assms,symmetric] * ..
  1.3727 +qed
  1.3728 +
  1.3729 +lemma affine_hull_convex_hull [simp]: "affine hull (convex hull S) = affine hull S"
  1.3730 +  by (metis Int_absorb1 Int_absorb2 convex_hull_subset_affine_hull hull_hull hull_mono hull_subset)
  1.3731 +
  1.3732 +
  1.3733 +subsection%unimportant \<open>Moving and scaling convex hulls\<close>
  1.3734 +
  1.3735 +lemma convex_hull_set_plus:
  1.3736 +  "convex hull (S + T) = convex hull S + convex hull T"
  1.3737 +  unfolding set_plus_image
  1.3738 +  apply (subst convex_hull_linear_image [symmetric])
  1.3739 +  apply (simp add: linear_iff scaleR_right_distrib)
  1.3740 +  apply (simp add: convex_hull_Times)
  1.3741 +  done
  1.3742 +
  1.3743 +lemma translation_eq_singleton_plus: "(\<lambda>x. a + x) ` T = {a} + T"
  1.3744 +  unfolding set_plus_def by auto
  1.3745 +
  1.3746 +lemma convex_hull_translation:
  1.3747 +  "convex hull ((\<lambda>x. a + x) ` S) = (\<lambda>x. a + x) ` (convex hull S)"
  1.3748 +  unfolding translation_eq_singleton_plus
  1.3749 +  by (simp only: convex_hull_set_plus convex_hull_singleton)
  1.3750 +
  1.3751 +lemma convex_hull_scaling:
  1.3752 +  "convex hull ((\<lambda>x. c *\<^sub>R x) ` S) = (\<lambda>x. c *\<^sub>R x) ` (convex hull S)"
  1.3753 +  using linear_scaleR by (rule convex_hull_linear_image [symmetric])
  1.3754 +
  1.3755 +lemma convex_hull_affinity:
  1.3756 +  "convex hull ((\<lambda>x. a + c *\<^sub>R x) ` S) = (\<lambda>x. a + c *\<^sub>R x) ` (convex hull S)"
  1.3757 +  by(simp only: image_image[symmetric] convex_hull_scaling convex_hull_translation)
  1.3758 +
  1.3759 +
  1.3760 +subsection%unimportant \<open>Convexity of cone hulls\<close>
  1.3761 +
  1.3762 +lemma convex_cone_hull:
  1.3763 +  assumes "convex S"
  1.3764 +  shows "convex (cone hull S)"
  1.3765 +proof (rule convexI)
  1.3766 +  fix x y
  1.3767 +  assume xy: "x \<in> cone hull S" "y \<in> cone hull S"
  1.3768 +  then have "S \<noteq> {}"
  1.3769 +    using cone_hull_empty_iff[of S] by auto
  1.3770 +  fix u v :: real
  1.3771 +  assume uv: "u \<ge> 0" "v \<ge> 0" "u + v = 1"
  1.3772 +  then have *: "u *\<^sub>R x \<in> cone hull S" "v *\<^sub>R y \<in> cone hull S"
  1.3773 +    using cone_cone_hull[of S] xy cone_def[of "cone hull S"] by auto
  1.3774 +  from * obtain cx :: real and xx where x: "u *\<^sub>R x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  1.3775 +    using cone_hull_expl[of S] by auto
  1.3776 +  from * obtain cy :: real and yy where y: "v *\<^sub>R y = cy *\<^sub>R yy" "cy \<ge> 0" "yy \<in> S"
  1.3777 +    using cone_hull_expl[of S] by auto
  1.3778 +  {
  1.3779 +    assume "cx + cy \<le> 0"
  1.3780 +    then have "u *\<^sub>R x = 0" and "v *\<^sub>R y = 0"
  1.3781 +      using x y by auto
  1.3782 +    then have "u *\<^sub>R x + v *\<^sub>R y = 0"
  1.3783 +      by auto
  1.3784 +    then have "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S"
  1.3785 +      using cone_hull_contains_0[of S] \<open>S \<noteq> {}\<close> by auto
  1.3786 +  }
  1.3787 +  moreover
  1.3788 +  {
  1.3789 +    assume "cx + cy > 0"
  1.3790 +    then have "(cx / (cx + cy)) *\<^sub>R xx + (cy / (cx + cy)) *\<^sub>R yy \<in> S"
  1.3791 +      using assms mem_convex_alt[of S xx yy cx cy] x y by auto
  1.3792 +    then have "cx *\<^sub>R xx + cy *\<^sub>R yy \<in> cone hull S"
  1.3793 +      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.3794 +      by (auto simp: scaleR_right_distrib)
  1.3795 +    then have "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S"
  1.3796 +      using x y by auto
  1.3797 +  }
  1.3798 +  moreover have "cx + cy \<le> 0 \<or> cx + cy > 0" by auto
  1.3799 +  ultimately show "u *\<^sub>R x + v *\<^sub>R y \<in> cone hull S" by blast
  1.3800 +qed
  1.3801 +
  1.3802 +lemma cone_convex_hull:
  1.3803 +  assumes "cone S"
  1.3804 +  shows "cone (convex hull S)"
  1.3805 +proof (cases "S = {}")
  1.3806 +  case True
  1.3807 +  then show ?thesis by auto
  1.3808 +next
  1.3809 +  case False
  1.3810 +  then have *: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (*\<^sub>R) c ` S = S)"
  1.3811 +    using cone_iff[of S] assms by auto
  1.3812 +  {
  1.3813 +    fix c :: real
  1.3814 +    assume "c > 0"
  1.3815 +    then have "(*\<^sub>R) c ` (convex hull S) = convex hull ((*\<^sub>R) c ` S)"
  1.3816 +      using convex_hull_scaling[of _ S] by auto
  1.3817 +    also have "\<dots> = convex hull S"
  1.3818 +      using * \<open>c > 0\<close> by auto
  1.3819 +    finally have "(*\<^sub>R) c ` (convex hull S) = convex hull S"
  1.3820 +      by auto
  1.3821 +  }
  1.3822 +  then have "0 \<in> convex hull S" "\<And>c. c > 0 \<Longrightarrow> ((*\<^sub>R) c ` (convex hull S)) = (convex hull S)"
  1.3823 +    using * hull_subset[of S convex] by auto
  1.3824 +  then show ?thesis
  1.3825 +    using \<open>S \<noteq> {}\<close> cone_iff[of "convex hull S"] by auto
  1.3826 +qed
  1.3827 +
  1.3828 +subsection \<open>Radon's theorem\<close>
  1.3829 +
  1.3830 +text "Formalized by Lars Schewe."
  1.3831 +
  1.3832 +lemma Radon_ex_lemma:
  1.3833 +  assumes "finite c" "affine_dependent c"
  1.3834 +  shows "\<exists>u. sum u c = 0 \<and> (\<exists>v\<in>c. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) c = 0"
  1.3835 +proof -
  1.3836 +  from assms(2)[unfolded affine_dependent_explicit]
  1.3837 +  obtain s u where
  1.3838 +      "finite s" "s \<subseteq> c" "sum u s = 0" "\<exists>v\<in>s. u v \<noteq> 0" "(\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  1.3839 +    by blast
  1.3840 +  then show ?thesis
  1.3841 +    apply (rule_tac x="\<lambda>v. if v\<in>s then u v else 0" in exI)
  1.3842 +    unfolding if_smult scaleR_zero_left and sum.inter_restrict[OF assms(1), symmetric]
  1.3843 +    apply (auto simp: Int_absorb1)
  1.3844 +    done
  1.3845 +qed
  1.3846 +
  1.3847 +lemma Radon_s_lemma:
  1.3848 +  assumes "finite s"
  1.3849 +    and "sum f s = (0::real)"
  1.3850 +  shows "sum f {x\<in>s. 0 < f x} = - sum f {x\<in>s. f x < 0}"
  1.3851 +proof -
  1.3852 +  have *: "\<And>x. (if f x < 0 then f x else 0) + (if 0 < f x then f x else 0) = f x"
  1.3853 +    by auto
  1.3854 +  show ?thesis
  1.3855 +    unfolding add_eq_0_iff[symmetric] and sum.inter_filter[OF assms(1)]
  1.3856 +      and sum.distrib[symmetric] and *
  1.3857 +    using assms(2)
  1.3858 +    by assumption
  1.3859 +qed
  1.3860 +
  1.3861 +lemma Radon_v_lemma:
  1.3862 +  assumes "finite s"
  1.3863 +    and "sum f s = 0"
  1.3864 +    and "\<forall>x. g x = (0::real) \<longrightarrow> f x = (0::'a::euclidean_space)"
  1.3865 +  shows "(sum f {x\<in>s. 0 < g x}) = - sum f {x\<in>s. g x < 0}"
  1.3866 +proof -
  1.3867 +  have *: "\<And>x. (if 0 < g x then f x else 0) + (if g x < 0 then f x else 0) = f x"
  1.3868 +    using assms(3) by auto
  1.3869 +  show ?thesis
  1.3870 +    unfolding eq_neg_iff_add_eq_0 and sum.inter_filter[OF assms(1)]
  1.3871 +      and sum.distrib[symmetric] and *
  1.3872 +    using assms(2)
  1.3873 +    apply assumption
  1.3874 +    done
  1.3875 +qed
  1.3876 +
  1.3877 +lemma Radon_partition:
  1.3878 +  assumes "finite c" "affine_dependent c"
  1.3879 +  shows "\<exists>m p. m \<inter> p = {} \<and> m \<union> p = c \<and> (convex hull m) \<inter> (convex hull p) \<noteq> {}"
  1.3880 +proof -
  1.3881 +  obtain u v where uv: "sum u c = 0" "v\<in>c" "u v \<noteq> 0"  "(\<Sum>v\<in>c. u v *\<^sub>R v) = 0"
  1.3882 +    using Radon_ex_lemma[OF assms] by auto
  1.3883 +  have fin: "finite {x \<in> c. 0 < u x}" "finite {x \<in> c. 0 > u x}"
  1.3884 +    using assms(1) by auto
  1.3885 +  define z  where "z = inverse (sum u {x\<in>c. u x > 0}) *\<^sub>R sum (\<lambda>x. u x *\<^sub>R x) {x\<in>c. u x > 0}"
  1.3886 +  have "sum u {x \<in> c. 0 < u x} \<noteq> 0"
  1.3887 +  proof (cases "u v \<ge> 0")
  1.3888 +    case False
  1.3889 +    then have "u v < 0" by auto
  1.3890 +    then show ?thesis
  1.3891 +    proof (cases "\<exists>w\<in>{x \<in> c. 0 < u x}. u w > 0")
  1.3892 +      case True
  1.3893 +      then show ?thesis
  1.3894 +        using sum_nonneg_eq_0_iff[of _ u, OF fin(1)] by auto
  1.3895 +    next
  1.3896 +      case False
  1.3897 +      then have "sum u c \<le> sum (\<lambda>x. if x=v then u v else 0) c"
  1.3898 +        apply (rule_tac sum_mono, auto)
  1.3899 +        done
  1.3900 +      then show ?thesis
  1.3901 +        unfolding sum.delta[OF assms(1)] using uv(2) and \<open>u v < 0\<close> and uv(1) by auto
  1.3902 +    qed
  1.3903 +  qed (insert sum_nonneg_eq_0_iff[of _ u, OF fin(1)] uv(2-3), auto)
  1.3904 +
  1.3905 +  then have *: "sum u {x\<in>c. u x > 0} > 0"
  1.3906 +    unfolding less_le
  1.3907 +    apply (rule_tac conjI)
  1.3908 +    apply (rule_tac sum_nonneg, auto)
  1.3909 +    done
  1.3910 +  moreover have "sum u ({x \<in> c. 0 < u x} \<union> {x \<in> c. u x < 0}) = sum u c"
  1.3911 +    "(\<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.3912 +    using assms(1)
  1.3913 +    apply (rule_tac[!] sum.mono_neutral_left, auto)
  1.3914 +    done
  1.3915 +  then have "sum u {x \<in> c. 0 < u x} = - sum u {x \<in> c. 0 > u x}"
  1.3916 +    "(\<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.3917 +    unfolding eq_neg_iff_add_eq_0
  1.3918 +    using uv(1,4)
  1.3919 +    by (auto simp: sum.union_inter_neutral[OF fin, symmetric])
  1.3920 +  moreover have "\<forall>x\<in>{v \<in> c. u v < 0}. 0 \<le> inverse (sum u {x \<in> c. 0 < u x}) * - u x"
  1.3921 +    apply rule
  1.3922 +    apply (rule mult_nonneg_nonneg)
  1.3923 +    using *
  1.3924 +    apply auto
  1.3925 +    done
  1.3926 +  ultimately have "z \<in> convex hull {v \<in> c. u v \<le> 0}"
  1.3927 +    unfolding convex_hull_explicit mem_Collect_eq
  1.3928 +    apply (rule_tac x="{v \<in> c. u v < 0}" in exI)
  1.3929 +    apply (rule_tac x="\<lambda>y. inverse (sum u {x\<in>c. u x > 0}) * - u y" in exI)
  1.3930 +    using assms(1) unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] and z_def
  1.3931 +    apply (auto simp: sum_negf sum_distrib_left[symmetric])
  1.3932 +    done
  1.3933 +  moreover have "\<forall>x\<in>{v \<in> c. 0 < u v}. 0 \<le> inverse (sum u {x \<in> c. 0 < u x}) * u x"
  1.3934 +    apply rule
  1.3935 +    apply (rule mult_nonneg_nonneg)
  1.3936 +    using *
  1.3937 +    apply auto
  1.3938 +    done
  1.3939 +  then have "z \<in> convex hull {v \<in> c. u v > 0}"
  1.3940 +    unfolding convex_hull_explicit mem_Collect_eq
  1.3941 +    apply (rule_tac x="{v \<in> c. 0 < u v}" in exI)
  1.3942 +    apply (rule_tac x="\<lambda>y. inverse (sum u {x\<in>c. u x > 0}) * u y" in exI)
  1.3943 +    using assms(1)
  1.3944 +    unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] and z_def
  1.3945 +    using *
  1.3946 +    apply (auto simp: sum_negf sum_distrib_left[symmetric])
  1.3947 +    done
  1.3948 +  ultimately show ?thesis
  1.3949 +    apply (rule_tac x="{v\<in>c. u v \<le> 0}" in exI)
  1.3950 +    apply (rule_tac x="{v\<in>c. u v > 0}" in exI, auto)
  1.3951 +    done
  1.3952 +qed
  1.3953 +
  1.3954 +theorem Radon:
  1.3955 +  assumes "affine_dependent c"
  1.3956 +  obtains m p where "m \<subseteq> c" "p \<subseteq> c" "m \<inter> p = {}" "(convex hull m) \<inter> (convex hull p) \<noteq> {}"
  1.3957 +proof -
  1.3958 +  from assms[unfolded affine_dependent_explicit]
  1.3959 +  obtain s u where
  1.3960 +      "finite s" "s \<subseteq> c" "sum u s = 0" "\<exists>v\<in>s. u v \<noteq> 0" "(\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  1.3961 +    by blast
  1.3962 +  then have *: "finite s" "affine_dependent s" and s: "s \<subseteq> c"
  1.3963 +    unfolding affine_dependent_explicit by auto
  1.3964 +  from Radon_partition[OF *]
  1.3965 +  obtain m p where "m \<inter> p = {}" "m \<union> p = s" "convex hull m \<inter> convex hull p \<noteq> {}"
  1.3966 +    by blast
  1.3967 +  then show ?thesis
  1.3968 +    apply (rule_tac that[of p m])
  1.3969 +    using s
  1.3970 +    apply auto
  1.3971 +    done
  1.3972 +qed
  1.3973 +
  1.3974 +
  1.3975 +subsection \<open>Helly's theorem\<close>
  1.3976 +
  1.3977 +lemma Helly_induct:
  1.3978 +  fixes f :: "'a::euclidean_space set set"
  1.3979 +  assumes "card f = n"
  1.3980 +    and "n \<ge> DIM('a) + 1"
  1.3981 +    and "\<forall>s\<in>f. convex s" "\<forall>t\<subseteq>f. card t = DIM('a) + 1 \<longrightarrow> \<Inter>t \<noteq> {}"
  1.3982 +  shows "\<Inter>f \<noteq> {}"
  1.3983 +  using assms
  1.3984 +proof (induction n arbitrary: f)
  1.3985 +  case 0
  1.3986 +  then show ?case by auto
  1.3987 +next
  1.3988 +  case (Suc n)
  1.3989 +  have "finite f"
  1.3990 +    using \<open>card f = Suc n\<close> by (auto intro: card_ge_0_finite)
  1.3991 +  show "\<Inter>f \<noteq> {}"
  1.3992 +  proof (cases "n = DIM('a)")
  1.3993 +    case True
  1.3994 +    then show ?thesis
  1.3995 +      by (simp add: Suc.prems(1) Suc.prems(4))
  1.3996 +  next
  1.3997 +    case False
  1.3998 +    have "\<Inter>(f - {s}) \<noteq> {}" if "s \<in> f" for s
  1.3999 +    proof (rule Suc.IH[rule_format])
  1.4000 +      show "card (f - {s}) = n"
  1.4001 +        by (simp add: Suc.prems(1) \<open>finite f\<close> that)
  1.4002 +      show "DIM('a) + 1 \<le> n"
  1.4003 +        using False Suc.prems(2) by linarith
  1.4004 +      show "\<And>t. \<lbrakk>t \<subseteq> f - {s}; card t = DIM('a) + 1\<rbrakk> \<Longrightarrow> \<Inter>t \<noteq> {}"
  1.4005 +        by (simp add: Suc.prems(4) subset_Diff_insert)
  1.4006 +    qed (use Suc in auto)
  1.4007 +    then have "\<forall>s\<in>f. \<exists>x. x \<in> \<Inter>(f - {s})"
  1.4008 +      by blast
  1.4009 +    then obtain X where X: "\<And>s. s\<in>f \<Longrightarrow> X s \<in> \<Inter>(f - {s})"
  1.4010 +      by metis
  1.4011 +    show ?thesis
  1.4012 +    proof (cases "inj_on X f")
  1.4013 +      case False
  1.4014 +      then obtain s t where "s\<noteq>t" and st: "s\<in>f" "t\<in>f" "X s = X t"
  1.4015 +        unfolding inj_on_def by auto
  1.4016 +      then have *: "\<Inter>f = \<Inter>(f - {s}) \<inter> \<Inter>(f - {t})" by auto
  1.4017 +      show ?thesis
  1.4018 +        by (metis "*" X disjoint_iff_not_equal st)
  1.4019 +    next
  1.4020 +      case True
  1.4021 +      then obtain m p where mp: "m \<inter> p = {}" "m \<union> p = X ` f" "convex hull m \<inter> convex hull p \<noteq> {}"
  1.4022 +        using Radon_partition[of "X ` f"] and affine_dependent_biggerset[of "X ` f"]
  1.4023 +        unfolding card_image[OF True] and \<open>card f = Suc n\<close>
  1.4024 +        using Suc(3) \<open>finite f\<close> and False
  1.4025 +        by auto
  1.4026 +      have "m \<subseteq> X ` f" "p \<subseteq> X ` f"
  1.4027 +        using mp(2) by auto
  1.4028 +      then obtain g h where gh:"m = X ` g" "p = X ` h" "g \<subseteq> f" "h \<subseteq> f"
  1.4029 +        unfolding subset_image_iff by auto
  1.4030 +      then have "f \<union> (g \<union> h) = f" by auto
  1.4031 +      then have f: "f = g \<union> h"
  1.4032 +        using inj_on_Un_image_eq_iff[of X f "g \<union> h"] and True
  1.4033 +        unfolding mp(2)[unfolded image_Un[symmetric] gh]
  1.4034 +        by auto
  1.4035 +      have *: "g \<inter> h = {}"
  1.4036 +        using mp(1)
  1.4037 +        unfolding gh
  1.4038 +        using inj_on_image_Int[OF True gh(3,4)]
  1.4039 +        by auto
  1.4040 +      have "convex hull (X ` h) \<subseteq> \<Inter>g" "convex hull (X ` g) \<subseteq> \<Inter>h"
  1.4041 +        by (rule hull_minimal; use X * f in \<open>auto simp: Suc.prems(3) convex_Inter\<close>)+
  1.4042 +      then show ?thesis
  1.4043 +        unfolding f using mp(3)[unfolded gh] by blast
  1.4044 +    qed
  1.4045 +  qed 
  1.4046 +qed
  1.4047 +
  1.4048 +theorem Helly:
  1.4049 +  fixes f :: "'a::euclidean_space set set"
  1.4050 +  assumes "card f \<ge> DIM('a) + 1" "\<forall>s\<in>f. convex s"
  1.4051 +    and "\<And>t. \<lbrakk>t\<subseteq>f; card t = DIM('a) + 1\<rbrakk> \<Longrightarrow> \<Inter>t \<noteq> {}"
  1.4052 +  shows "\<Inter>f \<noteq> {}"
  1.4053 +  apply (rule Helly_induct)
  1.4054 +  using assms
  1.4055 +  apply auto
  1.4056 +  done
  1.4057 +
  1.4058 +subsection \<open>Epigraphs of convex functions\<close>
  1.4059 +
  1.4060 +definition%important "epigraph S (f :: _ \<Rightarrow> real) = {xy. fst xy \<in> S \<and> f (fst xy) \<le> snd xy}"
  1.4061 +
  1.4062 +lemma mem_epigraph: "(x, y) \<in> epigraph S f \<longleftrightarrow> x \<in> S \<and> f x \<le> y"
  1.4063 +  unfolding epigraph_def by auto
  1.4064 +
  1.4065 +lemma convex_epigraph: "convex (epigraph S f) \<longleftrightarrow> convex_on S f \<and> convex S"
  1.4066 +proof safe
  1.4067 +  assume L: "convex (epigraph S f)"
  1.4068 +  then show "convex_on S f"
  1.4069 +    by (auto simp: convex_def convex_on_def epigraph_def)
  1.4070 +  show "convex S"
  1.4071 +    using L
  1.4072 +    apply (clarsimp simp: convex_def convex_on_def epigraph_def)
  1.4073 +    apply (erule_tac x=x in allE)
  1.4074 +    apply (erule_tac x="f x" in allE, safe)
  1.4075 +    apply (erule_tac x=y in allE)
  1.4076 +    apply (erule_tac x="f y" in allE)
  1.4077 +    apply (auto simp: )
  1.4078 +    done
  1.4079 +next
  1.4080 +  assume "convex_on S f" "convex S"
  1.4081 +  then show "convex (epigraph S f)"
  1.4082 +    unfolding convex_def convex_on_def epigraph_def
  1.4083 +    apply safe
  1.4084 +     apply (rule_tac [2] y="u * f a + v * f aa" in order_trans)
  1.4085 +      apply (auto intro!:mult_left_mono add_mono)
  1.4086 +    done
  1.4087 +qed
  1.4088 +
  1.4089 +lemma convex_epigraphI: "convex_on S f \<Longrightarrow> convex S \<Longrightarrow> convex (epigraph S f)"
  1.4090 +  unfolding convex_epigraph by auto
  1.4091 +
  1.4092 +lemma convex_epigraph_convex: "convex S \<Longrightarrow> convex_on S f \<longleftrightarrow> convex(epigraph S f)"
  1.4093 +  by (simp add: convex_epigraph)
  1.4094 +
  1.4095 +
  1.4096 +subsubsection%unimportant \<open>Use this to derive general bound property of convex function\<close>
  1.4097 +
  1.4098 +lemma convex_on:
  1.4099 +  assumes "convex S"
  1.4100 +  shows "convex_on S f \<longleftrightarrow>
  1.4101 +    (\<forall>k u x. (\<forall>i\<in>{1..k::nat}. 0 \<le> u i \<and> x i \<in> S) \<and> sum u {1..k} = 1 \<longrightarrow>
  1.4102 +      f (sum (\<lambda>i. u i *\<^sub>R x i) {1..k}) \<le> sum (\<lambda>i. u i * f(x i)) {1..k})"
  1.4103 +
  1.4104 +  unfolding convex_epigraph_convex[OF assms] convex epigraph_def Ball_def mem_Collect_eq
  1.4105 +  unfolding fst_sum snd_sum fst_scaleR snd_scaleR
  1.4106 +  apply safe
  1.4107 +    apply (drule_tac x=k in spec)
  1.4108 +    apply (drule_tac x=u in spec)
  1.4109 +    apply (drule_tac x="\<lambda>i. (x i, f (x i))" in spec)
  1.4110 +    apply simp
  1.4111 +  using assms[unfolded convex] apply simp
  1.4112 +  apply (rule_tac y="\<Sum>i = 1..k. u i * f (fst (x i))" in order_trans, force)
  1.4113 +   apply (rule sum_mono)
  1.4114 +   apply (erule_tac x=i in allE)
  1.4115 +  unfolding real_scaleR_def
  1.4116 +   apply (rule mult_left_mono)
  1.4117 +  using assms[unfolded convex] apply auto
  1.4118 +  done
  1.4119 +
  1.4120 +subsection%unimportant \<open>A bound within a convex hull\<close>
  1.4121 +
  1.4122 +lemma convex_on_convex_hull_bound:
  1.4123 +  assumes "convex_on (convex hull s) f"
  1.4124 +    and "\<forall>x\<in>s. f x \<le> b"
  1.4125 +  shows "\<forall>x\<in> convex hull s. f x \<le> b"
  1.4126 +proof
  1.4127 +  fix x
  1.4128 +  assume "x \<in> convex hull s"
  1.4129 +  then obtain k u v where
  1.4130 +    obt: "\<forall>i\<in>{1..k::nat}. 0 \<le> u i \<and> v i \<in> s" "sum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R v i) = x"
  1.4131 +    unfolding convex_hull_indexed mem_Collect_eq by auto
  1.4132 +  have "(\<Sum>i = 1..k. u i * f (v i)) \<le> b"
  1.4133 +    using sum_mono[of "{1..k}" "\<lambda>i. u i * f (v i)" "\<lambda>i. u i * b"]
  1.4134 +    unfolding sum_distrib_right[symmetric] obt(2) mult_1
  1.4135 +    apply (drule_tac meta_mp)
  1.4136 +    apply (rule mult_left_mono)
  1.4137 +    using assms(2) obt(1)
  1.4138 +    apply auto
  1.4139 +    done
  1.4140 +  then show "f x \<le> b"
  1.4141 +    using assms(1)[unfolded convex_on[OF convex_convex_hull], rule_format, of k u v]
  1.4142 +    unfolding obt(2-3)
  1.4143 +    using obt(1) and hull_subset[unfolded subset_eq, rule_format, of _ s]
  1.4144 +    by auto
  1.4145 +qed
  1.4146 +
  1.4147 +lemma inner_sum_Basis[simp]: "i \<in> Basis \<Longrightarrow> (\<Sum>Basis) \<bullet> i = 1"
  1.4148 +  by (simp add: inner_sum_left sum.If_cases inner_Basis)
  1.4149 +
  1.4150 +lemma convex_set_plus:
  1.4151 +  assumes "convex S" and "convex T" shows "convex (S + T)"
  1.4152 +proof -
  1.4153 +  have "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
  1.4154 +    using assms by (rule convex_sums)
  1.4155 +  moreover have "(\<Union>x\<in> S. \<Union>y \<in> T. {x + y}) = S + T"
  1.4156 +    unfolding set_plus_def by auto
  1.4157 +  finally show "convex (S + T)" .
  1.4158 +qed
  1.4159 +
  1.4160 +lemma convex_set_sum:
  1.4161 +  assumes "\<And>i. i \<in> A \<Longrightarrow> convex (B i)"
  1.4162 +  shows "convex (\<Sum>i\<in>A. B i)"
  1.4163 +proof (cases "finite A")
  1.4164 +  case True then show ?thesis using assms
  1.4165 +    by induct (auto simp: convex_set_plus)
  1.4166 +qed auto
  1.4167 +
  1.4168 +lemma finite_set_sum:
  1.4169 +  assumes "finite A" and "\<forall>i\<in>A. finite (B i)" shows "finite (\<Sum>i\<in>A. B i)"
  1.4170 +  using assms by (induct set: finite, simp, simp add: finite_set_plus)
  1.4171 +
  1.4172 +lemma box_eq_set_sum_Basis:
  1.4173 +  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.4174 +  apply (subst set_sum_alt [OF finite_Basis], safe)
  1.4175 +  apply (fast intro: euclidean_representation [symmetric])
  1.4176 +  apply (subst inner_sum_left)
  1.4177 +apply (rename_tac f)
  1.4178 +  apply (subgoal_tac "(\<Sum>x\<in>Basis. f x \<bullet> i) = f i \<bullet> i")
  1.4179 +  apply (drule (1) bspec)
  1.4180 +  apply clarsimp
  1.4181 +  apply (frule sum.remove [OF finite_Basis])
  1.4182 +  apply (erule trans, simp)
  1.4183 +  apply (rule sum.neutral, clarsimp)
  1.4184 +  apply (frule_tac x=i in bspec, assumption)
  1.4185 +  apply (drule_tac x=x in bspec, assumption, clarsimp)
  1.4186 +  apply (cut_tac u=x and v=i in inner_Basis, assumption+)
  1.4187 +  apply (rule ccontr, simp)
  1.4188 +  done
  1.4189 +
  1.4190 +lemma convex_hull_set_sum:
  1.4191 +  "convex hull (\<Sum>i\<in>A. B i) = (\<Sum>i\<in>A. convex hull (B i))"
  1.4192 +proof (cases "finite A")
  1.4193 +  assume "finite A" then show ?thesis
  1.4194 +    by (induct set: finite, simp, simp add: convex_hull_set_plus)
  1.4195 +qed simp
  1.4196 +
  1.4197 +
  1.4198 +end
  1.4199 \ No newline at end of file
     2.1 --- a/src/HOL/Analysis/Convex_Euclidean_Space.thy	Mon Jan 07 13:33:29 2019 +0100
     2.2 +++ b/src/HOL/Analysis/Convex_Euclidean_Space.thy	Mon Jan 07 14:06:54 2019 +0100
     2.3 @@ -6,1143 +6,15 @@
     2.4     Author:     Johannes Hoelzl, TU Muenchen
     2.5  *)
     2.6  
     2.7 -section \<open>Convex Sets and Functions\<close>
     2.8 +section \<open>Convex Sets and Functions on (Normed) Euclidean Spaces\<close>
     2.9  
    2.10  theory Convex_Euclidean_Space
    2.11  imports
    2.12 +  Convex
    2.13    Topology_Euclidean_Space
    2.14 -  "HOL-Library.Set_Algebras"
    2.15  begin
    2.16  
    2.17 -lemma swap_continuous: (*move to Topological_Spaces?*)
    2.18 -  assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
    2.19 -    shows "continuous_on (cbox (c,a) (d,b)) (\<lambda>(x, y). f y x)"
    2.20 -proof -
    2.21 -  have "(\<lambda>(x, y). f y x) = (\<lambda>(x, y). f x y) \<circ> prod.swap"
    2.22 -    by auto
    2.23 -  then show ?thesis
    2.24 -    apply (rule ssubst)
    2.25 -    apply (rule continuous_on_compose)
    2.26 -    apply (simp add: split_def)
    2.27 -    apply (rule continuous_intros | simp add: assms)+
    2.28 -    done
    2.29 -qed
    2.30 -
    2.31 -lemma substdbasis_expansion_unique:
    2.32 -  assumes d: "d \<subseteq> Basis"
    2.33 -  shows "(\<Sum>i\<in>d. f i *\<^sub>R i) = (x::'a::euclidean_space) \<longleftrightarrow>
    2.34 -    (\<forall>i\<in>Basis. (i \<in> d \<longrightarrow> f i = x \<bullet> i) \<and> (i \<notin> d \<longrightarrow> x \<bullet> i = 0))"
    2.35 -proof -
    2.36 -  have *: "\<And>x a b P. x * (if P then a else b) = (if P then x * a else x * b)"
    2.37 -    by auto
    2.38 -  have **: "finite d"
    2.39 -    by (auto intro: finite_subset[OF assms])
    2.40 -  have ***: "\<And>i. i \<in> Basis \<Longrightarrow> (\<Sum>i\<in>d. f i *\<^sub>R i) \<bullet> i = (\<Sum>x\<in>d. if x = i then f x else 0)"
    2.41 -    using d
    2.42 -    by (auto intro!: sum.cong simp: inner_Basis inner_sum_left)
    2.43 -  show ?thesis
    2.44 -    unfolding euclidean_eq_iff[where 'a='a] by (auto simp: sum.delta[OF **] ***)
    2.45 -qed
    2.46 -
    2.47 -lemma independent_substdbasis: "d \<subseteq> Basis \<Longrightarrow> independent d"
    2.48 -  by (rule independent_mono[OF independent_Basis])
    2.49 -
    2.50 -lemma dim_cball:
    2.51 -  assumes "e > 0"
    2.52 -  shows "dim (cball (0 :: 'n::euclidean_space) e) = DIM('n)"
    2.53 -proof -
    2.54 -  {
    2.55 -    fix x :: "'n::euclidean_space"
    2.56 -    define y where "y = (e / norm x) *\<^sub>R x"
    2.57 -    then have "y \<in> cball 0 e"
    2.58 -      using assms by auto
    2.59 -    moreover have *: "x = (norm x / e) *\<^sub>R y"
    2.60 -      using y_def assms by simp
    2.61 -    moreover from * have "x = (norm x/e) *\<^sub>R y"
    2.62 -      by auto
    2.63 -    ultimately have "x \<in> span (cball 0 e)"
    2.64 -      using span_scale[of y "cball 0 e" "norm x/e"]
    2.65 -        span_superset[of "cball 0 e"]
    2.66 -      by (simp add: span_base)
    2.67 -  }
    2.68 -  then have "span (cball 0 e) = (UNIV :: 'n::euclidean_space set)"
    2.69 -    by auto
    2.70 -  then show ?thesis
    2.71 -    using dim_span[of "cball (0 :: 'n::euclidean_space) e"] by (auto simp: dim_UNIV)
    2.72 -qed
    2.73 -
    2.74 -lemma sum_not_0: "sum f A \<noteq> 0 \<Longrightarrow> \<exists>a \<in> A. f a \<noteq> 0"
    2.75 -  by (rule ccontr) auto
    2.76 -
    2.77 -lemma subset_translation_eq [simp]:
    2.78 -    fixes a :: "'a::real_vector" shows "(+) a ` s \<subseteq> (+) a ` t \<longleftrightarrow> s \<subseteq> t"
    2.79 -  by auto
    2.80 -
    2.81 -lemma translate_inj_on:
    2.82 -  fixes A :: "'a::ab_group_add set"
    2.83 -  shows "inj_on (\<lambda>x. a + x) A"
    2.84 -  unfolding inj_on_def by auto
    2.85 -
    2.86 -lemma translation_assoc:
    2.87 -  fixes a b :: "'a::ab_group_add"
    2.88 -  shows "(\<lambda>x. b + x) ` ((\<lambda>x. a + x) ` S) = (\<lambda>x. (a + b) + x) ` S"
    2.89 -  by auto
    2.90 -
    2.91 -lemma translation_invert:
    2.92 -  fixes a :: "'a::ab_group_add"
    2.93 -  assumes "(\<lambda>x. a + x) ` A = (\<lambda>x. a + x) ` B"
    2.94 -  shows "A = B"
    2.95 -proof -
    2.96 -  have "(\<lambda>x. -a + x) ` ((\<lambda>x. a + x) ` A) = (\<lambda>x. - a + x) ` ((\<lambda>x. a + x) ` B)"
    2.97 -    using assms by auto
    2.98 -  then show ?thesis
    2.99 -    using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto
   2.100 -qed
   2.101 -
   2.102 -lemma translation_galois:
   2.103 -  fixes a :: "'a::ab_group_add"
   2.104 -  shows "T = ((\<lambda>x. a + x) ` S) \<longleftrightarrow> S = ((\<lambda>x. (- a) + x) ` T)"
   2.105 -  using translation_assoc[of "-a" a S]
   2.106 -  apply auto
   2.107 -  using translation_assoc[of a "-a" T]
   2.108 -  apply auto
   2.109 -  done
   2.110 -
   2.111 -lemma translation_inverse_subset:
   2.112 -  assumes "((\<lambda>x. - a + x) ` V) \<le> (S :: 'n::ab_group_add set)"
   2.113 -  shows "V \<le> ((\<lambda>x. a + x) ` S)"
   2.114 -proof -
   2.115 -  {
   2.116 -    fix x
   2.117 -    assume "x \<in> V"
   2.118 -    then have "x-a \<in> S" using assms by auto
   2.119 -    then have "x \<in> {a + v |v. v \<in> S}"
   2.120 -      apply auto
   2.121 -      apply (rule exI[of _ "x-a"], simp)
   2.122 -      done
   2.123 -    then have "x \<in> ((\<lambda>x. a+x) ` S)" by auto
   2.124 -  }
   2.125 -  then show ?thesis by auto
   2.126 -qed
   2.127 -
   2.128 -subsection \<open>Convexity\<close>
   2.129 -
   2.130 -definition%important convex :: "'a::real_vector set \<Rightarrow> bool"
   2.131 -  where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
   2.132 -
   2.133 -lemma convexI:
   2.134 -  assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   2.135 -  shows "convex s"
   2.136 -  using assms unfolding convex_def by fast
   2.137 -
   2.138 -lemma convexD:
   2.139 -  assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
   2.140 -  shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   2.141 -  using assms unfolding convex_def by fast
   2.142 -
   2.143 -lemma convex_alt: "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
   2.144 -  (is "_ \<longleftrightarrow> ?alt")
   2.145 -proof
   2.146 -  show "convex s" if alt: ?alt
   2.147 -  proof -
   2.148 -    {
   2.149 -      fix x y and u v :: real
   2.150 -      assume mem: "x \<in> s" "y \<in> s"
   2.151 -      assume "0 \<le> u" "0 \<le> v"
   2.152 -      moreover
   2.153 -      assume "u + v = 1"
   2.154 -      then have "u = 1 - v" by auto
   2.155 -      ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
   2.156 -        using alt [rule_format, OF mem] by auto
   2.157 -    }
   2.158 -    then show ?thesis
   2.159 -      unfolding convex_def by auto
   2.160 -  qed
   2.161 -  show ?alt if "convex s"
   2.162 -    using that by (auto simp: convex_def)
   2.163 -qed
   2.164 -
   2.165 -lemma convexD_alt:
   2.166 -  assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
   2.167 -  shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
   2.168 -  using assms unfolding convex_alt by auto
   2.169 -
   2.170 -lemma mem_convex_alt:
   2.171 -  assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
   2.172 -  shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
   2.173 -  apply (rule convexD)
   2.174 -  using assms
   2.175 -       apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
   2.176 -  done
   2.177 -
   2.178 -lemma convex_empty[intro,simp]: "convex {}"
   2.179 -  unfolding convex_def by simp
   2.180 -
   2.181 -lemma convex_singleton[intro,simp]: "convex {a}"
   2.182 -  unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
   2.183 -
   2.184 -lemma convex_UNIV[intro,simp]: "convex UNIV"
   2.185 -  unfolding convex_def by auto
   2.186 -
   2.187 -lemma convex_Inter: "(\<And>s. s\<in>f \<Longrightarrow> convex s) \<Longrightarrow> convex(\<Inter>f)"
   2.188 -  unfolding convex_def by auto
   2.189 -
   2.190 -lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
   2.191 -  unfolding convex_def by auto
   2.192 -
   2.193 -lemma convex_INT: "(\<And>i. i \<in> A \<Longrightarrow> convex (B i)) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
   2.194 -  unfolding convex_def by auto
   2.195 -
   2.196 -lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
   2.197 -  unfolding convex_def by auto
   2.198 -
   2.199 -lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
   2.200 -  unfolding convex_def
   2.201 -  by (auto simp: inner_add intro!: convex_bound_le)
   2.202 -
   2.203 -lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
   2.204 -proof -
   2.205 -  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
   2.206 -    by auto
   2.207 -  show ?thesis
   2.208 -    unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
   2.209 -qed
   2.210 -
   2.211 -lemma convex_halfspace_abs_le: "convex {x. \<bar>inner a x\<bar> \<le> b}"
   2.212 -proof -
   2.213 -  have *: "{x. \<bar>inner a x\<bar> \<le> b} = {x. inner a x \<le> b} \<inter> {x. -b \<le> inner a x}"
   2.214 -    by auto
   2.215 -  show ?thesis
   2.216 -    unfolding * by (simp add: convex_Int convex_halfspace_ge convex_halfspace_le)
   2.217 -qed
   2.218 -
   2.219 -lemma convex_hyperplane: "convex {x. inner a x = b}"
   2.220 -proof -
   2.221 -  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
   2.222 -    by auto
   2.223 -  show ?thesis using convex_halfspace_le convex_halfspace_ge
   2.224 -    by (auto intro!: convex_Int simp: *)
   2.225 -qed
   2.226 -
   2.227 -lemma convex_halfspace_lt: "convex {x. inner a x < b}"
   2.228 -  unfolding convex_def
   2.229 -  by (auto simp: convex_bound_lt inner_add)
   2.230 -
   2.231 -lemma convex_halfspace_gt: "convex {x. inner a x > b}"
   2.232 -  using convex_halfspace_lt[of "-a" "-b"] by auto
   2.233 -
   2.234 -lemma convex_halfspace_Re_ge: "convex {x. Re x \<ge> b}"
   2.235 -  using convex_halfspace_ge[of b "1::complex"] by simp
   2.236 -
   2.237 -lemma convex_halfspace_Re_le: "convex {x. Re x \<le> b}"
   2.238 -  using convex_halfspace_le[of "1::complex" b] by simp
   2.239 -
   2.240 -lemma convex_halfspace_Im_ge: "convex {x. Im x \<ge> b}"
   2.241 -  using convex_halfspace_ge[of b \<i>] by simp
   2.242 -
   2.243 -lemma convex_halfspace_Im_le: "convex {x. Im x \<le> b}"
   2.244 -  using convex_halfspace_le[of \<i> b] by simp
   2.245 -
   2.246 -lemma convex_halfspace_Re_gt: "convex {x. Re x > b}"
   2.247 -  using convex_halfspace_gt[of b "1::complex"] by simp
   2.248 -
   2.249 -lemma convex_halfspace_Re_lt: "convex {x. Re x < b}"
   2.250 -  using convex_halfspace_lt[of "1::complex" b] by simp
   2.251 -
   2.252 -lemma convex_halfspace_Im_gt: "convex {x. Im x > b}"
   2.253 -  using convex_halfspace_gt[of b \<i>] by simp
   2.254 -
   2.255 -lemma convex_halfspace_Im_lt: "convex {x. Im x < b}"
   2.256 -  using convex_halfspace_lt[of \<i> b] by simp
   2.257 -
   2.258 -lemma convex_real_interval [iff]:
   2.259 -  fixes a b :: "real"
   2.260 -  shows "convex {a..}" and "convex {..b}"
   2.261 -    and "convex {a<..}" and "convex {..<b}"
   2.262 -    and "convex {a..b}" and "convex {a<..b}"
   2.263 -    and "convex {a..<b}" and "convex {a<..<b}"
   2.264 -proof -
   2.265 -  have "{a..} = {x. a \<le> inner 1 x}"
   2.266 -    by auto
   2.267 -  then show 1: "convex {a..}"
   2.268 -    by (simp only: convex_halfspace_ge)
   2.269 -  have "{..b} = {x. inner 1 x \<le> b}"
   2.270 -    by auto
   2.271 -  then show 2: "convex {..b}"
   2.272 -    by (simp only: convex_halfspace_le)
   2.273 -  have "{a<..} = {x. a < inner 1 x}"
   2.274 -    by auto
   2.275 -  then show 3: "convex {a<..}"
   2.276 -    by (simp only: convex_halfspace_gt)
   2.277 -  have "{..<b} = {x. inner 1 x < b}"
   2.278 -    by auto
   2.279 -  then show 4: "convex {..<b}"
   2.280 -    by (simp only: convex_halfspace_lt)
   2.281 -  have "{a..b} = {a..} \<inter> {..b}"
   2.282 -    by auto
   2.283 -  then show "convex {a..b}"
   2.284 -    by (simp only: convex_Int 1 2)
   2.285 -  have "{a<..b} = {a<..} \<inter> {..b}"
   2.286 -    by auto
   2.287 -  then show "convex {a<..b}"
   2.288 -    by (simp only: convex_Int 3 2)
   2.289 -  have "{a..<b} = {a..} \<inter> {..<b}"
   2.290 -    by auto
   2.291 -  then show "convex {a..<b}"
   2.292 -    by (simp only: convex_Int 1 4)
   2.293 -  have "{a<..<b} = {a<..} \<inter> {..<b}"
   2.294 -    by auto
   2.295 -  then show "convex {a<..<b}"
   2.296 -    by (simp only: convex_Int 3 4)
   2.297 -qed
   2.298 -
   2.299 -lemma convex_Reals: "convex \<real>"
   2.300 -  by (simp add: convex_def scaleR_conv_of_real)
   2.301 -
   2.302 -
   2.303 -subsection%unimportant \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
   2.304 -
   2.305 -lemma convex_sum:
   2.306 -  fixes C :: "'a::real_vector set"
   2.307 -  assumes "finite s"
   2.308 -    and "convex C"
   2.309 -    and "(\<Sum> i \<in> s. a i) = 1"
   2.310 -  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   2.311 -    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   2.312 -  shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   2.313 -  using assms(1,3,4,5)
   2.314 -proof (induct arbitrary: a set: finite)
   2.315 -  case empty
   2.316 -  then show ?case by simp
   2.317 -next
   2.318 -  case (insert i s) note IH = this(3)
   2.319 -  have "a i + sum a s = 1"
   2.320 -    and "0 \<le> a i"
   2.321 -    and "\<forall>j\<in>s. 0 \<le> a j"
   2.322 -    and "y i \<in> C"
   2.323 -    and "\<forall>j\<in>s. y j \<in> C"
   2.324 -    using insert.hyps(1,2) insert.prems by simp_all
   2.325 -  then have "0 \<le> sum a s"
   2.326 -    by (simp add: sum_nonneg)
   2.327 -  have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
   2.328 -  proof (cases "sum a s = 0")
   2.329 -    case True
   2.330 -    with \<open>a i + sum a s = 1\<close> have "a i = 1"
   2.331 -      by simp
   2.332 -    from sum_nonneg_0 [OF \<open>finite s\<close> _ True] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
   2.333 -      by simp
   2.334 -    show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
   2.335 -      by simp
   2.336 -  next
   2.337 -    case False
   2.338 -    with \<open>0 \<le> sum a s\<close> have "0 < sum a s"
   2.339 -      by simp
   2.340 -    then have "(\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   2.341 -      using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
   2.342 -      by (simp add: IH sum_divide_distrib [symmetric])
   2.343 -    from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
   2.344 -      and \<open>0 \<le> sum a s\<close> and \<open>a i + sum a s = 1\<close>
   2.345 -    have "a i *\<^sub>R y i + sum a s *\<^sub>R (\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
   2.346 -      by (rule convexD)
   2.347 -    then show ?thesis
   2.348 -      by (simp add: scaleR_sum_right False)
   2.349 -  qed
   2.350 -  then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
   2.351 -    by simp
   2.352 -qed
   2.353 -
   2.354 -lemma convex:
   2.355 -  "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (sum u {1..k} = 1)
   2.356 -      \<longrightarrow> sum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   2.357 -proof safe
   2.358 -  fix k :: nat
   2.359 -  fix u :: "nat \<Rightarrow> real"
   2.360 -  fix x
   2.361 -  assume "convex s"
   2.362 -    "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   2.363 -    "sum u {1..k} = 1"
   2.364 -  with convex_sum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
   2.365 -    by auto
   2.366 -next
   2.367 -  assume *: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> sum u {1..k} = 1
   2.368 -    \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   2.369 -  {
   2.370 -    fix \<mu> :: real
   2.371 -    fix x y :: 'a
   2.372 -    assume xy: "x \<in> s" "y \<in> s"
   2.373 -    assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   2.374 -    let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   2.375 -    let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   2.376 -    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
   2.377 -      by auto
   2.378 -    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
   2.379 -      by simp
   2.380 -    then have "sum ?u {1 .. 2} = 1"
   2.381 -      using sum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   2.382 -      by auto
   2.383 -    with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   2.384 -      using mu xy by auto
   2.385 -    have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   2.386 -      using sum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   2.387 -    from sum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   2.388 -    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   2.389 -      by auto
   2.390 -    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
   2.391 -      using s by (auto simp: add.commute)
   2.392 -  }
   2.393 -  then show "convex s"
   2.394 -    unfolding convex_alt by auto
   2.395 -qed
   2.396 -
   2.397 -
   2.398 -lemma convex_explicit:
   2.399 -  fixes s :: "'a::real_vector set"
   2.400 -  shows "convex s \<longleftrightarrow>
   2.401 -    (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> sum u t = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   2.402 -proof safe
   2.403 -  fix t
   2.404 -  fix u :: "'a \<Rightarrow> real"
   2.405 -  assume "convex s"
   2.406 -    and "finite t"
   2.407 -    and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   2.408 -  then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   2.409 -    using convex_sum[of t s u "\<lambda> x. x"] by auto
   2.410 -next
   2.411 -  assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
   2.412 -    sum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   2.413 -  show "convex s"
   2.414 -    unfolding convex_alt
   2.415 -  proof safe
   2.416 -    fix x y
   2.417 -    fix \<mu> :: real
   2.418 -    assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   2.419 -    show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   2.420 -    proof (cases "x = y")
   2.421 -      case False
   2.422 -      then show ?thesis
   2.423 -        using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
   2.424 -        by auto
   2.425 -    next
   2.426 -      case True
   2.427 -      then show ?thesis
   2.428 -        using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
   2.429 -        by (auto simp: field_simps real_vector.scale_left_diff_distrib)
   2.430 -    qed
   2.431 -  qed
   2.432 -qed
   2.433 -
   2.434 -lemma convex_finite:
   2.435 -  assumes "finite s"
   2.436 -  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   2.437 -  unfolding convex_explicit
   2.438 -  apply safe
   2.439 -  subgoal for u by (erule allE [where x=s], erule allE [where x=u]) auto
   2.440 -  subgoal for t u
   2.441 -  proof -
   2.442 -    have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   2.443 -      by simp
   2.444 -    assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   2.445 -    assume *: "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
   2.446 -    assume "t \<subseteq> s"
   2.447 -    then have "s \<inter> t = t" by auto
   2.448 -    with sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] * show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   2.449 -      by (auto simp: assms sum.If_cases if_distrib if_distrib_arg)
   2.450 -  qed
   2.451 -  done
   2.452 -
   2.453 -
   2.454 -subsection \<open>Functions that are convex on a set\<close>
   2.455 -
   2.456 -definition%important convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   2.457 -  where "convex_on s f \<longleftrightarrow>
   2.458 -    (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   2.459 -
   2.460 -lemma convex_onI [intro?]:
   2.461 -  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   2.462 -    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   2.463 -  shows "convex_on A f"
   2.464 -  unfolding convex_on_def
   2.465 -proof clarify
   2.466 -  fix x y
   2.467 -  fix u v :: real
   2.468 -  assume A: "x \<in> A" "y \<in> A" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   2.469 -  from A(5) have [simp]: "v = 1 - u"
   2.470 -    by (simp add: algebra_simps)
   2.471 -  from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   2.472 -    using assms[of u y x]
   2.473 -    by (cases "u = 0 \<or> u = 1") (auto simp: algebra_simps)
   2.474 -qed
   2.475 -
   2.476 -lemma convex_on_linorderI [intro?]:
   2.477 -  fixes A :: "('a::{linorder,real_vector}) set"
   2.478 -  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x < y \<Longrightarrow>
   2.479 -    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   2.480 -  shows "convex_on A f"
   2.481 -proof
   2.482 -  fix x y
   2.483 -  fix t :: real
   2.484 -  assume A: "x \<in> A" "y \<in> A" "t > 0" "t < 1"
   2.485 -  with assms [of t x y] assms [of "1 - t" y x]
   2.486 -  show "f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   2.487 -    by (cases x y rule: linorder_cases) (auto simp: algebra_simps)
   2.488 -qed
   2.489 -
   2.490 -lemma convex_onD:
   2.491 -  assumes "convex_on A f"
   2.492 -  shows "\<And>t x y. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
   2.493 -    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   2.494 -  using assms by (auto simp: convex_on_def)
   2.495 -
   2.496 -lemma convex_onD_Icc:
   2.497 -  assumes "convex_on {x..y} f" "x \<le> (y :: _ :: {real_vector,preorder})"
   2.498 -  shows "\<And>t. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow>
   2.499 -    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
   2.500 -  using assms(2) by (intro convex_onD [OF assms(1)]) simp_all
   2.501 -
   2.502 -lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   2.503 -  unfolding convex_on_def by auto
   2.504 -
   2.505 -lemma convex_on_add [intro]:
   2.506 -  assumes "convex_on s f"
   2.507 -    and "convex_on s g"
   2.508 -  shows "convex_on s (\<lambda>x. f x + g x)"
   2.509 -proof -
   2.510 -  {
   2.511 -    fix x y
   2.512 -    assume "x \<in> s" "y \<in> s"
   2.513 -    moreover
   2.514 -    fix u v :: real
   2.515 -    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   2.516 -    ultimately
   2.517 -    have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   2.518 -      using assms unfolding convex_on_def by (auto simp: add_mono)
   2.519 -    then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   2.520 -      by (simp add: field_simps)
   2.521 -  }
   2.522 -  then show ?thesis
   2.523 -    unfolding convex_on_def by auto
   2.524 -qed
   2.525 -
   2.526 -lemma convex_on_cmul [intro]:
   2.527 -  fixes c :: real
   2.528 -  assumes "0 \<le> c"
   2.529 -    and "convex_on s f"
   2.530 -  shows "convex_on s (\<lambda>x. c * f x)"
   2.531 -proof -
   2.532 -  have *: "u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   2.533 -    for u c fx v fy :: real
   2.534 -    by (simp add: field_simps)
   2.535 -  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   2.536 -    unfolding convex_on_def and * by auto
   2.537 -qed
   2.538 -
   2.539 -lemma convex_lower:
   2.540 -  assumes "convex_on s f"
   2.541 -    and "x \<in> s"
   2.542 -    and "y \<in> s"
   2.543 -    and "0 \<le> u"
   2.544 -    and "0 \<le> v"
   2.545 -    and "u + v = 1"
   2.546 -  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   2.547 -proof -
   2.548 -  let ?m = "max (f x) (f y)"
   2.549 -  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   2.550 -    using assms(4,5) by (auto simp: mult_left_mono add_mono)
   2.551 -  also have "\<dots> = max (f x) (f y)"
   2.552 -    using assms(6) by (simp add: distrib_right [symmetric])
   2.553 -  finally show ?thesis
   2.554 -    using assms unfolding convex_on_def by fastforce
   2.555 -qed
   2.556 -
   2.557 -lemma convex_on_dist [intro]:
   2.558 -  fixes s :: "'a::real_normed_vector set"
   2.559 -  shows "convex_on s (\<lambda>x. dist a x)"
   2.560 -proof (auto simp: convex_on_def dist_norm)
   2.561 -  fix x y
   2.562 -  assume "x \<in> s" "y \<in> s"
   2.563 -  fix u v :: real
   2.564 -  assume "0 \<le> u"
   2.565 -  assume "0 \<le> v"
   2.566 -  assume "u + v = 1"
   2.567 -  have "a = u *\<^sub>R a + v *\<^sub>R a"
   2.568 -    unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
   2.569 -  then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   2.570 -    by (auto simp: algebra_simps)
   2.571 -  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   2.572 -    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   2.573 -    using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
   2.574 -qed
   2.575 -
   2.576 -
   2.577 -subsection%unimportant \<open>Arithmetic operations on sets preserve convexity\<close>
   2.578 -
   2.579 -lemma convex_linear_image:
   2.580 -  assumes "linear f"
   2.581 -    and "convex s"
   2.582 -  shows "convex (f ` s)"
   2.583 -proof -
   2.584 -  interpret f: linear f by fact
   2.585 -  from \<open>convex s\<close> show "convex (f ` s)"
   2.586 -    by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
   2.587 -qed
   2.588 -
   2.589 -lemma convex_linear_vimage:
   2.590 -  assumes "linear f"
   2.591 -    and "convex s"
   2.592 -  shows "convex (f -` s)"
   2.593 -proof -
   2.594 -  interpret f: linear f by fact
   2.595 -  from \<open>convex s\<close> show "convex (f -` s)"
   2.596 -    by (simp add: convex_def f.add f.scaleR)
   2.597 -qed
   2.598 -
   2.599 -lemma convex_scaling:
   2.600 -  assumes "convex s"
   2.601 -  shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   2.602 -proof -
   2.603 -  have "linear (\<lambda>x. c *\<^sub>R x)"
   2.604 -    by (simp add: linearI scaleR_add_right)
   2.605 -  then show ?thesis
   2.606 -    using \<open>convex s\<close> by (rule convex_linear_image)
   2.607 -qed
   2.608 -
   2.609 -lemma convex_scaled:
   2.610 -  assumes "convex S"
   2.611 -  shows "convex ((\<lambda>x. x *\<^sub>R c) ` S)"
   2.612 -proof -
   2.613 -  have "linear (\<lambda>x. x *\<^sub>R c)"
   2.614 -    by (simp add: linearI scaleR_add_left)
   2.615 -  then show ?thesis
   2.616 -    using \<open>convex S\<close> by (rule convex_linear_image)
   2.617 -qed
   2.618 -
   2.619 -lemma convex_negations:
   2.620 -  assumes "convex S"
   2.621 -  shows "convex ((\<lambda>x. - x) ` S)"
   2.622 -proof -
   2.623 -  have "linear (\<lambda>x. - x)"
   2.624 -    by (simp add: linearI)
   2.625 -  then show ?thesis
   2.626 -    using \<open>convex S\<close> by (rule convex_linear_image)
   2.627 -qed
   2.628 -
   2.629 -lemma convex_sums:
   2.630 -  assumes "convex S"
   2.631 -    and "convex T"
   2.632 -  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   2.633 -proof -
   2.634 -  have "linear (\<lambda>(x, y). x + y)"
   2.635 -    by (auto intro: linearI simp: scaleR_add_right)
   2.636 -  with assms have "convex ((\<lambda>(x, y). x + y) ` (S \<times> T))"
   2.637 -    by (intro convex_linear_image convex_Times)
   2.638 -  also have "((\<lambda>(x, y). x + y) ` (S \<times> T)) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   2.639 -    by auto
   2.640 -  finally show ?thesis .
   2.641 -qed
   2.642 -
   2.643 -lemma convex_differences:
   2.644 -  assumes "convex S" "convex T"
   2.645 -  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
   2.646 -proof -
   2.647 -  have "{x - y| x y. x \<in> S \<and> y \<in> T} = {x + y |x y. x \<in> S \<and> y \<in> uminus ` T}"
   2.648 -    by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
   2.649 -  then show ?thesis
   2.650 -    using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   2.651 -qed
   2.652 -
   2.653 -lemma convex_translation:
   2.654 -  assumes "convex S"
   2.655 -  shows "convex ((\<lambda>x. a + x) ` S)"
   2.656 -proof -
   2.657 -  have "(\<Union> x\<in> {a}. \<Union>y \<in> S. {x + y}) = (\<lambda>x. a + x) ` S"
   2.658 -    by auto
   2.659 -  then show ?thesis
   2.660 -    using convex_sums[OF convex_singleton[of a] assms] by auto
   2.661 -qed
   2.662 -
   2.663 -lemma convex_affinity:
   2.664 -  assumes "convex S"
   2.665 -  shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` S)"
   2.666 -proof -
   2.667 -  have "(\<lambda>x. a + c *\<^sub>R x) ` S = (+) a ` (*\<^sub>R) c ` S"
   2.668 -    by auto
   2.669 -  then show ?thesis
   2.670 -    using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   2.671 -qed
   2.672 -
   2.673 -lemma pos_is_convex: "convex {0 :: real <..}"
   2.674 -  unfolding convex_alt
   2.675 -proof safe
   2.676 -  fix y x \<mu> :: real
   2.677 -  assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   2.678 -  {
   2.679 -    assume "\<mu> = 0"
   2.680 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y"
   2.681 -      by simp
   2.682 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   2.683 -      using * by simp
   2.684 -  }
   2.685 -  moreover
   2.686 -  {
   2.687 -    assume "\<mu> = 1"
   2.688 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   2.689 -      using * by simp
   2.690 -  }
   2.691 -  moreover
   2.692 -  {
   2.693 -    assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   2.694 -    then have "\<mu> > 0" "(1 - \<mu>) > 0"
   2.695 -      using * by auto
   2.696 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
   2.697 -      using * by (auto simp: add_pos_pos)
   2.698 -  }
   2.699 -  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
   2.700 -    by fastforce
   2.701 -qed
   2.702 -
   2.703 -lemma convex_on_sum:
   2.704 -  fixes a :: "'a \<Rightarrow> real"
   2.705 -    and y :: "'a \<Rightarrow> 'b::real_vector"
   2.706 -    and f :: "'b \<Rightarrow> real"
   2.707 -  assumes "finite s" "s \<noteq> {}"
   2.708 -    and "convex_on C f"
   2.709 -    and "convex C"
   2.710 -    and "(\<Sum> i \<in> s. a i) = 1"
   2.711 -    and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   2.712 -    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   2.713 -  shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   2.714 -  using assms
   2.715 -proof (induct s arbitrary: a rule: finite_ne_induct)
   2.716 -  case (singleton i)
   2.717 -  then have ai: "a i = 1"
   2.718 -    by auto
   2.719 -  then show ?case
   2.720 -    by auto
   2.721 -next
   2.722 -  case (insert i s)
   2.723 -  then have "convex_on C f"
   2.724 -    by simp
   2.725 -  from this[unfolded convex_on_def, rule_format]
   2.726 -  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
   2.727 -      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   2.728 -    by simp
   2.729 -  show ?case
   2.730 -  proof (cases "a i = 1")
   2.731 -    case True
   2.732 -    then have "(\<Sum> j \<in> s. a j) = 0"
   2.733 -      using insert by auto
   2.734 -    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   2.735 -      using insert by (fastforce simp: sum_nonneg_eq_0_iff)
   2.736 -    then show ?thesis
   2.737 -      using insert by auto
   2.738 -  next
   2.739 -    case False
   2.740 -    from insert have yai: "y i \<in> C" "a i \<ge> 0"
   2.741 -      by auto
   2.742 -    have fis: "finite (insert i s)"
   2.743 -      using insert by auto
   2.744 -    then have ai1: "a i \<le> 1"
   2.745 -      using sum_nonneg_leq_bound[of "insert i s" a] insert by simp
   2.746 -    then have "a i < 1"
   2.747 -      using False by auto
   2.748 -    then have i0: "1 - a i > 0"
   2.749 -      by auto
   2.750 -    let ?a = "\<lambda>j. a j / (1 - a i)"
   2.751 -    have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
   2.752 -      using i0 insert that by fastforce
   2.753 -    have "(\<Sum> j \<in> insert i s. a j) = 1"
   2.754 -      using insert by auto
   2.755 -    then have "(\<Sum> j \<in> s. a j) = 1 - a i"
   2.756 -      using sum.insert insert by fastforce
   2.757 -    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
   2.758 -      using i0 by auto
   2.759 -    then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
   2.760 -      unfolding sum_divide_distrib by simp
   2.761 -    have "convex C" using insert by auto
   2.762 -    then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   2.763 -      using insert convex_sum [OF \<open>finite s\<close> \<open>convex C\<close> a1 a_nonneg] by auto
   2.764 -    have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   2.765 -      using a_nonneg a1 insert by blast
   2.766 -    have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) = f ((\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   2.767 -      using sum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
   2.768 -      by (auto simp only: add.commute)
   2.769 -    also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   2.770 -      using i0 by auto
   2.771 -    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   2.772 -      using scaleR_right.sum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   2.773 -      by (auto simp: algebra_simps)
   2.774 -    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   2.775 -      by (auto simp: divide_inverse)
   2.776 -    also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   2.777 -      using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   2.778 -      by (auto simp: add.commute)
   2.779 -    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   2.780 -      using add_right_mono [OF mult_left_mono [of _ _ "1 - a i",
   2.781 -            OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"]
   2.782 -      by simp
   2.783 -    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   2.784 -      unfolding sum_distrib_left[of "1 - a i" "\<lambda> j. ?a j * f (y j)"]
   2.785 -      using i0 by auto
   2.786 -    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
   2.787 -      using i0 by auto
   2.788 -    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
   2.789 -      using insert by auto
   2.790 -    finally show ?thesis
   2.791 -      by simp
   2.792 -  qed
   2.793 -qed
   2.794 -
   2.795 -lemma convex_on_alt:
   2.796 -  fixes C :: "'a::real_vector set"
   2.797 -  assumes "convex C"
   2.798 -  shows "convex_on C f \<longleftrightarrow>
   2.799 -    (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
   2.800 -      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   2.801 -proof safe
   2.802 -  fix x y
   2.803 -  fix \<mu> :: real
   2.804 -  assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   2.805 -  from this[unfolded convex_on_def, rule_format]
   2.806 -  have "0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" for u v
   2.807 -    by auto
   2.808 -  from this [of "\<mu>" "1 - \<mu>", simplified] *
   2.809 -  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   2.810 -    by auto
   2.811 -next
   2.812 -  assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
   2.813 -    f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   2.814 -  {
   2.815 -    fix x y
   2.816 -    fix u v :: real
   2.817 -    assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   2.818 -    then have[simp]: "1 - u = v" by auto
   2.819 -    from *[rule_format, of x y u]
   2.820 -    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   2.821 -      using ** by auto
   2.822 -  }
   2.823 -  then show "convex_on C f"
   2.824 -    unfolding convex_on_def by auto
   2.825 -qed
   2.826 -
   2.827 -lemma convex_on_diff:
   2.828 -  fixes f :: "real \<Rightarrow> real"
   2.829 -  assumes f: "convex_on I f"
   2.830 -    and I: "x \<in> I" "y \<in> I"
   2.831 -    and t: "x < t" "t < y"
   2.832 -  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   2.833 -    and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   2.834 -proof -
   2.835 -  define a where "a \<equiv> (t - y) / (x - y)"
   2.836 -  with t have "0 \<le> a" "0 \<le> 1 - a"
   2.837 -    by (auto simp: field_simps)
   2.838 -  with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   2.839 -    by (auto simp: convex_on_def)
   2.840 -  have "a * x + (1 - a) * y = a * (x - y) + y"
   2.841 -    by (simp add: field_simps)
   2.842 -  also have "\<dots> = t"
   2.843 -    unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
   2.844 -  finally have "f t \<le> a * f x + (1 - a) * f y"
   2.845 -    using cvx by simp
   2.846 -  also have "\<dots> = a * (f x - f y) + f y"
   2.847 -    by (simp add: field_simps)
   2.848 -  finally have "f t - f y \<le> a * (f x - f y)"
   2.849 -    by simp
   2.850 -  with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   2.851 -    by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   2.852 -  with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   2.853 -    by (simp add: le_divide_eq divide_le_eq field_simps)
   2.854 -qed
   2.855 -
   2.856 -lemma pos_convex_function:
   2.857 -  fixes f :: "real \<Rightarrow> real"
   2.858 -  assumes "convex C"
   2.859 -    and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   2.860 -  shows "convex_on C f"
   2.861 -  unfolding convex_on_alt[OF assms(1)]
   2.862 -  using assms
   2.863 -proof safe
   2.864 -  fix x y \<mu> :: real
   2.865 -  let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   2.866 -  assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   2.867 -  then have "1 - \<mu> \<ge> 0" by auto
   2.868 -  then have xpos: "?x \<in> C"
   2.869 -    using * unfolding convex_alt by fastforce
   2.870 -  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
   2.871 -      \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   2.872 -    using add_mono [OF mult_left_mono [OF leq [OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
   2.873 -        mult_left_mono [OF leq [OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
   2.874 -    by auto
   2.875 -  then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   2.876 -    by (auto simp: field_simps)
   2.877 -  then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   2.878 -    using convex_on_alt by auto
   2.879 -qed
   2.880 -
   2.881 -lemma atMostAtLeast_subset_convex:
   2.882 -  fixes C :: "real set"
   2.883 -  assumes "convex C"
   2.884 -    and "x \<in> C" "y \<in> C" "x < y"
   2.885 -  shows "{x .. y} \<subseteq> C"
   2.886 -proof safe
   2.887 -  fix z assume z: "z \<in> {x .. y}"
   2.888 -  have less: "z \<in> C" if *: "x < z" "z < y"
   2.889 -  proof -
   2.890 -    let ?\<mu> = "(y - z) / (y - x)"
   2.891 -    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
   2.892 -      using assms * by (auto simp: field_simps)
   2.893 -    then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   2.894 -      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   2.895 -      by (simp add: algebra_simps)
   2.896 -    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   2.897 -      by (auto simp: field_simps)
   2.898 -    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   2.899 -      using assms by (simp only: add_divide_distrib) (auto simp: field_simps)
   2.900 -    also have "\<dots> = z"
   2.901 -      using assms by (auto simp: field_simps)
   2.902 -    finally show ?thesis
   2.903 -      using comb by auto
   2.904 -  qed
   2.905 -  show "z \<in> C"
   2.906 -    using z less assms by (auto simp: le_less)
   2.907 -qed
   2.908 -
   2.909 -lemma f''_imp_f':
   2.910 -  fixes f :: "real \<Rightarrow> real"
   2.911 -  assumes "convex C"
   2.912 -    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   2.913 -    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   2.914 -    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   2.915 -    and x: "x \<in> C"
   2.916 -    and y: "y \<in> C"
   2.917 -  shows "f' x * (y - x) \<le> f y - f x"
   2.918 -  using assms
   2.919 -proof -
   2.920 -  have less_imp: "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   2.921 -    if *: "x \<in> C" "y \<in> C" "y > x" for x y :: real
   2.922 -  proof -
   2.923 -    from * have ge: "y - x > 0" "y - x \<ge> 0"
   2.924 -      by auto
   2.925 -    from * have le: "x - y < 0" "x - y \<le> 0"
   2.926 -      by auto
   2.927 -    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   2.928 -      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
   2.929 -          THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   2.930 -      by auto
   2.931 -    then have "z1 \<in> C"
   2.932 -      using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
   2.933 -      by fastforce
   2.934 -    from z1 have z1': "f x - f y = (x - y) * f' z1"
   2.935 -      by (simp add: field_simps)
   2.936 -    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   2.937 -      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
   2.938 -          THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   2.939 -      by auto
   2.940 -    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   2.941 -      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
   2.942 -          THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   2.943 -      by auto
   2.944 -    have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   2.945 -      using * z1' by auto
   2.946 -    also have "\<dots> = (y - z1) * f'' z3"
   2.947 -      using z3 by auto
   2.948 -    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
   2.949 -      by simp
   2.950 -    have A': "y - z1 \<ge> 0"
   2.951 -      using z1 by auto
   2.952 -    have "z3 \<in> C"
   2.953 -      using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
   2.954 -      by fastforce
   2.955 -    then have B': "f'' z3 \<ge> 0"
   2.956 -      using assms by auto
   2.957 -    from A' B' have "(y - z1) * f'' z3 \<ge> 0"
   2.958 -      by auto
   2.959 -    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
   2.960 -      by auto
   2.961 -    from mult_right_mono_neg[OF this le(2)]
   2.962 -    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   2.963 -      by (simp add: algebra_simps)
   2.964 -    then have "f' y * (x - y) - (f x - f y) \<le> 0"
   2.965 -      using le by auto
   2.966 -    then have res: "f' y * (x - y) \<le> f x - f y"
   2.967 -      by auto
   2.968 -    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   2.969 -      using * z1 by auto
   2.970 -    also have "\<dots> = (z1 - x) * f'' z2"
   2.971 -      using z2 by auto
   2.972 -    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
   2.973 -      by simp
   2.974 -    have A: "z1 - x \<ge> 0"
   2.975 -      using z1 by auto
   2.976 -    have "z2 \<in> C"
   2.977 -      using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
   2.978 -      by fastforce
   2.979 -    then have B: "f'' z2 \<ge> 0"
   2.980 -      using assms by auto
   2.981 -    from A B have "(z1 - x) * f'' z2 \<ge> 0"
   2.982 -      by auto
   2.983 -    with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
   2.984 -      by auto
   2.985 -    from mult_right_mono[OF this ge(2)]
   2.986 -    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   2.987 -      by (simp add: algebra_simps)
   2.988 -    then have "f y - f x - f' x * (y - x) \<ge> 0"
   2.989 -      using ge by auto
   2.990 -    then show "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   2.991 -      using res by auto
   2.992 -  qed
   2.993 -  show ?thesis
   2.994 -  proof (cases "x = y")
   2.995 -    case True
   2.996 -    with x y show ?thesis by auto
   2.997 -  next
   2.998 -    case False
   2.999 -    with less_imp x y show ?thesis
  2.1000 -      by (auto simp: neq_iff)
  2.1001 -  qed
  2.1002 -qed
  2.1003 -
  2.1004 -lemma f''_ge0_imp_convex:
  2.1005 -  fixes f :: "real \<Rightarrow> real"
  2.1006 -  assumes conv: "convex C"
  2.1007 -    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
  2.1008 -    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
  2.1009 -    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
  2.1010 -  shows "convex_on C f"
  2.1011 -  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
  2.1012 -  by fastforce
  2.1013 -
  2.1014 -lemma minus_log_convex:
  2.1015 -  fixes b :: real
  2.1016 -  assumes "b > 1"
  2.1017 -  shows "convex_on {0 <..} (\<lambda> x. - log b x)"
  2.1018 -proof -
  2.1019 -  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
  2.1020 -    using DERIV_log by auto
  2.1021 -  then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
  2.1022 -    by (auto simp: DERIV_minus)
  2.1023 -  have "\<And>z::real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
  2.1024 -    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
  2.1025 -  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
  2.1026 -  have "\<And>z::real. z > 0 \<Longrightarrow>
  2.1027 -    DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
  2.1028 -    by auto
  2.1029 -  then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
  2.1030 -    DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
  2.1031 -    unfolding inverse_eq_divide by (auto simp: mult.assoc)
  2.1032 -  have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
  2.1033 -    using \<open>b > 1\<close> by (auto intro!: less_imp_le)
  2.1034 -  from f''_ge0_imp_convex[OF pos_is_convex, unfolded greaterThan_iff, OF f' f''0 f''_ge0]
  2.1035 -  show ?thesis
  2.1036 -    by auto
  2.1037 -qed
  2.1038 -
  2.1039 -
  2.1040 -subsection%unimportant \<open>Convexity of real functions\<close>
  2.1041 -
  2.1042 -lemma convex_on_realI:
  2.1043 -  assumes "connected A"
  2.1044 -    and "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
  2.1045 -    and "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f' x \<le> f' y"
  2.1046 -  shows "convex_on A f"
  2.1047 -proof (rule convex_on_linorderI)
  2.1048 -  fix t x y :: real
  2.1049 -  assume t: "t > 0" "t < 1"
  2.1050 -  assume xy: "x \<in> A" "y \<in> A" "x < y"
  2.1051 -  define z where "z = (1 - t) * x + t * y"
  2.1052 -  with \<open>connected A\<close> and xy have ivl: "{x..y} \<subseteq> A"
  2.1053 -    using connected_contains_Icc by blast
  2.1054 -
  2.1055 -  from xy t have xz: "z > x"
  2.1056 -    by (simp add: z_def algebra_simps)
  2.1057 -  have "y - z = (1 - t) * (y - x)"
  2.1058 -    by (simp add: z_def algebra_simps)
  2.1059 -  also from xy t have "\<dots> > 0"
  2.1060 -    by (intro mult_pos_pos) simp_all
  2.1061 -  finally have yz: "z < y"
  2.1062 -    by simp
  2.1063 -
  2.1064 -  from assms xz yz ivl t have "\<exists>\<xi>. \<xi> > x \<and> \<xi> < z \<and> f z - f x = (z - x) * f' \<xi>"
  2.1065 -    by (intro MVT2) (auto intro!: assms(2))
  2.1066 -  then obtain \<xi> where \<xi>: "\<xi> > x" "\<xi> < z" "f' \<xi> = (f z - f x) / (z - x)"
  2.1067 -    by auto
  2.1068 -  from assms xz yz ivl t have "\<exists>\<eta>. \<eta> > z \<and> \<eta> < y \<and> f y - f z = (y - z) * f' \<eta>"
  2.1069 -    by (intro MVT2) (auto intro!: assms(2))
  2.1070 -  then obtain \<eta> where \<eta>: "\<eta> > z" "\<eta> < y" "f' \<eta> = (f y - f z) / (y - z)"
  2.1071 -    by auto
  2.1072 -
  2.1073 -  from \<eta>(3) have "(f y - f z) / (y - z) = f' \<eta>" ..
  2.1074 -  also from \<xi> \<eta> ivl have "\<xi> \<in> A" "\<eta> \<in> A"
  2.1075 -    by auto
  2.1076 -  with \<xi> \<eta> have "f' \<eta> \<ge> f' \<xi>"
  2.1077 -    by (intro assms(3)) auto
  2.1078 -  also from \<xi>(3) have "f' \<xi> = (f z - f x) / (z - x)" .
  2.1079 -  finally have "(f y - f z) * (z - x) \<ge> (f z - f x) * (y - z)"
  2.1080 -    using xz yz by (simp add: field_simps)
  2.1081 -  also have "z - x = t * (y - x)"
  2.1082 -    by (simp add: z_def algebra_simps)
  2.1083 -  also have "y - z = (1 - t) * (y - x)"
  2.1084 -    by (simp add: z_def algebra_simps)
  2.1085 -  finally have "(f y - f z) * t \<ge> (f z - f x) * (1 - t)"
  2.1086 -    using xy by simp
  2.1087 -  then show "(1 - t) * f x + t * f y \<ge> f ((1 - t) *\<^sub>R x + t *\<^sub>R y)"
  2.1088 -    by (simp add: z_def algebra_simps)
  2.1089 -qed
  2.1090 -
  2.1091 -lemma convex_on_inverse:
  2.1092 -  assumes "A \<subseteq> {0<..}"
  2.1093 -  shows "convex_on A (inverse :: real \<Rightarrow> real)"
  2.1094 -proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\<lambda>x. -inverse (x^2)"])
  2.1095 -  fix u v :: real
  2.1096 -  assume "u \<in> {0<..}" "v \<in> {0<..}" "u \<le> v"
  2.1097 -  with assms show "-inverse (u^2) \<le> -inverse (v^2)"
  2.1098 -    by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all)
  2.1099 -qed (insert assms, auto intro!: derivative_eq_intros simp: divide_simps power2_eq_square)
  2.1100 -
  2.1101 -lemma convex_onD_Icc':
  2.1102 -  assumes "convex_on {x..y} f" "c \<in> {x..y}"
  2.1103 -  defines "d \<equiv> y - x"
  2.1104 -  shows "f c \<le> (f y - f x) / d * (c - x) + f x"
  2.1105 -proof (cases x y rule: linorder_cases)
  2.1106 -  case less
  2.1107 -  then have d: "d > 0"
  2.1108 -    by (simp add: d_def)
  2.1109 -  from assms(2) less have A: "0 \<le> (c - x) / d" "(c - x) / d \<le> 1"
  2.1110 -    by (simp_all add: d_def divide_simps)
  2.1111 -  have "f c = f (x + (c - x) * 1)"
  2.1112 -    by simp
  2.1113 -  also from less have "1 = ((y - x) / d)"
  2.1114 -    by (simp add: d_def)
  2.1115 -  also from d have "x + (c - x) * \<dots> = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y"
  2.1116 -    by (simp add: field_simps)
  2.1117 -  also have "f \<dots> \<le> (1 - (c - x) / d) * f x + (c - x) / d * f y"
  2.1118 -    using assms less by (intro convex_onD_Icc) simp_all
  2.1119 -  also from d have "\<dots> = (f y - f x) / d * (c - x) + f x"
  2.1120 -    by (simp add: field_simps)
  2.1121 -  finally show ?thesis .
  2.1122 -qed (insert assms(2), simp_all)
  2.1123 -
  2.1124 -lemma convex_onD_Icc'':
  2.1125 -  assumes "convex_on {x..y} f" "c \<in> {x..y}"
  2.1126 -  defines "d \<equiv> y - x"
  2.1127 -  shows "f c \<le> (f x - f y) / d * (y - c) + f y"
  2.1128 -proof (cases x y rule: linorder_cases)
  2.1129 -  case less
  2.1130 -  then have d: "d > 0"
  2.1131 -    by (simp add: d_def)
  2.1132 -  from assms(2) less have A: "0 \<le> (y - c) / d" "(y - c) / d \<le> 1"
  2.1133 -    by (simp_all add: d_def divide_simps)
  2.1134 -  have "f c = f (y - (y - c) * 1)"
  2.1135 -    by simp
  2.1136 -  also from less have "1 = ((y - x) / d)"
  2.1137 -    by (simp add: d_def)
  2.1138 -  also from d have "y - (y - c) * \<dots> = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y"
  2.1139 -    by (simp add: field_simps)
  2.1140 -  also have "f \<dots> \<le> (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y"
  2.1141 -    using assms less by (intro convex_onD_Icc) (simp_all add: field_simps)
  2.1142 -  also from d have "\<dots> = (f x - f y) / d * (y - c) + f y"
  2.1143 -    by (simp add: field_simps)
  2.1144 -  finally show ?thesis .
  2.1145 -qed (insert assms(2), simp_all)
  2.1146 +subsection%unimportant \<open>Topological Properties of Convex Sets and Functions\<close>
  2.1147  
  2.1148  lemma convex_supp_sum:
  2.1149    assumes "convex S" and 1: "supp_sum u I = 1"
  2.1150 @@ -1160,14 +32,6 @@
  2.1151      done
  2.1152  qed
  2.1153  
  2.1154 -lemma convex_translation_eq [simp]: "convex ((\<lambda>x. a + x) ` s) \<longleftrightarrow> convex s"
  2.1155 -  by (metis convex_translation translation_galois)
  2.1156 -
  2.1157 -lemma convex_linear_image_eq [simp]:
  2.1158 -    fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
  2.1159 -    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> convex (f ` s) \<longleftrightarrow> convex s"
  2.1160 -    by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq)
  2.1161 -
  2.1162  lemma closure_bounded_linear_image_subset:
  2.1163    assumes f: "bounded_linear f"
  2.1164    shows "f ` closure S \<subseteq> closure (f ` S)"
  2.1165 @@ -1238,822 +102,11 @@
  2.1166      by (intro closure_minimal image_mono closure_subset closed_scaling closed_closure)
  2.1167  qed
  2.1168  
  2.1169 -lemma fst_linear: "linear fst"
  2.1170 -  unfolding linear_iff by (simp add: algebra_simps)
  2.1171 -
  2.1172 -lemma snd_linear: "linear snd"
  2.1173 -  unfolding linear_iff by (simp add: algebra_simps)
  2.1174 -
  2.1175 -lemma fst_snd_linear: "linear (\<lambda>(x,y). x + y)"
  2.1176 -  unfolding linear_iff by (simp add: algebra_simps)
  2.1177 -
  2.1178 -lemma vector_choose_size:
  2.1179 -  assumes "0 \<le> c"
  2.1180 -  obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c"
  2.1181 -proof -
  2.1182 -  obtain a::'a where "a \<noteq> 0"
  2.1183 -    using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce
  2.1184 -  then show ?thesis
  2.1185 -    by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms)
  2.1186 -qed
  2.1187 -
  2.1188 -lemma vector_choose_dist:
  2.1189 -  assumes "0 \<le> c"
  2.1190 -  obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c"
  2.1191 -by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size)
  2.1192 -
  2.1193  lemma sphere_eq_empty [simp]:
  2.1194    fixes a :: "'a::{real_normed_vector, perfect_space}"
  2.1195    shows "sphere a r = {} \<longleftrightarrow> r < 0"
  2.1196  by (auto simp: sphere_def dist_norm) (metis dist_norm le_less_linear vector_choose_dist)
  2.1197  
  2.1198 -lemma sum_delta_notmem:
  2.1199 -  assumes "x \<notin> s"
  2.1200 -  shows "sum (\<lambda>y. if (y = x) then P x else Q y) s = sum Q s"
  2.1201 -    and "sum (\<lambda>y. if (x = y) then P x else Q y) s = sum Q s"
  2.1202 -    and "sum (\<lambda>y. if (y = x) then P y else Q y) s = sum Q s"
  2.1203 -    and "sum (\<lambda>y. if (x = y) then P y else Q y) s = sum Q s"
  2.1204 -  apply (rule_tac [!] sum.cong)
  2.1205 -  using assms
  2.1206 -  apply auto
  2.1207 -  done
  2.1208 -
  2.1209 -lemma sum_delta'':
  2.1210 -  fixes s::"'a::real_vector set"
  2.1211 -  assumes "finite s"
  2.1212 -  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)"
  2.1213 -proof -
  2.1214 -  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)"
  2.1215 -    by auto
  2.1216 -  show ?thesis
  2.1217 -    unfolding * using sum.delta[OF assms, of y "\<lambda>x. f x *\<^sub>R x"] by auto
  2.1218 -qed
  2.1219 -
  2.1220 -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)"
  2.1221 -  by (fact if_distrib)
  2.1222 -
  2.1223 -lemma dist_triangle_eq:
  2.1224 -  fixes x y z :: "'a::real_inner"
  2.1225 -  shows "dist x z = dist x y + dist y z \<longleftrightarrow>
  2.1226 -    norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)"
  2.1227 -proof -
  2.1228 -  have *: "x - y + (y - z) = x - z" by auto
  2.1229 -  show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *]
  2.1230 -    by (auto simp:norm_minus_commute)
  2.1231 -qed
  2.1232 -
  2.1233 -
  2.1234 -subsection \<open>Affine set and affine hull\<close>
  2.1235 -
  2.1236 -definition%important affine :: "'a::real_vector set \<Rightarrow> bool"
  2.1237 -  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)"
  2.1238 -
  2.1239 -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)"
  2.1240 -  unfolding affine_def by (metis eq_diff_eq')
  2.1241 -
  2.1242 -lemma affine_empty [iff]: "affine {}"
  2.1243 -  unfolding affine_def by auto
  2.1244 -
  2.1245 -lemma affine_sing [iff]: "affine {x}"
  2.1246 -  unfolding affine_alt by (auto simp: scaleR_left_distrib [symmetric])
  2.1247 -
  2.1248 -lemma affine_UNIV [iff]: "affine UNIV"
  2.1249 -  unfolding affine_def by auto
  2.1250 -
  2.1251 -lemma affine_Inter [intro]: "(\<And>s. s\<in>f \<Longrightarrow> affine s) \<Longrightarrow> affine (\<Inter>f)"
  2.1252 -  unfolding affine_def by auto
  2.1253 -
  2.1254 -lemma affine_Int[intro]: "affine s \<Longrightarrow> affine t \<Longrightarrow> affine (s \<inter> t)"
  2.1255 -  unfolding affine_def by auto
  2.1256 -
  2.1257 -lemma affine_scaling: "affine s \<Longrightarrow> affine (image (\<lambda>x. c *\<^sub>R x) s)"
  2.1258 -  apply (clarsimp simp add: affine_def)
  2.1259 -  apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI)
  2.1260 -  apply (auto simp: algebra_simps)
  2.1261 -  done
  2.1262 -
  2.1263 -lemma affine_affine_hull [simp]: "affine(affine hull s)"
  2.1264 -  unfolding hull_def
  2.1265 -  using affine_Inter[of "{t. affine t \<and> s \<subseteq> t}"] by auto
  2.1266 -
  2.1267 -lemma affine_hull_eq[simp]: "(affine hull s = s) \<longleftrightarrow> affine s"
  2.1268 -  by (metis affine_affine_hull hull_same)
  2.1269 -
  2.1270 -lemma affine_hyperplane: "affine {x. a \<bullet> x = b}"
  2.1271 -  by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
  2.1272 -
  2.1273 -
  2.1274 -subsubsection%unimportant \<open>Some explicit formulations\<close>
  2.1275 -
  2.1276 -text "Formalized by Lars Schewe."
  2.1277 -
  2.1278 -lemma affine:
  2.1279 -  fixes V::"'a::real_vector set"
  2.1280 -  shows "affine V \<longleftrightarrow>
  2.1281 -         (\<forall>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> V \<and> sum u S = 1 \<longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V)"
  2.1282 -proof -
  2.1283 -  have "u *\<^sub>R x + v *\<^sub>R y \<in> V" if "x \<in> V" "y \<in> V" "u + v = (1::real)"
  2.1284 -    and *: "\<And>S u. \<lbrakk>finite S; S \<noteq> {}; S \<subseteq> V; sum u S = 1\<rbrakk> \<Longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V" for x y u v
  2.1285 -  proof (cases "x = y")
  2.1286 -    case True
  2.1287 -    then show ?thesis
  2.1288 -      using that by (metis scaleR_add_left scaleR_one)
  2.1289 -  next
  2.1290 -    case False
  2.1291 -    then show ?thesis
  2.1292 -      using that *[of "{x,y}" "\<lambda>w. if w = x then u else v"] by auto
  2.1293 -  qed
  2.1294 -  moreover have "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  2.1295 -                if *: "\<And>x y u v. \<lbrakk>x\<in>V; y\<in>V; u + v = 1\<rbrakk> \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V"
  2.1296 -                  and "finite S" "S \<noteq> {}" "S \<subseteq> V" "sum u S = 1" for S u
  2.1297 -  proof -
  2.1298 -    define n where "n = card S"
  2.1299 -    consider "card S = 0" | "card S = 1" | "card S = 2" | "card S > 2" by linarith
  2.1300 -    then show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  2.1301 -    proof cases
  2.1302 -      assume "card S = 1"
  2.1303 -      then obtain a where "S={a}"
  2.1304 -        by (auto simp: card_Suc_eq)
  2.1305 -      then show ?thesis
  2.1306 -        using that by simp
  2.1307 -    next
  2.1308 -      assume "card S = 2"
  2.1309 -      then obtain a b where "S = {a, b}"
  2.1310 -        by (metis Suc_1 card_1_singletonE card_Suc_eq)
  2.1311 -      then show ?thesis
  2.1312 -        using *[of a b] that
  2.1313 -        by (auto simp: sum_clauses(2))
  2.1314 -    next
  2.1315 -      assume "card S > 2"
  2.1316 -      then show ?thesis using that n_def
  2.1317 -      proof (induct n arbitrary: u S)
  2.1318 -        case 0
  2.1319 -        then show ?case by auto
  2.1320 -      next
  2.1321 -        case (Suc n u S)
  2.1322 -        have "sum u S = card S" if "\<not> (\<exists>x\<in>S. u x \<noteq> 1)"
  2.1323 -          using that unfolding card_eq_sum by auto
  2.1324 -        with Suc.prems obtain x where "x \<in> S" and x: "u x \<noteq> 1" by force
  2.1325 -        have c: "card (S - {x}) = card S - 1"
  2.1326 -          by (simp add: Suc.prems(3) \<open>x \<in> S\<close>)
  2.1327 -        have "sum u (S - {x}) = 1 - u x"
  2.1328 -          by (simp add: Suc.prems sum_diff1_ring \<open>x \<in> S\<close>)
  2.1329 -        with x have eq1: "inverse (1 - u x) * sum u (S - {x}) = 1"
  2.1330 -          by auto
  2.1331 -        have inV: "(\<Sum>y\<in>S - {x}. (inverse (1 - u x) * u y) *\<^sub>R y) \<in> V"
  2.1332 -        proof (cases "card (S - {x}) > 2")
  2.1333 -          case True
  2.1334 -          then have S: "S - {x} \<noteq> {}" "card (S - {x}) = n"
  2.1335 -            using Suc.prems c by force+
  2.1336 -          show ?thesis
  2.1337 -          proof (rule Suc.hyps)
  2.1338 -            show "(\<Sum>a\<in>S - {x}. inverse (1 - u x) * u a) = 1"
  2.1339 -              by (auto simp: eq1 sum_distrib_left[symmetric])
  2.1340 -          qed (use S Suc.prems True in auto)
  2.1341 -        next
  2.1342 -          case False
  2.1343 -          then have "card (S - {x}) = Suc (Suc 0)"
  2.1344 -            using Suc.prems c by auto
  2.1345 -          then obtain a b where ab: "(S - {x}) = {a, b}" "a\<noteq>b"
  2.1346 -            unfolding card_Suc_eq by auto
  2.1347 -          then show ?thesis
  2.1348 -            using eq1 \<open>S \<subseteq> V\<close>
  2.1349 -            by (auto simp: sum_distrib_left distrib_left intro!: Suc.prems(2)[of a b])
  2.1350 -        qed
  2.1351 -        have "u x + (1 - u x) = 1 \<Longrightarrow>
  2.1352 -          u x *\<^sub>R x + (1 - u x) *\<^sub>R ((\<Sum>y\<in>S - {x}. u y *\<^sub>R y) /\<^sub>R (1 - u x)) \<in> V"
  2.1353 -          by (rule Suc.prems) (use \<open>x \<in> S\<close> Suc.prems inV in \<open>auto simp: scaleR_right.sum\<close>)
  2.1354 -        moreover have "(\<Sum>a\<in>S. u a *\<^sub>R a) = u x *\<^sub>R x + (\<Sum>a\<in>S - {x}. u a *\<^sub>R a)"
  2.1355 -          by (meson Suc.prems(3) sum.remove \<open>x \<in> S\<close>)
  2.1356 -        ultimately show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
  2.1357 -          by (simp add: x)
  2.1358 -      qed
  2.1359 -    qed (use \<open>S\<noteq>{}\<close> \<open>finite S\<close> in auto)
  2.1360 -  qed
  2.1361 -  ultimately show ?thesis
  2.1362 -    unfolding affine_def by meson
  2.1363 -qed
  2.1364 -
  2.1365 -
  2.1366 -lemma affine_hull_explicit:
  2.1367 -  "affine hull p = {y. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and> sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  2.1368 -  (is "_ = ?rhs")
  2.1369 -proof (rule hull_unique)
  2.1370 -  show "p \<subseteq> ?rhs"
  2.1371 -  proof (intro subsetI CollectI exI conjI)
  2.1372 -    show "\<And>x. sum (\<lambda>z. 1) {x} = 1"
  2.1373 -      by auto
  2.1374 -  qed auto
  2.1375 -  show "?rhs \<subseteq> T" if "p \<subseteq> T" "affine T" for T
  2.1376 -    using that unfolding affine by blast
  2.1377 -  show "affine ?rhs"
  2.1378 -    unfolding affine_def
  2.1379 -  proof clarify
  2.1380 -    fix u v :: real and sx ux sy uy
  2.1381 -    assume uv: "u + v = 1"
  2.1382 -      and x: "finite sx" "sx \<noteq> {}" "sx \<subseteq> p" "sum ux sx = (1::real)"
  2.1383 -      and y: "finite sy" "sy \<noteq> {}" "sy \<subseteq> p" "sum uy sy = (1::real)" 
  2.1384 -    have **: "(sx \<union> sy) \<inter> sx = sx" "(sx \<union> sy) \<inter> sy = sy"
  2.1385 -      by auto
  2.1386 -    show "\<exists>S w. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and>
  2.1387 -        sum w S = 1 \<and> (\<Sum>v\<in>S. w v *\<^sub>R v) = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
  2.1388 -    proof (intro exI conjI)
  2.1389 -      show "finite (sx \<union> sy)"
  2.1390 -        using x y by auto
  2.1391 -      show "sum (\<lambda>i. (if i\<in>sx then u * ux i else 0) + (if i\<in>sy then v * uy i else 0)) (sx \<union> sy) = 1"
  2.1392 -        using x y uv
  2.1393 -        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric] sum_distrib_left [symmetric] **)
  2.1394 -      have "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i)
  2.1395 -          = (\<Sum>i\<in>sx. (u * ux i) *\<^sub>R i) + (\<Sum>i\<in>sy. (v * uy i) *\<^sub>R i)"
  2.1396 -        using x y
  2.1397 -        unfolding scaleR_left_distrib scaleR_zero_left if_smult
  2.1398 -        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric]  **)
  2.1399 -      also have "\<dots> = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
  2.1400 -        unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] by blast
  2.1401 -      finally show "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i) 
  2.1402 -                  = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)" .
  2.1403 -    qed (use x y in auto)
  2.1404 -  qed
  2.1405 -qed
  2.1406 -
  2.1407 -lemma affine_hull_finite:
  2.1408 -  assumes "finite S"
  2.1409 -  shows "affine hull S = {y. \<exists>u. sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  2.1410 -proof -
  2.1411 -  have *: "\<exists>h. sum h S = 1 \<and> (\<Sum>v\<in>S. h v *\<^sub>R v) = x" 
  2.1412 -    if "F \<subseteq> S" "finite F" "F \<noteq> {}" and sum: "sum u F = 1" and x: "(\<Sum>v\<in>F. u v *\<^sub>R v) = x" for x F u
  2.1413 -  proof -
  2.1414 -    have "S \<inter> F = F"
  2.1415 -      using that by auto
  2.1416 -    show ?thesis
  2.1417 -    proof (intro exI conjI)
  2.1418 -      show "(\<Sum>x\<in>S. if x \<in> F then u x else 0) = 1"
  2.1419 -        by (metis (mono_tags, lifting) \<open>S \<inter> F = F\<close> assms sum.inter_restrict sum)
  2.1420 -      show "(\<Sum>v\<in>S. (if v \<in> F then u v else 0) *\<^sub>R v) = x"
  2.1421 -        by (simp add: if_smult cong: if_cong) (metis (no_types) \<open>S \<inter> F = F\<close> assms sum.inter_restrict x)
  2.1422 -    qed
  2.1423 -  qed
  2.1424 -  show ?thesis
  2.1425 -    unfolding affine_hull_explicit using assms
  2.1426 -    by (fastforce dest: *)
  2.1427 -qed
  2.1428 -
  2.1429 -
  2.1430 -subsubsection%unimportant \<open>Stepping theorems and hence small special cases\<close>
  2.1431 -
  2.1432 -lemma affine_hull_empty[simp]: "affine hull {} = {}"
  2.1433 -  by simp
  2.1434 -
  2.1435 -lemma affine_hull_finite_step:
  2.1436 -  fixes y :: "'a::real_vector"
  2.1437 -  shows "finite S \<Longrightarrow>
  2.1438 -      (\<exists>u. sum u (insert a S) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a S) = y) \<longleftrightarrow>
  2.1439 -      (\<exists>v u. sum u S = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y - v *\<^sub>R a)" (is "_ \<Longrightarrow> ?lhs = ?rhs")
  2.1440 -proof -
  2.1441 -  assume fin: "finite S"
  2.1442 -  show "?lhs = ?rhs"
  2.1443 -  proof
  2.1444 -    assume ?lhs
  2.1445 -    then obtain u where u: "sum u (insert a S) = w \<and> (\<Sum>x\<in>insert a S. u x *\<^sub>R x) = y"
  2.1446 -      by auto
  2.1447 -    show ?rhs
  2.1448 -    proof (cases "a \<in> S")
  2.1449 -      case True
  2.1450 -      then show ?thesis
  2.1451 -        using u by (simp add: insert_absorb) (metis diff_zero real_vector.scale_zero_left)
  2.1452 -    next
  2.1453 -      case False
  2.1454 -      show ?thesis
  2.1455 -        by (rule exI [where x="u a"]) (use u fin False in auto)
  2.1456 -    qed
  2.1457 -  next
  2.1458 -    assume ?rhs
  2.1459 -    then obtain v u where vu: "sum u S = w - v"  "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  2.1460 -      by auto
  2.1461 -    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)"
  2.1462 -      by auto
  2.1463 -    show ?lhs
  2.1464 -    proof (cases "a \<in> S")
  2.1465 -      case True
  2.1466 -      show ?thesis
  2.1467 -        by (rule exI [where x="\<lambda>x. (if x=a then v else 0) + u x"])
  2.1468 -           (simp add: True scaleR_left_distrib sum.distrib sum_clauses fin vu * cong: if_cong)
  2.1469 -    next
  2.1470 -      case False
  2.1471 -      then show ?thesis
  2.1472 -        apply (rule_tac x="\<lambda>x. if x=a then v else u x" in exI) 
  2.1473 -        apply (simp add: vu sum_clauses(2)[OF fin] *)
  2.1474 -        by (simp add: sum_delta_notmem(3) vu)
  2.1475 -    qed
  2.1476 -  qed
  2.1477 -qed
  2.1478 -
  2.1479 -lemma affine_hull_2:
  2.1480 -  fixes a b :: "'a::real_vector"
  2.1481 -  shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}"
  2.1482 -  (is "?lhs = ?rhs")
  2.1483 -proof -
  2.1484 -  have *:
  2.1485 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  2.1486 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  2.1487 -  have "?lhs = {y. \<exists>u. sum u {a, b} = 1 \<and> (\<Sum>v\<in>{a, b}. u v *\<^sub>R v) = y}"
  2.1488 -    using affine_hull_finite[of "{a,b}"] by auto
  2.1489 -  also have "\<dots> = {y. \<exists>v u. u b = 1 - v \<and> u b *\<^sub>R b = y - v *\<^sub>R a}"
  2.1490 -    by (simp add: affine_hull_finite_step[of "{b}" a])
  2.1491 -  also have "\<dots> = ?rhs" unfolding * by auto
  2.1492 -  finally show ?thesis by auto
  2.1493 -qed
  2.1494 -
  2.1495 -lemma affine_hull_3:
  2.1496 -  fixes a b c :: "'a::real_vector"
  2.1497 -  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}"
  2.1498 -proof -
  2.1499 -  have *:
  2.1500 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
  2.1501 -    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
  2.1502 -  show ?thesis
  2.1503 -    apply (simp add: affine_hull_finite affine_hull_finite_step)
  2.1504 -    unfolding *
  2.1505 -    apply safe
  2.1506 -     apply (metis add.assoc)
  2.1507 -    apply (rule_tac x=u in exI, force)
  2.1508 -    done
  2.1509 -qed
  2.1510 -
  2.1511 -lemma mem_affine:
  2.1512 -  assumes "affine S" "x \<in> S" "y \<in> S" "u + v = 1"
  2.1513 -  shows "u *\<^sub>R x + v *\<^sub>R y \<in> S"
  2.1514 -  using assms affine_def[of S] by auto
  2.1515 -
  2.1516 -lemma mem_affine_3:
  2.1517 -  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S" "u + v + w = 1"
  2.1518 -  shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> S"
  2.1519 -proof -
  2.1520 -  have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> affine hull {x, y, z}"
  2.1521 -    using affine_hull_3[of x y z] assms by auto
  2.1522 -  moreover
  2.1523 -  have "affine hull {x, y, z} \<subseteq> affine hull S"
  2.1524 -    using hull_mono[of "{x, y, z}" "S"] assms by auto
  2.1525 -  moreover
  2.1526 -  have "affine hull S = S"
  2.1527 -    using assms affine_hull_eq[of S] by auto
  2.1528 -  ultimately show ?thesis by auto
  2.1529 -qed
  2.1530 -
  2.1531 -lemma mem_affine_3_minus:
  2.1532 -  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S"
  2.1533 -  shows "x + v *\<^sub>R (y-z) \<in> S"
  2.1534 -  using mem_affine_3[of S x y z 1 v "-v"] assms
  2.1535 -  by (simp add: algebra_simps)
  2.1536 -
  2.1537 -corollary mem_affine_3_minus2:
  2.1538 -    "\<lbrakk>affine S; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> x - v *\<^sub>R (y-z) \<in> S"
  2.1539 -  by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left)
  2.1540 -
  2.1541 -
  2.1542 -subsubsection%unimportant \<open>Some relations between affine hull and subspaces\<close>
  2.1543 -
  2.1544 -lemma affine_hull_insert_subset_span:
  2.1545 -  "affine hull (insert a S) \<subseteq> {a + v| v . v \<in> span {x - a | x . x \<in> S}}"
  2.1546 -proof -
  2.1547 -  have "\<exists>v T u. x = a + v \<and> (finite T \<and> T \<subseteq> {x - a |x. x \<in> S} \<and> (\<Sum>v\<in>T. u v *\<^sub>R v) = v)"
  2.1548 -    if "finite F" "F \<noteq> {}" "F \<subseteq> insert a S" "sum u F = 1" "(\<Sum>v\<in>F. u v *\<^sub>R v) = x"
  2.1549 -    for x F u
  2.1550 -  proof -
  2.1551 -    have *: "(\<lambda>x. x - a) ` (F - {a}) \<subseteq> {x - a |x. x \<in> S}"
  2.1552 -      using that by auto
  2.1553 -    show ?thesis
  2.1554 -    proof (intro exI conjI)
  2.1555 -      show "finite ((\<lambda>x. x - a) ` (F - {a}))"
  2.1556 -        by (simp add: that(1))
  2.1557 -      show "(\<Sum>v\<in>(\<lambda>x. x - a) ` (F - {a}). u(v+a) *\<^sub>R v) = x-a"
  2.1558 -        by (simp add: sum.reindex[unfolded inj_on_def] algebra_simps
  2.1559 -            sum_subtractf scaleR_left.sum[symmetric] sum_diff1 that)
  2.1560 -    qed (use \<open>F \<subseteq> insert a S\<close> in auto)
  2.1561 -  qed
  2.1562 -  then show ?thesis
  2.1563 -    unfolding affine_hull_explicit span_explicit by blast
  2.1564 -qed
  2.1565 -
  2.1566 -lemma affine_hull_insert_span:
  2.1567 -  assumes "a \<notin> S"
  2.1568 -  shows "affine hull (insert a S) = {a + v | v . v \<in> span {x - a | x.  x \<in> S}}"
  2.1569 -proof -
  2.1570 -  have *: "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
  2.1571 -    if "v \<in> span {x - a |x. x \<in> S}" "y = a + v" for y v
  2.1572 -  proof -
  2.1573 -    from that
  2.1574 -    obtain T u where u: "finite T" "T \<subseteq> {x - a |x. x \<in> S}" "a + (\<Sum>v\<in>T. u v *\<^sub>R v) = y"
  2.1575 -      unfolding span_explicit by auto
  2.1576 -    define F where "F = (\<lambda>x. x + a) ` T"
  2.1577 -    have F: "finite F" "F \<subseteq> S" "(\<Sum>v\<in>F. u (v - a) *\<^sub>R (v - a)) = y - a"
  2.1578 -      unfolding F_def using u by (auto simp: sum.reindex[unfolded inj_on_def])
  2.1579 -    have *: "F \<inter> {a} = {}" "F \<inter> - {a} = F"
  2.1580 -      using F assms by auto
  2.1581 -    show "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
  2.1582 -      apply (rule_tac x = "insert a F" in exI)
  2.1583 -      apply (rule_tac x = "\<lambda>x. if x=a then 1 - sum (\<lambda>x. u (x - a)) F else u (x - a)" in exI)
  2.1584 -      using assms F
  2.1585 -      apply (auto simp:  sum_clauses sum.If_cases if_smult sum_subtractf scaleR_left.sum algebra_simps *)
  2.1586 -      done
  2.1587 -  qed
  2.1588 -  show ?thesis
  2.1589 -    by (intro subset_antisym affine_hull_insert_subset_span) (auto simp: affine_hull_explicit dest!: *)
  2.1590 -qed
  2.1591 -
  2.1592 -lemma affine_hull_span:
  2.1593 -  assumes "a \<in> S"
  2.1594 -  shows "affine hull S = {a + v | v. v \<in> span {x - a | x. x \<in> S - {a}}}"
  2.1595 -  using affine_hull_insert_span[of a "S - {a}", unfolded insert_Diff[OF assms]] by auto
  2.1596 -
  2.1597 -
  2.1598 -subsubsection%unimportant \<open>Parallel affine sets\<close>
  2.1599 -
  2.1600 -definition affine_parallel :: "'a::real_vector set \<Rightarrow> 'a::real_vector set \<Rightarrow> bool"
  2.1601 -  where "affine_parallel S T \<longleftrightarrow> (\<exists>a. T = (\<lambda>x. a + x) ` S)"
  2.1602 -
  2.1603 -lemma affine_parallel_expl_aux:
  2.1604 -  fixes S T :: "'a::real_vector set"
  2.1605 -  assumes "\<And>x. x \<in> S \<longleftrightarrow> a + x \<in> T"
  2.1606 -  shows "T = (\<lambda>x. a + x) ` S"
  2.1607 -proof -
  2.1608 -  have "x \<in> ((\<lambda>x. a + x) ` S)" if "x \<in> T" for x
  2.1609 -    using that
  2.1610 -    by (simp add: image_iff) (metis add.commute diff_add_cancel assms)
  2.1611 -  moreover have "T \<ge> (\<lambda>x. a + x) ` S"
  2.1612 -    using assms by auto
  2.1613 -  ultimately show ?thesis by auto
  2.1614 -qed
  2.1615 -
  2.1616 -lemma affine_parallel_expl: "affine_parallel S T \<longleftrightarrow> (\<exists>a. \<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T)"
  2.1617 -  unfolding affine_parallel_def
  2.1618 -  using affine_parallel_expl_aux[of S _ T] by auto
  2.1619 -
  2.1620 -lemma affine_parallel_reflex: "affine_parallel S S"
  2.1621 -  unfolding affine_parallel_def
  2.1622 -  using image_add_0 by blast
  2.1623 -
  2.1624 -lemma affine_parallel_commut:
  2.1625 -  assumes "affine_parallel A B"
  2.1626 -  shows "affine_parallel B A"
  2.1627 -proof -
  2.1628 -  from assms obtain a where B: "B = (\<lambda>x. a + x) ` A"
  2.1629 -    unfolding affine_parallel_def by auto
  2.1630 -  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  2.1631 -  from B show ?thesis
  2.1632 -    using translation_galois [of B a A]
  2.1633 -    unfolding affine_parallel_def by auto
  2.1634 -qed
  2.1635 -
  2.1636 -lemma affine_parallel_assoc:
  2.1637 -  assumes "affine_parallel A B"
  2.1638 -    and "affine_parallel B C"
  2.1639 -  shows "affine_parallel A C"
  2.1640 -proof -
  2.1641 -  from assms obtain ab where "B = (\<lambda>x. ab + x) ` A"
  2.1642 -    unfolding affine_parallel_def by auto
  2.1643 -  moreover
  2.1644 -  from assms obtain bc where "C = (\<lambda>x. bc + x) ` B"
  2.1645 -    unfolding affine_parallel_def by auto
  2.1646 -  ultimately show ?thesis
  2.1647 -    using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto
  2.1648 -qed
  2.1649 -
  2.1650 -lemma affine_translation_aux:
  2.1651 -  fixes a :: "'a::real_vector"
  2.1652 -  assumes "affine ((\<lambda>x. a + x) ` S)"
  2.1653 -  shows "affine S"
  2.1654 -proof -
  2.1655 -  {
  2.1656 -    fix x y u v
  2.1657 -    assume xy: "x \<in> S" "y \<in> S" "(u :: real) + v = 1"
  2.1658 -    then have "(a + x) \<in> ((\<lambda>x. a + x) ` S)" "(a + y) \<in> ((\<lambda>x. a + x) ` S)"
  2.1659 -      by auto
  2.1660 -    then have h1: "u *\<^sub>R  (a + x) + v *\<^sub>R (a + y) \<in> (\<lambda>x. a + x) ` S"
  2.1661 -      using xy assms unfolding affine_def by auto
  2.1662 -    have "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) = (u + v) *\<^sub>R a + (u *\<^sub>R x + v *\<^sub>R y)"
  2.1663 -      by (simp add: algebra_simps)
  2.1664 -    also have "\<dots> = a + (u *\<^sub>R x + v *\<^sub>R y)"
  2.1665 -      using \<open>u + v = 1\<close> by auto
  2.1666 -    ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \<in> (\<lambda>x. a + x) ` S"
  2.1667 -      using h1 by auto
  2.1668 -    then have "u *\<^sub>R x + v *\<^sub>R y \<in> S" by auto
  2.1669 -  }
  2.1670 -  then show ?thesis unfolding affine_def by auto
  2.1671 -qed
  2.1672 -
  2.1673 -lemma affine_translation:
  2.1674 -  fixes a :: "'a::real_vector"
  2.1675 -  shows "affine S \<longleftrightarrow> affine ((\<lambda>x. a + x) ` S)"
  2.1676 -proof -
  2.1677 -  have "affine S \<Longrightarrow> affine ((\<lambda>x. a + x) ` S)"
  2.1678 -    using affine_translation_aux[of "-a" "((\<lambda>x. a + x) ` S)"]
  2.1679 -    using translation_assoc[of "-a" a S] by auto
  2.1680 -  then show ?thesis using affine_translation_aux by auto
  2.1681 -qed
  2.1682 -
  2.1683 -lemma parallel_is_affine:
  2.1684 -  fixes S T :: "'a::real_vector set"
  2.1685 -  assumes "affine S" "affine_parallel S T"
  2.1686 -  shows "affine T"
  2.1687 -proof -
  2.1688 -  from assms obtain a where "T = (\<lambda>x. a + x) ` S"
  2.1689 -    unfolding affine_parallel_def by auto
  2.1690 -  then show ?thesis
  2.1691 -    using affine_translation assms by auto
  2.1692 -qed
  2.1693 -
  2.1694 -lemma subspace_imp_affine: "subspace s \<Longrightarrow> affine s"
  2.1695 -  unfolding subspace_def affine_def by auto
  2.1696 -
  2.1697 -
  2.1698 -subsubsection%unimportant \<open>Subspace parallel to an affine set\<close>
  2.1699 -
  2.1700 -lemma subspace_affine: "subspace S \<longleftrightarrow> affine S \<and> 0 \<in> S"
  2.1701 -proof -
  2.1702 -  have h0: "subspace S \<Longrightarrow> affine S \<and> 0 \<in> S"
  2.1703 -    using subspace_imp_affine[of S] subspace_0 by auto
  2.1704 -  {
  2.1705 -    assume assm: "affine S \<and> 0 \<in> S"
  2.1706 -    {
  2.1707 -      fix c :: real
  2.1708 -      fix x
  2.1709 -      assume x: "x \<in> S"
  2.1710 -      have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto
  2.1711 -      moreover
  2.1712 -      have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \<in> S"
  2.1713 -        using affine_alt[of S] assm x by auto
  2.1714 -      ultimately have "c *\<^sub>R x \<in> S" by auto
  2.1715 -    }
  2.1716 -    then have h1: "\<forall>c. \<forall>x \<in> S. c *\<^sub>R x \<in> S" by auto
  2.1717 -
  2.1718 -    {
  2.1719 -      fix x y
  2.1720 -      assume xy: "x \<in> S" "y \<in> S"
  2.1721 -      define u where "u = (1 :: real)/2"
  2.1722 -      have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)"
  2.1723 -        by auto
  2.1724 -      moreover
  2.1725 -      have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y"
  2.1726 -        by (simp add: algebra_simps)
  2.1727 -      moreover
  2.1728 -      have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> S"
  2.1729 -        using affine_alt[of S] assm xy by auto
  2.1730 -      ultimately
  2.1731 -      have "(1/2) *\<^sub>R (x+y) \<in> S"
  2.1732 -        using u_def by auto
  2.1733 -      moreover
  2.1734 -      have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))"
  2.1735 -        by auto
  2.1736 -      ultimately
  2.1737 -      have "x + y \<in> S"
  2.1738 -        using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto
  2.1739 -    }
  2.1740 -    then have "\<forall>x \<in> S. \<forall>y \<in> S. x + y \<in> S"
  2.1741 -      by auto
  2.1742 -    then have "subspace S"
  2.1743 -      using h1 assm unfolding subspace_def by auto
  2.1744 -  }
  2.1745 -  then show ?thesis using h0 by metis
  2.1746 -qed
  2.1747 -
  2.1748 -lemma affine_diffs_subspace:
  2.1749 -  assumes "affine S" "a \<in> S"
  2.1750 -  shows "subspace ((\<lambda>x. (-a)+x) ` S)"
  2.1751 -proof -
  2.1752 -  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
  2.1753 -  have "affine ((\<lambda>x. (-a)+x) ` S)"
  2.1754 -    using  affine_translation assms by auto
  2.1755 -  moreover have "0 \<in> ((\<lambda>x. (-a)+x) ` S)"
  2.1756 -    using assms exI[of "(\<lambda>x. x\<in>S \<and> -a+x = 0)" a] by auto
  2.1757 -  ultimately show ?thesis using subspace_affine by auto
  2.1758 -qed
  2.1759 -
  2.1760 -lemma parallel_subspace_explicit:
  2.1761 -  assumes "affine S"
  2.1762 -    and "a \<in> S"
  2.1763 -  assumes "L \<equiv> {y. \<exists>x \<in> S. (-a) + x = y}"
  2.1764 -  shows "subspace L \<and> affine_parallel S L"
  2.1765 -proof -
  2.1766 -  from assms have "L = plus (- a) ` S" by auto
  2.1767 -  then have par: "affine_parallel S L"
  2.1768 -    unfolding affine_parallel_def ..
  2.1769 -  then have "affine L" using assms parallel_is_affine by auto
  2.1770 -  moreover have "0 \<in> L"
  2.1771 -    using assms by auto
  2.1772 -  ultimately show ?thesis
  2.1773 -    using subspace_affine par by auto
  2.1774 -qed
  2.1775 -
  2.1776 -lemma parallel_subspace_aux:
  2.1777 -  assumes "subspace A"
  2.1778 -    and "subspace B"
  2.1779 -    and "affine_parallel A B"
  2.1780 -  shows "A \<supseteq> B"
  2.1781 -proof -
  2.1782 -  from assms obtain a where a: "\<forall>x. x \<in> A \<longleftrightarrow> a + x \<in> B"
  2.1783 -    using affine_parallel_expl[of A B] by auto
  2.1784 -  then have "-a \<in> A"
  2.1785 -    using assms subspace_0[of B] by auto
  2.1786 -  then have "a \<in> A"
  2.1787 -    using assms subspace_neg[of A "-a"] by auto
  2.1788 -  then show ?thesis
  2.1789 -    using assms a unfolding subspace_def by auto
  2.1790 -qed
  2.1791 -
  2.1792 -lemma parallel_subspace:
  2.1793 -  assumes "subspace A"
  2.1794 -    and "subspace B"
  2.1795 -    and "affine_parallel A B"
  2.1796 -  shows "A = B"
  2.1797 -proof
  2.1798 -  show "A \<supseteq> B"
  2.1799 -    using assms parallel_subspace_aux by auto
  2.1800 -  show "A \<subseteq> B"
  2.1801 -    using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto
  2.1802 -qed
  2.1803 -
  2.1804 -lemma affine_parallel_subspace:
  2.1805 -  assumes "affine S" "S \<noteq> {}"
  2.1806 -  shows "\<exists>!L. subspace L \<and> affine_parallel S L"
  2.1807 -proof -
  2.1808 -  have ex: "\<exists>L. subspace L \<and> affine_parallel S L"
  2.1809 -    using assms parallel_subspace_explicit by auto
  2.1810 -  {
  2.1811 -    fix L1 L2
  2.1812 -    assume ass: "subspace L1 \<and> affine_parallel S L1" "subspace L2 \<and> affine_parallel S L2"
  2.1813 -    then have "affine_parallel L1 L2"
  2.1814 -      using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto
  2.1815 -    then have "L1 = L2"
  2.1816 -      using ass parallel_subspace by auto
  2.1817 -  }
  2.1818 -  then show ?thesis using ex by auto
  2.1819 -qed
  2.1820 -
  2.1821 -
  2.1822 -subsection \<open>Cones\<close>
  2.1823 -
  2.1824 -definition%important cone :: "'a::real_vector set \<Rightarrow> bool"
  2.1825 -  where "cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>c\<ge>0. c *\<^sub>R x \<in> s)"
  2.1826 -
  2.1827 -lemma cone_empty[intro, simp]: "cone {}"
  2.1828 -  unfolding cone_def by auto
  2.1829 -
  2.1830 -lemma cone_univ[intro, simp]: "cone UNIV"
  2.1831 -  unfolding cone_def by auto
  2.1832 -
  2.1833 -lemma cone_Inter[intro]: "\<forall>s\<in>f. cone s \<Longrightarrow> cone (\<Inter>f)"
  2.1834 -  unfolding cone_def by auto
  2.1835 -
  2.1836 -lemma subspace_imp_cone: "subspace S \<Longrightarrow> cone S"
  2.1837 -  by (simp add: cone_def subspace_scale)
  2.1838 -
  2.1839 -
  2.1840 -subsubsection \<open>Conic hull\<close>
  2.1841 -
  2.1842 -lemma cone_cone_hull: "cone (cone hull s)"
  2.1843 -  unfolding hull_def by auto
  2.1844 -
  2.1845 -lemma cone_hull_eq: "cone hull s = s \<longleftrightarrow> cone s"
  2.1846 -  apply (rule hull_eq)
  2.1847 -  using cone_Inter
  2.1848 -  unfolding subset_eq
  2.1849 -  apply auto
  2.1850 -  done
  2.1851 -
  2.1852 -lemma mem_cone:
  2.1853 -  assumes "cone S" "x \<in> S" "c \<ge> 0"
  2.1854 -  shows "c *\<^sub>R x \<in> S"
  2.1855 -  using assms cone_def[of S] by auto
  2.1856 -
  2.1857 -lemma cone_contains_0:
  2.1858 -  assumes "cone S"
  2.1859 -  shows "S \<noteq> {} \<longleftrightarrow> 0 \<in> S"
  2.1860 -proof -
  2.1861 -  {
  2.1862 -    assume "S \<noteq> {}"
  2.1863 -    then obtain a where "a \<in> S" by auto
  2.1864 -    then have "0 \<in> S"
  2.1865 -      using assms mem_cone[of S a 0] by auto
  2.1866 -  }
  2.1867 -  then show ?thesis by auto
  2.1868 -qed
  2.1869 -
  2.1870 -lemma cone_0: "cone {0}"
  2.1871 -  unfolding cone_def by auto
  2.1872 -
  2.1873 -lemma cone_Union[intro]: "(\<forall>s\<in>f. cone s) \<longrightarrow> cone (\<Union>f)"
  2.1874 -  unfolding cone_def by blast
  2.1875 -
  2.1876 -lemma cone_iff:
  2.1877 -  assumes "S \<noteq> {}"
  2.1878 -  shows "cone S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  2.1879 -proof -
  2.1880 -  {
  2.1881 -    assume "cone S"
  2.1882 -    {
  2.1883 -      fix c :: real
  2.1884 -      assume "c > 0"
  2.1885 -      {
  2.1886 -        fix x
  2.1887 -        assume "x \<in> S"
  2.1888 -        then have "x \<in> ((*\<^sub>R) c) ` S"
  2.1889 -          unfolding image_def
  2.1890 -          using \<open>cone S\<close> \<open>c>0\<close> mem_cone[of S x "1/c"]
  2.1891 -            exI[of "(\<lambda>t. t \<in> S \<and> x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"]
  2.1892 -          by auto
  2.1893 -      }
  2.1894 -      moreover
  2.1895 -      {
  2.1896 -        fix x
  2.1897 -        assume "x \<in> ((*\<^sub>R) c) ` S"
  2.1898 -        then have "x \<in> S"
  2.1899 -          using \<open>cone S\<close> \<open>c > 0\<close>
  2.1900 -          unfolding cone_def image_def \<open>c > 0\<close> by auto
  2.1901 -      }
  2.1902 -      ultimately have "((*\<^sub>R) c) ` S = S" by auto
  2.1903 -    }
  2.1904 -    then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  2.1905 -      using \<open>cone S\<close> cone_contains_0[of S] assms by auto
  2.1906 -  }
  2.1907 -  moreover
  2.1908 -  {
  2.1909 -    assume a: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ((*\<^sub>R) c) ` S = S)"
  2.1910 -    {
  2.1911 -      fix x
  2.1912 -      assume "x \<in> S"
  2.1913 -      fix c1 :: real
  2.1914 -      assume "c1 \<ge> 0"
  2.1915 -      then have "c1 = 0 \<or> c1 > 0" by auto
  2.1916 -      then have "c1 *\<^sub>R x \<in> S" using a \<open>x \<in> S\<close> by auto
  2.1917 -    }
  2.1918 -    then have "cone S" unfolding cone_def by auto
  2.1919 -  }
  2.1920 -  ultimately show ?thesis by blast
  2.1921 -qed
  2.1922 -
  2.1923 -lemma cone_hull_empty: "cone hull {} = {}"
  2.1924 -  by (metis cone_empty cone_hull_eq)
  2.1925 -
  2.1926 -lemma cone_hull_empty_iff: "S = {} \<longleftrightarrow> cone hull S = {}"
  2.1927 -  by (metis bot_least cone_hull_empty hull_subset xtrans(5))
  2.1928 -
  2.1929 -lemma cone_hull_contains_0: "S \<noteq> {} \<longleftrightarrow> 0 \<in> cone hull S"
  2.1930 -  using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S]
  2.1931 -  by auto
  2.1932 -
  2.1933 -lemma mem_cone_hull:
  2.1934 -  assumes "x \<in> S" "c \<ge> 0"
  2.1935 -  shows "c *\<^sub>R x \<in> cone hull S"
  2.1936 -  by (metis assms cone_cone_hull hull_inc mem_cone)
  2.1937 -
  2.1938 -proposition cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \<ge> 0 \<and> x \<in> S}"
  2.1939 -  (is "?lhs = ?rhs")
  2.1940 -proof -
  2.1941 -  {
  2.1942 -    fix x
  2.1943 -    assume "x \<in> ?rhs"
  2.1944 -    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  2.1945 -      by auto
  2.1946 -    fix c :: real
  2.1947 -    assume c: "c \<ge> 0"
  2.1948 -    then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx"
  2.1949 -      using x by (simp add: algebra_simps)
  2.1950 -    moreover
  2.1951 -    have "c * cx \<ge> 0" using c x by auto
  2.1952 -    ultimately
  2.1953 -    have "c *\<^sub>R x \<in> ?rhs" using x by auto
  2.1954 -  }
  2.1955 -  then have "cone ?rhs"
  2.1956 -    unfolding cone_def by auto
  2.1957 -  then have "?rhs \<in> Collect cone"
  2.1958 -    unfolding mem_Collect_eq by auto
  2.1959 -  {
  2.1960 -    fix x
  2.1961 -    assume "x \<in> S"
  2.1962 -    then have "1 *\<^sub>R x \<in> ?rhs"
  2.1963 -      apply auto
  2.1964 -      apply (rule_tac x = 1 in exI, auto)
  2.1965 -      done
  2.1966 -    then have "x \<in> ?rhs" by auto
  2.1967 -  }
  2.1968 -  then have "S \<subseteq> ?rhs" by auto
  2.1969 -  then have "?lhs \<subseteq> ?rhs"
  2.1970 -    using \<open>?rhs \<in> Collect cone\<close> hull_minimal[of S "?rhs" "cone"] by auto
  2.1971 -  moreover
  2.1972 -  {
  2.1973 -    fix x
  2.1974 -    assume "x \<in> ?rhs"
  2.1975 -    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
  2.1976 -      by auto
  2.1977 -    then have "xx \<in> cone hull S"
  2.1978 -      using hull_subset[of S] by auto
  2.1979 -    then have "x \<in> ?lhs"
  2.1980 -      using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto
  2.1981 -  }
  2.1982 -  ultimately show ?thesis by auto
  2.1983 -qed
  2.1984 -
  2.1985  lemma cone_closure:
  2.1986    fixes S :: "'a::real_normed_vector set"
  2.1987    assumes "cone S"
  2.1988 @@ -2071,122 +124,6 @@
  2.1989      using False cone_iff[of "closure S"] by auto
  2.1990  qed
  2.1991  
  2.1992 -
  2.1993 -subsection \<open>Affine dependence and consequential theorems\<close>
  2.1994 -
  2.1995 -text "Formalized by Lars Schewe."
  2.1996 -
  2.1997 -definition%important affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
  2.1998 -  where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
  2.1999 -
  2.2000 -lemma affine_dependent_subset:
  2.2001 -   "\<lbrakk>affine_dependent s; s \<subseteq> t\<rbrakk> \<Longrightarrow> affine_dependent t"
  2.2002 -apply (simp add: affine_dependent_def Bex_def)
  2.2003 -apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]])
  2.2004 -done
  2.2005 -
  2.2006 -lemma affine_independent_subset:
  2.2007 -  shows "\<lbrakk>\<not> affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> \<not> affine_dependent s"
  2.2008 -by (metis affine_dependent_subset)
  2.2009 -
  2.2010 -lemma affine_independent_Diff:
  2.2011 -   "\<not> affine_dependent s \<Longrightarrow> \<not> affine_dependent(s - t)"
  2.2012 -by (meson Diff_subset affine_dependent_subset)
  2.2013 -
  2.2014 -proposition affine_dependent_explicit:
  2.2015 -  "affine_dependent p \<longleftrightarrow>
  2.2016 -    (\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
  2.2017 -proof -
  2.2018 -  have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> (\<Sum>w\<in>S. u w *\<^sub>R w) = 0"
  2.2019 -    if "(\<Sum>w\<in>S. u w *\<^sub>R w) = x" "x \<in> p" "finite S" "S \<noteq> {}" "S \<subseteq> p - {x}" "sum u S = 1" for x S u
  2.2020 -  proof (intro exI conjI)
  2.2021 -    have "x \<notin> S" 
  2.2022 -      using that by auto
  2.2023 -    then show "(\<Sum>v \<in> insert x S. if v = x then - 1 else u v) = 0"
  2.2024 -      using that by (simp add: sum_delta_notmem)
  2.2025 -    show "(\<Sum>w \<in> insert x S. (if w = x then - 1 else u w) *\<^sub>R w) = 0"
  2.2026 -      using that \<open>x \<notin> S\<close> by (simp add: if_smult sum_delta_notmem cong: if_cong)
  2.2027 -  qed (use that in auto)
  2.2028 -  moreover have "\<exists>x\<in>p. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p - {x} \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
  2.2029 -    if "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0" "finite S" "S \<subseteq> p" "sum u S = 0" "v \<in> S" "u v \<noteq> 0" for S u v
  2.2030 -  proof (intro bexI exI conjI)
  2.2031 -    have "S \<noteq> {v}"
  2.2032 -      using that by auto
  2.2033 -    then show "S - {v} \<noteq> {}"
  2.2034 -      using that by auto
  2.2035 -    show "(\<Sum>x \<in> S - {v}. - (1 / u v) * u x) = 1"
  2.2036 -      unfolding sum_distrib_left[symmetric] sum_diff1[OF \<open>finite S\<close>] by (simp add: that)
  2.2037 -    show "(\<Sum>x\<in>S - {v}. (- (1 / u v) * u x) *\<^sub>R x) = v"
  2.2038 -      unfolding sum_distrib_left [symmetric] scaleR_scaleR[symmetric]
  2.2039 -                scaleR_right.sum [symmetric] sum_diff1[OF \<open>finite S\<close>] 
  2.2040 -      using that by auto
  2.2041 -    show "S - {v} \<subseteq> p - {v}"
  2.2042 -      using that by auto
  2.2043 -  qed (use that in auto)
  2.2044 -  ultimately show ?thesis
  2.2045 -    unfolding affine_dependent_def affine_hull_explicit by auto
  2.2046 -qed
  2.2047 -
  2.2048 -lemma affine_dependent_explicit_finite:
  2.2049 -  fixes S :: "'a::real_vector set"
  2.2050 -  assumes "finite S"
  2.2051 -  shows "affine_dependent S \<longleftrightarrow>
  2.2052 -    (\<exists>u. sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
  2.2053 -  (is "?lhs = ?rhs")
  2.2054 -proof
  2.2055 -  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)"
  2.2056 -    by auto
  2.2057 -  assume ?lhs
  2.2058 -  then obtain t u v where
  2.2059 -    "finite t" "t \<subseteq> S" "sum u t = 0" "v\<in>t" "u v \<noteq> 0"  "(\<Sum>v\<in>t. u v *\<^sub>R v) = 0"
  2.2060 -    unfolding affine_dependent_explicit by auto
  2.2061 -  then show ?rhs
  2.2062 -    apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
  2.2063 -    apply (auto simp: * sum.inter_restrict[OF assms, symmetric] Int_absorb1[OF \<open>t\<subseteq>S\<close>])
  2.2064 -    done
  2.2065 -next
  2.2066 -  assume ?rhs
  2.2067 -  then obtain u v where "sum u S = 0"  "v\<in>S" "u v \<noteq> 0" "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0"
  2.2068 -    by auto
  2.2069 -  then show ?lhs unfolding affine_dependent_explicit
  2.2070 -    using assms by auto
  2.2071 -qed
  2.2072 -
  2.2073 -
  2.2074 -subsection%unimportant \<open>Connectedness of convex sets\<close>
  2.2075 -
  2.2076 -lemma connectedD:
  2.2077 -  "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 = {}"
  2.2078 -  by (rule Topological_Spaces.topological_space_class.connectedD)
  2.2079 -
  2.2080 -lemma convex_connected:
  2.2081 -  fixes S :: "'a::real_normed_vector set"
  2.2082 -  assumes "convex S"
  2.2083 -  shows "connected S"
  2.2084 -proof (rule connectedI)
  2.2085 -  fix A B
  2.2086 -  assume "open A" "open B" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
  2.2087 -  moreover
  2.2088 -  assume "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}"
  2.2089 -  then obtain a b where a: "a \<in> A" "a \<in> S" and b: "b \<in> B" "b \<in> S" by auto
  2.2090 -  define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u
  2.2091 -  then have "continuous_on {0 .. 1} f"
  2.2092 -    by (auto intro!: continuous_intros)
  2.2093 -  then have "connected (f ` {0 .. 1})"
  2.2094 -    by (auto intro!: connected_continuous_image)
  2.2095 -  note connectedD[OF this, of A B]
  2.2096 -  moreover have "a \<in> A \<inter> f ` {0 .. 1}"
  2.2097 -    using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def)
  2.2098 -  moreover have "b \<in> B \<inter> f ` {0 .. 1}"
  2.2099 -    using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def)
  2.2100 -  moreover have "f ` {0 .. 1} \<subseteq> S"
  2.2101 -    using \<open>convex S\<close> a b unfolding convex_def f_def by auto
  2.2102 -  ultimately show False by auto
  2.2103 -qed
  2.2104 -
  2.2105 -corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
  2.2106 -  by (simp add: convex_connected)
  2.2107 -
  2.2108  corollary component_complement_connected:
  2.2109    fixes S :: "'a::real_normed_vector set"
  2.2110    assumes "connected S" "C \<in> components (-S)"
  2.2111 @@ -2216,15 +153,6 @@
  2.2112  
  2.2113  text \<open>Balls, being convex, are connected.\<close>
  2.2114  
  2.2115 -lemma convex_prod:
  2.2116 -  assumes "\<And>i. i \<in> Basis \<Longrightarrow> convex {x. P i x}"
  2.2117 -  shows "convex {x. \<forall>i\<in>Basis. P i (x\<bullet>i)}"
  2.2118 -  using assms unfolding convex_def
  2.2119 -  by (auto simp: inner_add_left)
  2.2120 -
  2.2121 -lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i)}"
  2.2122 -  by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval)
  2.2123 -
  2.2124  lemma convex_local_global_minimum:
  2.2125    fixes s :: "'a::real_normed_vector set"
  2.2126    assumes "e > 0"
  2.2127 @@ -2310,20 +238,6 @@
  2.2128    using convex_connected convex_cball by auto
  2.2129  
  2.2130  
  2.2131 -subsection \<open>Convex hull\<close>
  2.2132 -
  2.2133 -lemma convex_convex_hull [iff]: "convex (convex hull s)"
  2.2134 -  unfolding hull_def
  2.2135 -  using convex_Inter[of "{t. convex t \<and> s \<subseteq> t}"]
  2.2136 -  by auto
  2.2137 -
  2.2138 -lemma convex_hull_subset:
  2.2139 -    "s \<subseteq> convex hull t \<Longrightarrow> convex hull s \<subseteq> convex hull t"
  2.2140 -  by (simp add: convex_convex_hull subset_hull)
  2.2141 -
  2.2142 -lemma convex_hull_eq: "convex hull s = s \<longleftrightarrow> convex s"
  2.2143 -  by (metis convex_convex_hull hull_same)
  2.2144 -
  2.2145  lemma bounded_convex_hull:
  2.2146    fixes s :: "'a::real_normed_vector set"
  2.2147    assumes "bounded s"
  2.2148 @@ -2345,1499 +259,6 @@
  2.2149    using bounded_convex_hull finite_imp_bounded
  2.2150    by auto
  2.2151  
  2.2152 -
  2.2153 -subsubsection%unimportant \<open>Convex hull is "preserved" by a linear function\<close>
  2.2154 -
  2.2155 -lemma convex_hull_linear_image:
  2.2156 -  assumes f: "linear f"
  2.2157 -  shows "f ` (convex hull s) = convex hull (f ` s)"
  2.2158 -proof
  2.2159 -  show "convex hull (f ` s) \<subseteq> f ` (convex hull s)"
  2.2160 -    by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull)
  2.2161 -  show "f ` (convex hull s) \<subseteq> convex hull (f ` s)"
  2.2162 -  proof (unfold image_subset_iff_subset_vimage, rule hull_minimal)
  2.2163 -    show "s \<subseteq> f -` (convex hull (f ` s))"
  2.2164 -      by (fast intro: hull_inc)
  2.2165 -    show "convex (f -` (convex hull (f ` s)))"
  2.2166 -      by (intro convex_linear_vimage [OF f] convex_convex_hull)
  2.2167 -  qed
  2.2168 -qed
  2.2169 -
  2.2170 -lemma in_convex_hull_linear_image:
  2.2171 -  assumes "linear f"
  2.2172 -    and "x \<in> convex hull s"
  2.2173 -  shows "f x \<in> convex hull (f ` s)"
  2.2174 -  using convex_hull_linear_image[OF assms(1)] assms(2) by auto
  2.2175 -
  2.2176 -lemma convex_hull_Times:
  2.2177 -  "convex hull (s \<times> t) = (convex hull s) \<times> (convex hull t)"
  2.2178 -proof
  2.2179 -  show "convex hull (s \<times> t) \<subseteq> (convex hull s) \<times> (convex hull t)"
  2.2180 -    by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull)
  2.2181 -  have "(x, y) \<in> convex hull (s \<times> t)" if x: "x \<in> convex hull s" and y: "y \<in> convex hull t" for x y
  2.2182 -  proof (rule hull_induct [OF x], rule hull_induct [OF y])
  2.2183 -    fix x y assume "x \<in> s" and "y \<in> t"
  2.2184 -    then show "(x, y) \<in> convex hull (s \<times> t)"
  2.2185 -      by (simp add: hull_inc)
  2.2186 -  next
  2.2187 -    fix x let ?S = "((\<lambda>y. (0, y)) -` (\<lambda>p. (- x, 0) + p) ` (convex hull s \<times> t))"
  2.2188 -    have "convex ?S"
  2.2189 -      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  2.2190 -        simp add: linear_iff)
  2.2191 -    also have "?S = {y. (x, y) \<in> convex hull (s \<times> t)}"
  2.2192 -      by (auto simp: image_def Bex_def)
  2.2193 -    finally show "convex {y. (x, y) \<in> convex hull (s \<times> t)}" .
  2.2194 -  next
  2.2195 -    show "convex {x. (x, y) \<in> convex hull s \<times> t}"
  2.2196 -    proof -
  2.2197 -      fix y let ?S = "((\<lambda>x. (x, 0)) -` (\<lambda>p. (0, - y) + p) ` (convex hull s \<times> t))"
  2.2198 -      have "convex ?S"
  2.2199 -      by (intro convex_linear_vimage convex_translation convex_convex_hull,
  2.2200 -        simp add: linear_iff)
  2.2201 -      also have "?S = {x. (x, y) \<in> convex hull (s \<times> t)}"
  2.2202 -        by (auto simp: image_def Bex_def)
  2.2203 -      finally show "convex {x. (x, y) \<in> convex hull (s \<times> t)}" .
  2.2204 -    qed
  2.2205 -  qed
  2.2206 -  then show "(convex hull s) \<times> (convex hull t) \<subseteq> convex hull (s \<times> t)"
  2.2207 -    unfolding subset_eq split_paired_Ball_Sigma by blast
  2.2208 -qed
  2.2209 -
  2.2210 -
  2.2211 -subsubsection%unimportant \<open>Stepping theorems for convex hulls of finite sets\<close>
  2.2212 -
  2.2213 -lemma convex_hull_empty[simp]: "convex hull {} = {}"
  2.2214 -  by (rule hull_unique) auto
  2.2215 -
  2.2216 -lemma convex_hull_singleton[simp]: "convex hull {a} = {a}"
  2.2217 -  by (rule hull_unique) auto
  2.2218 -
  2.2219 -lemma convex_hull_insert:
  2.2220 -  fixes S :: "'a::real_vector set"
  2.2221 -  assumes "S \<noteq> {}"
  2.2222 -  shows "convex hull (insert a S) =
  2.2223 -         {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)}"
  2.2224 -  (is "_ = ?hull")
  2.2225 -proof (intro equalityI hull_minimal subsetI)
  2.2226 -  fix x
  2.2227 -  assume "x \<in> insert a S"
  2.2228 -  then have "\<exists>u\<ge>0. \<exists>v\<ge>0. u + v = 1 \<and> (\<exists>b. b \<in> convex hull S \<and> x = u *\<^sub>R a + v *\<^sub>R b)"
  2.2229 -  unfolding insert_iff
  2.2230 -  proof
  2.2231 -    assume "x = a"
  2.2232 -    then show ?thesis
  2.2233 -      by (rule_tac x=1 in exI) (use assms hull_subset in fastforce)
  2.2234 -  next
  2.2235 -    assume "x \<in> S"
  2.2236 -    with hull_subset[of S convex] show ?thesis
  2.2237 -      by force
  2.2238 -  qed
  2.2239 -  then show "x \<in> ?hull"
  2.2240 -    by simp
  2.2241 -next
  2.2242 -  fix x
  2.2243 -  assume "x \<in> ?hull"
  2.2244 -  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"
  2.2245 -    by auto
  2.2246 -  have "a \<in> convex hull insert a S" "b \<in> convex hull insert a S"
  2.2247 -    using hull_mono[of S "insert a S" convex] hull_mono[of "{a}" "insert a S" convex] and obt(4)
  2.2248 -    by auto
  2.2249 -  then show "x \<in> convex hull insert a S"
  2.2250 -    unfolding obt(5) using obt(1-3)
  2.2251 -    by (rule convexD [OF convex_convex_hull])
  2.2252 -next
  2.2253 -  show "convex ?hull"
  2.2254 -  proof (rule convexI)
  2.2255 -    fix x y u v
  2.2256 -    assume as: "(0::real) \<le> u" "0 \<le> v" "u + v = 1" and x: "x \<in> ?hull" and y: "y \<in> ?hull"
  2.2257 -    from x obtain u1 v1 b1 where
  2.2258 -      obt1: "u1\<ge>0" "v1\<ge>0" "u1 + v1 = 1" "b1 \<in> convex hull S" and xeq: "x = u1 *\<^sub>R a + v1 *\<^sub>R b1"
  2.2259 -      by auto
  2.2260 -    from y obtain u2 v2 b2 where
  2.2261 -      obt2: "u2\<ge>0" "v2\<ge>0" "u2 + v2 = 1" "b2 \<in> convex hull S" and yeq: "y = u2 *\<^sub>R a + v2 *\<^sub>R b2"
  2.2262 -      by auto
  2.2263 -    have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  2.2264 -      by (auto simp: algebra_simps)
  2.2265 -    have "\<exists>b \<in> convex hull S. u *\<^sub>R x + v *\<^sub>R y =
  2.2266 -      (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)"
  2.2267 -    proof (cases "u * v1 + v * v2 = 0")
  2.2268 -      case True
  2.2269 -      have *: "\<And>(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x"
  2.2270 -        by (auto simp: algebra_simps)
  2.2271 -      have eq0: "u * v1 = 0" "v * v2 = 0"
  2.2272 -        using True mult_nonneg_nonneg[OF \<open>u\<ge>0\<close> \<open>v1\<ge>0\<close>] mult_nonneg_nonneg[OF \<open>v\<ge>0\<close> \<open>v2\<ge>0\<close>]
  2.2273 -        by arith+
  2.2274 -      then have "u * u1 + v * u2 = 1"
  2.2275 -        using as(3) obt1(3) obt2(3) by auto
  2.2276 -      then show ?thesis
  2.2277 -        using "*" eq0 as obt1(4) xeq yeq by auto
  2.2278 -    next
  2.2279 -      case False
  2.2280 -      have "1 - (u * u1 + v * u2) = (u + v) - (u * u1 + v * u2)"
  2.2281 -        using as(3) obt1(3) obt2(3) by (auto simp: field_simps)
  2.2282 -      also have "\<dots> = u * (v1 + u1 - u1) + v * (v2 + u2 - u2)"
  2.2283 -        using as(3) obt1(3) obt2(3) by (auto simp: field_simps)
  2.2284 -      also have "\<dots> = u * v1 + v * v2"
  2.2285 -        by simp
  2.2286 -      finally have **:"1 - (u * u1 + v * u2) = u * v1 + v * v2" by auto
  2.2287 -      let ?b = "((u * v1) / (u * v1 + v * v2)) *\<^sub>R b1 + ((v * v2) / (u * v1 + v * v2)) *\<^sub>R b2"
  2.2288 -      have zeroes: "0 \<le> u * v1 + v * v2" "0 \<le> u * v1" "0 \<le> u * v1 + v * v2" "0 \<le> v * v2"
  2.2289 -        using as(1,2) obt1(1,2) obt2(1,2) by auto
  2.2290 -      show ?thesis
  2.2291 -      proof
  2.2292 -        show "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (?b - (u * u1) *\<^sub>R ?b - (v * u2) *\<^sub>R ?b)"
  2.2293 -          unfolding xeq yeq * **
  2.2294 -          using False by (auto simp: scaleR_left_distrib scaleR_right_distrib)
  2.2295 -        show "?b \<in> convex hull S"
  2.2296 -          using False zeroes obt1(4) obt2(4)
  2.2297 -          by (auto simp: convexD [OF convex_convex_hull] scaleR_left_distrib scaleR_right_distrib  add_divide_distrib[symmetric]  zero_le_divide_iff)
  2.2298 -      qed
  2.2299 -    qed
  2.2300 -    then obtain b where b: "b \<in> convex hull S" 
  2.2301 -       "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)" ..
  2.2302 -
  2.2303 -    have u1: "u1 \<le> 1"
  2.2304 -      unfolding obt1(3)[symmetric] and not_le using obt1(2) by auto
  2.2305 -    have u2: "u2 \<le> 1"
  2.2306 -      unfolding obt2(3)[symmetric] and not_le using obt2(2) by auto
  2.2307 -    have "u1 * u + u2 * v \<le> max u1 u2 * u + max u1 u2 * v"
  2.2308 -    proof (rule add_mono)
  2.2309 -      show "u1 * u \<le> max u1 u2 * u" "u2 * v \<le> max u1 u2 * v"
  2.2310 -        by (simp_all add: as mult_right_mono)
  2.2311 -    qed
  2.2312 -    also have "\<dots> \<le> 1"
  2.2313 -      unfolding distrib_left[symmetric] and as(3) using u1 u2 by auto
  2.2314 -    finally have le1: "u1 * u + u2 * v \<le> 1" .    
  2.2315 -    show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  2.2316 -    proof (intro CollectI exI conjI)
  2.2317 -      show "0 \<le> u * u1 + v * u2"
  2.2318 -        by (simp add: as(1) as(2) obt1(1) obt2(1))
  2.2319 -      show "0 \<le> 1 - u * u1 - v * u2"
  2.2320 -        by (simp add: le1 diff_diff_add mult.commute)
  2.2321 -    qed (use b in \<open>auto simp: algebra_simps\<close>)
  2.2322 -  qed
  2.2323 -qed
  2.2324 -
  2.2325 -lemma convex_hull_insert_alt:
  2.2326 -   "convex hull (insert a S) =
  2.2327 -     (if S = {} then {a}
  2.2328 -      else {(1 - u) *\<^sub>R a + u *\<^sub>R x |x u. 0 \<le> u \<and> u \<le> 1 \<and> x \<in> convex hull S})"
  2.2329 -  apply (auto simp: convex_hull_insert)
  2.2330 -  using diff_eq_eq apply fastforce
  2.2331 -  by (metis add.group_left_neutral add_le_imp_le_diff diff_add_cancel)
  2.2332 -
  2.2333 -subsubsection%unimportant \<open>Explicit expression for convex hull\<close>
  2.2334 -
  2.2335 -proposition convex_hull_indexed:
  2.2336 -  fixes S :: "'a::real_vector set"
  2.2337 -  shows "convex hull S =
  2.2338 -    {y. \<exists>k u x. (\<forall>i\<in>{1::nat .. k}. 0 \<le> u i \<and> x i \<in> S) \<and>
  2.2339 -                (sum u {1..k} = 1) \<and> (\<Sum>i = 1..k. u i *\<^sub>R x i) = y}"
  2.2340 -    (is "?xyz = ?hull")
  2.2341 -proof (rule hull_unique [OF _ convexI])
  2.2342 -  show "S \<subseteq> ?hull" 
  2.2343 -    by (clarsimp, rule_tac x=1 in exI, rule_tac x="\<lambda>x. 1" in exI, auto)
  2.2344 -next
  2.2345 -  fix T
  2.2346 -  assume "S \<subseteq> T" "convex T"
  2.2347 -  then show "?hull \<subseteq> T"
  2.2348 -    by (blast intro: convex_sum)
  2.2349 -next
  2.2350 -  fix x y u v
  2.2351 -  assume uv: "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
  2.2352 -  assume xy: "x \<in> ?hull" "y \<in> ?hull"
  2.2353 -  from xy obtain k1 u1 x1 where
  2.2354 -    x [rule_format]: "\<forall>i\<in>{1::nat..k1}. 0\<le>u1 i \<and> x1 i \<in> S" 
  2.2355 -                      "sum u1 {Suc 0..k1} = 1" "(\<Sum>i = Suc 0..k1. u1 i *\<^sub>R x1 i) = x"
  2.2356 -    by auto
  2.2357 -  from xy obtain k2 u2 x2 where
  2.2358 -    y [rule_format]: "\<forall>i\<in>{1::nat..k2}. 0\<le>u2 i \<and> x2 i \<in> S" 
  2.2359 -                     "sum u2 {Suc 0..k2} = 1" "(\<Sum>i = Suc 0..k2. u2 i *\<^sub>R x2 i) = y"
  2.2360 -    by auto
  2.2361 -  have *: "\<And>P (x::'a) y s t i. (if P i then s else t) *\<^sub>R (if P i then x else y) = (if P i then s *\<^sub>R x else t *\<^sub>R y)"
  2.2362 -          "{1..k1 + k2} \<inter> {1..k1} = {1..k1}" "{1..k1 + k2} \<inter> - {1..k1} = (\<lambda>i. i + k1) ` {1..k2}"
  2.2363 -    by auto
  2.2364 -  have inj: "inj_on (\<lambda>i. i + k1) {1..k2}"
  2.2365 -    unfolding inj_on_def by auto
  2.2366 -  let ?uu = "\<lambda>i. if i \<in> {1..k1} then u * u1 i else v * u2 (i - k1)"
  2.2367 -  let ?xx = "\<lambda>i. if i \<in> {1..k1} then x1 i else x2 (i - k1)"
  2.2368 -  show "u *\<^sub>R x + v *\<^sub>R y \<in> ?hull"
  2.2369 -  proof (intro CollectI exI conjI ballI)
  2.2370 -    show "0 \<le> ?uu i" "?xx i \<in> S" if "i \<in> {1..k1+k2}" for i
  2.2371 -      using that by (auto simp add: le_diff_conv uv(1) x(1) uv(2) y(1))
  2.2372 -    show "(\<Sum>i = 1..k1 + k2. ?uu i) = 1"  "(\<Sum>i = 1..k1 + k2. ?uu i *\<^sub>R ?xx i) = u *\<^sub>R x + v *\<^sub>R y"
  2.2373 -      unfolding * sum.If_cases[OF finite_atLeastAtMost[of 1 "k1 + k2"]]
  2.2374 -        sum.reindex[OF inj] Collect_mem_eq o_def
  2.2375 -      unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] sum_distrib_left[symmetric]
  2.2376 -      by (simp_all add: sum_distrib_left[symmetric]  x(2,3) y(2,3) uv(3))
  2.2377 -  qed 
  2.2378 -qed
  2.2379 -
  2.2380 -lemma convex_hull_finite:
  2.2381 -  fixes S :: "'a::real_vector set"
  2.2382 -  assumes "finite S"
  2.2383 -  shows "convex hull S = {y. \<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y}"
  2.2384 -  (is "?HULL = _")
  2.2385 -proof (rule hull_unique [OF _ convexI]; clarify)
  2.2386 -  fix x
  2.2387 -  assume "x \<in> S"
  2.2388 -  then show "\<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> (\<Sum>x\<in>S. u x *\<^sub>R x) = x"
  2.2389 -    by (rule_tac x="\<lambda>y. if x=y then 1 else 0" in exI) (auto simp: sum.delta'[OF assms] sum_delta''[OF assms])
  2.2390 -next
  2.2391 -  fix u v :: real
  2.2392 -  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
  2.2393 -  fix ux assume ux [rule_format]: "\<forall>x\<in>S. 0 \<le> ux x" "sum ux S = (1::real)"
  2.2394 -  fix uy assume uy [rule_format]: "\<forall>x\<in>S. 0 \<le> uy x" "sum uy S = (1::real)"
  2.2395 -  have "0 \<le> u * ux x + v * uy x" if "x\<in>S" for x
  2.2396 -    by (simp add: that uv ux(1) uy(1))
  2.2397 -  moreover
  2.2398 -  have "(\<Sum>x\<in>S. u * ux x + v * uy x) = 1"
  2.2399 -    unfolding sum.distrib and sum_distrib_left[symmetric] ux(2) uy(2)
  2.2400 -    using uv(3) by auto
  2.2401 -  moreover
  2.2402 -  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)"
  2.2403 -    unfolding scaleR_left_distrib sum.distrib scaleR_scaleR[symmetric] scaleR_right.sum [symmetric]
  2.2404 -    by auto
  2.2405 -  ultimately
  2.2406 -  show "\<exists>uc. (\<forall>x\<in>S. 0 \<le> uc x) \<and> sum uc S = 1 \<and>
  2.2407 -             (\<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)"
  2.2408 -    by (rule_tac x="\<lambda>x. u * ux x + v * uy x" in exI, auto)
  2.2409 -qed (use assms in \<open>auto simp: convex_explicit\<close>)
  2.2410 -
  2.2411 -
  2.2412 -subsubsection%unimportant \<open>Another formulation\<close>
  2.2413 -
  2.2414 -text "Formalized by Lars Schewe."
  2.2415 -
  2.2416 -lemma convex_hull_explicit:
  2.2417 -  fixes p :: "'a::real_vector set"
  2.2418 -  shows "convex hull p =
  2.2419 -    {y. \<exists>S u. finite S \<and> S \<subseteq> p \<and> (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
  2.2420 -  (is "?lhs = ?rhs")
  2.2421 -proof -
  2.2422 -  {
  2.2423 -    fix x
  2.2424 -    assume "x\<in>?lhs"
  2.2425 -    then obtain k u y where
  2.2426 -        obt: "\<forall>i\<in>{1::nat..k}. 0 \<le> u i \<and> y i \<in> p" "sum u {1..k} = 1" "(\<Sum>i = 1..k. u i *\<^sub>R y i) = x"
  2.2427 -      unfolding convex_hull_indexed by auto
  2.2428 -
  2.2429 -    have fin: "finite {1..k}" by auto
  2.2430 -    have fin': "\<And>v. finite {i \<in> {1..k}. y i = v}" by auto
  2.2431 -    {
  2.2432 -      fix j
  2.2433 -      assume "j\<in>{1..k}"
  2.2434 -      then have "y j \<in> p" "0 \<le> sum u {i. Suc 0 \<le> i \<and> i \<le> k \<and> y i = y j}"
  2.2435 -        using obt(1)[THEN bspec[where x=j]] and obt(2)
  2.2436 -        apply simp
  2.2437 -        apply (rule sum_nonneg)
  2.2438 -        using obt(1)
  2.2439 -        apply auto
  2.2440 -        done
  2.2441 -    }
  2.2442 -    moreover
  2.2443 -    have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v}) = 1"
  2.2444 -      unfolding sum.image_gen[OF fin, symmetric] using obt(2) by auto
  2.2445 -    moreover have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v} *\<^sub>R v) = x"
  2.2446 -      using sum.image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
  2.2447 -      unfolding scaleR_left.sum using obt(3) by auto
  2.2448 -    ultimately
  2.2449 -    have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
  2.2450 -      apply (rule_tac x="y ` {1..k}" in exI)
  2.2451 -      apply (rule_tac x="\<lambda>v. sum u {i\<in>{1..k}. y i = v}" in exI, auto)
  2.2452 -      done
  2.2453 -    then have "x\<in>?rhs" by auto
  2.2454 -  }
  2.2455 -  moreover
  2.2456 -  {
  2.2457 -    fix y
  2.2458 -    assume "y\<in>?rhs"
  2.2459 -    then obtain S u where
  2.2460 -      obt: "finite S" "S \<subseteq> p" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = 1" "(\<Sum>v\<in>S. u v *\<^sub>R v) = y"
  2.2461 -      by auto
  2.2462 -
  2.2463 -    obtain f where f: "inj_on f {1..card S}" "f ` {1..card S} = S"
  2.2464 -      using ex_bij_betw_nat_finite_1[OF obt(1)] unfolding bij_betw_def by auto
  2.2465 -
  2.2466 -    {
  2.2467 -      fix i :: nat
  2.2468 -      assume "i\<in>{1..card S}"
  2.2469 -      then have "f i \<in> S"
  2.2470 -        using f(2) by blast
  2.2471 -      then have "0 \<le> u (f i)" "f i \<in> p" using obt(2,3) by auto
  2.2472 -    }
  2.2473 -    moreover have *: "finite {1..card S}" by auto
  2.2474 -    {
  2.2475 -      fix y
  2.2476 -      assume "y\<in>S"
  2.2477 -      then obtain i where "i\<in>{1..card S}" "f i = y"
  2.2478 -        using f using image_iff[of y f "{1..card S}"]
  2.2479 -        by auto
  2.2480 -      then have "{x. Suc 0 \<le> x \<and> x \<le> card S \<and> f x = y} = {i}"
  2.2481 -        apply auto
  2.2482 -        using f(1)[unfolded inj_on_def]
  2.2483 -        by (metis One_nat_def atLeastAtMost_iff)
  2.2484 -      then have "card {x. Suc 0 \<le> x \<and> x \<le> card S \<and> f x = y} = 1" by auto
  2.2485 -      then have "(\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x)) = u y"
  2.2486 -          "(\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x) *\<^sub>R f x) = u y *\<^sub>R y"
  2.2487 -        by (auto simp: sum_constant_scaleR)
  2.2488 -    }
  2.2489 -    then have "(\<Sum>x = 1..card S. u (f x)) = 1" "(\<Sum>i = 1..card S. u (f i) *\<^sub>R f i) = y"
  2.2490 -      unfolding sum.image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
  2.2491 -        and sum.image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
  2.2492 -      unfolding f
  2.2493 -      using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x) *\<^sub>R f x)" "\<lambda>v. u v *\<^sub>R v"]
  2.2494 -      using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x))" u]
  2.2495 -      unfolding obt(4,5)
  2.2496 -      by auto
  2.2497 -    ultimately
  2.2498 -    have "\<exists>k u x. (\<forall>i\<in>{1..k}. 0 \<le> u i \<and> x i \<in> p) \<and> sum u {1..k} = 1 \<and>
  2.2499 -        (\<Sum>i::nat = 1..k. u i *\<^sub>R x i) = y"
  2.2500 -      apply (rule_tac x="card S" in exI)
  2.2501 -      apply (rule_tac x="u \<circ> f" in exI)
  2.2502 -      apply (rule_tac x=f in exI, fastforce)
  2.2503 -      done
  2.2504 -    then have "y \<in> ?lhs"
  2.2505 -      unfolding convex_hull_indexed by auto
  2.2506 -  }
  2.2507 -  ultimately show ?thesis
  2.2508 -    unfolding set_eq_iff by blast
  2.2509 -qed
  2.2510 -
  2.2511 -
  2.2512 -subsubsection%unimportant \<open>A stepping theorem for that expansion\<close>
  2.2513 -
  2.2514 -lemma convex_hull_finite_step:
  2.2515 -  fixes S :: "'a::real_vector set"
  2.2516 -  assumes "finite S"
  2.2517 -  shows
  2.2518 -    "(\<exists>u. (\<forall>x\<in>insert a S. 0 \<le> u x) \<and> sum u (insert a S) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a S) = y)
  2.2519 -      \<longleftrightarrow> (\<exists>v\<ge>0. \<exists>u. (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y - v *\<^sub>R a)"
  2.2520 -  (is "?lhs = ?rhs")
  2.2521 -proof (rule, case_tac[!] "a\<in>S")
  2.2522 -  assume "a \<in> S"
  2.2523 -  then have *: "insert a S = S" by auto
  2.2524 -  assume ?lhs
  2.2525 -  then show ?rhs
  2.2526 -    unfolding *  by (rule_tac x=0 in exI, auto)
  2.2527 -next
  2.2528 -  assume ?lhs
  2.2529 -  then obtain u where
  2.2530 -      u: "\<forall>x\<in>insert a S. 0 \<le> u x" "sum u (insert a S) = w" "(\<Sum>x\<in>insert a S. u x *\<^sub>R x) = y"
  2.2531 -    by auto
  2.2532 -  assume "a \<notin> S"
  2.2533 -  then show ?rhs
  2.2534 -    apply (rule_tac x="u a" in exI)
  2.2535 -    using u(1)[THEN bspec[where x=a]]
  2.2536 -    apply simp
  2.2537 -    apply (rule_tac x=u in exI)
  2.2538 -    using u[unfolded sum_clauses(2)[OF assms]] and \<open>a\<notin>S\<close>
  2.2539 -    apply auto
  2.2540 -    done
  2.2541 -next
  2.2542 -  assume "a \<in> S"
  2.2543 -  then have *: "insert a S = S" by auto
  2.2544 -  have fin: "finite (insert a S)" using assms by auto
  2.2545 -  assume ?rhs
  2.2546 -  then obtain v u where uv: "v\<ge>0" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = w - v" "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  2.2547 -    by auto
  2.2548 -  show ?lhs
  2.2549 -    apply (rule_tac x = "\<lambda>x. (if a = x then v else 0) + u x" in exI)
  2.2550 -    unfolding scaleR_left_distrib and sum.distrib and sum_delta''[OF fin] and sum.delta'[OF fin]
  2.2551 -    unfolding sum_clauses(2)[OF assms]
  2.2552 -    using uv and uv(2)[THEN bspec[where x=a]] and \<open>a\<in>S\<close>
  2.2553 -    apply auto
  2.2554 -    done
  2.2555 -next
  2.2556 -  assume ?rhs
  2.2557 -  then obtain v u where uv: "v\<ge>0" "\<forall>x\<in>S. 0 \<le> u x" "sum u S = w - v" "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
  2.2558 -    by auto
  2.2559 -  moreover assume "a \<notin> S"
  2.2560 -  moreover
  2.2561 -  have "(\<Sum>x\<in>S. if a = x then v else u x) = sum u S"  "(\<Sum>x\<in>S. (if a = x then v else u x) *\<^sub>R x) = (\<Sum>x\<in>S. u x *\<^sub>R x)"
  2.2562 -    using \<open>a \<notin> S\<close>
  2.2563 -    by (auto simp: intro!: sum.cong)
  2.2564 -  ultimately show ?lhs
  2.2565 -    by (rule_tac x="\<lambda>x. if a = x then v else u x" in exI) (auto simp: sum_clauses(2)[OF assms])
  2.2566 -qed
  2.2567 -
  2.2568 -
  2.2569 -subsubsection%unimportant \<open>Hence some special cases\<close>
  2.2570 -
  2.2571 -lemma convex_hull_2:
  2.2572 -  "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}"
  2.2573 -proof -
  2.2574 -  have *: "\<And>u. (\<forall>x\<in>{a, b}. 0 \<le> u x) \<longleftrightarrow> 0 \<le> u a \<and> 0 \<le> u b"
  2.2575 -    by auto
  2.2576 -  have **: "finite {b}" by auto
  2.2577 -  show ?thesis
  2.2578 -    apply (simp add: convex_hull_finite)
  2.2579 -    unfolding convex_hull_finite_step[OF **, of a 1, unfolded * conj_assoc]
  2.2580 -    apply auto
  2.2581 -    apply (rule_tac x=v in exI)
  2.2582 -    apply (rule_tac x="1 - v" in exI, simp)
  2.2583 -    apply (rule_tac x=u in exI, simp)
  2.2584 -    apply (rule_tac x="\<lambda>x. v" in exI, simp)
  2.2585 -    done
  2.2586 -qed
  2.2587 -
  2.2588 -lemma convex_hull_2_alt: "convex hull {a,b} = {a + u *\<^sub>R (b - a) | u.  0 \<le> u \<and> u \<le> 1}"
  2.2589 -  unfolding convex_hull_2
  2.2590 -proof (rule Collect_cong)
  2.2591 -  have *: "\<And>x y ::real. x + y = 1 \<longleftrightarrow> x = 1 - y"
  2.2592 -    by auto
  2.2593 -  fix x
  2.2594 -  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>
  2.2595 -    (\<exists>u. x = a + u *\<^sub>R (b - a) \<and> 0 \<le> u \<and> u \<le> 1)"
  2.2596 -    unfolding *
  2.2597 -    apply auto
  2.2598 -    apply (rule_tac[!] x=u in exI)
  2.2599 -    apply (auto simp: algebra_simps)
  2.2600 -    done
  2.2601 -qed
  2.2602 -
  2.2603 -lemma convex_hull_3:
  2.2604 -  "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}"
  2.2605 -proof -
  2.2606 -  have fin: "finite {a,b,c}" "finite {b,c}" "finite {c}"
  2.2607 -    by auto
  2.2608 -  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  2.2609 -    by (auto simp: field_simps)
  2.2610 -  show ?thesis
  2.2611 -    unfolding convex_hull_finite[OF fin(1)] and convex_hull_finite_step[OF fin(2)] and *
  2.2612 -    unfolding convex_hull_finite_step[OF fin(3)]
  2.2613 -    apply (rule Collect_cong, simp)
  2.2614 -    apply auto
  2.2615 -    apply (rule_tac x=va in exI)
  2.2616 -    apply (rule_tac x="u c" in exI, simp)
  2.2617 -    apply (rule_tac x="1 - v - w" in exI, simp)
  2.2618 -    apply (rule_tac x=v in exI, simp)
  2.2619 -    apply (rule_tac x="\<lambda>x. w" in exI, simp)
  2.2620 -    done
  2.2621 -qed
  2.2622 -
  2.2623 -lemma convex_hull_3_alt:
  2.2624 -  "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}"
  2.2625 -proof -
  2.2626 -  have *: "\<And>x y z ::real. x + y + z = 1 \<longleftrightarrow> x = 1 - y - z"
  2.2627 -    by auto
  2.2628 -  show ?thesis
  2.2629 -    unfolding convex_hull_3
  2.2630 -    apply (auto simp: *)
  2.2631 -    apply (rule_tac x=v in exI)
  2.2632 -    apply (rule_tac x=w in exI)
  2.2633 -    apply (simp add: algebra_simps)
  2.2634 -    apply (rule_tac x=u in exI)
  2.2635 -    apply (rule_tac x=v in exI)
  2.2636 -    apply (simp add: algebra_simps)
  2.2637 -    done
  2.2638 -qed
  2.2639 -
  2.2640 -
  2.2641 -subsection%unimportant \<open>Relations among closure notions and corresponding hulls\<close>
  2.2642 -
  2.2643 -lemma affine_imp_convex: "affine s \<Longrightarrow> convex s"
  2.2644 -  unfolding affine_def convex_def by auto
  2.2645 -
  2.2646 -lemma convex_affine_hull [simp]: "convex (affine hull S)"
  2.2647 -  by (simp add: affine_imp_convex)
  2.2648 -
  2.2649 -lemma subspace_imp_convex: "subspace s \<Longrightarrow> convex s"
  2.2650 -  using subspace_imp_affine affine_imp_convex by auto
  2.2651 -
  2.2652 -lemma affine_hull_subset_span: "(affine hull s) \<subseteq> (span s)"
  2.2653 -  by (metis hull_minimal span_superset subspace_imp_affine subspace_span)
  2.2654 -
  2.2655 -lemma convex_hull_subset_span: "(convex hull s) \<subseteq> (span s)"
  2.2656 -  by (metis hull_minimal span_superset subspace_imp_convex subspace_span)
  2.2657 -
  2.2658 -lemma convex_hull_subset_affine_hull: "(convex hull s) \<subseteq> (affine hull s)"
  2.2659 -  by (metis affine_affine_hull affine_imp_convex hull_minimal hull_subset)
  2.2660 -
  2.2661 -lemma affine_dependent_imp_dependent: "affine_dependent s \<Longrightarrow> dependent s"
  2.2662 -  unfolding affine_dependent_def dependent_def
  2.2663 -  using affine_hull_subset_span by auto
  2.2664 -
  2.2665 -lemma dependent_imp_affine_dependent:
  2.2666 -  assumes "dependent {x - a| x . x \<in> s}"
  2.2667 -    and "a \<notin> s"
  2.2668 -  shows "affine_dependent (insert a s)"
  2.2669 -proof -
  2.2670 -  from assms(1)[unfolded dependent_explicit] obtain S u v
  2.2671 -    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"
  2.2672 -    by auto
  2.2673 -  define t where "t = (\<lambda>x. x + a) ` S"
  2.2674 -
  2.2675 -  have inj: "inj_on (\<lambda>x. x + a) S"
  2.2676 -    unfolding inj_on_def by auto
  2.2677 -  have "0 \<notin> S"
  2.2678 -    using obt(2) assms(2) unfolding subset_eq by auto
  2.2679 -  have fin: "finite t" and "t \<subseteq> s"
  2.2680 -    unfolding t_def using obt(1,2) by auto
  2.2681 -  then have "finite (insert a t)" and "insert a t \<subseteq> insert a s"
  2.2682 -    by auto
  2.2683 -  moreover have *: "\<And>P Q. (\<Sum>x\<in>t. (if x = a then P x else Q x)) = (\<Sum>x\<in>t. Q x)"
  2.2684 -    apply (rule sum.cong)
  2.2685 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  2.2686 -    apply auto
  2.2687 -    done
  2.2688 -  have "(\<Sum>x\<in>insert a t. if x = a then - (\<Sum>x\<in>t. u (x - a)) else u (x - a)) = 0"
  2.2689 -    unfolding sum_clauses(2)[OF fin] * using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close> by auto
  2.2690 -  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"
  2.2691 -    using obt(3,4) \<open>0\<notin>S\<close>
  2.2692 -    by (rule_tac x="v + a" in bexI) (auto simp: t_def)
  2.2693 -  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)"
  2.2694 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close> by (auto intro!: sum.cong)
  2.2695 -  have "(\<Sum>x\<in>t. u (x - a)) *\<^sub>R a = (\<Sum>v\<in>t. u (v - a) *\<^sub>R v)"
  2.2696 -    unfolding scaleR_left.sum
  2.2697 -    unfolding t_def and sum.reindex[OF inj] and o_def
  2.2698 -    using obt(5)
  2.2699 -    by (auto simp: sum.distrib scaleR_right_distrib)
  2.2700 -  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"
  2.2701 -    unfolding sum_clauses(2)[OF fin]
  2.2702 -    using \<open>a\<notin>s\<close> \<open>t\<subseteq>s\<close>
  2.2703 -    by (auto simp: *)
  2.2704 -  ultimately show ?thesis
  2.2705 -    unfolding affine_dependent_explicit
  2.2706 -    apply (rule_tac x="insert a t" in exI, auto)
  2.2707 -    done
  2.2708 -qed
  2.2709 -
  2.2710 -lemma convex_cone:
  2.2711 -  "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)"
  2.2712 -  (is "?lhs = ?rhs")
  2.2713 -proof -
  2.2714 -  {
  2.2715 -    fix x y
  2.2716 -    assume "x\<in>s" "y\<in>s" and ?lhs
  2.2717 -    then have "2 *\<^sub>R x \<in>s" "2 *\<^sub>R y \<in> s"
  2.2718 -      unfolding cone_def by auto
  2.2719 -    then have "x + y \<in> s"
  2.2720 -      using \<open>?lhs\<close>[unfolded convex_def, THEN conjunct1]
  2.2721 -      apply (erule_tac x="2*\<^sub>R x" in ballE)
  2.2722 -      apply (erule_tac x="2*\<^sub>R y" in ballE)
  2.2723 -      apply (erule_tac x="1/2" in allE, simp)
  2.2724 -      apply (erule_tac x="1/2" in allE, auto)
  2.2725 -      done
  2.2726 -  }
  2.2727 -  then show ?thesis
  2.2728 -    unfolding convex_def cone_def by blast
  2.2729 -qed
  2.2730 -
  2.2731 -lemma affine_dependent_biggerset:
  2.2732 -  fixes s :: "'a::euclidean_space set"
  2.2733 -  assumes "finite s" "card s \<ge> DIM('a) + 2"
  2.2734 -  shows "affine_dependent s"
  2.2735 -proof -
  2.2736 -  have "s \<noteq> {}" using assms by auto
  2.2737 -  then obtain a where "a\<in>s" by auto
  2.2738 -  have *: "{x - a |x. x \<in> s - {a}} = (\<lambda>x. x - a) ` (s - {a})"
  2.2739 -    by auto
  2.2740 -  have "card {x - a |x. x \<in> s - {a}} = card (s - {a})"
  2.2741 -    unfolding * by (simp add: card_image inj_on_def)
  2.2742 -  also have "\<dots> > DIM('a)" using assms(2)
  2.2743 -    unfolding card_Diff_singleton[OF assms(1) \<open>a\<in>s\<close>] by auto
  2.2744 -  finally show ?thesis
  2.2745 -    apply (subst insert_Diff[OF \<open>a\<in>s\<close>, symmetric])
  2.2746 -    apply (rule dependent_imp_affine_dependent)
  2.2747 -    apply (rule dependent_biggerset, auto)
  2.2748 -    done
  2.2749 -qed
  2.2750 -
  2.2751 -lemma affine_dependent_biggerset_general:
  2.2752 -  assumes "finite (S :: 'a::euclidean_space set)"
  2.2753 -    and "card S \<ge> dim S + 2"
  2.2754 -  shows "affine_dependent S"
  2.2755 -proof -
  2.2756 -  from assms(2) have "S \<noteq> {}" by auto
  2.2757 -  then obtain a where "a\<in>S" by auto
  2.2758 -  have *: "{x - a |x. x \<in> S - {a}} = (\<lambda>x. x - a) ` (S - {a})"
  2.2759 -    by auto
  2.2760 -  have **: "card {x - a |x. x \<in> S - {a}} = card (S - {a})"
  2.2761 -    by (metis (no_types, lifting) "*" card_image diff_add_cancel inj_on_def)
  2.2762 -  have "dim {x - a |x. x \<in> S - {a}} \<le> dim S"
  2.2763 -    using \<open>a\<in>S\<close> by (auto simp: span_base span_diff intro: subset_le_dim)
  2.2764 -  also have "\<dots> < dim S + 1" by auto
  2.2765 -  also have "\<dots> \<le> card (S - {a})"
  2.2766 -    using assms
  2.2767 -    using card_Diff_singleton[OF assms(1) \<open>a\<in>S\<close>]
  2.2768 -    by auto
  2.2769 -  finally show ?thesis
  2.2770 -    apply (subst insert_Diff[OF \<open>a\<in>S\<close>, symmetric])
  2.2771 -    apply (rule dependent_imp_affine_dependent)
  2.2772 -    apply (rule dependent_biggerset_general)
  2.2773 -    unfolding **
  2.2774 -    apply auto
  2.2775 -    done
  2.2776 -qed
  2.2777 -
  2.2778 -
  2.2779 -subsection%unimportant \<open>Some Properties of Affine Dependent Sets\<close>
  2.2780 -
  2.2781 -lemma affine_independent_0 [simp]: "\<not> affine_dependent {}"
  2.2782 -  by (simp add: affine_dependent_def)
  2.2783 -
  2.2784 -lemma affine_independent_1 [simp]: "\<not> affine_dependent {a}"
  2.2785 -  by (simp add: affine_dependent_def)
  2.2786 -
  2.2787 -lemma affine_independent_2 [simp]: "\<not> affine_dependent {a,b}"
  2.2788 -  by (simp add: affine_dependent_def insert_Diff_if hull_same)
  2.2789 -
  2.2790 -lemma affine_hull_translation: "affine hull ((\<lambda>x. a + x) `  S) = (\<lambda>x. a + x) ` (affine hull S)"
  2.2791 -proof -
  2.2792 -  have "affine ((\<lambda>x. a + x) ` (affine hull S))"
  2.2793 -    using affine_translation affine_affine_hull by blast
  2.2794 -  moreover have "(\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  2.2795 -    using hull_subset[of S] by auto
  2.2796 -  ultimately have h1: "affine hull ((\<lambda>x. a + x) `  S) \<subseteq> (\<lambda>x. a + x) ` (affine hull S)"
  2.2797 -    by (metis hull_minimal)
  2.2798 -  have "affine((\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)))"
  2.2799 -    using affine_translation affine_affine_hull by blast
  2.2800 -  moreover have "(\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S \<subseteq> (\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S))"
  2.2801 -    using hull_subset[of "(\<lambda>x. a + x) `  S"] by auto
  2.2802 -  moreover have "S = (\<lambda>x. -a + x) ` (\<lambda>x. a + x) `  S"
  2.2803 -    using translation_assoc[of "-a" a] by auto
  2.2804 -  ultimately have "(\<lambda>x. -a + x) ` (affine hull ((\<lambda>x. a + x) `  S)) >= (affine hull S)"
  2.2805 -    by (metis hull_minimal)
  2.2806 -  then have "affine hull ((\<lambda>x. a + x) ` S) >= (\<lambda>x. a + x) ` (affine hull S)"
  2.2807 -    by auto
  2.2808 -  then show ?thesis using h1 by auto
  2.2809 -qed
  2.2810 -
  2.2811 -lemma affine_dependent_translation:
  2.2812 -  assumes "affine_dependent S"
  2.2813 -  shows "affine_dependent ((\<lambda>x. a + x) ` S)"
  2.2814 -proof -
  2.2815 -  obtain x where x: "x \<in> S \<and> x \<in> affine hull (S - {x})"
  2.2816 -    using assms affine_dependent_def by auto
  2.2817 -  have "(+) a ` (S - {x}) = (+) a ` S - {a + x}"
  2.2818 -    by auto
  2.2819 -  then have "a + x \<in> affine hull ((\<lambda>x. a + x) ` S - {a + x})"
  2.2820 -    using affine_hull_translation[of a "S - {x}"] x by auto
  2.2821 -  moreover have "a + x \<in> (\<lambda>x. a + x) ` S"
  2.2822 -    using x by auto
  2.2823 -  ultimately show ?thesis
  2.2824 -    unfolding affine_dependent_def by auto
  2.2825 -qed
  2.2826 -
  2.2827 -lemma affine_dependent_translation_eq:
  2.2828 -  "affine_dependent S \<longleftrightarrow> affine_dependent ((\<lambda>x. a + x) ` S)"
  2.2829 -proof -
  2.2830 -  {
  2.2831 -    assume "affine_dependent ((\<lambda>x. a + x) ` S)"
  2.2832 -    then have "affine_dependent S"
  2.2833 -      using affine_dependent_translation[of "((\<lambda>x. a + x) ` S)" "-a"] translation_assoc[of "-a" a]
  2.2834 -      by auto
  2.2835 -  }
  2.2836 -  then show ?thesis
  2.2837 -    using affine_dependent_translation by auto
  2.2838 -qed
  2.2839 -
  2.2840 -lemma affine_hull_0_dependent:
  2.2841 -  assumes "0 \<in> affine hull S"
  2.2842 -  shows "dependent S"
  2.2843 -proof -
  2.2844 -  obtain s u where s_u: "finite s \<and> s \<noteq> {} \<and> s \<subseteq> S \<and> sum u s = 1 \<and> (\<Sum>v\<in>s. u v *\<^sub>R v) = 0"
  2.2845 -    using assms affine_hull_explicit[of S] by auto
  2.2846 -  then have "\<exists>v\<in>s. u v \<noteq> 0"
  2.2847 -    using sum_not_0[of "u" "s"] by auto
  2.2848 -  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)"
  2.2849 -    using s_u by auto
  2.2850 -  then show ?thesis
  2.2851 -    unfolding dependent_explicit[of S] by auto
  2.2852 -qed
  2.2853 -
  2.2854 -lemma affine_dependent_imp_dependent2:
  2.2855 -  assumes "affine_dependent (insert 0 S)"
  2.2856 -  shows "dependent S"
  2.2857 -proof -
  2.2858 -  obtain x where x: "x \<in> insert 0 S \<and> x \<in> affine hull (insert 0 S - {x})"
  2.2859 -    using affine_dependent_def[of "(insert 0 S)"] assms by blast
  2.2860 -  then have "x \<in> span (insert 0 S - {x})"
  2.2861 -    using affine_hull_subset_span by auto
  2.2862 -  moreover have "span (insert 0 S - {x}) = span (S - {x})"
  2.2863 -    using insert_Diff_if[of "0" S "{x}"] span_insert_0[of "S-{x}"] by auto
  2.2864 -  ultimately have "x \<in> span (S - {x})" by auto
  2.2865 -  then have "x \<noteq> 0 \<Longrightarrow> dependent S"
  2.2866 -    using x dependent_def by auto
  2.2867 -  moreover
  2.2868 -  {
  2.2869 -    assume "x = 0"
  2.2870 -    then have "0 \<in> affine hull S"
  2.2871 -      using x hull_mono[of "S - {0}" S] by auto
  2.2872 -    then have "dependent S"
  2.2873 -      using affine_hull_0_dependent by auto
  2.2874 -  }
  2.2875 -  ultimately show ?thesis by auto
  2.2876 -qed
  2.2877 -
  2.2878 -lemma affine_dependent_iff_dependent:
  2.2879 -  assumes "a \<notin> S"
  2.2880 -  shows "affine_dependent (insert a S) \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` S)"
  2.2881 -proof -
  2.2882 -  have "((+) (- a) ` S) = {x - a| x . x \<in> S}" by auto
  2.2883 -  then show ?thesis
  2.2884 -    using affine_dependent_translation_eq[of "(insert a S)" "-a"]
  2.2885 -      affine_dependent_imp_dependent2 assms
  2.2886 -      dependent_imp_affine_dependent[of a S]
  2.2887 -    by (auto simp del: uminus_add_conv_diff)
  2.2888 -qed
  2.2889 -
  2.2890 -lemma affine_dependent_iff_dependent2:
  2.2891 -  assumes "a \<in> S"
  2.2892 -  shows "affine_dependent S \<longleftrightarrow> dependent ((\<lambda>x. -a + x) ` (S-{a}))"
  2.2893 -proof -
  2.2894 -  have "insert a (S - {a}) = S"
  2.2895 -    using assms by auto
  2.2896 -  then show ?thesis
  2.2897 -    using assms affine_dependent_iff_dependent[of a "S-{a}"] by auto
  2.2898 -qed
  2.2899 -
  2.2900 -lemma affine_hull_insert_span_gen:
  2.2901 -  "affine hull (insert a s) = (\<lambda>x. a + x) ` span ((\<lambda>x. - a + x) ` s)"
  2.2902 -proof -
  2.2903 -  have h1: "{x - a |x. x \<in> s} = ((\<lambda>x. -a+x) ` s)"
  2.2904 -    by auto
  2.2905 -  {
  2.2906 -    assume "a \<notin> s"
  2.2907 -    then have ?thesis
  2.2908 -      using affine_hull_insert_span[of a s] h1 by auto
  2.2909 -  }
  2.2910 -  moreover
  2.2911 -  {
  2.2912 -    assume a1: "a \<in> s"
  2.2913 -    have "\<exists>x. x \<in> s \<and> -a+x=0"
  2.2914 -      apply (rule exI[of _ a])
  2.2915 -      using a1
  2.2916 -      apply auto
  2.2917 -      done
  2.2918 -    then have "insert 0 ((\<lambda>x. -a+x) ` (s - {a})) = (\<lambda>x. -a+x) ` s"
  2.2919 -      by auto
  2.2920 -    then have "span ((\<lambda>x. -a+x) ` (s - {a}))=span ((\<lambda>x. -a+x) ` s)"
  2.2921 -      using span_insert_0[of "(+) (- a) ` (s - {a})"] by (auto simp del: uminus_add_conv_diff)
  2.2922 -    moreover have "{x - a |x. x \<in> (s - {a})} = ((\<lambda>x. -a+x) ` (s - {a}))"
  2.2923 -      by auto
  2.2924 -    moreover have "insert a (s - {a}) = insert a s"
  2.2925 -      by auto