src/HOL/Library/Convex_Euclidean_Space.thy
changeset 31289 847f00f435d4
parent 31286 424874813840
child 31345 80667d5bee32
child 31360 fef52c5c1462
equal deleted inserted replaced
31288:67dff9c5b2bd 31289:847f00f435d4
   116   shows " dest_vec1 (setsum f S) = setsum (\<lambda>x. dest_vec1 (f x)) S"
   116   shows " dest_vec1 (setsum f S) = setsum (\<lambda>x. dest_vec1 (f x)) S"
   117   using dest_vec1_sum[OF assms] by auto
   117   using dest_vec1_sum[OF assms] by auto
   118 
   118 
   119 lemma dist_triangle_eq:"dist x z = dist x y + dist y z \<longleftrightarrow> norm (x - y) *s (y - z) = norm (y - z) *s (x - y)"
   119 lemma dist_triangle_eq:"dist x z = dist x y + dist y z \<longleftrightarrow> norm (x - y) *s (y - z) = norm (y - z) *s (x - y)"
   120 proof- have *:"x - y + (y - z) = x - z" by auto
   120 proof- have *:"x - y + (y - z) = x - z" by auto
   121   show ?thesis unfolding dist_def norm_triangle_eq[of "x - y" "y - z", unfolded *] 
   121   show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *] 
   122     by(auto simp add:norm_minus_commute) qed
   122     by(auto simp add:norm_minus_commute) qed
   123 
   123 
   124 lemma norm_eqI:"x = y \<Longrightarrow> norm x = norm y" by auto 
   124 lemma norm_eqI:"x = y \<Longrightarrow> norm x = norm y" by auto 
   125 lemma norm_minus_eqI:"(x::real^'n::finite) = - y \<Longrightarrow> norm x = norm y" by auto
   125 lemma norm_minus_eqI:"(x::real^'n::finite) = - y \<Longrightarrow> norm x = norm y" by auto
   126 
   126 
   599 	apply(rule_tac x="e / norm (x1 - x2)" in exI) using as
   599 	apply(rule_tac x="e / norm (x1 - x2)" in exI) using as
   600 	apply auto unfolding zero_less_divide_iff using n by simp  }  note * = this
   600 	apply auto unfolding zero_less_divide_iff using n by simp  }  note * = this
   601 
   601 
   602     have "\<exists>x\<ge>0. x \<le> 1 \<and> (1 - x) *s x1 + x *s x2 \<notin> e1 \<and> (1 - x) *s x1 + x *s x2 \<notin> e2"
   602     have "\<exists>x\<ge>0. x \<le> 1 \<and> (1 - x) *s x1 + x *s x2 \<notin> e1 \<and> (1 - x) *s x1 + x *s x2 \<notin> e2"
   603       apply(rule connected_real_lemma) apply (simp add: `x1\<in>e1` `x2\<in>e2` dist_commute)+
   603       apply(rule connected_real_lemma) apply (simp add: `x1\<in>e1` `x2\<in>e2` dist_commute)+
   604       using * apply(simp add: dist_def)
   604       using * apply(simp add: dist_norm)
   605       using as(1,2)[unfolded open_def] apply simp
   605       using as(1,2)[unfolded open_def] apply simp
   606       using as(1,2)[unfolded open_def] apply simp
   606       using as(1,2)[unfolded open_def] apply simp
   607       using assms[unfolded convex_alt, THEN bspec[where x=x1], THEN bspec[where x=x2]] using x1 x2
   607       using assms[unfolded convex_alt, THEN bspec[where x=x1], THEN bspec[where x=x2]] using x1 x2
   608       using as(3) by auto
   608       using as(3) by auto
   609     then obtain x where "x\<ge>0" "x\<le>1" "(1 - x) *s x1 + x *s x2 \<notin> e1"  "(1 - x) *s x1 + x *s x2 \<notin> e2" by auto
   609     then obtain x where "x\<ge>0" "x\<le>1" "(1 - x) *s x1 + x *s x2 \<notin> e1"  "(1 - x) *s x1 + x *s x2 \<notin> e2" by auto
   673     using real_lbound_gt_zero[of 1 "e / dist x y"] using xy `e>0` and divide_pos_pos[of e "dist x y"] by auto
   673     using real_lbound_gt_zero[of 1 "e / dist x y"] using xy `e>0` and divide_pos_pos[of e "dist x y"] by auto
   674   hence "f ((1-u) *s x + u *s y) \<le> (1-u) * f x + u * f y" using `x\<in>s` `y\<in>s`
   674   hence "f ((1-u) *s x + u *s y) \<le> (1-u) * f x + u * f y" using `x\<in>s` `y\<in>s`
   675     using assms(2)[unfolded convex_on_def, THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x="1-u"]] by auto
   675     using assms(2)[unfolded convex_on_def, THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x="1-u"]] by auto
   676   moreover
   676   moreover
   677   have *:"x - ((1 - u) *s x + u *s y) = u *s (x - y)" by (simp add: vector_ssub_ldistrib vector_sub_rdistrib)
   677   have *:"x - ((1 - u) *s x + u *s y) = u *s (x - y)" by (simp add: vector_ssub_ldistrib vector_sub_rdistrib)
   678   have "(1 - u) *s x + u *s y \<in> ball x e" unfolding mem_ball dist_def unfolding * and norm_mul and abs_of_pos[OF `0<u`] unfolding dist_def[THEN sym]
   678   have "(1 - u) *s x + u *s y \<in> ball x e" unfolding mem_ball dist_norm unfolding * and norm_mul and abs_of_pos[OF `0<u`] unfolding dist_norm[THEN sym]
   679     using u unfolding pos_less_divide_eq[OF xy] by auto
   679     using u unfolding pos_less_divide_eq[OF xy] by auto
   680   hence "f x \<le> f ((1 - u) *s x + u *s y)" using assms(4) by auto
   680   hence "f x \<le> f ((1 - u) *s x + u *s y)" using assms(4) by auto
   681   ultimately show False using mult_strict_left_mono[OF y `u>0`] unfolding left_diff_distrib by auto
   681   ultimately show False using mult_strict_left_mono[OF y `u>0`] unfolding left_diff_distrib by auto
   682 qed
   682 qed
   683 
   683 
   684 lemma convex_distance: "convex_on s (\<lambda>x. dist a x)"
   684 lemma convex_distance: "convex_on s (\<lambda>x. dist a x)"
   685 proof(auto simp add: convex_on_def dist_def)
   685 proof(auto simp add: convex_on_def dist_norm)
   686   fix x y assume "x\<in>s" "y\<in>s"
   686   fix x y assume "x\<in>s" "y\<in>s"
   687   fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   687   fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   688   have "a = u *s a + v *s a" unfolding vector_sadd_rdistrib[THEN sym] and `u+v=1` by simp
   688   have "a = u *s a + v *s a" unfolding vector_sadd_rdistrib[THEN sym] and `u+v=1` by simp
   689   hence *:"a - (u *s x + v *s y) = (u *s (a - x)) + (v *s (a - y))" by auto
   689   hence *:"a - (u *s x + v *s y) = (u *s (a - x)) + (v *s (a - y))" by auto
   690   show "norm (a - (u *s x + v *s y)) \<le> u * norm (a - x) + v * norm (a - y)"
   690   show "norm (a - (u *s x + v *s y)) \<le> u * norm (a - x) + v * norm (a - y)"
   780 
   780 
   781 lemma bounded_convex_hull: assumes "bounded s" shows "bounded(convex hull s)"
   781 lemma bounded_convex_hull: assumes "bounded s" shows "bounded(convex hull s)"
   782 proof- from assms obtain B where B:"\<forall>x\<in>s. norm x \<le> B" unfolding bounded_def by auto
   782 proof- from assms obtain B where B:"\<forall>x\<in>s. norm x \<le> B" unfolding bounded_def by auto
   783   show ?thesis apply(rule bounded_subset[OF bounded_cball, of _ 0 B])
   783   show ?thesis apply(rule bounded_subset[OF bounded_cball, of _ 0 B])
   784     unfolding subset_hull[unfolded mem_def, of convex, OF convex_cball]
   784     unfolding subset_hull[unfolded mem_def, of convex, OF convex_cball]
   785     unfolding subset_eq mem_cball dist_def using B by auto qed
   785     unfolding subset_eq mem_cball dist_norm using B by auto qed
   786 
   786 
   787 lemma finite_imp_bounded_convex_hull:
   787 lemma finite_imp_bounded_convex_hull:
   788   "finite s \<Longrightarrow> bounded(convex hull s)"
   788   "finite s \<Longrightarrow> bounded(convex hull s)"
   789   using bounded_convex_hull finite_imp_bounded by auto
   789   using bounded_convex_hull finite_imp_bounded by auto
   790 
   790 
  1220     apply(rule_tac x="Min i" in exI) unfolding subset_eq apply rule defer apply rule unfolding mem_Collect_eq
  1220     apply(rule_tac x="Min i" in exI) unfolding subset_eq apply rule defer apply rule unfolding mem_Collect_eq
  1221   proof-
  1221   proof-
  1222     show "0 < Min i" unfolding i_def and Min_gr_iff[OF finite_imageI[OF obt(1)] `b \` t\<noteq>{}`]
  1222     show "0 < Min i" unfolding i_def and Min_gr_iff[OF finite_imageI[OF obt(1)] `b \` t\<noteq>{}`]
  1223       using b apply simp apply rule apply(erule_tac x=x in ballE) using `t\<subseteq>s` by auto
  1223       using b apply simp apply rule apply(erule_tac x=x in ballE) using `t\<subseteq>s` by auto
  1224   next  fix y assume "y \<in> cball a (Min i)"
  1224   next  fix y assume "y \<in> cball a (Min i)"
  1225     hence y:"norm (a - y) \<le> Min i" unfolding dist_def[THEN sym] by auto
  1225     hence y:"norm (a - y) \<le> Min i" unfolding dist_norm[THEN sym] by auto
  1226     { fix x assume "x\<in>t"
  1226     { fix x assume "x\<in>t"
  1227       hence "Min i \<le> b x" unfolding i_def apply(rule_tac Min_le) using obt(1) by auto
  1227       hence "Min i \<le> b x" unfolding i_def apply(rule_tac Min_le) using obt(1) by auto
  1228       hence "x + (y - a) \<in> cball x (b x)" using y unfolding mem_cball dist_def by auto
  1228       hence "x + (y - a) \<in> cball x (b x)" using y unfolding mem_cball dist_norm by auto
  1229       moreover from `x\<in>t` have "x\<in>s" using obt(2) by auto
  1229       moreover from `x\<in>t` have "x\<in>s" using obt(2) by auto
  1230       ultimately have "x + (y - a) \<in> s" using y and b[THEN bspec[where x=x]] unfolding subset_eq by auto }
  1230       ultimately have "x + (y - a) \<in> s" using y and b[THEN bspec[where x=x]] unfolding subset_eq by auto }
  1231     moreover
  1231     moreover
  1232     have *:"inj_on (\<lambda>v. v + (y - a)) t" unfolding inj_on_def by auto
  1232     have *:"inj_on (\<lambda>v. v + (y - a)) t" unfolding inj_on_def by auto
  1233     have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a))) = 1"
  1233     have "(\<Sum>v\<in>(\<lambda>v. v + (y - a)) ` t. u (v - (y - a))) = 1"
  1353   assumes "d \<noteq> 0"
  1353   assumes "d \<noteq> 0"
  1354   shows "dist a (b + d) > dist a b \<or> dist a (b - d) > dist a b"
  1354   shows "dist a (b + d) > dist a b \<or> dist a (b - d) > dist a b"
  1355 proof(cases "a \<bullet> d - b \<bullet> d > 0")
  1355 proof(cases "a \<bullet> d - b \<bullet> d > 0")
  1356   case True hence "0 < d \<bullet> d + (a \<bullet> d * 2 - b \<bullet> d * 2)" 
  1356   case True hence "0 < d \<bullet> d + (a \<bullet> d * 2 - b \<bullet> d * 2)" 
  1357     apply(rule_tac add_pos_pos) using assms by auto
  1357     apply(rule_tac add_pos_pos) using assms by auto
  1358   thus ?thesis apply(rule_tac disjI2) unfolding dist_def and real_vector_norm_def and real_sqrt_less_iff
  1358   thus ?thesis apply(rule_tac disjI2) unfolding dist_norm and real_vector_norm_def and real_sqrt_less_iff
  1359     by(simp add: dot_rsub dot_radd dot_lsub dot_ladd dot_sym field_simps)
  1359     by(simp add: dot_rsub dot_radd dot_lsub dot_ladd dot_sym field_simps)
  1360 next
  1360 next
  1361   case False hence "0 < d \<bullet> d + (b \<bullet> d * 2 - a \<bullet> d * 2)" 
  1361   case False hence "0 < d \<bullet> d + (b \<bullet> d * 2 - a \<bullet> d * 2)" 
  1362     apply(rule_tac add_pos_nonneg) using assms by auto
  1362     apply(rule_tac add_pos_nonneg) using assms by auto
  1363   thus ?thesis apply(rule_tac disjI1) unfolding dist_def and real_vector_norm_def and real_sqrt_less_iff
  1363   thus ?thesis apply(rule_tac disjI1) unfolding dist_norm and real_vector_norm_def and real_sqrt_less_iff
  1364     by(simp add: dot_rsub dot_radd dot_lsub dot_ladd dot_sym field_simps)
  1364     by(simp add: dot_rsub dot_radd dot_lsub dot_ladd dot_sym field_simps)
  1365 qed
  1365 qed
  1366 
  1366 
  1367 lemma norm_increases_online:
  1367 lemma norm_increases_online:
  1368  "(d::real^'n::finite) \<noteq> 0 \<Longrightarrow> norm(a + d) > norm a \<or> norm(a - d) > norm a"
  1368  "(d::real^'n::finite) \<noteq> 0 \<Longrightarrow> norm(a + d) > norm a \<or> norm(a - d) > norm a"
  1369   using dist_increases_online[of d a 0] unfolding dist_def by auto
  1369   using dist_increases_online[of d a 0] unfolding dist_norm by auto
  1370 
  1370 
  1371 lemma simplex_furthest_lt:
  1371 lemma simplex_furthest_lt:
  1372   fixes s::"(real^'n::finite) set" assumes "finite s"
  1372   fixes s::"(real^'n::finite) set" assumes "finite s"
  1373   shows "\<forall>x \<in> (convex hull s).  x \<notin> s \<longrightarrow> (\<exists>y\<in>(convex hull s). norm(x - a) < norm(y - a))"
  1373   shows "\<forall>x \<in> (convex hull s).  x \<notin> s \<longrightarrow> (\<exists>y\<in>(convex hull s). norm(x - a) < norm(y - a))"
  1374 proof(induct_tac rule: finite_induct[of s])
  1374 proof(induct_tac rule: finite_induct[of s])
  1400 	hence *:"w *s (x - b) \<noteq> 0" using w(1) by auto
  1400 	hence *:"w *s (x - b) \<noteq> 0" using w(1) by auto
  1401 	show ?thesis using dist_increases_online[OF *, of a y]
  1401 	show ?thesis using dist_increases_online[OF *, of a y]
  1402  	proof(erule_tac disjE)
  1402  	proof(erule_tac disjE)
  1403 	  assume "dist a y < dist a (y + w *s (x - b))"
  1403 	  assume "dist a y < dist a (y + w *s (x - b))"
  1404 	  hence "norm (y - a) < norm ((u + w) *s x + (v - w) *s b - a)"
  1404 	  hence "norm (y - a) < norm ((u + w) *s x + (v - w) *s b - a)"
  1405 	    unfolding dist_commute[of a] unfolding dist_def obt(5) by (simp add: ring_simps)
  1405 	    unfolding dist_commute[of a] unfolding dist_norm obt(5) by (simp add: ring_simps)
  1406 	  moreover have "(u + w) *s x + (v - w) *s b \<in> convex hull insert x s"
  1406 	  moreover have "(u + w) *s x + (v - w) *s b \<in> convex hull insert x s"
  1407 	    unfolding convex_hull_insert[OF `s\<noteq>{}`] and mem_Collect_eq
  1407 	    unfolding convex_hull_insert[OF `s\<noteq>{}`] and mem_Collect_eq
  1408 	    apply(rule_tac x="u + w" in exI) apply rule defer 
  1408 	    apply(rule_tac x="u + w" in exI) apply rule defer 
  1409 	    apply(rule_tac x="v - w" in exI) using `u\<ge>0` and w and obt(3,4) by auto
  1409 	    apply(rule_tac x="v - w" in exI) using `u\<ge>0` and w and obt(3,4) by auto
  1410 	  ultimately show ?thesis by auto
  1410 	  ultimately show ?thesis by auto
  1411 	next
  1411 	next
  1412 	  assume "dist a y < dist a (y - w *s (x - b))"
  1412 	  assume "dist a y < dist a (y - w *s (x - b))"
  1413 	  hence "norm (y - a) < norm ((u - w) *s x + (v + w) *s b - a)"
  1413 	  hence "norm (y - a) < norm ((u - w) *s x + (v + w) *s b - a)"
  1414 	    unfolding dist_commute[of a] unfolding dist_def obt(5) by (simp add: ring_simps)
  1414 	    unfolding dist_commute[of a] unfolding dist_norm obt(5) by (simp add: ring_simps)
  1415 	  moreover have "(u - w) *s x + (v + w) *s b \<in> convex hull insert x s"
  1415 	  moreover have "(u - w) *s x + (v + w) *s b \<in> convex hull insert x s"
  1416 	    unfolding convex_hull_insert[OF `s\<noteq>{}`] and mem_Collect_eq
  1416 	    unfolding convex_hull_insert[OF `s\<noteq>{}`] and mem_Collect_eq
  1417 	    apply(rule_tac x="u - w" in exI) apply rule defer 
  1417 	    apply(rule_tac x="u - w" in exI) apply rule defer 
  1418 	    apply(rule_tac x="v + w" in exI) using `u\<ge>0` and w and obt(3,4) by auto
  1418 	    apply(rule_tac x="v + w" in exI) using `u\<ge>0` and w and obt(3,4) by auto
  1419 	  ultimately show ?thesis by auto
  1419 	  ultimately show ?thesis by auto
  1428   shows "\<exists>y\<in>s. \<forall>x\<in>(convex hull s). norm(x - a) \<le> norm(y - a)"
  1428   shows "\<exists>y\<in>s. \<forall>x\<in>(convex hull s). norm(x - a) \<le> norm(y - a)"
  1429 proof-
  1429 proof-
  1430   have "convex hull s \<noteq> {}" using hull_subset[of s convex] and assms(2) by auto
  1430   have "convex hull s \<noteq> {}" using hull_subset[of s convex] and assms(2) by auto
  1431   then obtain x where x:"x\<in>convex hull s" "\<forall>y\<in>convex hull s. norm (y - a) \<le> norm (x - a)"
  1431   then obtain x where x:"x\<in>convex hull s" "\<forall>y\<in>convex hull s. norm (y - a) \<le> norm (x - a)"
  1432     using distance_attains_sup[OF finite_imp_compact_convex_hull[OF assms(1)], of a]
  1432     using distance_attains_sup[OF finite_imp_compact_convex_hull[OF assms(1)], of a]
  1433     unfolding dist_commute[of a] unfolding dist_def by auto
  1433     unfolding dist_commute[of a] unfolding dist_norm by auto
  1434   thus ?thesis proof(cases "x\<in>s")
  1434   thus ?thesis proof(cases "x\<in>s")
  1435     case False then obtain y where "y\<in>convex hull s" "norm (x - a) < norm (y - a)"
  1435     case False then obtain y where "y\<in>convex hull s" "norm (x - a) < norm (y - a)"
  1436       using simplex_furthest_lt[OF assms(1), THEN bspec[where x=x]] and x(1) by auto
  1436       using simplex_furthest_lt[OF assms(1), THEN bspec[where x=x]] and x(1) by auto
  1437     thus ?thesis using x(2)[THEN bspec[where x=y]] by auto
  1437     thus ?thesis using x(2)[THEN bspec[where x=y]] by auto
  1438   qed auto
  1438   qed auto
  1467   \<Longrightarrow> (\<exists>u\<in>s. \<exists>v\<in>s. norm(x - y) \<le> norm(u - v))"
  1467   \<Longrightarrow> (\<exists>u\<in>s. \<exists>v\<in>s. norm(x - y) \<le> norm(u - v))"
  1468   using convex_hull_empty simplex_extremal_le[of s] by(cases "s={}")auto
  1468   using convex_hull_empty simplex_extremal_le[of s] by(cases "s={}")auto
  1469 
  1469 
  1470 subsection {* Closest point of a convex set is unique, with a continuous projection. *}
  1470 subsection {* Closest point of a convex set is unique, with a continuous projection. *}
  1471 
  1471 
  1472 definition 
  1472 definition
       
  1473   closest_point :: "(real ^ 'n::finite) set \<Rightarrow> real ^ 'n \<Rightarrow> real ^ 'n" where
  1473  "closest_point s a = (SOME x. x \<in> s \<and> (\<forall>y\<in>s. dist a x \<le> dist a y))"
  1474  "closest_point s a = (SOME x. x \<in> s \<and> (\<forall>y\<in>s. dist a x \<le> dist a y))"
  1474 
  1475 
  1475 lemma closest_point_exists:
  1476 lemma closest_point_exists:
  1476   assumes "closed s" "s \<noteq> {}"
  1477   assumes "closed s" "s \<noteq> {}"
  1477   shows  "closest_point s a \<in> s" "\<forall>y\<in>s. dist a (closest_point s a) \<le> dist a y"
  1478   shows  "closest_point s a \<in> s" "\<forall>y\<in>s. dist a (closest_point s a) \<le> dist a y"
  1510   assumes "(y - x) \<bullet> (z - x) > 0"
  1511   assumes "(y - x) \<bullet> (z - x) > 0"
  1511   shows "\<exists>u>0. u \<le> 1 \<and> dist (x + u *s (z - x)) y < dist x y"
  1512   shows "\<exists>u>0. u \<le> 1 \<and> dist (x + u *s (z - x)) y < dist x y"
  1512 proof- obtain u where "u>0" and u:"\<forall>v>0. v \<le> u \<longrightarrow> norm (v *s (z - x) - (y - x)) < norm (y - x)"
  1513 proof- obtain u where "u>0" and u:"\<forall>v>0. v \<le> u \<longrightarrow> norm (v *s (z - x) - (y - x)) < norm (y - x)"
  1513     using closer_points_lemma[OF assms] by auto
  1514     using closer_points_lemma[OF assms] by auto
  1514   show ?thesis apply(rule_tac x="min u 1" in exI) using u[THEN spec[where x="min u 1"]] and `u>0`
  1515   show ?thesis apply(rule_tac x="min u 1" in exI) using u[THEN spec[where x="min u 1"]] and `u>0`
  1515     unfolding dist_def by(auto simp add: norm_minus_commute field_simps) qed
  1516     unfolding dist_norm by(auto simp add: norm_minus_commute field_simps) qed
  1516 
  1517 
  1517 lemma any_closest_point_dot:
  1518 lemma any_closest_point_dot:
  1518   assumes "convex s" "closed s" "x \<in> s" "y \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  1519   assumes "convex s" "closed s" "x \<in> s" "y \<in> s" "\<forall>z\<in>s. dist a x \<le> dist a z"
  1519   shows "(a - x) \<bullet> (y - x) \<le> 0"
  1520   shows "(a - x) \<bullet> (y - x) \<le> 0"
  1520 proof(rule ccontr) assume "\<not> (a - x) \<bullet> (y - x) \<le> 0"
  1521 proof(rule ccontr) assume "\<not> (a - x) \<bullet> (y - x) \<le> 0"
  1553 proof-
  1554 proof-
  1554   have "(x - closest_point s x) \<bullet> (closest_point s y - closest_point s x) \<le> 0"
  1555   have "(x - closest_point s x) \<bullet> (closest_point s y - closest_point s x) \<le> 0"
  1555        "(y - closest_point s y) \<bullet> (closest_point s x - closest_point s y) \<le> 0"
  1556        "(y - closest_point s y) \<bullet> (closest_point s x - closest_point s y) \<le> 0"
  1556     apply(rule_tac[!] any_closest_point_dot[OF assms(1-2)])
  1557     apply(rule_tac[!] any_closest_point_dot[OF assms(1-2)])
  1557     using closest_point_exists[OF assms(2-3)] by auto
  1558     using closest_point_exists[OF assms(2-3)] by auto
  1558   thus ?thesis unfolding dist_def and norm_le
  1559   thus ?thesis unfolding dist_norm and norm_le
  1559     using dot_pos_le[of "(x - closest_point s x) - (y - closest_point s y)"]
  1560     using dot_pos_le[of "(x - closest_point s x) - (y - closest_point s y)"]
  1560     by (auto simp add: dot_sym dot_ladd dot_radd) qed
  1561     by (auto simp add: dot_sym dot_ladd dot_radd) qed
  1561 
  1562 
  1562 lemma continuous_at_closest_point:
  1563 lemma continuous_at_closest_point:
  1563   assumes "convex s" "closed s" "s \<noteq> {}"
  1564   assumes "convex s" "closed s" "s \<noteq> {}"
  1684       using subset_hull[unfolded mem_def, of convex, OF assms(1), THEN sym, of c] by auto
  1685       using subset_hull[unfolded mem_def, of convex, OF assms(1), THEN sym, of c] by auto
  1685     hence "\<exists>x. norm x = 1 \<and> (\<forall>y\<in>c. 0 \<le> y \<bullet> x)" apply(rule_tac x="inverse(norm a) *s a" in exI)
  1686     hence "\<exists>x. norm x = 1 \<and> (\<forall>y\<in>c. 0 \<le> y \<bullet> x)" apply(rule_tac x="inverse(norm a) *s a" in exI)
  1686        using hull_subset[of c convex] unfolding subset_eq and dot_rmult
  1687        using hull_subset[of c convex] unfolding subset_eq and dot_rmult
  1687        apply- apply rule defer apply rule apply(rule mult_nonneg_nonneg)
  1688        apply- apply rule defer apply rule apply(rule mult_nonneg_nonneg)
  1688        by(auto simp add: dot_sym elim!: ballE) 
  1689        by(auto simp add: dot_sym elim!: ballE) 
  1689     thus "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}" unfolding c(1) frontier_cball dist_def by auto
  1690     thus "frontier (cball 0 1) \<inter> \<Inter>f \<noteq> {}" unfolding c(1) frontier_cball dist_norm by auto
  1690   qed(insert closed_halfspace_ge, auto)
  1691   qed(insert closed_halfspace_ge, auto)
  1691   then obtain x where "norm x = 1" "\<forall>y\<in>s. x\<in>?k y" unfolding frontier_cball dist_def by auto
  1692   then obtain x where "norm x = 1" "\<forall>y\<in>s. x\<in>?k y" unfolding frontier_cball dist_norm by auto
  1692   thus ?thesis apply(rule_tac x=x in exI) by(auto simp add: dot_sym) qed
  1693   thus ?thesis apply(rule_tac x=x in exI) by(auto simp add: dot_sym) qed
  1693 
  1694 
  1694 lemma separating_hyperplane_sets:
  1695 lemma separating_hyperplane_sets:
  1695   assumes "convex s" "convex (t::(real^'n::finite) set)" "s \<noteq> {}" "t \<noteq> {}" "s \<inter> t = {}"
  1696   assumes "convex s" "convex (t::(real^'n::finite) set)" "s \<noteq> {}" "t \<noteq> {}" "s \<inter> t = {}"
  1696   shows "\<exists>a b. a \<noteq> 0 \<and> (\<forall>x\<in>s. a \<bullet> x \<le> b) \<and> (\<forall>x\<in>t. a \<bullet> x \<ge> b)"
  1697   shows "\<exists>a b. a \<noteq> 0 \<and> (\<forall>x\<in>s. a \<bullet> x \<le> b) \<and> (\<forall>x\<in>t. a \<bullet> x \<ge> b)"
  1717   show "\<exists>e>0. ball ((1 - u) *s x + u *s y) e \<subseteq> s" apply(rule_tac x="min d e" in exI)
  1718   show "\<exists>e>0. ball ((1 - u) *s x + u *s y) e \<subseteq> s" apply(rule_tac x="min d e" in exI)
  1718     apply rule unfolding subset_eq defer apply rule proof-
  1719     apply rule unfolding subset_eq defer apply rule proof-
  1719     fix z assume "z \<in> ball ((1 - u) *s x + u *s y) (min d e)"
  1720     fix z assume "z \<in> ball ((1 - u) *s x + u *s y) (min d e)"
  1720     hence "(1- u) *s (z - u *s (y - x)) + u *s (z + (1 - u) *s (y - x)) \<in> s"
  1721     hence "(1- u) *s (z - u *s (y - x)) + u *s (z + (1 - u) *s (y - x)) \<in> s"
  1721       apply(rule_tac assms[unfolded convex_alt, rule_format])
  1722       apply(rule_tac assms[unfolded convex_alt, rule_format])
  1722       using ed(1,2) and u unfolding subset_eq mem_ball Ball_def dist_def by(auto simp add: ring_simps)
  1723       using ed(1,2) and u unfolding subset_eq mem_ball Ball_def dist_norm by(auto simp add: ring_simps)
  1723     thus "z \<in> s" using u by (auto simp add: ring_simps) qed(insert u ed(3-4), auto) qed
  1724     thus "z \<in> s" using u by (auto simp add: ring_simps) qed(insert u ed(3-4), auto) qed
  1724 
  1725 
  1725 lemma convex_hull_eq_empty: "convex hull s = {} \<longleftrightarrow> s = {}"
  1726 lemma convex_hull_eq_empty: "convex hull s = {} \<longleftrightarrow> s = {}"
  1726   using hull_subset[of s convex] convex_hull_empty by auto
  1727   using hull_subset[of s convex] convex_hull_empty by auto
  1727 
  1728 
  1932   have "u *s x \<in> frontier s" unfolding frontier_straddle apply(rule,rule,rule) apply(rule_tac x="u *s x" in bexI) unfolding obt(3)[THEN sym]
  1933   have "u *s x \<in> frontier s" unfolding frontier_straddle apply(rule,rule,rule) apply(rule_tac x="u *s x" in bexI) unfolding obt(3)[THEN sym]
  1933     prefer 3 apply(rule_tac x="(u + (e / 2) / norm x) *s x" in exI) apply(rule, rule) proof-
  1934     prefer 3 apply(rule_tac x="(u + (e / 2) / norm x) *s x" in exI) apply(rule, rule) proof-
  1934     fix e  assume "0 < e" and as:"(u + e / 2 / norm x) *s x \<in> s"
  1935     fix e  assume "0 < e" and as:"(u + e / 2 / norm x) *s x \<in> s"
  1935     hence "u + e / 2 / norm x > u" using`norm x > 0` by(auto simp del:zero_less_norm_iff intro!: divide_pos_pos)
  1936     hence "u + e / 2 / norm x > u" using`norm x > 0` by(auto simp del:zero_less_norm_iff intro!: divide_pos_pos)
  1936     thus False using u_max[OF _ as] by auto
  1937     thus False using u_max[OF _ as] by auto
  1937   qed(insert `y\<in>s`, auto simp add: dist_def obt(3))
  1938   qed(insert `y\<in>s`, auto simp add: dist_norm obt(3))
  1938   thus ?thesis apply(rule_tac that[of u]) apply(rule obt(1), assumption)
  1939   thus ?thesis apply(rule_tac that[of u]) apply(rule obt(1), assumption)
  1939     apply(rule,rule,rule ccontr) apply(rule u_max) by auto qed
  1940     apply(rule,rule,rule ccontr) apply(rule u_max) by auto qed
  1940 
  1941 
  1941 lemma starlike_compact_projective:
  1942 lemma starlike_compact_projective:
  1942   assumes "compact s" "cball (0::real^'n::finite) 1 \<subseteq> s "
  1943   assumes "compact s" "cball (0::real^'n::finite) 1 \<subseteq> s "
  2002   have cont_surfpi:"continuous_on (UNIV -  {0}) (surf \<circ> pi)" apply(rule continuous_on_compose, rule contpi)
  2003   have cont_surfpi:"continuous_on (UNIV -  {0}) (surf \<circ> pi)" apply(rule continuous_on_compose, rule contpi)
  2003     apply(rule continuous_on_subset[of sphere], rule surf(6)) using pi(1) by auto
  2004     apply(rule continuous_on_subset[of sphere], rule surf(6)) using pi(1) by auto
  2004 
  2005 
  2005   { fix x assume as:"x \<in> cball (0::real^'n) 1"
  2006   { fix x assume as:"x \<in> cball (0::real^'n) 1"
  2006     have "norm x *s surf (pi x) \<in> s" proof(cases "x=0 \<or> norm x = 1") 
  2007     have "norm x *s surf (pi x) \<in> s" proof(cases "x=0 \<or> norm x = 1") 
  2007       case False hence "pi x \<in> sphere" "norm x < 1" using pi(1)[of x] as by(auto simp add: dist_def)
  2008       case False hence "pi x \<in> sphere" "norm x < 1" using pi(1)[of x] as by(auto simp add: dist_norm)
  2008       thus ?thesis apply(rule_tac assms(3)[rule_format, THEN DiffD1])
  2009       thus ?thesis apply(rule_tac assms(3)[rule_format, THEN DiffD1])
  2009 	apply(rule_tac fs[unfolded subset_eq, rule_format])
  2010 	apply(rule_tac fs[unfolded subset_eq, rule_format])
  2010 	unfolding surf(5)[THEN sym] by auto
  2011 	unfolding surf(5)[THEN sym] by auto
  2011     next case True thus ?thesis apply rule defer unfolding pi_def apply(rule fs[unfolded subset_eq, rule_format])
  2012     next case True thus ?thesis apply rule defer unfolding pi_def apply(rule fs[unfolded subset_eq, rule_format])
  2012 	unfolding  surf(5)[unfolded sphere_def, THEN sym] using `0\<in>s` by auto qed } note hom = this
  2013 	unfolding  surf(5)[unfolded sphere_def, THEN sym] using `0\<in>s` by auto qed } note hom = this
  2025       hence "norm x = norm ((?a * norm x) *s surf (pi x))"
  2026       hence "norm x = norm ((?a * norm x) *s surf (pi x))"
  2026 	unfolding norm_mul abs_mult abs_norm_cancel abs_of_pos[OF `?a > 0`] by auto
  2027 	unfolding norm_mul abs_mult abs_norm_cancel abs_of_pos[OF `?a > 0`] by auto
  2027       moreover have "pi x = pi ((inverse (norm (surf (pi x))) * norm x) *s surf (pi x))" 
  2028       moreover have "pi x = pi ((inverse (norm (surf (pi x))) * norm x) *s surf (pi x))" 
  2028 	unfolding pi(2)[OF *] surf(4)[rule_format, OF pix] ..
  2029 	unfolding pi(2)[OF *] surf(4)[rule_format, OF pix] ..
  2029       moreover have "surf (pi x) \<in> frontier s" using surf(5) pix by auto
  2030       moreover have "surf (pi x) \<in> frontier s" using surf(5) pix by auto
  2030       hence "dist 0 (inverse (norm (surf (pi x))) *s x) \<le> 1" unfolding dist_def
  2031       hence "dist 0 (inverse (norm (surf (pi x))) *s x) \<le> 1" unfolding dist_norm
  2031 	using ** and * using front_smul[THEN bspec[where x="surf (pi x)"], THEN spec[where x="norm x * ?a"]]
  2032 	using ** and * using front_smul[THEN bspec[where x="surf (pi x)"], THEN spec[where x="norm x * ?a"]]
  2032 	using False `x\<in>s` by(auto simp add:field_simps)
  2033 	using False `x\<in>s` by(auto simp add:field_simps)
  2033       ultimately show ?thesis unfolding image_iff apply(rule_tac x="inverse (norm (surf(pi x))) *s x" in bexI)
  2034       ultimately show ?thesis unfolding image_iff apply(rule_tac x="inverse (norm (surf(pi x))) *s x" in bexI)
  2034 	apply(subst injpi[THEN sym]) unfolding norm_mul abs_mult abs_norm_cancel abs_of_pos[OF `?a > 0`]
  2035 	apply(subst injpi[THEN sym]) unfolding norm_mul abs_mult abs_norm_cancel abs_of_pos[OF `?a > 0`]
  2035 	unfolding pi(2)[OF `?a > 0`] by auto
  2036 	unfolding pi(2)[OF `?a > 0`] by auto
  2038   show ?thesis apply(subst homeomorphic_sym) apply(rule homeomorphic_compact[where f="\<lambda>x. norm x *s surf (pi x)"])
  2039   show ?thesis apply(subst homeomorphic_sym) apply(rule homeomorphic_compact[where f="\<lambda>x. norm x *s surf (pi x)"])
  2039     apply(rule compact_cball) defer apply(rule set_ext, rule, erule imageE, drule hom)
  2040     apply(rule compact_cball) defer apply(rule set_ext, rule, erule imageE, drule hom)
  2040     prefer 4 apply(rule continuous_at_imp_continuous_on, rule) apply(rule_tac [3] hom2) proof-
  2041     prefer 4 apply(rule continuous_at_imp_continuous_on, rule) apply(rule_tac [3] hom2) proof-
  2041     fix x::"real^'n" assume as:"x \<in> cball 0 1"
  2042     fix x::"real^'n" assume as:"x \<in> cball 0 1"
  2042     thus "continuous (at x) (\<lambda>x. norm x *s surf (pi x))" proof(cases "x=0")
  2043     thus "continuous (at x) (\<lambda>x. norm x *s surf (pi x))" proof(cases "x=0")
  2043       case False thus ?thesis apply(rule_tac continuous_mul, rule_tac continuous_at_vec1_norm[THEN spec])
  2044       case False thus ?thesis apply(rule_tac continuous_mul, rule_tac continuous_at_vec1_norm)
  2044 	using cont_surfpi unfolding continuous_on_eq_continuous_at[OF open_delete[OF open_UNIV]] o_def by auto
  2045 	using cont_surfpi unfolding continuous_on_eq_continuous_at[OF open_delete[OF open_UNIV]] o_def by auto
  2045     next guess a using UNIV_witness[where 'a = 'n] ..
  2046     next guess a using UNIV_witness[where 'a = 'n] ..
  2046       obtain B where B:"\<forall>x\<in>s. norm x \<le> B" using compact_imp_bounded[OF assms(1)] unfolding bounded_def by auto
  2047       obtain B where B:"\<forall>x\<in>s. norm x \<le> B" using compact_imp_bounded[OF assms(1)] unfolding bounded_def by auto
  2047       hence "B > 0" using assms(2) unfolding subset_eq apply(erule_tac x="basis a" in ballE) defer apply(erule_tac x="basis a" in ballE)
  2048       hence "B > 0" using assms(2) unfolding subset_eq apply(erule_tac x="basis a" in ballE) defer apply(erule_tac x="basis a" in ballE)
  2048 	unfolding Ball_def mem_cball dist_def by (auto simp add: norm_basis[unfolded One_nat_def])
  2049 	unfolding Ball_def mem_cball dist_norm by (auto simp add: norm_basis[unfolded One_nat_def])
  2049       case True show ?thesis unfolding True continuous_at Lim_at apply(rule,rule) apply(rule_tac x="e / B" in exI)
  2050       case True show ?thesis unfolding True continuous_at Lim_at apply(rule,rule) apply(rule_tac x="e / B" in exI)
  2050 	apply(rule) apply(rule divide_pos_pos) prefer 3 apply(rule,rule,erule conjE)
  2051 	apply(rule) apply(rule divide_pos_pos) prefer 3 apply(rule,rule,erule conjE)
  2051 	unfolding norm_0 vector_smult_lzero dist_def diff_0_right norm_mul abs_norm_cancel proof-
  2052 	unfolding norm_0 vector_smult_lzero dist_norm diff_0_right norm_mul abs_norm_cancel proof-
  2052 	fix e and x::"real^'n" assume as:"norm x < e / B" "0 < norm x" "0<e"
  2053 	fix e and x::"real^'n" assume as:"norm x < e / B" "0 < norm x" "0<e"
  2053 	hence "surf (pi x) \<in> frontier s" using pi(1)[of x] unfolding surf(5)[THEN sym] by auto
  2054 	hence "surf (pi x) \<in> frontier s" using pi(1)[of x] unfolding surf(5)[THEN sym] by auto
  2054 	hence "norm (surf (pi x)) \<le> B" using B fs by auto
  2055 	hence "norm (surf (pi x)) \<le> B" using B fs by auto
  2055 	hence "norm x * norm (surf (pi x)) \<le> norm x * B" using as(2) by auto
  2056 	hence "norm x * norm (surf (pi x)) \<le> norm x * B" using as(2) by auto
  2056 	also have "\<dots> < e / B * B" apply(rule mult_strict_right_mono) using as(1) `B>0` by auto
  2057 	also have "\<dots> < e / B * B" apply(rule mult_strict_right_mono) using as(1) `B>0` by auto
  2084   hence "u *s x \<in> interior s" unfolding interior_def mem_Collect_eq
  2085   hence "u *s x \<in> interior s" unfolding interior_def mem_Collect_eq
  2085     apply(rule_tac x="ball (u *s x) (1 - u)" in exI) apply(rule, rule open_ball)
  2086     apply(rule_tac x="ball (u *s x) (1 - u)" in exI) apply(rule, rule open_ball)
  2086     unfolding centre_in_ball apply rule defer apply(rule) unfolding mem_ball proof-
  2087     unfolding centre_in_ball apply rule defer apply(rule) unfolding mem_ball proof-
  2087     fix y assume "dist (u *s x) y < 1 - u"
  2088     fix y assume "dist (u *s x) y < 1 - u"
  2088     hence "inverse (1 - u) *s (y - u *s x) \<in> s"
  2089     hence "inverse (1 - u) *s (y - u *s x) \<in> s"
  2089       using assms(3) apply(erule_tac subsetD) unfolding mem_cball dist_commute dist_def
  2090       using assms(3) apply(erule_tac subsetD) unfolding mem_cball dist_commute dist_norm
  2090       unfolding group_add_class.diff_0 group_add_class.diff_0_right norm_minus_cancel norm_mul      
  2091       unfolding group_add_class.diff_0 group_add_class.diff_0_right norm_minus_cancel norm_mul      
  2091       apply (rule mult_left_le_imp_le[of "1 - u"])
  2092       apply (rule mult_left_le_imp_le[of "1 - u"])
  2092       unfolding class_semiring.mul_a using `u<1` by auto
  2093       unfolding class_semiring.mul_a using `u<1` by auto
  2093     thus "y \<in> s" using assms(1)[unfolded convex_def, rule_format, of "inverse(1 - u) *s (y - u *s x)" x "1 - u" u]
  2094     thus "y \<in> s" using assms(1)[unfolded convex_def, rule_format, of "inverse(1 - u) *s (y - u *s x)" x "1 - u" u]
  2094       using as unfolding vector_smult_assoc by auto qed auto
  2095       using as unfolding vector_smult_assoc by auto qed auto
  2100 proof- obtain a where "a\<in>interior s" using assms(3) by auto
  2101 proof- obtain a where "a\<in>interior s" using assms(3) by auto
  2101   then obtain d where "d>0" and d:"cball a d \<subseteq> s" unfolding mem_interior_cball by auto
  2102   then obtain d where "d>0" and d:"cball a d \<subseteq> s" unfolding mem_interior_cball by auto
  2102   let ?d = "inverse d" and ?n = "0::real^'n"
  2103   let ?d = "inverse d" and ?n = "0::real^'n"
  2103   have "cball ?n 1 \<subseteq> (\<lambda>x. inverse d *s (x - a)) ` s"
  2104   have "cball ?n 1 \<subseteq> (\<lambda>x. inverse d *s (x - a)) ` s"
  2104     apply(rule, rule_tac x="d *s x + a" in image_eqI) defer
  2105     apply(rule, rule_tac x="d *s x + a" in image_eqI) defer
  2105     apply(rule d[unfolded subset_eq, rule_format]) using `d>0` unfolding mem_cball dist_def
  2106     apply(rule d[unfolded subset_eq, rule_format]) using `d>0` unfolding mem_cball dist_norm
  2106     by(auto simp add: mult_right_le_one_le)
  2107     by(auto simp add: mult_right_le_one_le)
  2107   hence "(\<lambda>x. inverse d *s (x - a)) ` s homeomorphic cball ?n 1"
  2108   hence "(\<lambda>x. inverse d *s (x - a)) ` s homeomorphic cball ?n 1"
  2108     using homeomorphic_convex_compact_lemma[of "(\<lambda>x. ?d *s -a + ?d *s x) ` s", OF convex_affinity compact_affinity]
  2109     using homeomorphic_convex_compact_lemma[of "(\<lambda>x. ?d *s -a + ?d *s x) ` s", OF convex_affinity compact_affinity]
  2109     using assms(1,2) by(auto simp add: uminus_add_conv_diff)
  2110     using assms(1,2) by(auto simp add: uminus_add_conv_diff)
  2110   thus ?thesis apply(rule_tac homeomorphic_trans[OF _ homeomorphic_balls(2)[of 1 _ ?n]])
  2111   thus ?thesis apply(rule_tac homeomorphic_trans[OF _ homeomorphic_balls(2)[of 1 _ ?n]])
  2366     apply(rule_tac x="min (k / 2) (e / (2 * B) * k)" in exI) apply rule defer proof(rule,rule)
  2367     apply(rule_tac x="min (k / 2) (e / (2 * B) * k)" in exI) apply rule defer proof(rule,rule)
  2367     fix y assume as:"norm (y - x) < min (k / 2) (e / (2 * B) * k)" 
  2368     fix y assume as:"norm (y - x) < min (k / 2) (e / (2 * B) * k)" 
  2368     show "\<bar>f y - f x\<bar> < e" proof(cases "y=x")
  2369     show "\<bar>f y - f x\<bar> < e" proof(cases "y=x")
  2369       case False def t \<equiv> "k / norm (y - x)"
  2370       case False def t \<equiv> "k / norm (y - x)"
  2370       have "2 < t" "0<t" unfolding t_def using as False and `k>0` by(auto simp add:field_simps)
  2371       have "2 < t" "0<t" unfolding t_def using as False and `k>0` by(auto simp add:field_simps)
  2371       have "y\<in>s" apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_def
  2372       have "y\<in>s" apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_norm
  2372 	apply(rule order_trans[of _ "2 * norm (x - y)"]) using as by(auto simp add: field_simps norm_minus_commute) 
  2373 	apply(rule order_trans[of _ "2 * norm (x - y)"]) using as by(auto simp add: field_simps norm_minus_commute) 
  2373       { def w \<equiv> "x + t *s (y - x)"
  2374       { def w \<equiv> "x + t *s (y - x)"
  2374 	have "w\<in>s" unfolding w_def apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_def 
  2375 	have "w\<in>s" unfolding w_def apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_norm 
  2375 	  unfolding t_def using `k>0` by(auto simp add: norm_mul simp del: vector_ssub_ldistrib) 
  2376 	  unfolding t_def using `k>0` by(auto simp add: norm_mul simp del: vector_ssub_ldistrib) 
  2376 	have "(1 / t) *s x + - x + ((t - 1) / t) *s x = (1 / t - 1 + (t - 1) / t) *s x" by auto 
  2377 	have "(1 / t) *s x + - x + ((t - 1) / t) *s x = (1 / t - 1 + (t - 1) / t) *s x" by auto 
  2377 	also have "\<dots> = 0"  using `t>0` by(auto simp add:field_simps simp del:vector_sadd_rdistrib)
  2378 	also have "\<dots> = 0"  using `t>0` by(auto simp add:field_simps simp del:vector_sadd_rdistrib)
  2378 	finally have w:"(1 / t) *s w + ((t - 1) / t) *s x = y" unfolding w_def using False and `t>0` by auto
  2379 	finally have w:"(1 / t) *s w + ((t - 1) / t) *s x = y" unfolding w_def using False and `t>0` by auto
  2379 	have  "2 * B < e * t" unfolding t_def using `0<e` `0<k` `B>0` and as and False by (auto simp add:field_simps) 
  2380 	have  "2 * B < e * t" unfolding t_def using `0<e` `0<k` `B>0` and as and False by (auto simp add:field_simps) 
  2382 	hence th1:"f y - f x < e" apply- apply(rule le_less_trans) defer apply assumption
  2383 	hence th1:"f y - f x < e" apply- apply(rule le_less_trans) defer apply assumption
  2383 	  using assms(2)[unfolded convex_on_def,rule_format,of w x "1/t" "(t - 1)/t", unfolded w]
  2384 	  using assms(2)[unfolded convex_on_def,rule_format,of w x "1/t" "(t - 1)/t", unfolded w]
  2384 	  using `0<t` `2<t` and `x\<in>s` `w\<in>s` by(auto simp add:field_simps) }
  2385 	  using `0<t` `2<t` and `x\<in>s` `w\<in>s` by(auto simp add:field_simps) }
  2385       moreover 
  2386       moreover 
  2386       { def w \<equiv> "x - t *s (y - x)"
  2387       { def w \<equiv> "x - t *s (y - x)"
  2387 	have "w\<in>s" unfolding w_def apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_def 
  2388 	have "w\<in>s" unfolding w_def apply(rule k[unfolded subset_eq,rule_format]) unfolding mem_cball dist_norm 
  2388 	  unfolding t_def using `k>0` by(auto simp add: norm_mul simp del: vector_ssub_ldistrib) 
  2389 	  unfolding t_def using `k>0` by(auto simp add: norm_mul simp del: vector_ssub_ldistrib) 
  2389 	have "(1 / (1 + t)) *s x + (t / (1 + t)) *s x = (1 / (1 + t) + t / (1 + t)) *s x" by auto
  2390 	have "(1 / (1 + t)) *s x + (t / (1 + t)) *s x = (1 / (1 + t) + t / (1 + t)) *s x" by auto
  2390 	also have "\<dots>=x" using `t>0` by (auto simp add:field_simps simp del:vector_sadd_rdistrib)
  2391 	also have "\<dots>=x" using `t>0` by (auto simp add:field_simps simp del:vector_sadd_rdistrib)
  2391 	finally have w:"(1 / (1+t)) *s w + (t / (1 + t)) *s y = x" unfolding w_def using False and `t>0` by auto 
  2392 	finally have w:"(1 / (1+t)) *s w + (t / (1 + t)) *s y = x" unfolding w_def using False and `t>0` by auto 
  2392 	have  "2 * B < e * t" unfolding t_def using `0<e` `0<k` `B>0` and as and False by (auto simp add:field_simps) 
  2393 	have  "2 * B < e * t" unfolding t_def using `0<e` `0<k` `B>0` and as and False by (auto simp add:field_simps) 
  2407   assumes "convex_on (cball x e) f"  "\<forall>y \<in> cball x e. f y \<le> b"
  2408   assumes "convex_on (cball x e) f"  "\<forall>y \<in> cball x e. f y \<le> b"
  2408   shows "\<forall>y \<in> cball x e. abs(f y) \<le> b + 2 * abs(f x)"
  2409   shows "\<forall>y \<in> cball x e. abs(f y) \<le> b + 2 * abs(f x)"
  2409   apply(rule) proof(cases "0 \<le> e") case True
  2410   apply(rule) proof(cases "0 \<le> e") case True
  2410   fix y assume y:"y\<in>cball x e" def z \<equiv> "2 *s x - y"
  2411   fix y assume y:"y\<in>cball x e" def z \<equiv> "2 *s x - y"
  2411   have *:"x - (2 *s x - y) = y - x" by vector
  2412   have *:"x - (2 *s x - y) = y - x" by vector
  2412   have z:"z\<in>cball x e" using y unfolding z_def mem_cball dist_def * by(auto simp add: norm_minus_commute)
  2413   have z:"z\<in>cball x e" using y unfolding z_def mem_cball dist_norm * by(auto simp add: norm_minus_commute)
  2413   have "(1 / 2) *s y + (1 / 2) *s z = x" unfolding z_def by auto
  2414   have "(1 / 2) *s y + (1 / 2) *s z = x" unfolding z_def by auto
  2414   thus "\<bar>f y\<bar> \<le> b + 2 * \<bar>f x\<bar>" using assms(1)[unfolded convex_on_def,rule_format, OF y z, of "1/2" "1/2"]
  2415   thus "\<bar>f y\<bar> \<le> b + 2 * \<bar>f x\<bar>" using assms(1)[unfolded convex_on_def,rule_format, OF y z, of "1/2" "1/2"]
  2415     using assms(2)[rule_format,OF y] assms(2)[rule_format,OF z] by(auto simp add:field_simps)
  2416     using assms(2)[rule_format,OF y] assms(2)[rule_format,OF z] by(auto simp add:field_simps)
  2416 next case False fix y assume "y\<in>cball x e" 
  2417 next case False fix y assume "y\<in>cball x e" 
  2417   hence "dist x y < 0" using False unfolding mem_cball not_le by (auto simp del: dist_not_less_zero)
  2418   hence "dist x y < 0" using False unfolding mem_cball not_le by (auto simp del: dist_not_less_zero)
  2436   have "convex_on {x - ?d..x + ?d} f" apply(rule convex_on_subset[OF assms(2)])
  2437   have "convex_on {x - ?d..x + ?d} f" apply(rule convex_on_subset[OF assms(2)])
  2437     apply(rule subset_trans[OF _ e(1)]) unfolding subset_eq mem_cball proof 
  2438     apply(rule subset_trans[OF _ e(1)]) unfolding subset_eq mem_cball proof 
  2438     fix z assume z:"z\<in>{x - ?d..x + ?d}"
  2439     fix z assume z:"z\<in>{x - ?d..x + ?d}"
  2439     have e:"e = setsum (\<lambda>i. d) (UNIV::'n set)" unfolding setsum_constant d_def using dimge1
  2440     have e:"e = setsum (\<lambda>i. d) (UNIV::'n set)" unfolding setsum_constant d_def using dimge1
  2440       by (metis card_enum field_simps d_def not_one_le_zero of_nat_le_iff real_eq_of_nat real_of_nat_1)
  2441       by (metis card_enum field_simps d_def not_one_le_zero of_nat_le_iff real_eq_of_nat real_of_nat_1)
  2441     show "dist x z \<le> e" unfolding dist_def e apply(rule_tac order_trans[OF norm_le_l1], rule setsum_mono)
  2442     show "dist x z \<le> e" unfolding dist_norm e apply(rule_tac order_trans[OF norm_le_l1], rule setsum_mono)
  2442       using z[unfolded mem_interval] apply(erule_tac x=i in allE) by(auto simp add:field_simps vector_component_simps) qed
  2443       using z[unfolded mem_interval] apply(erule_tac x=i in allE) by(auto simp add:field_simps vector_component_simps) qed
  2443   hence k:"\<forall>y\<in>{x - ?d..x + ?d}. f y \<le> k" unfolding c(2) apply(rule_tac convex_on_convex_hull_bound) apply assumption
  2444   hence k:"\<forall>y\<in>{x - ?d..x + ?d}. f y \<le> k" unfolding c(2) apply(rule_tac convex_on_convex_hull_bound) apply assumption
  2444     unfolding k_def apply(rule, rule Max_ge) using c(1) by auto
  2445     unfolding k_def apply(rule, rule Max_ge) using c(1) by auto
  2445   have "d \<le> e" unfolding d_def apply(rule mult_imp_div_pos_le) using `e>0` dimge1 unfolding mult_le_cancel_left1 using real_dimindex_ge_1 by auto
  2446   have "d \<le> e" unfolding d_def apply(rule mult_imp_div_pos_le) using `e>0` dimge1 unfolding mult_le_cancel_left1 using real_dimindex_ge_1 by auto
  2446   hence dsube:"cball x d \<subseteq> cball x e" unfolding subset_eq Ball_def mem_cball by auto
  2447   hence dsube:"cball x d \<subseteq> cball x e" unfolding subset_eq Ball_def mem_cball by auto
  2447   have conv:"convex_on (cball x d) f" apply(rule convex_on_subset, rule convex_on_subset[OF assms(2)]) apply(rule e(1)) using dsube by auto
  2448   have conv:"convex_on (cball x d) f" apply(rule convex_on_subset, rule convex_on_subset[OF assms(2)]) apply(rule e(1)) using dsube by auto
  2448   hence "\<forall>y\<in>cball x d. abs (f y) \<le> k + 2 * abs (f x)" apply(rule_tac convex_bounds_lemma) apply assumption proof
  2449   hence "\<forall>y\<in>cball x d. abs (f y) \<le> k + 2 * abs (f x)" apply(rule_tac convex_bounds_lemma) apply assumption proof
  2449     fix y assume y:"y\<in>cball x d"
  2450     fix y assume y:"y\<in>cball x d"
  2450     { fix i::'n have "x $ i - d \<le> y $ i"  "y $ i \<le> x $ i + d" 
  2451     { fix i::'n have "x $ i - d \<le> y $ i"  "y $ i \<le> x $ i + d" 
  2451 	using order_trans[OF component_le_norm y[unfolded mem_cball dist_def], of i] by(auto simp add: vector_component)  }
  2452 	using order_trans[OF component_le_norm y[unfolded mem_cball dist_norm], of i] by(auto simp add: vector_component)  }
  2452     thus "f y \<le> k" apply(rule_tac k[rule_format]) unfolding mem_cball mem_interval dist_def 
  2453     thus "f y \<le> k" apply(rule_tac k[rule_format]) unfolding mem_cball mem_interval dist_norm 
  2453       by(auto simp add: vector_component_simps) qed
  2454       by(auto simp add: vector_component_simps) qed
  2454   hence "continuous_on (ball x d) (vec1 \<circ> f)" apply(rule_tac convex_on_bounded_continuous)
  2455   hence "continuous_on (ball x d) (vec1 \<circ> f)" apply(rule_tac convex_on_bounded_continuous)
  2455     apply(rule open_ball, rule convex_on_subset[OF conv], rule ball_subset_cball) by auto
  2456     apply(rule open_ball, rule convex_on_subset[OF conv], rule ball_subset_cball) by auto
  2456   thus "continuous (at x) (vec1 \<circ> f)" unfolding continuous_on_eq_continuous_at[OF open_ball] using `d>0` by auto qed
  2457   thus "continuous (at x) (vec1 \<circ> f)" unfolding continuous_on_eq_continuous_at[OF open_ball] using `d>0` by auto qed
  2457 
  2458 
  2482   "dist (midpoint a b) a = (dist a b) / 2" (is ?t3)
  2483   "dist (midpoint a b) a = (dist a b) / 2" (is ?t3)
  2483   "dist (midpoint a b) b = (dist a b) / 2" (is ?t4)
  2484   "dist (midpoint a b) b = (dist a b) / 2" (is ?t4)
  2484 proof-
  2485 proof-
  2485   have *: "\<And>x y::real^'n::finite. 2 *s x = - y \<Longrightarrow> norm x = (norm y) / 2" unfolding equation_minus_iff by auto
  2486   have *: "\<And>x y::real^'n::finite. 2 *s x = - y \<Longrightarrow> norm x = (norm y) / 2" unfolding equation_minus_iff by auto
  2486   have **:"\<And>x y::real^'n::finite. 2 *s x =   y \<Longrightarrow> norm x = (norm y) / 2" by auto
  2487   have **:"\<And>x y::real^'n::finite. 2 *s x =   y \<Longrightarrow> norm x = (norm y) / 2" by auto
  2487   show ?t1 unfolding midpoint_def dist_def apply (rule **) by(auto,vector)
  2488   show ?t1 unfolding midpoint_def dist_norm apply (rule **) by(auto,vector)
  2488   show ?t2 unfolding midpoint_def dist_def apply (rule *)  by(auto,vector)
  2489   show ?t2 unfolding midpoint_def dist_norm apply (rule *)  by(auto,vector)
  2489   show ?t3 unfolding midpoint_def dist_def apply (rule *)  by(auto,vector)
  2490   show ?t3 unfolding midpoint_def dist_norm apply (rule *)  by(auto,vector)
  2490   show ?t4 unfolding midpoint_def dist_def apply (rule **) by(auto,vector) qed
  2491   show ?t4 unfolding midpoint_def dist_norm apply (rule **) by(auto,vector) qed
  2491 
  2492 
  2492 lemma midpoint_eq_endpoint:
  2493 lemma midpoint_eq_endpoint:
  2493   "midpoint a b = a \<longleftrightarrow> a = (b::real^'n::finite)"
  2494   "midpoint a b = a \<longleftrightarrow> a = (b::real^'n::finite)"
  2494   "midpoint a b = b \<longleftrightarrow> a = b"
  2495   "midpoint a b = b \<longleftrightarrow> a = b"
  2495   unfolding dist_eq_0_iff[where 'a="real^'n", THEN sym] dist_midpoint by auto
  2496   unfolding dist_eq_0_iff[where 'a="real^'n", THEN sym] dist_midpoint by auto
  2548 	unfolding as(1) by(auto simp add:field_simps)
  2549 	unfolding as(1) by(auto simp add:field_simps)
  2549       show "norm (a - x) *s (x - b) = norm (x - b) *s (a - x)"
  2550       show "norm (a - x) *s (x - b) = norm (x - b) *s (a - x)"
  2550 	unfolding norm_minus_commute[of x a] * norm_mul Cart_eq using as(2,3)
  2551 	unfolding norm_minus_commute[of x a] * norm_mul Cart_eq using as(2,3)
  2551 	by(auto simp add: vector_component_simps field_simps)
  2552 	by(auto simp add: vector_component_simps field_simps)
  2552     next assume as:"dist a b = dist a x + dist x b"
  2553     next assume as:"dist a b = dist a x + dist x b"
  2553       have "norm (a - x) / norm (a - b) \<le> 1" unfolding divide_le_eq_1_pos[OF Fal2] unfolding as[unfolded dist_def] norm_ge_zero by auto 
  2554       have "norm (a - x) / norm (a - b) \<le> 1" unfolding divide_le_eq_1_pos[OF Fal2] unfolding as[unfolded dist_norm] norm_ge_zero by auto 
  2554       thus "\<exists>u. x = (1 - u) *s a + u *s b \<and> 0 \<le> u \<and> u \<le> 1" apply(rule_tac x="dist a x / dist a b" in exI)
  2555       thus "\<exists>u. x = (1 - u) *s a + u *s b \<and> 0 \<le> u \<and> u \<le> 1" apply(rule_tac x="dist a x / dist a b" in exI)
  2555 	unfolding dist_def Cart_eq apply- apply rule defer apply(rule, rule divide_nonneg_pos) prefer 4 proof rule
  2556 	unfolding dist_norm Cart_eq apply- apply rule defer apply(rule, rule divide_nonneg_pos) prefer 4 proof rule
  2556 	  fix i::'n have "((1 - norm (a - x) / norm (a - b)) *s a + (norm (a - x) / norm (a - b)) *s b) $ i =
  2557 	  fix i::'n have "((1 - norm (a - x) / norm (a - b)) *s a + (norm (a - x) / norm (a - b)) *s b) $ i =
  2557 	    ((norm (a - b) - norm (a - x)) * (a $ i) + norm (a - x) * (b $ i)) / norm (a - b)"
  2558 	    ((norm (a - b) - norm (a - x)) * (a $ i) + norm (a - x) * (b $ i)) / norm (a - b)"
  2558 	    using Fal by(auto simp add:vector_component_simps field_simps)
  2559 	    using Fal by(auto simp add:vector_component_simps field_simps)
  2559 	  also have "\<dots> = x$i" apply(rule divide_eq_imp[OF Fal])
  2560 	  also have "\<dots> = x$i" apply(rule divide_eq_imp[OF Fal])
  2560 	    unfolding as[unfolded dist_def] using as[unfolded dist_triangle_eq Cart_eq,rule_format, of i]
  2561 	    unfolding as[unfolded dist_norm] using as[unfolded dist_triangle_eq Cart_eq,rule_format, of i]
  2561 	    by(auto simp add:field_simps vector_component_simps)
  2562 	    by(auto simp add:field_simps vector_component_simps)
  2562 	  finally show "x $ i = ((1 - norm (a - x) / norm (a - b)) *s a + (norm (a - x) / norm (a - b)) *s b) $ i" by auto
  2563 	  finally show "x $ i = ((1 - norm (a - x) / norm (a - b)) *s a + (norm (a - x) / norm (a - b)) *s b) $ i" by auto
  2563 	qed(insert Fal2, auto) qed qed
  2564 	qed(insert Fal2, auto) qed qed
  2564 
  2565 
  2565 lemma between_midpoint: fixes a::"real^'n::finite" shows
  2566 lemma between_midpoint: fixes a::"real^'n::finite" shows
  2566   "between (a,b) (midpoint a b)" (is ?t1) 
  2567   "between (a,b) (midpoint a b)" (is ?t1) 
  2567   "between (b,a) (midpoint a b)" (is ?t2)
  2568   "between (b,a) (midpoint a b)" (is ?t2)
  2568 proof- have *:"\<And>x y z. x = (1/2::real) *s z \<Longrightarrow> y = (1/2) *s z \<Longrightarrow> norm z = norm x + norm y" by auto
  2569 proof- have *:"\<And>x y z. x = (1/2::real) *s z \<Longrightarrow> y = (1/2) *s z \<Longrightarrow> norm z = norm x + norm y" by auto
  2569   show ?t1 ?t2 unfolding between midpoint_def dist_def apply(rule_tac[!] *)
  2570   show ?t1 ?t2 unfolding between midpoint_def dist_norm apply(rule_tac[!] *)
  2570     by(auto simp add:field_simps Cart_eq vector_component_simps) qed
  2571     by(auto simp add:field_simps Cart_eq vector_component_simps) qed
  2571 
  2572 
  2572 lemma between_mem_convex_hull:
  2573 lemma between_mem_convex_hull:
  2573   "between (a,b) x \<longleftrightarrow> x \<in> convex hull {a,b}"
  2574   "between (a,b) x \<longleftrightarrow> x \<in> convex hull {a,b}"
  2574   unfolding between_mem_segment segment_convex_hull ..
  2575   unfolding between_mem_segment segment_convex_hull ..
  2582   show ?thesis unfolding mem_interior apply(rule_tac x="e*d" in exI)
  2583   show ?thesis unfolding mem_interior apply(rule_tac x="e*d" in exI)
  2583     apply(rule) defer unfolding subset_eq Ball_def mem_ball proof(rule,rule)
  2584     apply(rule) defer unfolding subset_eq Ball_def mem_ball proof(rule,rule)
  2584     fix y assume as:"dist (x - e *s (x - c)) y < e * d"
  2585     fix y assume as:"dist (x - e *s (x - c)) y < e * d"
  2585     have *:"y = (1 - (1 - e)) *s ((1 / e) *s y - ((1 - e) / e) *s x) + (1 - e) *s x" using `e>0` by auto
  2586     have *:"y = (1 - (1 - e)) *s ((1 / e) *s y - ((1 - e) / e) *s x) + (1 - e) *s x" using `e>0` by auto
  2586     have "dist c ((1 / e) *s y - ((1 - e) / e) *s x) = abs(1/e) * norm (e *s c - y + (1 - e) *s x)"
  2587     have "dist c ((1 / e) *s y - ((1 - e) / e) *s x) = abs(1/e) * norm (e *s c - y + (1 - e) *s x)"
  2587       unfolding dist_def unfolding norm_mul[THEN sym] apply(rule norm_eqI) using `e>0`
  2588       unfolding dist_norm unfolding norm_mul[THEN sym] apply(rule norm_eqI) using `e>0`
  2588       by(auto simp add:vector_component_simps Cart_eq field_simps) 
  2589       by(auto simp add:vector_component_simps Cart_eq field_simps) 
  2589     also have "\<dots> = abs(1/e) * norm (x - e *s (x - c) - y)" by(auto intro!:norm_eqI)
  2590     also have "\<dots> = abs(1/e) * norm (x - e *s (x - c) - y)" by(auto intro!:norm_eqI)
  2590     also have "\<dots> < d" using as[unfolded dist_def] and `e>0`
  2591     also have "\<dots> < d" using as[unfolded dist_norm] and `e>0`
  2591       by(auto simp add:pos_divide_less_eq[OF `e>0`] real_mult_commute)
  2592       by(auto simp add:pos_divide_less_eq[OF `e>0`] real_mult_commute)
  2592     finally show "y \<in> s" apply(subst *) apply(rule assms(1)[unfolded convex_alt,rule_format])
  2593     finally show "y \<in> s" apply(subst *) apply(rule assms(1)[unfolded convex_alt,rule_format])
  2593       apply(rule d[unfolded subset_eq,rule_format]) unfolding mem_ball using assms(3-5) by auto
  2594       apply(rule d[unfolded subset_eq,rule_format]) unfolding mem_ball using assms(3-5) by auto
  2594   qed(rule mult_pos_pos, insert `e>0` `d>0`, auto) qed
  2595   qed(rule mult_pos_pos, insert `e>0` `d>0`, auto) qed
  2595 
  2596 
  2606       thus ?thesis apply(rule_tac x=y in bexI) unfolding True using `d>0` by auto next
  2607       thus ?thesis apply(rule_tac x=y in bexI) unfolding True using `d>0` by auto next
  2607       case False hence "0 < e * d / (1 - e)" and *:"1 - e > 0"
  2608       case False hence "0 < e * d / (1 - e)" and *:"1 - e > 0"
  2608 	using `e\<le>1` `e>0` `d>0` by(auto intro!:mult_pos_pos divide_pos_pos)
  2609 	using `e\<le>1` `e>0` `d>0` by(auto intro!:mult_pos_pos divide_pos_pos)
  2609       then obtain y where "y\<in>s" "y \<noteq> x" "dist y x < e * d / (1 - e)"
  2610       then obtain y where "y\<in>s" "y \<noteq> x" "dist y x < e * d / (1 - e)"
  2610 	using x[unfolded islimpt_approachable,THEN spec[where x="e*d / (1 - e)"]] by auto
  2611 	using x[unfolded islimpt_approachable,THEN spec[where x="e*d / (1 - e)"]] by auto
  2611       thus ?thesis apply(rule_tac x=y in bexI) unfolding dist_def using pos_less_divide_eq[OF *] by auto qed qed
  2612       thus ?thesis apply(rule_tac x=y in bexI) unfolding dist_norm using pos_less_divide_eq[OF *] by auto qed qed
  2612   then obtain y where "y\<in>s" and y:"norm (y - x) * (1 - e) < e * d" by auto
  2613   then obtain y where "y\<in>s" and y:"norm (y - x) * (1 - e) < e * d" by auto
  2613   def z \<equiv> "c + ((1 - e) / e) *s (x - y)"
  2614   def z \<equiv> "c + ((1 - e) / e) *s (x - y)"
  2614   have *:"x - e *s (x - c) = y - e *s (y - z)" unfolding z_def using `e>0` by auto
  2615   have *:"x - e *s (x - c) = y - e *s (y - z)" unfolding z_def using `e>0` by auto
  2615   have "z\<in>interior s" apply(rule subset_interior[OF d,unfolded subset_eq,rule_format])
  2616   have "z\<in>interior s" apply(rule subset_interior[OF d,unfolded subset_eq,rule_format])
  2616     unfolding interior_open[OF open_ball] mem_ball z_def dist_def using y and assms(4,5)
  2617     unfolding interior_open[OF open_ball] mem_ball z_def dist_norm using y and assms(4,5)
  2617     by(auto simp del:vector_ssub_ldistrib simp add:field_simps norm_minus_commute) 
  2618     by(auto simp del:vector_ssub_ldistrib simp add:field_simps norm_minus_commute) 
  2618   thus ?thesis unfolding * apply - apply(rule mem_interior_convex_shrink) 
  2619   thus ?thesis unfolding * apply - apply(rule mem_interior_convex_shrink) 
  2619     using assms(1,4-5) `y\<in>s` by auto qed
  2620     using assms(1,4-5) `y\<in>s` by auto qed
  2620 
  2621 
  2621 subsection {* Some obvious but surprisingly hard simplex lemmas. *}
  2622 subsection {* Some obvious but surprisingly hard simplex lemmas. *}
  2654   apply(rule set_ext) unfolding mem_interior std_simplex unfolding subset_eq mem_Collect_eq Ball_def mem_ball
  2655   apply(rule set_ext) unfolding mem_interior std_simplex unfolding subset_eq mem_Collect_eq Ball_def mem_ball
  2655   unfolding Ball_def[symmetric] apply rule apply(erule exE, (erule conjE)+) defer apply(erule conjE) proof-
  2656   unfolding Ball_def[symmetric] apply rule apply(erule exE, (erule conjE)+) defer apply(erule conjE) proof-
  2656   fix x::"real^'n" and e assume "0<e" and as:"\<forall>xa. dist x xa < e \<longrightarrow> (\<forall>x. 0 \<le> xa $ x) \<and> setsum (op $ xa) UNIV \<le> 1"
  2657   fix x::"real^'n" and e assume "0<e" and as:"\<forall>xa. dist x xa < e \<longrightarrow> (\<forall>x. 0 \<le> xa $ x) \<and> setsum (op $ xa) UNIV \<le> 1"
  2657   show "(\<forall>xa. 0 < x $ xa) \<and> setsum (op $ x) UNIV < 1" apply(rule,rule) proof-
  2658   show "(\<forall>xa. 0 < x $ xa) \<and> setsum (op $ x) UNIV < 1" apply(rule,rule) proof-
  2658     fix i::'n show "0 < x $ i" using as[THEN spec[where x="x - (e / 2) *s basis i"]] and `e>0`
  2659     fix i::'n show "0 < x $ i" using as[THEN spec[where x="x - (e / 2) *s basis i"]] and `e>0`
  2659       unfolding dist_def by(auto simp add: norm_basis vector_component_simps basis_component elim:allE[where x=i])
  2660       unfolding dist_norm by(auto simp add: norm_basis vector_component_simps basis_component elim:allE[where x=i])
  2660   next guess a using UNIV_witness[where 'a='n] ..
  2661   next guess a using UNIV_witness[where 'a='n] ..
  2661     have **:"dist x (x + (e / 2) *s basis a) < e" using  `e>0` and norm_basis[of a]
  2662     have **:"dist x (x + (e / 2) *s basis a) < e" using  `e>0` and norm_basis[of a]
  2662       unfolding dist_def by(auto simp add: vector_component_simps basis_component intro!: mult_strict_left_mono_comm)
  2663       unfolding dist_norm by(auto simp add: vector_component_simps basis_component intro!: mult_strict_left_mono_comm)
  2663     have "\<And>i. (x + (e / 2) *s basis a) $ i = x$i + (if i = a then e/2 else 0)" by(auto simp add:vector_component_simps)
  2664     have "\<And>i. (x + (e / 2) *s basis a) $ i = x$i + (if i = a then e/2 else 0)" by(auto simp add:vector_component_simps)
  2664     hence *:"setsum (op $ (x + (e / 2) *s basis a)) UNIV = setsum (\<lambda>i. x$i + (if a = i then e/2 else 0)) UNIV" by(rule_tac setsum_cong, auto) 
  2665     hence *:"setsum (op $ (x + (e / 2) *s basis a)) UNIV = setsum (\<lambda>i. x$i + (if a = i then e/2 else 0)) UNIV" by(rule_tac setsum_cong, auto) 
  2665     have "setsum (op $ x) UNIV < setsum (op $ (x + (e / 2) *s basis a)) UNIV" unfolding * setsum_addf
  2666     have "setsum (op $ x) UNIV < setsum (op $ (x + (e / 2) *s basis a)) UNIV" unfolding * setsum_addf
  2666       using `0<e` dimindex_ge_1 by(auto simp add: setsum_delta')
  2667       using `0<e` dimindex_ge_1 by(auto simp add: setsum_delta')
  2667     also have "\<dots> \<le> 1" using ** apply(drule_tac as[rule_format]) by auto
  2668     also have "\<dots> \<le> 1" using ** apply(drule_tac as[rule_format]) by auto
  2675   ultimately show "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> (\<forall>i. 0 \<le> y $ i) \<and> setsum (op $ y) UNIV \<le> 1"
  2676   ultimately show "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> (\<forall>i. 0 \<le> y $ i) \<and> setsum (op $ y) UNIV \<le> 1"
  2676     apply(rule_tac x="min (Min ((op $ x) ` UNIV)) ?D" in exI) apply rule defer apply(rule,rule) proof-
  2677     apply(rule_tac x="min (Min ((op $ x) ` UNIV)) ?D" in exI) apply rule defer apply(rule,rule) proof-
  2677     fix y assume y:"dist x y < min (Min (op $ x ` UNIV)) ?d"
  2678     fix y assume y:"dist x y < min (Min (op $ x ` UNIV)) ?d"
  2678     have "setsum (op $ y) UNIV \<le> setsum (\<lambda>i. x$i + ?d) UNIV" proof(rule setsum_mono)
  2679     have "setsum (op $ y) UNIV \<le> setsum (\<lambda>i. x$i + ?d) UNIV" proof(rule setsum_mono)
  2679       fix i::'n have "abs (y$i - x$i) < ?d" apply(rule le_less_trans) using component_le_norm[of "y - x" i]
  2680       fix i::'n have "abs (y$i - x$i) < ?d" apply(rule le_less_trans) using component_le_norm[of "y - x" i]
  2680 	using y[unfolded min_less_iff_conj dist_def, THEN conjunct2] by(auto simp add:vector_component_simps norm_minus_commute)
  2681 	using y[unfolded min_less_iff_conj dist_norm, THEN conjunct2] by(auto simp add:vector_component_simps norm_minus_commute)
  2681       thus "y $ i \<le> x $ i + ?d" by auto qed
  2682       thus "y $ i \<le> x $ i + ?d" by auto qed
  2682     also have "\<dots> \<le> 1" unfolding setsum_addf setsum_constant card_enum real_eq_of_nat using dimindex_ge_1 by(auto simp add: Suc_le_eq)
  2683     also have "\<dots> \<le> 1" unfolding setsum_addf setsum_constant card_enum real_eq_of_nat using dimindex_ge_1 by(auto simp add: Suc_le_eq)
  2683     finally show "(\<forall>i. 0 \<le> y $ i) \<and> setsum (op $ y) UNIV \<le> 1" apply- proof(rule,rule)
  2684     finally show "(\<forall>i. 0 \<le> y $ i) \<and> setsum (op $ y) UNIV \<le> 1" apply- proof(rule,rule)
  2684       fix i::'n have "norm (x - y) < x$i" using y[unfolded min_less_iff_conj dist_def, THEN conjunct1]
  2685       fix i::'n have "norm (x - y) < x$i" using y[unfolded min_less_iff_conj dist_norm, THEN conjunct1]
  2685 	using Min_gr_iff[of "op $ x ` dimset x"] dimindex_ge_1 by auto
  2686 	using Min_gr_iff[of "op $ x ` dimset x"] dimindex_ge_1 by auto
  2686       thus "0 \<le> y$i" using component_le_norm[of "x - y" i] and as(1)[rule_format, of i] by(auto simp add: vector_component_simps)
  2687       thus "0 \<le> y$i" using component_le_norm[of "x - y" i] and as(1)[rule_format, of i] by(auto simp add: vector_component_simps)
  2687     qed auto qed auto qed
  2688     qed auto qed auto qed
  2688 
  2689 
  2689 lemma interior_std_simplex_nonempty: obtains a::"real^'n::finite" where
  2690 lemma interior_std_simplex_nonempty: obtains a::"real^'n::finite" where
  3223   have ***:"\<And>xa. (if xa = 0 then 0 else 1) \<noteq> 1 \<Longrightarrow> xa = 0" apply(rule ccontr) by auto
  3224   have ***:"\<And>xa. (if xa = 0 then 0 else 1) \<noteq> 1 \<Longrightarrow> xa = 0" apply(rule ccontr) by auto
  3224   have **:"{x::real^'n. norm x = 1} = (\<lambda>x. (1/norm x) *s x) ` (UNIV - {0})" apply(rule set_ext,rule)
  3225   have **:"{x::real^'n. norm x = 1} = (\<lambda>x. (1/norm x) *s x) ` (UNIV - {0})" apply(rule set_ext,rule)
  3225     unfolding image_iff apply(rule_tac x=x in bexI) unfolding mem_Collect_eq norm_mul by(auto intro!: ***) 
  3226     unfolding image_iff apply(rule_tac x=x in bexI) unfolding mem_Collect_eq norm_mul by(auto intro!: ***) 
  3226   have "continuous_on (UNIV - {0}) (vec1 \<circ> (\<lambda>x::real^'n. 1 / norm x))" unfolding o_def continuous_on_eq_continuous_within
  3227   have "continuous_on (UNIV - {0}) (vec1 \<circ> (\<lambda>x::real^'n. 1 / norm x))" unfolding o_def continuous_on_eq_continuous_within
  3227     apply(rule, rule continuous_at_within_inv[unfolded o_def inverse_eq_divide]) apply(rule continuous_at_within)
  3228     apply(rule, rule continuous_at_within_inv[unfolded o_def inverse_eq_divide]) apply(rule continuous_at_within)
  3228     apply(rule continuous_at_vec1_norm[unfolded o_def,THEN spec]) by auto
  3229     apply(rule continuous_at_vec1_norm[unfolded o_def]) by auto
  3229   thus ?thesis unfolding * ** using path_connected_punctured_universe[OF assms]
  3230   thus ?thesis unfolding * ** using path_connected_punctured_universe[OF assms]
  3230     by(auto intro!: path_connected_continuous_image continuous_on_intros continuous_on_mul) qed
  3231     by(auto intro!: path_connected_continuous_image continuous_on_intros continuous_on_mul) qed
  3231 
  3232 
  3232 lemma connected_sphere: "2 \<le> CARD('n) \<Longrightarrow> connected {x::real^'n::finite. norm(x - a) = r}"
  3233 lemma connected_sphere: "2 \<le> CARD('n) \<Longrightarrow> connected {x::real^'n::finite. norm(x - a) = r}"
  3233   using path_connected_sphere path_connected_imp_connected by auto
  3234   using path_connected_sphere path_connected_imp_connected by auto