1 (* Title: HOL/Library/Convex.thy
2 Author: Armin Heller, TU Muenchen
3 Author: Johannes Hoelzl, TU Muenchen
6 header {* Convexity in real vector spaces *}
12 subsection {* Convexity. *}
14 definition convex :: "'a::real_vector set \<Rightarrow> bool"
15 where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
18 assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
20 using assms unfolding convex_def by fast
23 assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
24 shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
25 using assms unfolding convex_def by fast
28 "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)"
29 (is "_ \<longleftrightarrow> ?alt")
31 assume alt[rule_format]: ?alt
32 { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
33 assume "0 \<le> u" "0 \<le> v"
34 moreover assume "u + v = 1" then have "u = 1 - v" by auto
35 ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
36 then show "convex s" unfolding convex_def by auto
37 qed (auto simp: convex_def)
40 assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
41 shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
42 using assms unfolding convex_alt by auto
44 lemma convex_empty[intro]: "convex {}"
45 unfolding convex_def by simp
47 lemma convex_singleton[intro]: "convex {a}"
48 unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
50 lemma convex_UNIV[intro]: "convex UNIV"
51 unfolding convex_def by auto
53 lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
54 unfolding convex_def by auto
56 lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
57 unfolding convex_def by auto
59 lemma convex_INT: "\<forall>i\<in>A. convex (B i) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
60 unfolding convex_def by auto
62 lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
63 unfolding convex_def by auto
65 lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
67 by (auto simp: inner_add intro!: convex_bound_le)
69 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
71 have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
72 show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
75 lemma convex_hyperplane: "convex {x. inner a x = b}"
77 have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
78 show ?thesis using convex_halfspace_le convex_halfspace_ge
79 by (auto intro!: convex_Int simp: *)
82 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
84 by (auto simp: convex_bound_lt inner_add)
86 lemma convex_halfspace_gt: "convex {x. inner a x > b}"
87 using convex_halfspace_lt[of "-a" "-b"] by auto
89 lemma convex_real_interval:
91 shows "convex {a..}" and "convex {..b}"
92 and "convex {a<..}" and "convex {..<b}"
93 and "convex {a..b}" and "convex {a<..b}"
94 and "convex {a..<b}" and "convex {a<..<b}"
96 have "{a..} = {x. a \<le> inner 1 x}" by auto
97 then show 1: "convex {a..}" by (simp only: convex_halfspace_ge)
98 have "{..b} = {x. inner 1 x \<le> b}" by auto
99 then show 2: "convex {..b}" by (simp only: convex_halfspace_le)
100 have "{a<..} = {x. a < inner 1 x}" by auto
101 then show 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
102 have "{..<b} = {x. inner 1 x < b}" by auto
103 then show 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
104 have "{a..b} = {a..} \<inter> {..b}" by auto
105 then show "convex {a..b}" by (simp only: convex_Int 1 2)
106 have "{a<..b} = {a<..} \<inter> {..b}" by auto
107 then show "convex {a<..b}" by (simp only: convex_Int 3 2)
108 have "{a..<b} = {a..} \<inter> {..<b}" by auto
109 then show "convex {a..<b}" by (simp only: convex_Int 1 4)
110 have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
111 then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
114 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
117 fixes C :: "'a::real_vector set"
118 assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
119 assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
120 shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
122 proof (induct arbitrary: a set: finite)
124 then show ?case by simp
126 case (insert i s) note IH = this(3)
127 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"
128 using insert.hyps(1,2) insert.prems by simp_all
129 then have "0 \<le> setsum a s" by (simp add: setsum_nonneg)
130 have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
132 assume z: "setsum a s = 0"
133 with `a i + setsum a s = 1` have "a i = 1" by simp
134 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
135 show ?thesis using `a i = 1` and `\<forall>j\<in>s. a j = 0` and `y i \<in> C` by simp
137 assume nz: "setsum a s \<noteq> 0"
138 with `0 \<le> setsum a s` have "0 < setsum a s" by simp
139 then have "(\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
140 using `\<forall>j\<in>s. 0 \<le> a j` and `\<forall>j\<in>s. y j \<in> C`
141 by (simp add: IH setsum_divide_distrib [symmetric])
142 from `convex C` and `y i \<in> C` and this and `0 \<le> a i`
143 and `0 \<le> setsum a s` and `a i + setsum a s = 1`
144 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"
146 then show ?thesis by (simp add: scaleR_setsum_right nz)
148 then show ?case using `finite s` and `i \<notin> s` by simp
152 "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)
153 \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
156 fix u :: "nat \<Rightarrow> real"
159 "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
160 "setsum u {1..k} = 1"
161 from this convex_setsum[of "{1 .. k}" s]
162 show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s" by auto
164 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
165 \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
168 assume xy: "x \<in> s" "y \<in> s"
169 assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
170 let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
171 let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
172 have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
173 then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
174 then have "setsum ?u {1 .. 2} = 1"
175 using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
177 with asm[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
179 have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
180 using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
181 from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
182 have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
183 then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute)
185 then show "convex s" unfolding convex_alt by auto
189 lemma convex_explicit:
190 fixes s :: "'a::real_vector set"
191 shows "convex s \<longleftrightarrow>
192 (\<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)"
195 fix u :: "'a \<Rightarrow> real"
196 assume "convex s" "finite t"
197 "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
198 then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
199 using convex_setsum[of t s u "\<lambda> x. x"] by auto
201 assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
202 \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
208 assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
209 { assume "x \<noteq> y"
210 then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
211 using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
215 then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
216 using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
217 asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
218 ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
224 shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
225 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
226 unfolding convex_explicit
229 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"
230 and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
231 have *: "s \<inter> t = t" using as(2) by auto
232 have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
234 show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
235 using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
236 by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
237 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
239 subsection {* Functions that are convex on a set *}
241 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
242 where "convex_on s f \<longleftrightarrow>
243 (\<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)"
245 lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
246 unfolding convex_on_def by auto
248 lemma convex_on_add [intro]:
249 assumes "convex_on s f" "convex_on s g"
250 shows "convex_on s (\<lambda>x. f x + g x)"
253 assume "x\<in>s" "y\<in>s"
256 assume "0 \<le> u" "0 \<le> v" "u + v = 1"
258 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)"
259 using assms unfolding convex_on_def by (auto simp add: add_mono)
260 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)"
261 by (simp add: field_simps)
263 then show ?thesis unfolding convex_on_def by auto
266 lemma convex_on_cmul [intro]:
267 assumes "0 \<le> (c::real)" "convex_on s f"
268 shows "convex_on s (\<lambda>x. c * f x)"
270 have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
271 by (simp add: field_simps)
272 show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
273 unfolding convex_on_def and * by auto
277 assumes "convex_on s f" "x\<in>s" "y \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
278 shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
280 let ?m = "max (f x) (f y)"
281 have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
282 using assms(4,5) by (auto simp add: mult_left_mono add_mono)
283 also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
285 using assms unfolding convex_on_def by fastforce
288 lemma convex_on_dist [intro]:
289 fixes s :: "'a::real_normed_vector set"
290 shows "convex_on s (\<lambda>x. dist a x)"
291 proof (auto simp add: convex_on_def dist_norm)
293 assume "x\<in>s" "y\<in>s"
295 assume "0 \<le> u" "0 \<le> v" "u + v = 1"
296 have "a = u *\<^sub>R a + v *\<^sub>R a"
297 unfolding scaleR_left_distrib[symmetric] and `u+v=1` by simp
298 then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
299 by (auto simp add: algebra_simps)
300 show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
301 unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
302 using `0 \<le> u` `0 \<le> v` by auto
306 subsection {* Arithmetic operations on sets preserve convexity. *}
308 lemma convex_linear_image:
309 assumes "linear f" and "convex s" shows "convex (f ` s)"
311 interpret f: linear f by fact
312 from `convex s` show "convex (f ` s)"
313 by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
316 lemma convex_linear_vimage:
317 assumes "linear f" and "convex s" shows "convex (f -` s)"
319 interpret f: linear f by fact
320 from `convex s` show "convex (f -` s)"
321 by (simp add: convex_def f.add f.scaleR)
324 lemma convex_scaling:
325 assumes "convex s" shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
327 have "linear (\<lambda>x. c *\<^sub>R x)" by (simp add: linearI scaleR_add_right)
328 then show ?thesis using `convex s` by (rule convex_linear_image)
331 lemma convex_negations:
332 assumes "convex s" shows "convex ((\<lambda>x. - x) ` s)"
334 have "linear (\<lambda>x. - x)" by (simp add: linearI)
335 then show ?thesis using `convex s` by (rule convex_linear_image)
339 assumes "convex s" and "convex t"
340 shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
342 have "linear (\<lambda>(x, y). x + y)"
343 by (auto intro: linearI simp add: scaleR_add_right)
344 with assms have "convex ((\<lambda>(x, y). x + y) ` (s \<times> t))"
345 by (intro convex_linear_image convex_Times)
346 also have "((\<lambda>(x, y). x + y) ` (s \<times> t)) = {x + y| x y. x \<in> s \<and> y \<in> t}"
348 finally show ?thesis .
351 lemma convex_differences:
352 assumes "convex s" "convex t"
353 shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
355 have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
356 by (auto simp add: diff_conv_add_uminus simp del: add_uminus_conv_diff)
358 using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
361 lemma convex_translation:
363 shows "convex ((\<lambda>x. a + x) ` s)"
365 have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
367 using convex_sums[OF convex_singleton[of a] assms] by auto
370 lemma convex_affinity:
372 shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
374 have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
376 using convex_translation[OF convex_scaling[OF assms], of a c] by auto
379 lemma pos_is_convex: "convex {0 :: real <..}"
382 fix y x \<mu> :: real
383 assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
385 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
386 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
389 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
391 { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
392 then have "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
393 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
394 by (auto simp add: add_pos_pos) }
395 ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
398 lemma convex_on_setsum:
399 fixes a :: "'a \<Rightarrow> real"
400 and y :: "'a \<Rightarrow> 'b::real_vector"
401 and f :: "'b \<Rightarrow> real"
402 assumes "finite s" "s \<noteq> {}"
405 and "(\<Sum> i \<in> s. a i) = 1"
406 and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
407 and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
408 shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
410 proof (induct s arbitrary: a rule: finite_ne_induct)
412 then have ai: "a i = 1" by auto
413 then show ?case by auto
415 case (insert i s) note asms = this
416 then have "convex_on C f" by simp
417 from this[unfolded convex_on_def, rule_format]
418 have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1
419 \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
422 then have "(\<Sum> j \<in> s. a j) = 0"
424 then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
425 using setsum_nonneg_0[where 'b=real] asms by fastforce
426 then have ?case using asms by auto }
428 { assume asm: "a i \<noteq> 1"
429 from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
430 have fis: "finite (insert i s)" using asms by auto
431 then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
432 then have "a i < 1" using asm by auto
433 then have i0: "1 - a i > 0" by auto
434 let ?a = "\<lambda>j. a j / (1 - a i)"
435 { fix j assume "j \<in> s" with i0 asms have "?a j \<ge> 0"
438 have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
439 then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
440 then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
441 then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
442 have "convex C" using asms by auto
443 then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
444 using asms convex_setsum[OF `finite s`
445 `convex C` a1 a_nonneg] by auto
446 have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
447 using a_nonneg a1 asms by blast
448 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)"
449 using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF `finite s` `i \<notin> s`] asms
450 by (auto simp only:add_commute)
451 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)"
453 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)"
454 using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
455 by (auto simp:algebra_simps)
456 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)"
457 by (auto simp: divide_inverse)
458 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)"
459 using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
460 by (auto simp add:add_commute)
461 also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
462 using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
463 OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
464 also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
465 unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
466 also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
467 also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
468 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))"
470 ultimately show ?case by auto
474 fixes C :: "'a::real_vector set"
476 shows "convex_on C f =
477 (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
478 \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
482 assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
483 from this[unfolded convex_on_def, rule_format]
484 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
485 from this[of "\<mu>" "1 - \<mu>", simplified] asms
486 show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
488 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"
491 assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
492 then have[simp]: "1 - u = v" by auto
493 from asm[rule_format, of x y u]
494 have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
496 then show "convex_on C f" unfolding convex_on_def by auto
499 lemma convex_on_diff:
500 fixes f :: "real \<Rightarrow> real"
501 assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
502 shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
503 "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
505 def a \<equiv> "(t - y) / (x - y)"
506 with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
507 with f `x \<in> I` `y \<in> I` have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
508 by (auto simp: convex_on_def)
509 have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps)
510 also have "\<dots> = t" unfolding a_def using `x < t` `t < y` by simp
511 finally have "f t \<le> a * f x + (1 - a) * f y" using cvx by simp
512 also have "\<dots> = a * (f x - f y) + f y" by (simp add: field_simps)
513 finally have "f t - f y \<le> a * (f x - f y)" by simp
514 with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
515 by (simp add: le_divide_eq divide_le_eq field_simps a_def)
516 with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
517 by (simp add: le_divide_eq divide_le_eq field_simps)
520 lemma pos_convex_function:
521 fixes f :: "real \<Rightarrow> real"
523 and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
524 shows "convex_on C f"
525 unfolding convex_on_alt[OF assms(1)]
528 fix x y \<mu> :: real
529 let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
530 assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
531 then have "1 - \<mu> \<ge> 0" by auto
532 then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
533 have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
534 \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
535 using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
536 mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
537 then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
538 by (auto simp add: field_simps)
539 then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
540 using convex_on_alt by auto
543 lemma atMostAtLeast_subset_convex:
544 fixes C :: "real set"
546 and "x \<in> C" "y \<in> C" "x < y"
547 shows "{x .. y} \<subseteq> C"
549 fix z assume zasm: "z \<in> {x .. y}"
550 { assume asm: "x < z" "z < y"
551 let ?\<mu> = "(y - z) / (y - x)"
552 have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
553 then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
554 using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
555 by (simp add: algebra_simps)
556 have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
557 by (auto simp add: field_simps)
558 also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
559 using assms unfolding add_divide_distrib by (auto simp: field_simps)
560 also have "\<dots> = z"
561 using assms by (auto simp: field_simps)
562 finally have "z \<in> C"
565 show "z \<in> C" using zasm less assms
566 unfolding atLeastAtMost_iff le_less by auto
570 fixes f :: "real \<Rightarrow> real"
572 and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
573 and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
574 and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
575 and "x \<in> C" "y \<in> C"
576 shows "f' x * (y - x) \<le> f y - f x"
580 assume asm: "x \<in> C" "y \<in> C" "y > x"
581 then have ge: "y - x > 0" "y - x \<ge> 0" by auto
582 from asm have le: "x - y < 0" "x - y \<le> 0" by auto
583 then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
584 using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
585 THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
587 then have "z1 \<in> C" using atMostAtLeast_subset_convex
588 `convex C` `x \<in> C` `y \<in> C` `x < y` by fastforce
589 from z1 have z1': "f x - f y = (x - y) * f' z1"
590 by (simp add:field_simps)
591 obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
592 using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `z1 \<in> C` `x < z1`],
593 THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
595 obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
596 using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `z1 \<in> C` `y \<in> C` `z1 < y`],
597 THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
599 have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
600 using asm z1' by auto
601 also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
602 finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
603 have A': "y - z1 \<ge> 0" using z1 by auto
604 have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
605 `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastforce
606 then have B': "f'' z3 \<ge> 0" using assms by auto
607 from A' B' have "(y - z1) * f'' z3 \<ge> 0" by auto
608 from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
609 from mult_right_mono_neg[OF this le(2)]
610 have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
611 by (simp add: algebra_simps)
612 then have "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
613 then have res: "f' y * (x - y) \<le> f x - f y" by auto
614 have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
616 also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
617 finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
618 have A: "z1 - x \<ge> 0" using z1 by auto
619 have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
620 `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastforce
621 then have B: "f'' z2 \<ge> 0" using assms by auto
622 from A B have "(z1 - x) * f'' z2 \<ge> 0" by auto
623 from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
624 from mult_right_mono[OF this ge(2)]
625 have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
626 by (simp add: algebra_simps)
627 then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
628 then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
629 using res by auto } note less_imp = this
631 assume "x \<in> C" "y \<in> C" "x \<noteq> y"
632 then have"f y - f x \<ge> f' x * (y - x)"
633 unfolding neq_iff using less_imp by auto } note neq_imp = this
636 assume asm: "x \<in> C" "y \<in> C" "x = y"
637 then have "f y - f x \<ge> f' x * (y - x)" by auto }
638 ultimately show ?thesis using assms by blast
641 lemma f''_ge0_imp_convex:
642 fixes f :: "real \<Rightarrow> real"
643 assumes conv: "convex C"
644 and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
645 and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
646 and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
647 shows "convex_on C f"
648 using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
650 lemma minus_log_convex:
653 shows "convex_on {0 <..} (\<lambda> x. - log b x)"
655 have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
656 then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
657 by (auto simp: DERIV_minus)
658 have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
659 using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
660 from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
661 have "\<And>z :: real. z > 0 \<Longrightarrow>
662 DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
664 then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
665 unfolding inverse_eq_divide by (auto simp add: mult_assoc)
666 have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
667 using `b > 1` by (auto intro!:less_imp_le)
668 from f''_ge0_imp_convex[OF pos_is_convex,
669 unfolded greaterThan_iff, OF f' f''0 f''_ge0]