src/HOL/Library/Convex.thy
changeset 56796 9f84219715a7
parent 56571 f4635657d66f
child 57418 6ab1c7cb0b8d
     1.1 --- a/src/HOL/Library/Convex.thy	Tue Apr 29 21:54:26 2014 +0200
     1.2 +++ b/src/HOL/Library/Convex.thy	Tue Apr 29 22:50:55 2014 +0200
     1.3 @@ -29,11 +29,18 @@
     1.4    (is "_ \<longleftrightarrow> ?alt")
     1.5  proof
     1.6    assume alt[rule_format]: ?alt
     1.7 -  { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
     1.8 +  {
     1.9 +    fix x y and u v :: real
    1.10 +    assume mem: "x \<in> s" "y \<in> s"
    1.11      assume "0 \<le> u" "0 \<le> v"
    1.12 -    moreover assume "u + v = 1" then have "u = 1 - v" by auto
    1.13 -    ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
    1.14 -  then show "convex s" unfolding convex_def by auto
    1.15 +    moreover
    1.16 +    assume "u + v = 1"
    1.17 +    then have "u = 1 - v" by auto
    1.18 +    ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
    1.19 +      using alt[OF mem] by auto
    1.20 +  }
    1.21 +  then show "convex s"
    1.22 +    unfolding convex_def by auto
    1.23  qed (auto simp: convex_def)
    1.24  
    1.25  lemma mem_convex:
    1.26 @@ -50,7 +57,7 @@
    1.27  lemma convex_UNIV[intro]: "convex UNIV"
    1.28    unfolding convex_def by auto
    1.29  
    1.30 -lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
    1.31 +lemma convex_Inter: "(\<forall>s\<in>f. convex s) \<Longrightarrow> convex(\<Inter> f)"
    1.32    unfolding convex_def by auto
    1.33  
    1.34  lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
    1.35 @@ -68,13 +75,16 @@
    1.36  
    1.37  lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    1.38  proof -
    1.39 -  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    1.40 -  show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    1.41 +  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
    1.42 +    by auto
    1.43 +  show ?thesis
    1.44 +    unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    1.45  qed
    1.46  
    1.47  lemma convex_hyperplane: "convex {x. inner a x = b}"
    1.48  proof -
    1.49 -  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    1.50 +  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
    1.51 +    by auto
    1.52    show ?thesis using convex_halfspace_le convex_halfspace_ge
    1.53      by (auto intro!: convex_Int simp: *)
    1.54  qed
    1.55 @@ -115,8 +125,11 @@
    1.56  
    1.57  lemma convex_setsum:
    1.58    fixes C :: "'a::real_vector set"
    1.59 -  assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
    1.60 -  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
    1.61 +  assumes "finite s"
    1.62 +    and "convex C"
    1.63 +    and "(\<Sum> i \<in> s. a i) = 1"
    1.64 +  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
    1.65 +    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
    1.66    shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
    1.67    using assms(1,3,4,5)
    1.68  proof (induct arbitrary: a set: finite)
    1.69 @@ -124,18 +137,27 @@
    1.70    then show ?case by simp
    1.71  next
    1.72    case (insert i s) note IH = this(3)
    1.73 -  have "a i + setsum a s = 1" and "0 \<le> a i" and "\<forall>j\<in>s. 0 \<le> a j" and "y i \<in> C" and "\<forall>j\<in>s. y j \<in> C"
    1.74 +  have "a i + setsum a s = 1"
    1.75 +    and "0 \<le> a i"
    1.76 +    and "\<forall>j\<in>s. 0 \<le> a j"
    1.77 +    and "y i \<in> C"
    1.78 +    and "\<forall>j\<in>s. y j \<in> C"
    1.79      using insert.hyps(1,2) insert.prems by simp_all
    1.80 -  then have "0 \<le> setsum a s" by (simp add: setsum_nonneg)
    1.81 +  then have "0 \<le> setsum a s"
    1.82 +    by (simp add: setsum_nonneg)
    1.83    have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
    1.84    proof (cases)
    1.85      assume z: "setsum a s = 0"
    1.86 -    with `a i + setsum a s = 1` have "a i = 1" by simp
    1.87 -    from setsum_nonneg_0 [OF `finite s` _ z] `\<forall>j\<in>s. 0 \<le> a j` have "\<forall>j\<in>s. a j = 0" by simp
    1.88 -    show ?thesis using `a i = 1` and `\<forall>j\<in>s. a j = 0` and `y i \<in> C` by simp
    1.89 +    with `a i + setsum a s = 1` have "a i = 1"
    1.90 +      by simp
    1.91 +    from setsum_nonneg_0 [OF `finite s` _ z] `\<forall>j\<in>s. 0 \<le> a j` have "\<forall>j\<in>s. a j = 0"
    1.92 +      by simp
    1.93 +    show ?thesis using `a i = 1` and `\<forall>j\<in>s. a j = 0` and `y i \<in> C`
    1.94 +      by simp
    1.95    next
    1.96      assume nz: "setsum a s \<noteq> 0"
    1.97 -    with `0 \<le> setsum a s` have "0 < setsum a s" by simp
    1.98 +    with `0 \<le> setsum a s` have "0 < setsum a s"
    1.99 +      by simp
   1.100      then have "(\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
   1.101        using `\<forall>j\<in>s. 0 \<le> a j` and `\<forall>j\<in>s. y j \<in> C`
   1.102        by (simp add: IH setsum_divide_distrib [symmetric])
   1.103 @@ -143,9 +165,11 @@
   1.104        and `0 \<le> setsum a s` and `a i + setsum a s = 1`
   1.105      have "a i *\<^sub>R y i + setsum a s *\<^sub>R (\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
   1.106        by (rule convexD)
   1.107 -    then show ?thesis by (simp add: scaleR_setsum_right nz)
   1.108 +    then show ?thesis
   1.109 +      by (simp add: scaleR_setsum_right nz)
   1.110    qed
   1.111 -  then show ?case using `finite s` and `i \<notin> s` by simp
   1.112 +  then show ?case using `finite s` and `i \<notin> s`
   1.113 +    by simp
   1.114  qed
   1.115  
   1.116  lemma convex:
   1.117 @@ -159,18 +183,22 @@
   1.118      "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   1.119      "setsum u {1..k} = 1"
   1.120    from this convex_setsum[of "{1 .. k}" s]
   1.121 -  show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s" by auto
   1.122 +  show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
   1.123 +    by auto
   1.124  next
   1.125    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.126      \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   1.127 -  { fix \<mu> :: real
   1.128 +  {
   1.129 +    fix \<mu> :: real
   1.130      fix x y :: 'a
   1.131      assume xy: "x \<in> s" "y \<in> s"
   1.132      assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.133      let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   1.134      let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   1.135 -    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
   1.136 -    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   1.137 +    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
   1.138 +      by auto
   1.139 +    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
   1.140 +      by simp
   1.141      then have "setsum ?u {1 .. 2} = 1"
   1.142        using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   1.143        by auto
   1.144 @@ -179,10 +207,13 @@
   1.145      have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   1.146        using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   1.147      from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   1.148 -    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
   1.149 -    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute)
   1.150 +    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.151 +      by auto
   1.152 +    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
   1.153 +      using s by (auto simp:add_commute)
   1.154    }
   1.155 -  then show "convex s" unfolding convex_alt by auto
   1.156 +  then show "convex s"
   1.157 +    unfolding convex_alt by auto
   1.158  qed
   1.159  
   1.160  
   1.161 @@ -193,42 +224,48 @@
   1.162  proof safe
   1.163    fix t
   1.164    fix u :: "'a \<Rightarrow> real"
   1.165 -  assume "convex s" "finite t"
   1.166 -    "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   1.167 +  assume "convex s"
   1.168 +    and "finite t"
   1.169 +    and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   1.170    then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.171      using convex_setsum[of t s u "\<lambda> x. x"] by auto
   1.172  next
   1.173 -  assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
   1.174 -    \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.175 +  assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
   1.176 +    setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.177    show "convex s"
   1.178      unfolding convex_alt
   1.179    proof safe
   1.180      fix x y
   1.181      fix \<mu> :: real
   1.182      assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.183 -    { assume "x \<noteq> y"
   1.184 +    {
   1.185 +      assume "x \<noteq> y"
   1.186        then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.187          using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
   1.188 -          asm by auto }
   1.189 +          asm by auto
   1.190 +    }
   1.191      moreover
   1.192 -    { assume "x = y"
   1.193 +    {
   1.194 +      assume "x = y"
   1.195        then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.196          using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
   1.197 -          asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
   1.198 -    ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
   1.199 +          asm by (auto simp: field_simps real_vector.scale_left_diff_distrib)
   1.200 +    }
   1.201 +    ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.202 +      by blast
   1.203    qed
   1.204  qed
   1.205  
   1.206  lemma convex_finite:
   1.207    assumes "finite s"
   1.208 -  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   1.209 -                      \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   1.210 +  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   1.211    unfolding convex_explicit
   1.212  proof safe
   1.213    fix t u
   1.214    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.215      and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   1.216 -  have *: "s \<inter> t = t" using as(2) by auto
   1.217 +  have *: "s \<inter> t = t"
   1.218 +    using as(2) by auto
   1.219    have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   1.220      by simp
   1.221    show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.222 @@ -236,6 +273,7 @@
   1.223     by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   1.224  qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   1.225  
   1.226 +
   1.227  subsection {* Functions that are convex on a set *}
   1.228  
   1.229  definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   1.230 @@ -246,11 +284,13 @@
   1.231    unfolding convex_on_def by auto
   1.232  
   1.233  lemma convex_on_add [intro]:
   1.234 -  assumes "convex_on s f" "convex_on s g"
   1.235 +  assumes "convex_on s f"
   1.236 +    and "convex_on s g"
   1.237    shows "convex_on s (\<lambda>x. f x + g x)"
   1.238  proof -
   1.239 -  { fix x y
   1.240 -    assume "x\<in>s" "y\<in>s"
   1.241 +  {
   1.242 +    fix x y
   1.243 +    assume "x \<in> s" "y \<in> s"
   1.244      moreover
   1.245      fix u v :: real
   1.246      assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.247 @@ -260,13 +300,16 @@
   1.248      then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   1.249        by (simp add: field_simps)
   1.250    }
   1.251 -  then show ?thesis unfolding convex_on_def by auto
   1.252 +  then show ?thesis
   1.253 +    unfolding convex_on_def by auto
   1.254  qed
   1.255  
   1.256  lemma convex_on_cmul [intro]:
   1.257 -  assumes "0 \<le> (c::real)" "convex_on s f"
   1.258 +  fixes c :: real
   1.259 +  assumes "0 \<le> c"
   1.260 +    and "convex_on s f"
   1.261    shows "convex_on s (\<lambda>x. c * f x)"
   1.262 -proof-
   1.263 +proof -
   1.264    have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   1.265      by (simp add: field_simps)
   1.266    show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   1.267 @@ -274,13 +317,19 @@
   1.268  qed
   1.269  
   1.270  lemma convex_lower:
   1.271 -  assumes "convex_on s f"  "x\<in>s"  "y \<in> s"  "0 \<le> u"  "0 \<le> v"  "u + v = 1"
   1.272 +  assumes "convex_on s f"
   1.273 +    and "x \<in> s"
   1.274 +    and "y \<in> s"
   1.275 +    and "0 \<le> u"
   1.276 +    and "0 \<le> v"
   1.277 +    and "u + v = 1"
   1.278    shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   1.279 -proof-
   1.280 +proof -
   1.281    let ?m = "max (f x) (f y)"
   1.282    have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   1.283      using assms(4,5) by (auto simp add: mult_left_mono add_mono)
   1.284 -  also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
   1.285 +  also have "\<dots> = max (f x) (f y)"
   1.286 +    using assms(6) unfolding distrib[symmetric] by auto
   1.287    finally show ?thesis
   1.288      using assms unfolding convex_on_def by fastforce
   1.289  qed
   1.290 @@ -290,11 +339,13 @@
   1.291    shows "convex_on s (\<lambda>x. dist a x)"
   1.292  proof (auto simp add: convex_on_def dist_norm)
   1.293    fix x y
   1.294 -  assume "x\<in>s" "y\<in>s"
   1.295 +  assume "x \<in> s" "y \<in> s"
   1.296    fix u v :: real
   1.297 -  assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.298 +  assume "0 \<le> u"
   1.299 +  assume "0 \<le> v"
   1.300 +  assume "u + v = 1"
   1.301    have "a = u *\<^sub>R a + v *\<^sub>R a"
   1.302 -    unfolding scaleR_left_distrib[symmetric] and `u+v=1` by simp
   1.303 +    unfolding scaleR_left_distrib[symmetric] and `u + v = 1` by simp
   1.304    then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   1.305      by (auto simp add: algebra_simps)
   1.306    show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   1.307 @@ -306,7 +357,9 @@
   1.308  subsection {* Arithmetic operations on sets preserve convexity. *}
   1.309  
   1.310  lemma convex_linear_image:
   1.311 -  assumes "linear f" and "convex s" shows "convex (f ` s)"
   1.312 +  assumes "linear f"
   1.313 +    and "convex s"
   1.314 +  shows "convex (f ` s)"
   1.315  proof -
   1.316    interpret f: linear f by fact
   1.317    from `convex s` show "convex (f ` s)"
   1.318 @@ -314,7 +367,9 @@
   1.319  qed
   1.320  
   1.321  lemma convex_linear_vimage:
   1.322 -  assumes "linear f" and "convex s" shows "convex (f -` s)"
   1.323 +  assumes "linear f"
   1.324 +    and "convex s"
   1.325 +  shows "convex (f -` s)"
   1.326  proof -
   1.327    interpret f: linear f by fact
   1.328    from `convex s` show "convex (f -` s)"
   1.329 @@ -322,21 +377,28 @@
   1.330  qed
   1.331  
   1.332  lemma convex_scaling:
   1.333 -  assumes "convex s" shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   1.334 +  assumes "convex s"
   1.335 +  shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   1.336  proof -
   1.337 -  have "linear (\<lambda>x. c *\<^sub>R x)" by (simp add: linearI scaleR_add_right)
   1.338 -  then show ?thesis using `convex s` by (rule convex_linear_image)
   1.339 +  have "linear (\<lambda>x. c *\<^sub>R x)"
   1.340 +    by (simp add: linearI scaleR_add_right)
   1.341 +  then show ?thesis
   1.342 +    using `convex s` by (rule convex_linear_image)
   1.343  qed
   1.344  
   1.345  lemma convex_negations:
   1.346 -  assumes "convex s" shows "convex ((\<lambda>x. - x) ` s)"
   1.347 +  assumes "convex s"
   1.348 +  shows "convex ((\<lambda>x. - x) ` s)"
   1.349  proof -
   1.350 -  have "linear (\<lambda>x. - x)" by (simp add: linearI)
   1.351 -  then show ?thesis using `convex s` by (rule convex_linear_image)
   1.352 +  have "linear (\<lambda>x. - x)"
   1.353 +    by (simp add: linearI)
   1.354 +  then show ?thesis
   1.355 +    using `convex s` by (rule convex_linear_image)
   1.356  qed
   1.357  
   1.358  lemma convex_sums:
   1.359 -  assumes "convex s" and "convex t"
   1.360 +  assumes "convex s"
   1.361 +    and "convex t"
   1.362    shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   1.363  proof -
   1.364    have "linear (\<lambda>(x, y). x + y)"
   1.365 @@ -362,7 +424,8 @@
   1.366    assumes "convex s"
   1.367    shows "convex ((\<lambda>x. a + x) ` s)"
   1.368  proof -
   1.369 -  have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   1.370 +  have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s"
   1.371 +    by auto
   1.372    then show ?thesis
   1.373      using convex_sums[OF convex_singleton[of a] assms] by auto
   1.374  qed
   1.375 @@ -371,7 +434,8 @@
   1.376    assumes "convex s"
   1.377    shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   1.378  proof -
   1.379 -  have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   1.380 +  have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s"
   1.381 +    by auto
   1.382    then show ?thesis
   1.383      using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   1.384  qed
   1.385 @@ -381,18 +445,25 @@
   1.386  proof safe
   1.387    fix y x \<mu> :: real
   1.388    assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.389 -  { assume "\<mu> = 0"
   1.390 +  {
   1.391 +    assume "\<mu> = 0"
   1.392      then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   1.393 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.394 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp
   1.395 +  }
   1.396    moreover
   1.397 -  { assume "\<mu> = 1"
   1.398 -    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.399 +  {
   1.400 +    assume "\<mu> = 1"
   1.401 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp
   1.402 +  }
   1.403    moreover
   1.404 -  { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   1.405 +  {
   1.406 +    assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   1.407      then have "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   1.408      then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   1.409 -      by (auto simp add: add_pos_pos) }
   1.410 -  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
   1.411 +      by (auto simp add: add_pos_pos)
   1.412 +  }
   1.413 +  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
   1.414 +    using assms by fastforce
   1.415  qed
   1.416  
   1.417  lemma convex_on_setsum:
   1.418 @@ -415,25 +486,32 @@
   1.419    case (insert i s) note asms = this
   1.420    then have "convex_on C f" by simp
   1.421    from this[unfolded convex_on_def, rule_format]
   1.422 -  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1
   1.423 -      \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.424 +  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
   1.425 +      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.426      by simp
   1.427 -  { assume "a i = 1"
   1.428 +  {
   1.429 +    assume "a i = 1"
   1.430      then have "(\<Sum> j \<in> s. a j) = 0"
   1.431        using asms by auto
   1.432      then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   1.433        using setsum_nonneg_0[where 'b=real] asms by fastforce
   1.434 -    then have ?case using asms by auto }
   1.435 +    then have ?case using asms by auto
   1.436 +  }
   1.437    moreover
   1.438 -  { assume asm: "a i \<noteq> 1"
   1.439 +  {
   1.440 +    assume asm: "a i \<noteq> 1"
   1.441      from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   1.442      have fis: "finite (insert i s)" using asms by auto
   1.443      then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   1.444      then have "a i < 1" using asm by auto
   1.445      then have i0: "1 - a i > 0" by auto
   1.446      let ?a = "\<lambda>j. a j / (1 - a i)"
   1.447 -    { fix j assume "j \<in> s" with i0 asms have "?a j \<ge> 0"
   1.448 -        by fastforce }
   1.449 +    {
   1.450 +      fix j
   1.451 +      assume "j \<in> s"
   1.452 +      with i0 asms have "?a j \<ge> 0"
   1.453 +        by fastforce
   1.454 +    }
   1.455      note a_nonneg = this
   1.456      have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   1.457      then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   1.458 @@ -466,51 +544,66 @@
   1.459      also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
   1.460      also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
   1.461      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.462 -      by simp }
   1.463 +      by simp
   1.464 +  }
   1.465    ultimately show ?case by auto
   1.466  qed
   1.467  
   1.468  lemma convex_on_alt:
   1.469    fixes C :: "'a::real_vector set"
   1.470    assumes "convex C"
   1.471 -  shows "convex_on C f =
   1.472 -  (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
   1.473 -      \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   1.474 +  shows "convex_on C f \<longleftrightarrow>
   1.475 +    (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
   1.476 +      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   1.477  proof safe
   1.478    fix x y
   1.479    fix \<mu> :: real
   1.480    assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.481    from this[unfolded convex_on_def, rule_format]
   1.482 -  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.483 +  have "\<And>u v. 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   1.484 +    by auto
   1.485    from this[of "\<mu>" "1 - \<mu>", simplified] asms
   1.486 -  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   1.487 +  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.488 +    by auto
   1.489  next
   1.490 -  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.491 -  { fix x y
   1.492 +  assume asm: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
   1.493 +    f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.494 +  {
   1.495 +    fix x y
   1.496      fix u v :: real
   1.497      assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   1.498      then have[simp]: "1 - u = v" by auto
   1.499      from asm[rule_format, of x y u]
   1.500 -    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
   1.501 +    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
   1.502 +      using lasm by auto
   1.503    }
   1.504 -  then show "convex_on C f" unfolding convex_on_def by auto
   1.505 +  then show "convex_on C f"
   1.506 +    unfolding convex_on_def by auto
   1.507  qed
   1.508  
   1.509  lemma convex_on_diff:
   1.510    fixes f :: "real \<Rightarrow> real"
   1.511 -  assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
   1.512 +  assumes f: "convex_on I f"
   1.513 +    and I: "x \<in> I" "y \<in> I"
   1.514 +    and t: "x < t" "t < y"
   1.515    shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   1.516 -    "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.517 +    and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.518  proof -
   1.519    def a \<equiv> "(t - y) / (x - y)"
   1.520 -  with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
   1.521 +  with t have "0 \<le> a" "0 \<le> 1 - a"
   1.522 +    by (auto simp: field_simps)
   1.523    with f `x \<in> I` `y \<in> I` have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   1.524      by (auto simp: convex_on_def)
   1.525 -  have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps)
   1.526 -  also have "\<dots> = t" unfolding a_def using `x < t` `t < y` by simp
   1.527 -  finally have "f t \<le> a * f x + (1 - a) * f y" using cvx by simp
   1.528 -  also have "\<dots> = a * (f x - f y) + f y" by (simp add: field_simps)
   1.529 -  finally have "f t - f y \<le> a * (f x - f y)" by simp
   1.530 +  have "a * x + (1 - a) * y = a * (x - y) + y"
   1.531 +    by (simp add: field_simps)
   1.532 +  also have "\<dots> = t"
   1.533 +    unfolding a_def using `x < t` `t < y` by simp
   1.534 +  finally have "f t \<le> a * f x + (1 - a) * f y"
   1.535 +    using cvx by simp
   1.536 +  also have "\<dots> = a * (f x - f y) + f y"
   1.537 +    by (simp add: field_simps)
   1.538 +  finally have "f t - f y \<le> a * (f x - f y)"
   1.539 +    by simp
   1.540    with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   1.541      by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   1.542    with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.543 @@ -520,7 +613,7 @@
   1.544  lemma pos_convex_function:
   1.545    fixes f :: "real \<Rightarrow> real"
   1.546    assumes "convex C"
   1.547 -    and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.548 +    and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.549    shows "convex_on C f"
   1.550    unfolding convex_on_alt[OF assms(1)]
   1.551    using assms
   1.552 @@ -529,11 +622,13 @@
   1.553    let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.554    assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.555    then have "1 - \<mu> \<ge> 0" by auto
   1.556 -  then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
   1.557 -  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
   1.558 -            \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   1.559 +  then have xpos: "?x \<in> C"
   1.560 +    using asm unfolding convex_alt by fastforce
   1.561 +  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
   1.562 +      \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   1.563      using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
   1.564 -      mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
   1.565 +      mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]]
   1.566 +    by auto
   1.567    then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   1.568      by (auto simp add: field_simps)
   1.569    then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.570 @@ -547,9 +642,11 @@
   1.571    shows "{x .. y} \<subseteq> C"
   1.572  proof safe
   1.573    fix z assume zasm: "z \<in> {x .. y}"
   1.574 -  { assume asm: "x < z" "z < y"
   1.575 +  {
   1.576 +    assume asm: "x < z" "z < y"
   1.577      let ?\<mu> = "(y - z) / (y - x)"
   1.578 -    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
   1.579 +    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
   1.580 +      using assms asm by (auto simp add: field_simps)
   1.581      then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   1.582        using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   1.583        by (simp add: algebra_simps)
   1.584 @@ -560,7 +657,8 @@
   1.585      also have "\<dots> = z"
   1.586        using assms by (auto simp: field_simps)
   1.587      finally have "z \<in> C"
   1.588 -      using comb by auto }
   1.589 +      using comb by auto
   1.590 +  }
   1.591    note less = this
   1.592    show "z \<in> C" using zasm less assms
   1.593      unfolding atLeastAtMost_iff le_less by auto
   1.594 @@ -576,7 +674,8 @@
   1.595    shows "f' x * (y - x) \<le> f y - f x"
   1.596    using assms
   1.597  proof -
   1.598 -  { fix x y :: real
   1.599 +  {
   1.600 +    fix x y :: real
   1.601      assume asm: "x \<in> C" "y \<in> C" "y > x"
   1.602      then have ge: "y - x > 0" "y - x \<ge> 0" by auto
   1.603      from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   1.604 @@ -627,14 +726,18 @@
   1.605      then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   1.606      then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.607        using res by auto } note less_imp = this
   1.608 -  { fix x y :: real
   1.609 +  {
   1.610 +    fix x y :: real
   1.611      assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   1.612      then have"f y - f x \<ge> f' x * (y - x)"
   1.613 -    unfolding neq_iff using less_imp by auto } note neq_imp = this
   1.614 +    unfolding neq_iff using less_imp by auto
   1.615 +  }
   1.616    moreover
   1.617 -  { fix x y :: real
   1.618 +  {
   1.619 +    fix x y :: real
   1.620      assume asm: "x \<in> C" "y \<in> C" "x = y"
   1.621 -    then have "f y - f x \<ge> f' x * (y - x)" by auto }
   1.622 +    then have "f y - f x \<ge> f' x * (y - x)" by auto
   1.623 +  }
   1.624    ultimately show ?thesis using assms by blast
   1.625  qed
   1.626  
   1.627 @@ -645,14 +748,16 @@
   1.628      and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.629      and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.630    shows "convex_on C f"
   1.631 -using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
   1.632 +  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
   1.633 +  by fastforce
   1.634  
   1.635  lemma minus_log_convex:
   1.636    fixes b :: real
   1.637    assumes "b > 1"
   1.638    shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   1.639  proof -
   1.640 -  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   1.641 +  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
   1.642 +    using DERIV_log by auto
   1.643    then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   1.644      by (auto simp: DERIV_minus)
   1.645    have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   1.646 @@ -661,9 +766,10 @@
   1.647    have "\<And>z :: real. z > 0 \<Longrightarrow>
   1.648      DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   1.649      by auto
   1.650 -  then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.651 +  then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
   1.652 +    DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.653      unfolding inverse_eq_divide by (auto simp add: mult_assoc)
   1.654 -  have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.655 +  have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.656      using `b > 1` by (auto intro!:less_imp_le)
   1.657    from f''_ge0_imp_convex[OF pos_is_convex,
   1.658      unfolded greaterThan_iff, OF f' f''0 f''_ge0]