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