Moved Convex theory to library.
authorhoelzl
Mon May 03 14:35:10 2010 +0200 (2010-05-03)
changeset 36623d26348b667f2
parent 36622 e393a91f86df
child 36624 25153c08655e
Moved Convex theory to library.
src/HOL/Library/Convex.thy
src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
src/HOL/Probability/Information.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Convex.thy	Mon May 03 14:35:10 2010 +0200
     1.3 @@ -0,0 +1,610 @@
     1.4 +theory Convex
     1.5 +imports Product_Vector
     1.6 +begin
     1.7 +
     1.8 +subsection {* Convexity. *}
     1.9 +
    1.10 +definition
    1.11 +  convex :: "'a::real_vector set \<Rightarrow> bool" where
    1.12 +  "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    1.13 +
    1.14 +lemma convex_alt:
    1.15 +  "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
    1.16 +  (is "_ \<longleftrightarrow> ?alt")
    1.17 +proof
    1.18 +  assume alt[rule_format]: ?alt
    1.19 +  { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
    1.20 +    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
    1.21 +    moreover hence "u = 1 - v" by auto
    1.22 +    ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
    1.23 +  thus "convex s" unfolding convex_def by auto
    1.24 +qed (auto simp: convex_def)
    1.25 +
    1.26 +lemma mem_convex:
    1.27 +  assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
    1.28 +  shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
    1.29 +  using assms unfolding convex_alt by auto
    1.30 +
    1.31 +lemma convex_empty[intro]: "convex {}"
    1.32 +  unfolding convex_def by simp
    1.33 +
    1.34 +lemma convex_singleton[intro]: "convex {a}"
    1.35 +  unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
    1.36 +
    1.37 +lemma convex_UNIV[intro]: "convex UNIV"
    1.38 +  unfolding convex_def by auto
    1.39 +
    1.40 +lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
    1.41 +  unfolding convex_def by auto
    1.42 +
    1.43 +lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
    1.44 +  unfolding convex_def by auto
    1.45 +
    1.46 +lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
    1.47 +  unfolding convex_def
    1.48 +  by (auto simp: inner_add inner_scaleR intro!: convex_bound_le)
    1.49 +
    1.50 +lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    1.51 +proof -
    1.52 +  have *:"{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    1.53 +  show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    1.54 +qed
    1.55 +
    1.56 +lemma convex_hyperplane: "convex {x. inner a x = b}"
    1.57 +proof-
    1.58 +  have *:"{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    1.59 +  show ?thesis using convex_halfspace_le convex_halfspace_ge
    1.60 +    by (auto intro!: convex_Int simp: *)
    1.61 +qed
    1.62 +
    1.63 +lemma convex_halfspace_lt: "convex {x. inner a x < b}"
    1.64 +  unfolding convex_def
    1.65 +  by (auto simp: convex_bound_lt inner_add)
    1.66 +
    1.67 +lemma convex_halfspace_gt: "convex {x. inner a x > b}"
    1.68 +   using convex_halfspace_lt[of "-a" "-b"] by auto
    1.69 +
    1.70 +lemma convex_real_interval:
    1.71 +  fixes a b :: "real"
    1.72 +  shows "convex {a..}" and "convex {..b}"
    1.73 +  and "convex {a<..}" and "convex {..<b}"
    1.74 +  and "convex {a..b}" and "convex {a<..b}"
    1.75 +  and "convex {a..<b}" and "convex {a<..<b}"
    1.76 +proof -
    1.77 +  have "{a..} = {x. a \<le> inner 1 x}" by auto
    1.78 +  thus 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    1.79 +  have "{..b} = {x. inner 1 x \<le> b}" by auto
    1.80 +  thus 2: "convex {..b}" by (simp only: convex_halfspace_le)
    1.81 +  have "{a<..} = {x. a < inner 1 x}" by auto
    1.82 +  thus 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
    1.83 +  have "{..<b} = {x. inner 1 x < b}" by auto
    1.84 +  thus 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
    1.85 +  have "{a..b} = {a..} \<inter> {..b}" by auto
    1.86 +  thus "convex {a..b}" by (simp only: convex_Int 1 2)
    1.87 +  have "{a<..b} = {a<..} \<inter> {..b}" by auto
    1.88 +  thus "convex {a<..b}" by (simp only: convex_Int 3 2)
    1.89 +  have "{a..<b} = {a..} \<inter> {..<b}" by auto
    1.90 +  thus "convex {a..<b}" by (simp only: convex_Int 1 4)
    1.91 +  have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
    1.92 +  thus "convex {a<..<b}" by (simp only: convex_Int 3 4)
    1.93 +qed
    1.94 +
    1.95 +subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
    1.96 +
    1.97 +lemma convex_setsum:
    1.98 +  fixes C :: "'a::real_vector set"
    1.99 +  assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
   1.100 +  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.101 +  shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   1.102 +using assms
   1.103 +proof (induct s arbitrary:a rule:finite_induct)
   1.104 +  case empty thus ?case by auto
   1.105 +next
   1.106 +  case (insert i s) note asms = this
   1.107 +  { assume "a i = 1"
   1.108 +    hence "(\<Sum> j \<in> s. a j) = 0"
   1.109 +      using asms by auto
   1.110 +    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0"
   1.111 +      using setsum_nonneg_0[where 'b=real] asms by fastsimp
   1.112 +    hence ?case using asms by auto }
   1.113 +  moreover
   1.114 +  { assume asm: "a i \<noteq> 1"
   1.115 +    from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   1.116 +    have fis: "finite (insert i s)" using asms by auto
   1.117 +    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   1.118 +    hence "a i < 1" using asm by auto
   1.119 +    hence i0: "1 - a i > 0" by auto
   1.120 +    let "?a j" = "a j / (1 - a i)"
   1.121 +    { fix j assume "j \<in> s"
   1.122 +      hence "?a j \<ge> 0"
   1.123 +        using i0 asms divide_nonneg_pos
   1.124 +        by fastsimp } note a_nonneg = this
   1.125 +    have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   1.126 +    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastsimp
   1.127 +    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.128 +    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding divide.setsum by simp
   1.129 +    from this asms
   1.130 +    have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastsimp
   1.131 +    hence "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.132 +      using asms[unfolded convex_def, rule_format] yai ai1 by auto
   1.133 +    hence "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
   1.134 +      using scaleR_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j *\<^sub>R y j" s] by auto
   1.135 +    hence "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
   1.136 +    hence ?case using setsum.insert asms by auto }
   1.137 +  ultimately show ?case by auto
   1.138 +qed
   1.139 +
   1.140 +lemma convex:
   1.141 +  shows "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (setsum u {1..k} = 1)
   1.142 +           \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   1.143 +proof safe
   1.144 +  fix k :: nat fix u :: "nat \<Rightarrow> real" fix x
   1.145 +  assume "convex s"
   1.146 +    "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   1.147 +    "setsum u {1..k} = 1"
   1.148 +  from this convex_setsum[of "{1 .. k}" s]
   1.149 +  show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s" by auto
   1.150 +next
   1.151 +  assume asm: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> setsum u {1..k} = 1
   1.152 +    \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   1.153 +  { fix \<mu> :: real fix x y :: 'a assume xy: "x \<in> s" "y \<in> s" assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.154 +    let "?u i" = "if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   1.155 +    let "?x i" = "if (i :: nat) = 1 then x else y"
   1.156 +    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
   1.157 +    hence card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   1.158 +    hence "setsum ?u {1 .. 2} = 1"
   1.159 +      using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   1.160 +      by auto
   1.161 +    from this asm[rule_format, of "2" ?u ?x]
   1.162 +    have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   1.163 +      using mu xy by auto
   1.164 +    have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   1.165 +      using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   1.166 +    from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   1.167 +    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
   1.168 +    hence "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute) }
   1.169 +  thus "convex s" unfolding convex_alt by auto
   1.170 +qed
   1.171 +
   1.172 +
   1.173 +lemma convex_explicit:
   1.174 +  fixes s :: "'a::real_vector set"
   1.175 +  shows "convex s \<longleftrightarrow>
   1.176 +  (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   1.177 +proof safe
   1.178 +  fix t fix u :: "'a \<Rightarrow> real"
   1.179 +  assume "convex s" "finite t"
   1.180 +    "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   1.181 +  thus "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.182 +    using convex_setsum[of t s u "\<lambda> x. x"] by auto
   1.183 +next
   1.184 +  assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
   1.185 +    \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.186 +  show "convex s"
   1.187 +    unfolding convex_alt
   1.188 +  proof safe
   1.189 +    fix x y fix \<mu> :: real
   1.190 +    assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.191 +    { assume "x \<noteq> y"
   1.192 +      hence "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.193 +        using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
   1.194 +          asm by auto }
   1.195 +    moreover
   1.196 +    { assume "x = y"
   1.197 +      hence "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.198 +        using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
   1.199 +          asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
   1.200 +    ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
   1.201 +  qed
   1.202 +qed
   1.203 +
   1.204 +lemma convex_finite: assumes "finite s"
   1.205 +  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   1.206 +                      \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   1.207 +  unfolding convex_explicit
   1.208 +proof (safe elim!: conjE)
   1.209 +  fix t u assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   1.210 +    and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   1.211 +  have *:"s \<inter> t = t" using as(2) by auto
   1.212 +  have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)" by simp
   1.213 +  show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.214 +   using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   1.215 +   by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   1.216 +qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   1.217 +
   1.218 +definition
   1.219 +  convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
   1.220 +  "convex_on s f \<longleftrightarrow>
   1.221 +  (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   1.222 +
   1.223 +lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   1.224 +  unfolding convex_on_def by auto
   1.225 +
   1.226 +lemma convex_add[intro]:
   1.227 +  assumes "convex_on s f" "convex_on s g"
   1.228 +  shows "convex_on s (\<lambda>x. f x + g x)"
   1.229 +proof-
   1.230 +  { fix x y assume "x\<in>s" "y\<in>s" moreover
   1.231 +    fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.232 +    ultimately have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   1.233 +      using assms unfolding convex_on_def by (auto simp add:add_mono)
   1.234 +    hence "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)" by (simp add: field_simps)  }
   1.235 +  thus ?thesis unfolding convex_on_def by auto
   1.236 +qed
   1.237 +
   1.238 +lemma convex_cmul[intro]:
   1.239 +  assumes "0 \<le> (c::real)" "convex_on s f"
   1.240 +  shows "convex_on s (\<lambda>x. c * f x)"
   1.241 +proof-
   1.242 +  have *:"\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)" by (simp add: field_simps)
   1.243 +  show ?thesis using assms(2) and mult_mono1[OF _ assms(1)] unfolding convex_on_def and * by auto
   1.244 +qed
   1.245 +
   1.246 +lemma convex_lower:
   1.247 +  assumes "convex_on s f"  "x\<in>s"  "y \<in> s"  "0 \<le> u"  "0 \<le> v"  "u + v = 1"
   1.248 +  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   1.249 +proof-
   1.250 +  let ?m = "max (f x) (f y)"
   1.251 +  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   1.252 +    using assms(4,5) by(auto simp add: mult_mono1 add_mono)
   1.253 +  also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[THEN sym] by auto
   1.254 +  finally show ?thesis
   1.255 +    using assms unfolding convex_on_def by fastsimp
   1.256 +qed
   1.257 +
   1.258 +lemma convex_distance[intro]:
   1.259 +  fixes s :: "'a::real_normed_vector set"
   1.260 +  shows "convex_on s (\<lambda>x. dist a x)"
   1.261 +proof(auto simp add: convex_on_def dist_norm)
   1.262 +  fix x y assume "x\<in>s" "y\<in>s"
   1.263 +  fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.264 +  have "a = u *\<^sub>R a + v *\<^sub>R a" unfolding scaleR_left_distrib[THEN sym] and `u+v=1` by simp
   1.265 +  hence *:"a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   1.266 +    by (auto simp add: algebra_simps)
   1.267 +  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   1.268 +    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   1.269 +    using `0 \<le> u` `0 \<le> v` by auto
   1.270 +qed
   1.271 +
   1.272 +subsection {* Arithmetic operations on sets preserve convexity. *}
   1.273 +lemma convex_scaling:
   1.274 +  assumes "convex s"
   1.275 +  shows"convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   1.276 +using assms unfolding convex_def image_iff
   1.277 +proof safe
   1.278 +  fix x xa y xb :: "'a::real_vector" fix u v :: real
   1.279 +  assume asm: "\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.280 +    "xa \<in> s" "xb \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.281 +  show "\<exists>x\<in>s. u *\<^sub>R c *\<^sub>R xa + v *\<^sub>R c *\<^sub>R xb = c *\<^sub>R x"
   1.282 +    using bexI[of _ "u *\<^sub>R xa +v *\<^sub>R xb"] asm by (auto simp add: algebra_simps)
   1.283 +qed
   1.284 +
   1.285 +lemma convex_negations: "convex s \<Longrightarrow> convex ((\<lambda>x. -x)` s)"
   1.286 +using assms unfolding convex_def image_iff
   1.287 +proof safe
   1.288 +  fix x xa y xb :: "'a::real_vector" fix u v :: real
   1.289 +  assume asm: "\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.290 +    "xa \<in> s" "xb \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.291 +  show "\<exists>x\<in>s. u *\<^sub>R - xa + v *\<^sub>R - xb = - x"
   1.292 +    using bexI[of _ "u *\<^sub>R xa +v *\<^sub>R xb"] asm by auto
   1.293 +qed
   1.294 +
   1.295 +lemma convex_sums:
   1.296 +  assumes "convex s" "convex t"
   1.297 +  shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   1.298 +using assms unfolding convex_def image_iff
   1.299 +proof safe
   1.300 +  fix xa xb ya yb assume xy:"xa\<in>s" "xb\<in>s" "ya\<in>t" "yb\<in>t"
   1.301 +  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   1.302 +  show "\<exists>x y. u *\<^sub>R (xa + ya) + v *\<^sub>R (xb + yb) = x + y \<and> x \<in> s \<and> y \<in> t"
   1.303 +    using exI[of _ "u *\<^sub>R xa + v *\<^sub>R xb"] exI[of _ "u *\<^sub>R ya + v *\<^sub>R yb"]
   1.304 +      assms[unfolded convex_def] uv xy by (auto simp add:scaleR_right_distrib)
   1.305 +qed
   1.306 +
   1.307 +lemma convex_differences:
   1.308 +  assumes "convex s" "convex t"
   1.309 +  shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
   1.310 +proof -
   1.311 +  have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
   1.312 +  proof safe
   1.313 +    fix x x' y assume "x' \<in> s" "y \<in> t"
   1.314 +    thus "\<exists>x y'. x' - y = x + y' \<and> x \<in> s \<and> y' \<in> uminus ` t"
   1.315 +      using exI[of _ x'] exI[of _ "-y"] by auto
   1.316 +  next
   1.317 +    fix x x' y y' assume "x' \<in> s" "y' \<in> t"
   1.318 +    thus "\<exists>x y. x' + - y' = x - y \<and> x \<in> s \<and> y \<in> t"
   1.319 +      using exI[of _ x'] exI[of _ y'] by auto
   1.320 +  qed
   1.321 +  thus ?thesis using convex_sums[OF assms(1)  convex_negations[OF assms(2)]] by auto
   1.322 +qed
   1.323 +
   1.324 +lemma convex_translation: assumes "convex s" shows "convex ((\<lambda>x. a + x) ` s)"
   1.325 +proof- have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   1.326 +  thus ?thesis using convex_sums[OF convex_singleton[of a] assms] by auto qed
   1.327 +
   1.328 +lemma convex_affinity: assumes "convex s" shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   1.329 +proof- have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   1.330 +  thus ?thesis using convex_translation[OF convex_scaling[OF assms], of a c] by auto qed
   1.331 +
   1.332 +lemma convex_linear_image:
   1.333 +  assumes c:"convex s" and l:"bounded_linear f"
   1.334 +  shows "convex(f ` s)"
   1.335 +proof(auto simp add: convex_def)
   1.336 +  interpret f: bounded_linear f by fact
   1.337 +  fix x y assume xy:"x \<in> s" "y \<in> s"
   1.338 +  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   1.339 +  show "u *\<^sub>R f x + v *\<^sub>R f y \<in> f ` s" unfolding image_iff
   1.340 +    using bexI[of _ "u *\<^sub>R x + v *\<^sub>R y"] f.add f.scaleR
   1.341 +      c[unfolded convex_def] xy uv by auto
   1.342 +qed
   1.343 +
   1.344 +
   1.345 +lemma pos_is_convex:
   1.346 +  shows "convex {0 :: real <..}"
   1.347 +unfolding convex_alt
   1.348 +proof safe
   1.349 +  fix y x \<mu> :: real
   1.350 +  assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.351 +  { assume "\<mu> = 0"
   1.352 +    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   1.353 +    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.354 +  moreover
   1.355 +  { assume "\<mu> = 1"
   1.356 +    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.357 +  moreover
   1.358 +  { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   1.359 +    hence "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   1.360 +    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   1.361 +      using add_nonneg_pos[of "\<mu> *\<^sub>R x" "(1 - \<mu>) *\<^sub>R y"]
   1.362 +        real_mult_order by auto fastsimp }
   1.363 +  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastsimp
   1.364 +qed
   1.365 +
   1.366 +lemma convex_on_setsum:
   1.367 +  fixes a :: "'a \<Rightarrow> real"
   1.368 +  fixes y :: "'a \<Rightarrow> 'b::real_vector"
   1.369 +  fixes f :: "'b \<Rightarrow> real"
   1.370 +  assumes "finite s" "s \<noteq> {}"
   1.371 +  assumes "convex_on C f"
   1.372 +  assumes "convex C"
   1.373 +  assumes "(\<Sum> i \<in> s. a i) = 1"
   1.374 +  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   1.375 +  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.376 +  shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   1.377 +using assms
   1.378 +proof (induct s arbitrary:a rule:finite_ne_induct)
   1.379 +  case (singleton i)
   1.380 +  hence ai: "a i = 1" by auto
   1.381 +  thus ?case by auto
   1.382 +next
   1.383 +  case (insert i s) note asms = this
   1.384 +  hence "convex_on C f" by simp
   1.385 +  from this[unfolded convex_on_def, rule_format]
   1.386 +  have conv: "\<And> x y \<mu>. \<lbrakk>x \<in> C; y \<in> C; 0 \<le> \<mu>; \<mu> \<le> 1\<rbrakk>
   1.387 +  \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.388 +    by simp
   1.389 +  { assume "a i = 1"
   1.390 +    hence "(\<Sum> j \<in> s. a j) = 0"
   1.391 +      using asms by auto
   1.392 +    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0"
   1.393 +      using setsum_nonneg_0[where 'b=real] asms by fastsimp
   1.394 +    hence ?case using asms by auto }
   1.395 +  moreover
   1.396 +  { assume asm: "a i \<noteq> 1"
   1.397 +    from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   1.398 +    have fis: "finite (insert i s)" using asms by auto
   1.399 +    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   1.400 +    hence "a i < 1" using asm by auto
   1.401 +    hence i0: "1 - a i > 0" by auto
   1.402 +    let "?a j" = "a j / (1 - a i)"
   1.403 +    { fix j assume "j \<in> s"
   1.404 +      hence "?a j \<ge> 0"
   1.405 +        using i0 asms divide_nonneg_pos
   1.406 +        by fastsimp } note a_nonneg = this
   1.407 +    have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   1.408 +    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastsimp
   1.409 +    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.410 +    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding divide.setsum by simp
   1.411 +    have "convex C" using asms by auto
   1.412 +    hence asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.413 +      using asms convex_setsum[OF `finite s`
   1.414 +        `convex C` a1 a_nonneg] by auto
   1.415 +    have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   1.416 +      using a_nonneg a1 asms by blast
   1.417 +    have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) = f ((\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.418 +      using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF `finite s` `i \<notin> s`] asms
   1.419 +      by (auto simp only:add_commute)
   1.420 +    also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.421 +      using i0 by auto
   1.422 +    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.423 +      using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric] by (auto simp:algebra_simps)
   1.424 +    also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.425 +      by (auto simp:real_divide_def)
   1.426 +    also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   1.427 +      using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   1.428 +      by (auto simp add:add_commute)
   1.429 +    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   1.430 +      using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
   1.431 +        OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
   1.432 +    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   1.433 +      unfolding mult_right.setsum[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
   1.434 +    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
   1.435 +    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
   1.436 +    finally have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) \<le> (\<Sum> j \<in> insert i s. a j * f (y j))"
   1.437 +      by simp }
   1.438 +  ultimately show ?case by auto
   1.439 +qed
   1.440 +
   1.441 +lemma convex_on_alt:
   1.442 +  fixes C :: "'a::real_vector set"
   1.443 +  assumes "convex C"
   1.444 +  shows "convex_on C f =
   1.445 +  (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
   1.446 +      \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   1.447 +proof safe
   1.448 +  fix x y fix \<mu> :: real
   1.449 +  assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.450 +  from this[unfolded convex_on_def, rule_format]
   1.451 +  have "\<And> u v. \<lbrakk>0 \<le> u; 0 \<le> v; u + v = 1\<rbrakk> \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" by auto
   1.452 +  from this[of "\<mu>" "1 - \<mu>", simplified] asms
   1.453 +  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y)
   1.454 +          \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   1.455 +next
   1.456 +  assume asm: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.457 +  {fix x y fix u v :: real
   1.458 +    assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   1.459 +    hence[simp]: "1 - u = v" by auto
   1.460 +    from asm[rule_format, of x y u]
   1.461 +    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto }
   1.462 +  thus "convex_on C f" unfolding convex_on_def by auto
   1.463 +qed
   1.464 +
   1.465 +
   1.466 +lemma pos_convex_function:
   1.467 +  fixes f :: "real \<Rightarrow> real"
   1.468 +  assumes "convex C"
   1.469 +  assumes leq: "\<And> x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.470 +  shows "convex_on C f"
   1.471 +unfolding convex_on_alt[OF assms(1)]
   1.472 +using assms
   1.473 +proof safe
   1.474 +  fix x y \<mu> :: real
   1.475 +  let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.476 +  assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.477 +  hence "1 - \<mu> \<ge> 0" by auto
   1.478 +  hence xpos: "?x \<in> C" using asm unfolding convex_alt by fastsimp
   1.479 +  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
   1.480 +            \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   1.481 +    using add_mono[OF mult_mono1[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
   1.482 +      mult_mono1[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
   1.483 +  hence "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   1.484 +    by (auto simp add:field_simps)
   1.485 +  thus "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.486 +    using convex_on_alt by auto
   1.487 +qed
   1.488 +
   1.489 +lemma atMostAtLeast_subset_convex:
   1.490 +  fixes C :: "real set"
   1.491 +  assumes "convex C"
   1.492 +  assumes "x \<in> C" "y \<in> C" "x < y"
   1.493 +  shows "{x .. y} \<subseteq> C"
   1.494 +proof safe
   1.495 +  fix z assume zasm: "z \<in> {x .. y}"
   1.496 +  { assume asm: "x < z" "z < y"
   1.497 +    let "?\<mu>" = "(y - z) / (y - x)"
   1.498 +    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add:field_simps)
   1.499 +    hence comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   1.500 +      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>] by (simp add:algebra_simps)
   1.501 +    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   1.502 +      by (auto simp add:field_simps)
   1.503 +    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   1.504 +      using assms unfolding add_divide_distrib by (auto simp:field_simps)
   1.505 +    also have "\<dots> = z"
   1.506 +      using assms by (auto simp:field_simps)
   1.507 +    finally have "z \<in> C"
   1.508 +      using comb by auto } note less = this
   1.509 +  show "z \<in> C" using zasm less assms
   1.510 +    unfolding atLeastAtMost_iff le_less by auto
   1.511 +qed
   1.512 +
   1.513 +lemma f''_imp_f':
   1.514 +  fixes f :: "real \<Rightarrow> real"
   1.515 +  assumes "convex C"
   1.516 +  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.517 +  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.518 +  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.519 +  assumes "x \<in> C" "y \<in> C"
   1.520 +  shows "f' x * (y - x) \<le> f y - f x"
   1.521 +using assms
   1.522 +proof -
   1.523 +  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "y > x"
   1.524 +    hence ge: "y - x > 0" "y - x \<ge> 0" by auto
   1.525 +    from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   1.526 +    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   1.527 +      using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
   1.528 +        THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   1.529 +      by auto
   1.530 +    hence "z1 \<in> C" using atMostAtLeast_subset_convex
   1.531 +      `convex C` `x \<in> C` `y \<in> C` `x < y` by fastsimp
   1.532 +    from z1 have z1': "f x - f y = (x - y) * f' z1"
   1.533 +      by (simp add:field_simps)
   1.534 +    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   1.535 +      using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `z1 \<in> C` `x < z1`],
   1.536 +        THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   1.537 +      by auto
   1.538 +    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   1.539 +      using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `z1 \<in> C` `y \<in> C` `z1 < y`],
   1.540 +        THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   1.541 +      by auto
   1.542 +    have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   1.543 +      using asm z1' by auto
   1.544 +    also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
   1.545 +    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
   1.546 +    have A': "y - z1 \<ge> 0" using z1 by auto
   1.547 +    have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
   1.548 +      `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastsimp
   1.549 +    hence B': "f'' z3 \<ge> 0" using assms by auto
   1.550 +    from A' B' have "(y - z1) * f'' z3 \<ge> 0" using mult_nonneg_nonneg by auto
   1.551 +    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
   1.552 +    from mult_right_mono_neg[OF this le(2)]
   1.553 +    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   1.554 +      unfolding diff_def using real_add_mult_distrib by auto
   1.555 +    hence "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   1.556 +    hence res: "f' y * (x - y) \<le> f x - f y" by auto
   1.557 +    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   1.558 +      using asm z1 by auto
   1.559 +    also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
   1.560 +    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
   1.561 +    have A: "z1 - x \<ge> 0" using z1 by auto
   1.562 +    have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
   1.563 +      `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastsimp
   1.564 +    hence B: "f'' z2 \<ge> 0" using assms by auto
   1.565 +    from A B have "(z1 - x) * f'' z2 \<ge> 0" using mult_nonneg_nonneg by auto
   1.566 +    from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
   1.567 +    from mult_right_mono[OF this ge(2)]
   1.568 +    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   1.569 +      unfolding diff_def using real_add_mult_distrib by auto
   1.570 +    hence "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   1.571 +    hence "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.572 +      using res by auto } note less_imp = this
   1.573 +  { fix x y :: real assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   1.574 +    hence"f y - f x \<ge> f' x * (y - x)"
   1.575 +    unfolding neq_iff using less_imp by auto } note neq_imp = this
   1.576 +  moreover
   1.577 +  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "x = y"
   1.578 +    hence "f y - f x \<ge> f' x * (y - x)" by auto }
   1.579 +  ultimately show ?thesis using assms by blast
   1.580 +qed
   1.581 +
   1.582 +lemma f''_ge0_imp_convex:
   1.583 +  fixes f :: "real \<Rightarrow> real"
   1.584 +  assumes conv: "convex C"
   1.585 +  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.586 +  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.587 +  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.588 +  shows "convex_on C f"
   1.589 +using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastsimp
   1.590 +
   1.591 +lemma minus_log_convex:
   1.592 +  fixes b :: real
   1.593 +  assumes "b > 1"
   1.594 +  shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   1.595 +proof -
   1.596 +  have "\<And> z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   1.597 +  hence f': "\<And> z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   1.598 +    using DERIV_minus by auto
   1.599 +  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   1.600 +    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   1.601 +  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   1.602 +  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   1.603 +    by auto
   1.604 +  hence f''0: "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.605 +    unfolding inverse_eq_divide by (auto simp add:real_mult_assoc)
   1.606 +  have f''_ge0: "\<And> z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.607 +    using `b > 1` by (auto intro!:less_imp_le simp add:divide_pos_pos[of 1] real_mult_order)
   1.608 +  from f''_ge0_imp_convex[OF pos_is_convex,
   1.609 +    unfolded greaterThan_iff, OF f' f''0 f''_ge0]
   1.610 +  show ?thesis by auto
   1.611 +qed
   1.612 +
   1.613 +end
     2.1 --- a/src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy	Tue Apr 20 17:58:34 2010 +0200
     2.2 +++ b/src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy	Mon May 03 14:35:10 2010 +0200
     2.3 @@ -5,7 +5,7 @@
     2.4  header {* Convex sets, functions and related things. *}
     2.5  
     2.6  theory Convex_Euclidean_Space
     2.7 -imports Topology_Euclidean_Space
     2.8 +imports Topology_Euclidean_Space Convex
     2.9  begin
    2.10  
    2.11  
    2.12 @@ -315,176 +315,6 @@
    2.13    shows "affine hull s = {a + v | v. v \<in> span {x - a | x. x \<in> s - {a}}}"
    2.14    using affine_hull_insert_span[of a "s - {a}", unfolded insert_Diff[OF assms]] by auto
    2.15  
    2.16 -subsection {* Convexity. *}
    2.17 -
    2.18 -definition
    2.19 -  convex :: "'a::real_vector set \<Rightarrow> bool" where
    2.20 -  "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    2.21 -
    2.22 -lemma convex_alt: "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
    2.23 -proof- have *:"\<And>u v::real. u + v = 1 \<longleftrightarrow> u = 1 - v" by auto
    2.24 -  show ?thesis unfolding convex_def apply auto
    2.25 -    apply(erule_tac x=x in ballE) apply(erule_tac x=y in ballE) apply(erule_tac x="1 - u" in allE)
    2.26 -    by (auto simp add: *) qed
    2.27 -
    2.28 -lemma mem_convex:
    2.29 -  assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
    2.30 -  shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
    2.31 -  using assms unfolding convex_alt by auto
    2.32 -
    2.33 -lemma convex_empty[intro]: "convex {}"
    2.34 -  unfolding convex_def by simp
    2.35 -
    2.36 -lemma convex_singleton[intro]: "convex {a}"
    2.37 -  unfolding convex_def by (auto simp add:scaleR_left_distrib[THEN sym])
    2.38 -
    2.39 -lemma convex_UNIV[intro]: "convex UNIV"
    2.40 -  unfolding convex_def by auto
    2.41 -
    2.42 -lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
    2.43 -  unfolding convex_def by auto
    2.44 -
    2.45 -lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
    2.46 -  unfolding convex_def by auto
    2.47 -
    2.48 -lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
    2.49 -  unfolding convex_def apply auto
    2.50 -  unfolding inner_add inner_scaleR
    2.51 -  by (metis real_convex_bound_le)
    2.52 -
    2.53 -lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    2.54 -proof- have *:"{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    2.55 -  show ?thesis apply(unfold *) using convex_halfspace_le[of "-a" "-b"] by auto qed
    2.56 -
    2.57 -lemma convex_hyperplane: "convex {x. inner a x = b}"
    2.58 -proof-
    2.59 -  have *:"{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    2.60 -  show ?thesis unfolding * apply(rule convex_Int)
    2.61 -    using convex_halfspace_le convex_halfspace_ge by auto
    2.62 -qed
    2.63 -
    2.64 -lemma convex_halfspace_lt: "convex {x. inner a x < b}"
    2.65 -  unfolding convex_def
    2.66 -  by(auto simp add: real_convex_bound_lt inner_add)
    2.67 -
    2.68 -lemma convex_halfspace_gt: "convex {x. inner a x > b}"
    2.69 -   using convex_halfspace_lt[of "-a" "-b"] by auto
    2.70 -
    2.71 -lemma convex_real_interval:
    2.72 -  fixes a b :: "real"
    2.73 -  shows "convex {a..}" and "convex {..b}"
    2.74 -  and "convex {a<..}" and "convex {..<b}"
    2.75 -  and "convex {a..b}" and "convex {a<..b}"
    2.76 -  and "convex {a..<b}" and "convex {a<..<b}"
    2.77 -proof -
    2.78 -  have "{a..} = {x. a \<le> inner 1 x}" by auto
    2.79 -  thus 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    2.80 -  have "{..b} = {x. inner 1 x \<le> b}" by auto
    2.81 -  thus 2: "convex {..b}" by (simp only: convex_halfspace_le)
    2.82 -  have "{a<..} = {x. a < inner 1 x}" by auto
    2.83 -  thus 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
    2.84 -  have "{..<b} = {x. inner 1 x < b}" by auto
    2.85 -  thus 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
    2.86 -  have "{a..b} = {a..} \<inter> {..b}" by auto
    2.87 -  thus "convex {a..b}" by (simp only: convex_Int 1 2)
    2.88 -  have "{a<..b} = {a<..} \<inter> {..b}" by auto
    2.89 -  thus "convex {a<..b}" by (simp only: convex_Int 3 2)
    2.90 -  have "{a..<b} = {a..} \<inter> {..<b}" by auto
    2.91 -  thus "convex {a..<b}" by (simp only: convex_Int 1 4)
    2.92 -  have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
    2.93 -  thus "convex {a<..<b}" by (simp only: convex_Int 3 4)
    2.94 -qed
    2.95 -
    2.96 -lemma convex_box:
    2.97 -  assumes "\<And>i. convex {x. P i x}"
    2.98 -  shows "convex {x. \<forall>i. P i (x$i)}"
    2.99 -using assms unfolding convex_def by auto
   2.100 -
   2.101 -lemma convex_positive_orthant: "convex {x::real^'n. (\<forall>i. 0 \<le> x$i)}"
   2.102 -by (rule convex_box, simp add: atLeast_def [symmetric] convex_real_interval)
   2.103 -
   2.104 -subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
   2.105 -
   2.106 -lemma convex: "convex s \<longleftrightarrow>
   2.107 -  (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (setsum u {1..k} = 1)
   2.108 -           \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   2.109 -  unfolding convex_def apply rule apply(rule allI)+ defer apply(rule ballI)+ apply(rule allI)+ proof(rule,rule,rule,rule)
   2.110 -  fix x y u v assume as:"\<forall>(k::nat) u x. (\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> setsum u {1..k} = 1 \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R x i) \<in> s"
   2.111 -    "x \<in> s" "y \<in> s" "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
   2.112 -  show "u *\<^sub>R x + v *\<^sub>R y \<in> s" using as(1)[THEN spec[where x=2], THEN spec[where x="\<lambda>n. if n=1 then u else v"], THEN spec[where x="\<lambda>n. if n=1 then x else y"]] and as(2-)
   2.113 -    by (auto simp add: setsum_head_Suc) 
   2.114 -next
   2.115 -  fix k u x assume as:"\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s" 
   2.116 -  show "(\<forall>i::nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> setsum u {1..k} = 1 \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R x i) \<in> s" apply(rule,erule conjE) proof(induct k arbitrary: u)
   2.117 -  case (Suc k) show ?case proof(cases "u (Suc k) = 1")
   2.118 -    case True hence "(\<Sum>i = Suc 0..k. u i *\<^sub>R x i) = 0" apply(rule_tac setsum_0') apply(rule ccontr) unfolding ball_simps apply(erule bexE) proof-
   2.119 -      fix i assume i:"i \<in> {Suc 0..k}" "u i *\<^sub>R x i \<noteq> 0"
   2.120 -      hence ui:"u i \<noteq> 0" by auto
   2.121 -      hence "setsum (\<lambda>k. if k=i then u i else 0) {1 .. k} \<le> setsum u {1 .. k}" apply(rule_tac setsum_mono) using Suc(2) by auto
   2.122 -      hence "setsum u {1 .. k} \<ge> u i" using i(1) by(auto simp add: setsum_delta) 
   2.123 -      hence "setsum u {1 .. k} > 0"  using ui apply(rule_tac less_le_trans[of _ "u i"]) using Suc(2)[THEN spec[where x=i]] and i(1) by auto
   2.124 -      thus False using Suc(3) unfolding setsum_cl_ivl_Suc and True by simp qed
   2.125 -    thus ?thesis unfolding setsum_cl_ivl_Suc using True and Suc(2) by auto
   2.126 -  next
   2.127 -    have *:"setsum u {1..k} = 1 - u (Suc k)" using Suc(3)[unfolded setsum_cl_ivl_Suc] by auto
   2.128 -    have **:"u (Suc k) \<le> 1" unfolding not_le using Suc(3) using setsum_nonneg[of "{1..k}" u] using Suc(2) by auto
   2.129 -    have ***:"\<And>i k. (u i / (1 - u (Suc k))) *\<^sub>R x i = (inverse (1 - u (Suc k))) *\<^sub>R (u i *\<^sub>R x i)" unfolding real_divide_def by (auto simp add: algebra_simps)
   2.130 -    case False hence nn:"1 - u (Suc k) \<noteq> 0" by auto
   2.131 -    have "(\<Sum>i = 1..k. (u i / (1 - u (Suc k))) *\<^sub>R x i) \<in> s" apply(rule Suc(1)) unfolding setsum_divide_distrib[THEN sym] and *
   2.132 -      apply(rule_tac allI) apply(rule,rule) apply(rule divide_nonneg_pos) using nn Suc(2) ** by auto
   2.133 -    hence "(1 - u (Suc k)) *\<^sub>R (\<Sum>i = 1..k. (u i / (1 - u (Suc k))) *\<^sub>R x i) + u (Suc k) *\<^sub>R x (Suc k) \<in> s"
   2.134 -      apply(rule as[THEN bspec, THEN bspec, THEN spec, THEN mp, THEN spec, THEN mp, THEN mp]) using Suc(2)[THEN spec[where x="Suc k"]] and ** by auto
   2.135 -    thus ?thesis unfolding setsum_cl_ivl_Suc and *** and scaleR_right.setsum [symmetric] using nn by auto qed qed auto qed
   2.136 -
   2.137 -
   2.138 -lemma convex_explicit:
   2.139 -  fixes s :: "'a::real_vector set"
   2.140 -  shows "convex s \<longleftrightarrow>
   2.141 -  (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   2.142 -  unfolding convex_def apply(rule,rule,rule) apply(subst imp_conjL,rule) defer apply(rule,rule,rule,rule,rule,rule,rule) proof-
   2.143 -  fix x y u v assume as:"\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s" "x \<in> s" "y \<in> s" "0 \<le> u" "0 \<le> v" "u + v = (1::real)"
   2.144 -  show "u *\<^sub>R x + v *\<^sub>R y \<in> s" proof(cases "x=y")
   2.145 -    case True show ?thesis unfolding True and scaleR_left_distrib[THEN sym] using as(3,6) by auto next
   2.146 -    case False thus ?thesis using as(1)[THEN spec[where x="{x,y}"], THEN spec[where x="\<lambda>z. if z=x then u else v"]] and as(2-) by auto qed
   2.147 -next 
   2.148 -  fix t u assume asm:"\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s" "finite (t::'a set)"
   2.149 -  (*"finite t" "t \<subseteq> s" "\<forall>x\<in>t. (0::real) \<le> u x" "setsum u t = 1"*)
   2.150 -  from this(2) have "\<forall>u. t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s" apply(induct t rule:finite_induct)
   2.151 -    prefer 2 apply (rule,rule) apply(erule conjE)+ proof-
   2.152 -    fix x f u assume ind:"\<forall>u. f \<subseteq> s \<and> (\<forall>x\<in>f. 0 \<le> u x) \<and> setsum u f = 1 \<longrightarrow> (\<Sum>x\<in>f. u x *\<^sub>R x) \<in> s"
   2.153 -    assume as:"finite f" "x \<notin> f" "insert x f \<subseteq> s" "\<forall>x\<in>insert x f. 0 \<le> u x" "setsum u (insert x f) = (1::real)"
   2.154 -    show "(\<Sum>x\<in>insert x f. u x *\<^sub>R x) \<in> s" proof(cases "u x = 1")
   2.155 -      case True hence "setsum (\<lambda>x. u x *\<^sub>R x) f = 0" apply(rule_tac setsum_0') apply(rule ccontr) unfolding ball_simps apply(erule bexE) proof-
   2.156 -        fix y assume y:"y \<in> f" "u y *\<^sub>R y \<noteq> 0"
   2.157 -        hence uy:"u y \<noteq> 0" by auto
   2.158 -        hence "setsum (\<lambda>k. if k=y then u y else 0) f \<le> setsum u f" apply(rule_tac setsum_mono) using as(4) by auto
   2.159 -        hence "setsum u f \<ge> u y" using y(1) and as(1) by(auto simp add: setsum_delta) 
   2.160 -        hence "setsum u f > 0" using uy apply(rule_tac less_le_trans[of _ "u y"]) using as(4) and y(1) by auto
   2.161 -        thus False using as(2,5) unfolding setsum_clauses(2)[OF as(1)] and True by auto qed
   2.162 -      thus ?thesis unfolding setsum_clauses(2)[OF as(1)] using as(2,3) unfolding True by auto
   2.163 -    next
   2.164 -      have *:"setsum u f = setsum u (insert x f) - u x" using as(2) unfolding setsum_clauses(2)[OF as(1)] by auto
   2.165 -      have **:"u x \<le> 1" unfolding not_le using as(5)[unfolded setsum_clauses(2)[OF as(1)]] and as(2)
   2.166 -        using setsum_nonneg[of f u] and as(4) by auto
   2.167 -      case False hence "inverse (1 - u x) *\<^sub>R (\<Sum>x\<in>f. u x *\<^sub>R x) \<in> s" unfolding scaleR_right.setsum and scaleR_scaleR
   2.168 -        apply(rule_tac ind[THEN spec, THEN mp]) apply rule defer apply rule apply rule apply(rule mult_nonneg_nonneg)
   2.169 -        unfolding setsum_right_distrib[THEN sym] and * using as and ** by auto
   2.170 -      hence "u x *\<^sub>R x + (1 - u x) *\<^sub>R ((inverse (1 - u x)) *\<^sub>R setsum (\<lambda>x. u x *\<^sub>R x) f) \<in>s" 
   2.171 -        apply(rule_tac asm(1)[THEN bspec, THEN bspec, THEN spec, THEN mp, THEN spec, THEN mp, THEN mp]) using as and ** False by auto 
   2.172 -      thus ?thesis unfolding setsum_clauses(2)[OF as(1)] using as(2) and False by auto qed
   2.173 -  qed auto thus "t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s" by auto
   2.174 -qed
   2.175 -
   2.176 -lemma convex_finite: assumes "finite s"
   2.177 -  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   2.178 -                      \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   2.179 -  unfolding convex_explicit apply(rule, rule, rule) defer apply(rule,rule,rule)apply(erule conjE)+ proof-
   2.180 -  fix t u assume as:"\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s" " finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   2.181 -  have *:"s \<inter> t = t" using as(3) by auto
   2.182 -  show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s" using as(1)[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]]
   2.183 -    unfolding if_smult and setsum_cases[OF assms] using as(2-) * by auto
   2.184 -qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   2.185 -
   2.186  subsection {* Cones. *}
   2.187  
   2.188  definition
   2.189 @@ -595,49 +425,15 @@
   2.190  lemma connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
   2.191    by(simp add: convex_connected convex_UNIV)
   2.192  
   2.193 -subsection {* Convex functions into the reals. *}
   2.194 -
   2.195 -definition
   2.196 -  convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
   2.197 -  "convex_on s f \<longleftrightarrow>
   2.198 -  (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   2.199 -
   2.200 -lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   2.201 -  unfolding convex_on_def by auto
   2.202 +subsection {* Balls, being convex, are connected. *}
   2.203  
   2.204 -lemma convex_add[intro]:
   2.205 -  assumes "convex_on s f" "convex_on s g"
   2.206 -  shows "convex_on s (\<lambda>x. f x + g x)"
   2.207 -proof-
   2.208 -  { fix x y assume "x\<in>s" "y\<in>s" moreover
   2.209 -    fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   2.210 -    ultimately have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   2.211 -      using assms(1)[unfolded convex_on_def, THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x=u]]
   2.212 -      using assms(2)[unfolded convex_on_def, THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x=u]]
   2.213 -      apply - apply(rule add_mono) by auto
   2.214 -    hence "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)" by (simp add: field_simps)  }
   2.215 -  thus ?thesis unfolding convex_on_def by auto 
   2.216 -qed
   2.217 +lemma convex_box:
   2.218 +  assumes "\<And>i. convex {x. P i x}"
   2.219 +  shows "convex {x. \<forall>i. P i (x$i)}"
   2.220 +using assms unfolding convex_def by auto
   2.221  
   2.222 -lemma convex_cmul[intro]:
   2.223 -  assumes "0 \<le> (c::real)" "convex_on s f"
   2.224 -  shows "convex_on s (\<lambda>x. c * f x)"
   2.225 -proof-
   2.226 -  have *:"\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)" by (simp add: field_simps)
   2.227 -  show ?thesis using assms(2) and mult_mono1[OF _ assms(1)] unfolding convex_on_def and * by auto
   2.228 -qed
   2.229 -
   2.230 -lemma convex_lower:
   2.231 -  assumes "convex_on s f"  "x\<in>s"  "y \<in> s"  "0 \<le> u"  "0 \<le> v"  "u + v = 1"
   2.232 -  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   2.233 -proof-
   2.234 -  let ?m = "max (f x) (f y)"
   2.235 -  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)" apply(rule add_mono) 
   2.236 -    using assms(4,5) by(auto simp add: mult_mono1)
   2.237 -  also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[THEN sym] by auto
   2.238 -  finally show ?thesis using assms(1)[unfolded convex_on_def, THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x=u]]
   2.239 -    using assms(2-6) by auto 
   2.240 -qed
   2.241 +lemma convex_positive_orthant: "convex {x::real^'n. (\<forall>i. 0 \<le> x$i)}"
   2.242 +  by (rule convex_box) (simp add: atLeast_def[symmetric] convex_real_interval)
   2.243  
   2.244  lemma convex_local_global_minimum:
   2.245    fixes s :: "'a::real_normed_vector set"
   2.246 @@ -661,76 +457,6 @@
   2.247    ultimately show False using mult_strict_left_mono[OF y `u>0`] unfolding left_diff_distrib by auto
   2.248  qed
   2.249  
   2.250 -lemma convex_distance[intro]:
   2.251 -  fixes s :: "'a::real_normed_vector set"
   2.252 -  shows "convex_on s (\<lambda>x. dist a x)"
   2.253 -proof(auto simp add: convex_on_def dist_norm)
   2.254 -  fix x y assume "x\<in>s" "y\<in>s"
   2.255 -  fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   2.256 -  have "a = u *\<^sub>R a + v *\<^sub>R a" unfolding scaleR_left_distrib[THEN sym] and `u+v=1` by simp
   2.257 -  hence *:"a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   2.258 -    by (auto simp add: algebra_simps)
   2.259 -  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   2.260 -    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   2.261 -    using `0 \<le> u` `0 \<le> v` by auto
   2.262 -qed
   2.263 -
   2.264 -subsection {* Arithmetic operations on sets preserve convexity. *}
   2.265 -
   2.266 -lemma convex_scaling: "convex s \<Longrightarrow> convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   2.267 -  unfolding convex_def and image_iff apply auto
   2.268 -  apply (rule_tac x="u *\<^sub>R x+v *\<^sub>R y" in bexI) by (auto simp add: algebra_simps)
   2.269 -
   2.270 -lemma convex_negations: "convex s \<Longrightarrow> convex ((\<lambda>x. -x)` s)"
   2.271 -  unfolding convex_def and image_iff apply auto
   2.272 -  apply (rule_tac x="u *\<^sub>R x+v *\<^sub>R y" in bexI) by auto
   2.273 -
   2.274 -lemma convex_sums:
   2.275 -  assumes "convex s" "convex t"
   2.276 -  shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   2.277 -proof(auto simp add: convex_def image_iff scaleR_right_distrib)
   2.278 -  fix xa xb ya yb assume xy:"xa\<in>s" "xb\<in>s" "ya\<in>t" "yb\<in>t"
   2.279 -  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   2.280 -  show "\<exists>x y. u *\<^sub>R xa + u *\<^sub>R ya + (v *\<^sub>R xb + v *\<^sub>R yb) = x + y \<and> x \<in> s \<and> y \<in> t"
   2.281 -    apply(rule_tac x="u *\<^sub>R xa + v *\<^sub>R xb" in exI) apply(rule_tac x="u *\<^sub>R ya + v *\<^sub>R yb" in exI)
   2.282 -    using assms(1)[unfolded convex_def, THEN bspec[where x=xa], THEN bspec[where x=xb]]
   2.283 -    using assms(2)[unfolded convex_def, THEN bspec[where x=ya], THEN bspec[where x=yb]]
   2.284 -    using uv xy by auto
   2.285 -qed
   2.286 -
   2.287 -lemma convex_differences: 
   2.288 -  assumes "convex s" "convex t"
   2.289 -  shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
   2.290 -proof-
   2.291 -  have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}" unfolding image_iff apply auto
   2.292 -    apply(rule_tac x=xa in exI) apply(rule_tac x="-y" in exI) apply simp
   2.293 -    apply(rule_tac x=xa in exI) apply(rule_tac x=xb in exI) by simp
   2.294 -  thus ?thesis using convex_sums[OF assms(1)  convex_negations[OF assms(2)]] by auto
   2.295 -qed
   2.296 -
   2.297 -lemma convex_translation: assumes "convex s" shows "convex ((\<lambda>x. a + x) ` s)"
   2.298 -proof- have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   2.299 -  thus ?thesis using convex_sums[OF convex_singleton[of a] assms] by auto qed
   2.300 -
   2.301 -lemma convex_affinity: assumes "convex s" shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   2.302 -proof- have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   2.303 -  thus ?thesis using convex_translation[OF convex_scaling[OF assms], of a c] by auto qed
   2.304 -
   2.305 -lemma convex_linear_image:
   2.306 -  assumes c:"convex s" and l:"bounded_linear f"
   2.307 -  shows "convex(f ` s)"
   2.308 -proof(auto simp add: convex_def)
   2.309 -  interpret f: bounded_linear f by fact
   2.310 -  fix x y assume xy:"x \<in> s" "y \<in> s"
   2.311 -  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   2.312 -  show "u *\<^sub>R f x + v *\<^sub>R f y \<in> f ` s" unfolding image_iff
   2.313 -    apply(rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in bexI)
   2.314 -    unfolding f.add f.scaleR
   2.315 -    using c[unfolded convex_def] xy uv by auto
   2.316 -qed
   2.317 -
   2.318 -subsection {* Balls, being convex, are connected. *}
   2.319 -
   2.320  lemma convex_ball:
   2.321    fixes x :: "'a::real_normed_vector"
   2.322    shows "convex (ball x e)" 
   2.323 @@ -739,7 +465,7 @@
   2.324    fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   2.325    have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z" using uv yz
   2.326      using convex_distance[of "ball x e" x, unfolded convex_on_def, THEN bspec[where x=y], THEN bspec[where x=z]] by auto
   2.327 -  thus "dist x (u *\<^sub>R y + v *\<^sub>R z) < e" using real_convex_bound_lt[OF yz uv] by auto 
   2.328 +  thus "dist x (u *\<^sub>R y + v *\<^sub>R z) < e" using convex_bound_lt[OF yz uv] by auto
   2.329  qed
   2.330  
   2.331  lemma convex_cball:
   2.332 @@ -750,7 +476,7 @@
   2.333    fix u v ::real assume uv:" 0 \<le> u" "0 \<le> v" "u + v = 1"
   2.334    have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z" using uv yz
   2.335      using convex_distance[of "cball x e" x, unfolded convex_on_def, THEN bspec[where x=y], THEN bspec[where x=z]] by auto
   2.336 -  thus "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> e" using real_convex_bound_le[OF yz uv] by auto 
   2.337 +  thus "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> e" using convex_bound_le[OF yz uv] by auto 
   2.338  qed
   2.339  
   2.340  lemma connected_ball:
     3.1 --- a/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Tue Apr 20 17:58:34 2010 +0200
     3.2 +++ b/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Mon May 03 14:35:10 2010 +0200
     3.3 @@ -8,7 +8,7 @@
     3.4  imports
     3.5    Complex_Main "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
     3.6    Finite_Cartesian_Product Infinite_Set Numeral_Type
     3.7 -  Inner_Product L2_Norm
     3.8 +  Inner_Product L2_Norm Convex
     3.9  uses "positivstellensatz.ML" ("normarith.ML")
    3.10  begin
    3.11  
    3.12 @@ -1411,40 +1411,6 @@
    3.13    done
    3.14  
    3.15  
    3.16 -lemma real_convex_bound_lt:
    3.17 -  assumes xa: "(x::real) < a" and ya: "y < a" and u: "0 <= u" and v: "0 <= v"
    3.18 -  and uv: "u + v = 1"
    3.19 -  shows "u * x + v * y < a"
    3.20 -proof-
    3.21 -  have uv': "u = 0 \<longrightarrow> v \<noteq> 0" using u v uv by arith
    3.22 -  have "a = a * (u + v)" unfolding uv  by simp
    3.23 -  hence th: "u * a + v * a = a" by (simp add: field_simps)
    3.24 -  from xa u have "u \<noteq> 0 \<Longrightarrow> u*x < u*a" by (simp add: mult_strict_left_mono)
    3.25 -  from ya v have "v \<noteq> 0 \<Longrightarrow> v * y < v * a" by (simp add: mult_strict_left_mono)
    3.26 -  from xa ya u v have "u * x + v * y < u * a + v * a"
    3.27 -    apply (cases "u = 0", simp_all add: uv')
    3.28 -    apply(rule mult_strict_left_mono)
    3.29 -    using uv' apply simp_all
    3.30 -
    3.31 -    apply (rule add_less_le_mono)
    3.32 -    apply(rule mult_strict_left_mono)
    3.33 -    apply simp_all
    3.34 -    apply (rule mult_left_mono)
    3.35 -    apply simp_all
    3.36 -    done
    3.37 -  thus ?thesis unfolding th .
    3.38 -qed
    3.39 -
    3.40 -lemma real_convex_bound_le:
    3.41 -  assumes xa: "(x::real) \<le> a" and ya: "y \<le> a" and u: "0 <= u" and v: "0 <= v"
    3.42 -  and uv: "u + v = 1"
    3.43 -  shows "u * x + v * y \<le> a"
    3.44 -proof-
    3.45 -  from xa ya u v have "u * x + v * y \<le> u * a + v * a" by (simp add: add_mono mult_left_mono)
    3.46 -  also have "\<dots> \<le> (u + v) * a" by (simp add: field_simps)
    3.47 -  finally show ?thesis unfolding uv by simp
    3.48 -qed
    3.49 -
    3.50  lemma infinite_enumerate: assumes fS: "infinite S"
    3.51    shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
    3.52  unfolding subseq_def
     4.1 --- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Tue Apr 20 17:58:34 2010 +0200
     4.2 +++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon May 03 14:35:10 2010 +0200
     4.3 @@ -6,7 +6,7 @@
     4.4  header {* Elementary topology in Euclidean space. *}
     4.5  
     4.6  theory Topology_Euclidean_Space
     4.7 -imports SEQ Euclidean_Space Product_Vector Glbs
     4.8 +imports SEQ Euclidean_Space Glbs
     4.9  begin
    4.10  
    4.11  subsection{* General notion of a topology *}
     5.1 --- a/src/HOL/Probability/Information.thy	Tue Apr 20 17:58:34 2010 +0200
     5.2 +++ b/src/HOL/Probability/Information.thy	Mon May 03 14:35:10 2010 +0200
     5.3 @@ -1,5 +1,5 @@
     5.4  theory Information
     5.5 -imports Probability_Space Product_Measure
     5.6 +imports Probability_Space Product_Measure "../Multivariate_Analysis/Convex"
     5.7  begin
     5.8  
     5.9  lemma pos_neg_part_abs:
    5.10 @@ -699,338 +699,6 @@
    5.11  qed
    5.12  (* --------------- upper bound on entropy for a rv ------------------------- *)
    5.13  
    5.14 -definition convex_set :: "real set \<Rightarrow> bool"
    5.15 -where
    5.16 -  "convex_set C \<equiv> (\<forall> x y \<mu>. x \<in> C \<and> y \<in> C \<and> 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow> \<mu> * x + (1 - \<mu>) * y \<in> C)"
    5.17 -
    5.18 -lemma pos_is_convex:
    5.19 -  shows "convex_set {0 <..}"
    5.20 -unfolding convex_set_def
    5.21 -proof safe
    5.22 -  fix x y \<mu> :: real
    5.23 -  assume asms: "\<mu> \<ge> 0" "\<mu> \<le> 1" "x > 0" "y > 0"
    5.24 -  { assume "\<mu> = 0"
    5.25 -    hence "\<mu> * x + (1 - \<mu>) * y = y" by simp
    5.26 -    hence "\<mu> * x + (1 - \<mu>) * y > 0" using asms by simp }
    5.27 -  moreover
    5.28 -  { assume "\<mu> = 1"
    5.29 -    hence "\<mu> * x + (1 - \<mu>) * y > 0" using asms by simp }
    5.30 -  moreover
    5.31 -  { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
    5.32 -    hence "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
    5.33 -    hence "\<mu> * x + (1 - \<mu>) * y > 0" using asms
    5.34 -      apply (subst add_nonneg_pos[of "\<mu> * x" "(1 - \<mu>) * y"])
    5.35 -      using real_mult_order by auto fastsimp }
    5.36 -  ultimately show "\<mu> * x + (1 - \<mu>) * y > 0" using assms by blast
    5.37 -qed
    5.38 -
    5.39 -definition convex_fun :: "(real \<Rightarrow> real) \<Rightarrow> real set \<Rightarrow> bool"
    5.40 -where
    5.41 -  "convex_fun f C \<equiv> (\<forall> x y \<mu>. convex_set C \<and> (x \<in> C \<and> y \<in> C \<and> 0 \<le> \<mu> \<and> \<mu> \<le> 1 
    5.42 -                   \<longrightarrow> f (\<mu> * x + (1 - \<mu>) * y) \<le> \<mu> * f x + (1 - \<mu>) * f y))"
    5.43 -
    5.44 -lemma pos_convex_function:
    5.45 -  fixes f :: "real \<Rightarrow> real"
    5.46 -  assumes "convex_set C"
    5.47 -  assumes leq: "\<And> x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
    5.48 -  shows "convex_fun f C"
    5.49 -unfolding convex_fun_def
    5.50 -using assms
    5.51 -proof safe
    5.52 -  fix x y \<mu> :: real
    5.53 -  let ?x = "\<mu> * x + (1 - \<mu>) * y"
    5.54 -  assume asm: "convex_set C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
    5.55 -  hence "1 - \<mu> \<ge> 0" by auto
    5.56 -  hence xpos: "?x \<in> C" using asm unfolding convex_set_def by auto
    5.57 -  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) 
    5.58 -            \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
    5.59 -    using add_mono[OF mult_mono1[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
    5.60 -      mult_mono1[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
    5.61 -  hence "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
    5.62 -    by (auto simp add:field_simps)
    5.63 -  thus "\<mu> * f x + (1 - \<mu>) * f y \<ge> f ?x" by simp
    5.64 -qed
    5.65 -
    5.66 -lemma atMostAtLeast_subset_convex:
    5.67 -  assumes "convex_set C"
    5.68 -  assumes "x \<in> C" "y \<in> C" "x < y"
    5.69 -  shows "{x .. y} \<subseteq> C"
    5.70 -proof safe
    5.71 -  fix z assume zasm: "z \<in> {x .. y}"
    5.72 -  { assume asm: "x < z" "z < y"
    5.73 -    let "?\<mu>" = "(y - z) / (y - x)"
    5.74 -    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add:field_simps)
    5.75 -    hence comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C" 
    5.76 -      using assms[unfolded convex_set_def] by blast
    5.77 -    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
    5.78 -      by (auto simp add:field_simps)
    5.79 -    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
    5.80 -      using assms unfolding add_divide_distrib by (auto simp:field_simps)
    5.81 -    also have "\<dots> = z" 
    5.82 -      using assms by (auto simp:field_simps)
    5.83 -    finally have "z \<in> C"
    5.84 -      using comb by auto } note less = this
    5.85 -  show "z \<in> C" using zasm less assms
    5.86 -    unfolding atLeastAtMost_iff le_less by auto
    5.87 -qed
    5.88 -
    5.89 -lemma f''_imp_f':
    5.90 -  fixes f :: "real \<Rightarrow> real"
    5.91 -  assumes "convex_set C"
    5.92 -  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
    5.93 -  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
    5.94 -  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
    5.95 -  assumes "x \<in> C" "y \<in> C"
    5.96 -  shows "f' x * (y - x) \<le> f y - f x"
    5.97 -using assms
    5.98 -proof -
    5.99 -  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "y > x"
   5.100 -    hence ge: "y - x > 0" "y - x \<ge> 0" by auto
   5.101 -    from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   5.102 -    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   5.103 -      using subsetD[OF atMostAtLeast_subset_convex[OF `convex_set C` `x \<in> C` `y \<in> C` `x < y`],
   5.104 -        THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   5.105 -      by auto
   5.106 -    hence "z1 \<in> C" using atMostAtLeast_subset_convex
   5.107 -      `convex_set C` `x \<in> C` `y \<in> C` `x < y` by fastsimp
   5.108 -    from z1 have z1': "f x - f y = (x - y) * f' z1"
   5.109 -      by (simp add:field_simps)
   5.110 -    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   5.111 -      using subsetD[OF atMostAtLeast_subset_convex[OF `convex_set C` `x \<in> C` `z1 \<in> C` `x < z1`],
   5.112 -        THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   5.113 -      by auto
   5.114 -    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   5.115 -      using subsetD[OF atMostAtLeast_subset_convex[OF `convex_set C` `z1 \<in> C` `y \<in> C` `z1 < y`],
   5.116 -        THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   5.117 -      by auto
   5.118 -    have "f' y - (f x - f y) / (x - y) = f' y - f' z1" 
   5.119 -      using asm z1' by auto
   5.120 -    also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
   5.121 -    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
   5.122 -    have A': "y - z1 \<ge> 0" using z1 by auto
   5.123 -    have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
   5.124 -      `convex_set C` `x \<in> C` `z1 \<in> C` `x < z1` by fastsimp
   5.125 -    hence B': "f'' z3 \<ge> 0" using assms by auto
   5.126 -    from A' B' have "(y - z1) * f'' z3 \<ge> 0" using mult_nonneg_nonneg by auto
   5.127 -    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
   5.128 -    from mult_right_mono_neg[OF this le(2)]
   5.129 -    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   5.130 -      unfolding diff_def using real_add_mult_distrib by auto
   5.131 -    hence "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   5.132 -    hence res: "f' y * (x - y) \<le> f x - f y" by auto
   5.133 -    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   5.134 -      using asm z1 by auto
   5.135 -    also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
   5.136 -    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
   5.137 -    have A: "z1 - x \<ge> 0" using z1 by auto
   5.138 -    have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
   5.139 -      `convex_set C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastsimp
   5.140 -    hence B: "f'' z2 \<ge> 0" using assms by auto
   5.141 -    from A B have "(z1 - x) * f'' z2 \<ge> 0" using mult_nonneg_nonneg by auto
   5.142 -    from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
   5.143 -    from mult_right_mono[OF this ge(2)]
   5.144 -    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)" 
   5.145 -      unfolding diff_def using real_add_mult_distrib by auto
   5.146 -    hence "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   5.147 -    hence "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   5.148 -      using res by auto } note less_imp = this
   5.149 -  { fix x y :: real assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   5.150 -    hence"f y - f x \<ge> f' x * (y - x)"
   5.151 -    unfolding neq_iff apply safe
   5.152 -    using less_imp by auto } note neq_imp = this
   5.153 -  moreover
   5.154 -  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "x = y"
   5.155 -    hence "f y - f x \<ge> f' x * (y - x)" by auto }
   5.156 -  ultimately show ?thesis using assms by blast
   5.157 -qed
   5.158 -
   5.159 -lemma f''_ge0_imp_convex:
   5.160 -  fixes f :: "real \<Rightarrow> real"
   5.161 -  assumes conv: "convex_set C"
   5.162 -  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   5.163 -  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   5.164 -  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   5.165 -  shows "convex_fun f C"
   5.166 -using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastsimp
   5.167 -
   5.168 -lemma minus_log_convex:
   5.169 -  fixes b :: real
   5.170 -  assumes "b > 1"
   5.171 -  shows "convex_fun (\<lambda> x. - log b x) {0 <..}"
   5.172 -proof -
   5.173 -  have "\<And> z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   5.174 -  hence f': "\<And> z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   5.175 -    using DERIV_minus by auto
   5.176 -  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   5.177 -    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   5.178 -  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   5.179 -  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   5.180 -    by auto
   5.181 -  hence f''0: "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   5.182 -    unfolding inverse_eq_divide by (auto simp add:real_mult_assoc)
   5.183 -  have f''_ge0: "\<And> z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   5.184 -    using `b > 1` by (auto intro!:less_imp_le simp add:divide_pos_pos[of 1] real_mult_order)
   5.185 -  from f''_ge0_imp_convex[OF pos_is_convex, 
   5.186 -    unfolded greaterThan_iff, OF f' f''0 f''_ge0]
   5.187 -  show ?thesis by auto
   5.188 -qed
   5.189 -
   5.190 -lemma setsum_nonneg_0:
   5.191 -  fixes f :: "'a \<Rightarrow> real"
   5.192 -  assumes "finite s"
   5.193 -  assumes "\<And> i. i \<in> s \<Longrightarrow> f i \<ge> 0"
   5.194 -  assumes "(\<Sum> i \<in> s. f i) = 0"
   5.195 -  assumes "i \<in> s"
   5.196 -  shows "f i = 0"
   5.197 -proof -
   5.198 -  { assume asm: "f i > 0"
   5.199 -    from assms have "\<forall> j \<in> s - {i}. f j \<ge> 0" by auto
   5.200 -    from setsum_nonneg[of "s - {i}" f, OF this]
   5.201 -    have "(\<Sum> j \<in> s - {i}. f j) \<ge> 0" by simp
   5.202 -    hence "(\<Sum> j \<in> s - {i}. f j) + f i > 0" using asm by auto
   5.203 -    from this setsum.remove[of s i f, OF `finite s` `i \<in> s`]
   5.204 -    have "(\<Sum> j \<in> s. f j) > 0" by auto
   5.205 -    hence "False" using assms by auto }
   5.206 -  thus ?thesis using assms by fastsimp
   5.207 -qed
   5.208 -
   5.209 -lemma setsum_nonneg_leq_1:
   5.210 -  fixes f :: "'a \<Rightarrow> real"
   5.211 -  assumes "finite s"
   5.212 -  assumes "\<And> i. i \<in> s \<Longrightarrow> f i \<ge> 0"
   5.213 -  assumes "(\<Sum> i \<in> s. f i) = 1"
   5.214 -  assumes "i \<in> s"
   5.215 -  shows "f i \<le> 1"
   5.216 -proof -
   5.217 -  { assume asm: "f i > 1"
   5.218 -    from assms have "\<forall> j \<in> s - {i}. f j \<ge> 0" by auto
   5.219 -    from setsum_nonneg[of "s - {i}" f, OF this]
   5.220 -    have "(\<Sum> j \<in> s - {i}. f j) \<ge> 0" by simp
   5.221 -    hence "(\<Sum> j \<in> s - {i}. f j) + f i > 1" using asm by auto
   5.222 -    from this setsum.remove[of s i f, OF `finite s` `i \<in> s`]
   5.223 -    have "(\<Sum> j \<in> s. f j) > 1" by auto
   5.224 -    hence "False" using assms by auto }
   5.225 -  thus ?thesis using assms by fastsimp
   5.226 -qed
   5.227 -
   5.228 -lemma convex_set_setsum:
   5.229 -  assumes "finite s" "s \<noteq> {}"
   5.230 -  assumes "convex_set C"
   5.231 -  assumes "(\<Sum> i \<in> s. a i) = 1"
   5.232 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   5.233 -  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
   5.234 -  shows "(\<Sum> j \<in> s. a j * y j) \<in> C"
   5.235 -using assms
   5.236 -proof (induct s arbitrary:a rule:finite_ne_induct)
   5.237 -  case (singleton i) note asms = this
   5.238 -  hence "a i = 1" by auto
   5.239 -  thus ?case using asms by auto
   5.240 -next
   5.241 -  case (insert i s) note asms = this
   5.242 -  { assume "a i = 1"
   5.243 -    hence "(\<Sum> j \<in> s. a j) = 0"
   5.244 -      using asms by auto
   5.245 -    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0" 
   5.246 -      using setsum_nonneg_0 asms by fastsimp
   5.247 -    hence ?case using asms by auto }
   5.248 -  moreover
   5.249 -  { assume asm: "a i \<noteq> 1"
   5.250 -    from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   5.251 -    have fis: "finite (insert i s)" using asms by auto
   5.252 -    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_1[of "insert i s" a] asms by simp
   5.253 -    hence "a i < 1" using asm by auto
   5.254 -    hence i0: "1 - a i > 0" by auto
   5.255 -    let "?a j" = "a j / (1 - a i)"
   5.256 -    { fix j assume "j \<in> s"
   5.257 -      hence "?a j \<ge> 0" 
   5.258 -        using i0 asms divide_nonneg_pos 
   5.259 -        by fastsimp } note a_nonneg = this
   5.260 -    have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   5.261 -    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastsimp
   5.262 -    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   5.263 -    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding divide.setsum by simp
   5.264 -    from this asms
   5.265 -    have "(\<Sum>j\<in>s. ?a j * y j) \<in> C" using a_nonneg by fastsimp
   5.266 -    hence "a i * y i + (1 - a i) * (\<Sum> j \<in> s. ?a j * y j) \<in> C"
   5.267 -      using asms[unfolded convex_set_def, rule_format] yai ai1 by auto
   5.268 -    hence "a i * y i + (\<Sum> j \<in> s. (1 - a i) * (?a j * y j)) \<in> C"
   5.269 -      using mult_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j * y j" s] by auto
   5.270 -    hence "a i * y i + (\<Sum> j \<in> s. a j * y j) \<in> C" using i0 by auto
   5.271 -    hence ?case using setsum.insert asms by auto }
   5.272 -  ultimately show ?case by auto
   5.273 -qed
   5.274 -
   5.275 -lemma convex_fun_setsum:
   5.276 -  fixes a :: "'a \<Rightarrow> real"
   5.277 -  assumes "finite s" "s \<noteq> {}"
   5.278 -  assumes "convex_fun f C"
   5.279 -  assumes "(\<Sum> i \<in> s. a i) = 1"
   5.280 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   5.281 -  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
   5.282 -  shows "f (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   5.283 -using assms
   5.284 -proof (induct s arbitrary:a rule:finite_ne_induct)
   5.285 -  case (singleton i)
   5.286 -  hence ai: "a i = 1" by auto
   5.287 -  thus ?case by auto
   5.288 -next
   5.289 -  case (insert i s) note asms = this
   5.290 -  hence "convex_fun f C" by simp
   5.291 -  from this[unfolded convex_fun_def, rule_format]
   5.292 -  have conv: "\<And> x y \<mu>. \<lbrakk>x \<in> C; y \<in> C; 0 \<le> \<mu>; \<mu> \<le> 1\<rbrakk>
   5.293 -  \<Longrightarrow> f (\<mu> * x + (1 - \<mu>) * y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   5.294 -    by simp
   5.295 -  { assume "a i = 1"
   5.296 -    hence "(\<Sum> j \<in> s. a j) = 0"
   5.297 -      using asms by auto
   5.298 -    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0" 
   5.299 -      using setsum_nonneg_0 asms by fastsimp
   5.300 -    hence ?case using asms by auto }
   5.301 -  moreover
   5.302 -  { assume asm: "a i \<noteq> 1"
   5.303 -    from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   5.304 -    have fis: "finite (insert i s)" using asms by auto
   5.305 -    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_1[of "insert i s" a] asms by simp
   5.306 -    hence "a i < 1" using asm by auto
   5.307 -    hence i0: "1 - a i > 0" by auto
   5.308 -    let "?a j" = "a j / (1 - a i)"
   5.309 -    { fix j assume "j \<in> s"
   5.310 -      hence "?a j \<ge> 0" 
   5.311 -        using i0 asms divide_nonneg_pos 
   5.312 -        by fastsimp } note a_nonneg = this
   5.313 -    have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   5.314 -    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastsimp
   5.315 -    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   5.316 -    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding divide.setsum by simp
   5.317 -    have "convex_set C" using asms unfolding convex_fun_def by auto
   5.318 -    hence asum: "(\<Sum> j \<in> s. ?a j * y j) \<in> C"
   5.319 -      using asms convex_set_setsum[OF `finite s` `s \<noteq> {}` 
   5.320 -        `convex_set C` a1 a_nonneg] by auto
   5.321 -    have asum_le: "f (\<Sum> j \<in> s. ?a j * y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   5.322 -      using a_nonneg a1 asms by blast
   5.323 -    have "f (\<Sum> j \<in> insert i s. a j * y j) = f ((\<Sum> j \<in> s. a j * y j) + a i * y i)"
   5.324 -      using setsum.insert[of s i "\<lambda> j. a j * y j", OF `finite s` `i \<notin> s`] asms 
   5.325 -      by (auto simp only:add_commute)
   5.326 -    also have "\<dots> = f ((1 - a i) * (\<Sum> j \<in> s. a j * y j) / (1 - a i) + a i * y i)"
   5.327 -      using i0 by auto
   5.328 -    also have "\<dots> = f ((1 - a i) * (\<Sum> j \<in> s. a j * y j / (1 - a i)) + a i * y i)"
   5.329 -      unfolding divide.setsum[of "\<lambda> j. a j * y j" s "1 - a i", symmetric] by auto
   5.330 -    also have "\<dots> = f ((1 - a i) * (\<Sum> j \<in> s. ?a j * y j) + a i * y i)" by auto
   5.331 -    also have "\<dots> \<le> (1 - a i) * f ((\<Sum> j \<in> s. ?a j * y j)) + a i * f (y i)"
   5.332 -      using conv[of "y i" "(\<Sum> j \<in> s. ?a j * y j)" "a i", OF yai(1) asum yai(2) ai1]
   5.333 -      by (auto simp only:add_commute)
   5.334 -    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   5.335 -      using add_right_mono[OF mult_left_mono[of _ _ "1 - a i", 
   5.336 -        OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
   5.337 -    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   5.338 -      unfolding mult_right.setsum[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
   5.339 -    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
   5.340 -    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
   5.341 -    finally have "f (\<Sum> j \<in> insert i s. a j * y j) \<le> (\<Sum> j \<in> insert i s. a j * f (y j))"
   5.342 -      by simp }
   5.343 -  ultimately show ?case by auto
   5.344 -qed
   5.345 -
   5.346  lemma log_setsum:
   5.347    assumes "finite s" "s \<noteq> {}"
   5.348    assumes "b > 1"
   5.349 @@ -1039,10 +707,10 @@
   5.350    assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> {0 <..}"
   5.351    shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
   5.352  proof -
   5.353 -  have "convex_fun (\<lambda> x. - log b x) {0 <..}"
   5.354 +  have "convex_on {0 <..} (\<lambda> x. - log b x)"
   5.355      by (rule minus_log_convex[OF `b > 1`])
   5.356    hence "- log b (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * - log b (y i))"
   5.357 -    using convex_fun_setsum assms by blast
   5.358 +    using convex_on_setsum[of _ _ "\<lambda> x. - log b x"] assms pos_is_convex by fastsimp
   5.359    thus ?thesis by (auto simp add:setsum_negf le_imp_neg_le)
   5.360  qed
   5.361