1 (* Title: HOL/Library/Convex.thy
2 Author: Armin Heller, TU Muenchen
3 Author: Johannes Hoelzl, TU Muenchen
6 section \<open>Convexity in real vector spaces\<close>
12 subsection \<open>Convexity\<close>
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
33 fix x y and u v :: real
34 assume mem: "x \<in> s" "y \<in> s"
35 assume "0 \<le> u" "0 \<le> v"
38 then have "u = 1 - v" by auto
39 ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
40 using alt[OF mem] by auto
43 unfolding convex_def by auto
44 qed (auto simp: convex_def)
47 assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
48 shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
49 using assms unfolding convex_alt by auto
52 assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
53 shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
56 apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
59 lemma convex_empty[intro,simp]: "convex {}"
60 unfolding convex_def by simp
62 lemma convex_singleton[intro,simp]: "convex {a}"
63 unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
65 lemma convex_UNIV[intro,simp]: "convex UNIV"
66 unfolding convex_def by auto
68 lemma convex_Inter: "(\<forall>s\<in>f. convex s) \<Longrightarrow> convex(\<Inter>f)"
69 unfolding convex_def by auto
71 lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
72 unfolding convex_def by auto
74 lemma convex_INT: "\<forall>i\<in>A. convex (B i) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
75 unfolding convex_def by auto
77 lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
78 unfolding convex_def by auto
80 lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
82 by (auto simp: inner_add intro!: convex_bound_le)
84 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
86 have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
89 unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
92 lemma convex_hyperplane: "convex {x. inner a x = b}"
94 have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
96 show ?thesis using convex_halfspace_le convex_halfspace_ge
97 by (auto intro!: convex_Int simp: *)
100 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
102 by (auto simp: convex_bound_lt inner_add)
104 lemma convex_halfspace_gt: "convex {x. inner a x > b}"
105 using convex_halfspace_lt[of "-a" "-b"] by auto
107 lemma convex_real_interval [iff]:
109 shows "convex {a..}" and "convex {..b}"
110 and "convex {a<..}" and "convex {..<b}"
111 and "convex {a..b}" and "convex {a<..b}"
112 and "convex {a..<b}" and "convex {a<..<b}"
114 have "{a..} = {x. a \<le> inner 1 x}"
116 then show 1: "convex {a..}"
117 by (simp only: convex_halfspace_ge)
118 have "{..b} = {x. inner 1 x \<le> b}"
120 then show 2: "convex {..b}"
121 by (simp only: convex_halfspace_le)
122 have "{a<..} = {x. a < inner 1 x}"
124 then show 3: "convex {a<..}"
125 by (simp only: convex_halfspace_gt)
126 have "{..<b} = {x. inner 1 x < b}"
128 then show 4: "convex {..<b}"
129 by (simp only: convex_halfspace_lt)
130 have "{a..b} = {a..} \<inter> {..b}"
132 then show "convex {a..b}"
133 by (simp only: convex_Int 1 2)
134 have "{a<..b} = {a<..} \<inter> {..b}"
136 then show "convex {a<..b}"
137 by (simp only: convex_Int 3 2)
138 have "{a..<b} = {a..} \<inter> {..<b}"
140 then show "convex {a..<b}"
141 by (simp only: convex_Int 1 4)
142 have "{a<..<b} = {a<..} \<inter> {..<b}"
144 then show "convex {a<..<b}"
145 by (simp only: convex_Int 3 4)
148 lemma convex_Reals: "convex \<real>"
149 by (simp add: convex_def scaleR_conv_of_real)
152 subsection \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
155 fixes C :: "'a::real_vector set"
158 and "(\<Sum> i \<in> s. a i) = 1"
159 assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
160 and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
161 shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
163 proof (induct arbitrary: a set: finite)
165 then show ?case by simp
167 case (insert i s) note IH = this(3)
168 have "a i + setsum a s = 1"
170 and "\<forall>j\<in>s. 0 \<le> a j"
172 and "\<forall>j\<in>s. y j \<in> C"
173 using insert.hyps(1,2) insert.prems by simp_all
174 then have "0 \<le> setsum a s"
175 by (simp add: setsum_nonneg)
176 have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
178 assume z: "setsum a s = 0"
179 with \<open>a i + setsum a s = 1\<close> have "a i = 1"
181 from setsum_nonneg_0 [OF \<open>finite s\<close> _ z] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
183 show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
186 assume nz: "setsum a s \<noteq> 0"
187 with \<open>0 \<le> setsum a s\<close> have "0 < setsum a s"
189 then have "(\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
190 using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
191 by (simp add: IH setsum_divide_distrib [symmetric])
192 from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
193 and \<open>0 \<le> setsum a s\<close> and \<open>a i + setsum a s = 1\<close>
194 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"
197 by (simp add: scaleR_setsum_right nz)
199 then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
204 "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)
205 \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
208 fix u :: "nat \<Rightarrow> real"
211 "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
212 "setsum u {1..k} = 1"
213 with convex_setsum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
216 assume *: "\<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
217 \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
221 assume xy: "x \<in> s" "y \<in> s"
222 assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
223 let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
224 let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
225 have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
227 then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
229 then have "setsum ?u {1 .. 2} = 1"
230 using setsum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
232 with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
234 have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
235 using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
236 from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
237 have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
239 then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
240 using s by (auto simp: add.commute)
243 unfolding convex_alt by auto
247 lemma convex_explicit:
248 fixes s :: "'a::real_vector set"
249 shows "convex s \<longleftrightarrow>
250 (\<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)"
253 fix u :: "'a \<Rightarrow> real"
256 and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
257 then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
258 using convex_setsum[of t s u "\<lambda> x. x"] by auto
260 assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
261 setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
267 assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
268 show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
269 proof (cases "x = y")
272 using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
277 using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
278 by (auto simp: field_simps real_vector.scale_left_diff_distrib)
285 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)"
286 unfolding convex_explicit
289 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"
290 and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
291 have *: "s \<inter> t = t"
293 have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
295 show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
296 using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
297 by (auto simp: assms setsum.If_cases if_distrib if_distrib_arg)
298 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
301 subsection \<open>Functions that are convex on a set\<close>
303 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
304 where "convex_on s f \<longleftrightarrow>
305 (\<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)"
307 lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
308 unfolding convex_on_def by auto
310 lemma convex_on_add [intro]:
311 assumes "convex_on s f"
313 shows "convex_on s (\<lambda>x. f x + g x)"
317 assume "x \<in> s" "y \<in> s"
320 assume "0 \<le> u" "0 \<le> v" "u + v = 1"
322 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)"
323 using assms unfolding convex_on_def by (auto simp: add_mono)
324 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)"
325 by (simp add: field_simps)
328 unfolding convex_on_def by auto
331 lemma convex_on_cmul [intro]:
335 shows "convex_on s (\<lambda>x. c * f x)"
337 have *: "\<And>u c fx v fy :: real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
338 by (simp add: field_simps)
339 show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
340 unfolding convex_on_def and * by auto
344 assumes "convex_on s f"
350 shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
352 let ?m = "max (f x) (f y)"
353 have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
354 using assms(4,5) by (auto simp: mult_left_mono add_mono)
355 also have "\<dots> = max (f x) (f y)"
356 using assms(6) by (simp add: distrib_right [symmetric])
358 using assms unfolding convex_on_def by fastforce
361 lemma convex_on_dist [intro]:
362 fixes s :: "'a::real_normed_vector set"
363 shows "convex_on s (\<lambda>x. dist a x)"
364 proof (auto simp: convex_on_def dist_norm)
366 assume "x \<in> s" "y \<in> s"
371 have "a = u *\<^sub>R a + v *\<^sub>R a"
372 unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
373 then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
374 by (auto simp: algebra_simps)
375 show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
376 unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
377 using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
381 subsection \<open>Arithmetic operations on sets preserve convexity\<close>
383 lemma convex_linear_image:
386 shows "convex (f ` s)"
388 interpret f: linear f by fact
389 from \<open>convex s\<close> show "convex (f ` s)"
390 by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
393 lemma convex_linear_vimage:
396 shows "convex (f -` s)"
398 interpret f: linear f by fact
399 from \<open>convex s\<close> show "convex (f -` s)"
400 by (simp add: convex_def f.add f.scaleR)
403 lemma convex_scaling:
405 shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
407 have "linear (\<lambda>x. c *\<^sub>R x)"
408 by (simp add: linearI scaleR_add_right)
410 using \<open>convex s\<close> by (rule convex_linear_image)
415 shows "convex ((\<lambda>x. x *\<^sub>R c) ` s)"
417 have "linear (\<lambda>x. x *\<^sub>R c)"
418 by (simp add: linearI scaleR_add_left)
420 using \<open>convex s\<close> by (rule convex_linear_image)
423 lemma convex_negations:
425 shows "convex ((\<lambda>x. - x) ` s)"
427 have "linear (\<lambda>x. - x)"
428 by (simp add: linearI)
430 using \<open>convex s\<close> by (rule convex_linear_image)
436 shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
438 have "linear (\<lambda>(x, y). x + y)"
439 by (auto intro: linearI simp: scaleR_add_right)
440 with assms have "convex ((\<lambda>(x, y). x + y) ` (s \<times> t))"
441 by (intro convex_linear_image convex_Times)
442 also have "((\<lambda>(x, y). x + y) ` (s \<times> t)) = {x + y| x y. x \<in> s \<and> y \<in> t}"
444 finally show ?thesis .
447 lemma convex_differences:
448 assumes "convex s" "convex t"
449 shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
451 have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
452 by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
454 using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
457 lemma convex_translation:
459 shows "convex ((\<lambda>x. a + x) ` s)"
461 have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s"
464 using convex_sums[OF convex_singleton[of a] assms] by auto
467 lemma convex_affinity:
469 shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
471 have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s"
474 using convex_translation[OF convex_scaling[OF assms], of a c] by auto
477 lemma pos_is_convex: "convex {0 :: real <..}"
480 fix y x \<mu> :: real
481 assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
484 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
485 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using * by simp
490 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using * by simp
494 assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
495 then have "\<mu> > 0" "(1 - \<mu>) > 0" using * by auto
496 then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using *
497 by (auto simp: add_pos_pos)
499 ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
500 using assms by fastforce
503 lemma convex_on_setsum:
504 fixes a :: "'a \<Rightarrow> real"
505 and y :: "'a \<Rightarrow> 'b::real_vector"
506 and f :: "'b \<Rightarrow> real"
507 assumes "finite s" "s \<noteq> {}"
510 and "(\<Sum> i \<in> s. a i) = 1"
511 and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
512 and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
513 shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
515 proof (induct s arbitrary: a rule: finite_ne_induct)
517 then have ai: "a i = 1" by auto
518 then show ?case by auto
521 then have "convex_on C f" by simp
522 from this[unfolded convex_on_def, rule_format]
523 have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
524 f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
527 proof (cases "a i = 1")
529 then have "(\<Sum> j \<in> s. a j) = 0"
531 then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
532 using setsum_nonneg_0[where 'b=real] insert by fastforce
537 from insert have yai: "y i \<in> C" "a i \<ge> 0"
539 have fis: "finite (insert i s)"
541 then have ai1: "a i \<le> 1"
542 using setsum_nonneg_leq_bound[of "insert i s" a] insert by simp
545 then have i0: "1 - a i > 0"
547 let ?a = "\<lambda>j. a j / (1 - a i)"
548 have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
549 using i0 insert that by fastforce
550 have "(\<Sum> j \<in> insert i s. a j) = 1"
552 then have "(\<Sum> j \<in> s. a j) = 1 - a i"
553 using setsum.insert insert by fastforce
554 then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
556 then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
557 unfolding setsum_divide_distrib by simp
558 have "convex C" using insert by auto
559 then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
560 using insert convex_setsum[OF \<open>finite s\<close>
561 \<open>convex C\<close> a1 a_nonneg] by auto
562 have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
563 using a_nonneg a1 insert by blast
564 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)"
565 using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
566 by (auto simp only: add.commute)
567 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)"
569 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)"
570 using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
571 by (auto simp: algebra_simps)
572 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)"
573 by (auto simp: divide_inverse)
574 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)"
575 using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
576 by (auto simp: add.commute)
577 also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
578 using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
579 OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
580 also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
581 unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
582 also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
584 also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
592 fixes C :: "'a::real_vector set"
594 shows "convex_on C f \<longleftrightarrow>
595 (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
596 f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
600 assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
601 from this[unfolded convex_on_def, rule_format]
602 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"
604 from this[of "\<mu>" "1 - \<mu>", simplified] *
605 show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
608 assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
609 f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
613 assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
614 then have[simp]: "1 - u = v" by auto
615 from *[rule_format, of x y u]
616 have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
619 then show "convex_on C f"
620 unfolding convex_on_def by auto
623 lemma convex_on_diff:
624 fixes f :: "real \<Rightarrow> real"
625 assumes f: "convex_on I f"
626 and I: "x \<in> I" "y \<in> I"
627 and t: "x < t" "t < y"
628 shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
629 and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
631 def a \<equiv> "(t - y) / (x - y)"
632 with t have "0 \<le> a" "0 \<le> 1 - a"
633 by (auto simp: field_simps)
634 with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
635 by (auto simp: convex_on_def)
636 have "a * x + (1 - a) * y = a * (x - y) + y"
637 by (simp add: field_simps)
638 also have "\<dots> = t"
639 unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
640 finally have "f t \<le> a * f x + (1 - a) * f y"
642 also have "\<dots> = a * (f x - f y) + f y"
643 by (simp add: field_simps)
644 finally have "f t - f y \<le> a * (f x - f y)"
646 with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
647 by (simp add: le_divide_eq divide_le_eq field_simps a_def)
648 with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
649 by (simp add: le_divide_eq divide_le_eq field_simps)
652 lemma pos_convex_function:
653 fixes f :: "real \<Rightarrow> real"
655 and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
656 shows "convex_on C f"
657 unfolding convex_on_alt[OF assms(1)]
660 fix x y \<mu> :: real
661 let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
662 assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
663 then have "1 - \<mu> \<ge> 0" by auto
664 then have xpos: "?x \<in> C"
665 using * unfolding convex_alt by fastforce
666 have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
667 \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
668 using add_mono[OF mult_left_mono[OF leq[OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
669 mult_left_mono[OF leq[OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
671 then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
672 by (auto simp: field_simps)
673 then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
674 using convex_on_alt by auto
677 lemma atMostAtLeast_subset_convex:
678 fixes C :: "real set"
680 and "x \<in> C" "y \<in> C" "x < y"
681 shows "{x .. y} \<subseteq> C"
683 fix z assume z: "z \<in> {x .. y}"
684 have less: "z \<in> C" if *: "x < z" "z < y"
686 let ?\<mu> = "(y - z) / (y - x)"
687 have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
688 using assms * by (auto simp: field_simps)
689 then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
690 using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
691 by (simp add: algebra_simps)
692 have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
693 by (auto simp: field_simps)
694 also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
695 using assms unfolding add_divide_distrib by (auto simp: field_simps)
696 also have "\<dots> = z"
697 using assms by (auto simp: field_simps)
701 show "z \<in> C" using z less assms
702 unfolding atLeastAtMost_iff le_less by auto
706 fixes f :: "real \<Rightarrow> real"
708 and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
709 and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
710 and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
711 and "x \<in> C" "y \<in> C"
712 shows "f' x * (y - x) \<le> f y - f x"
717 assume *: "x \<in> C" "y \<in> C" "y > x"
718 then have ge: "y - x > 0" "y - x \<ge> 0"
720 from * have le: "x - y < 0" "x - y \<le> 0"
722 then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
723 using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
724 THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
726 then have "z1 \<in> C"
727 using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
729 from z1 have z1': "f x - f y = (x - y) * f' z1"
730 by (simp add: field_simps)
731 obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
732 using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
733 THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
735 obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
736 using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
737 THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
739 have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
741 also have "\<dots> = (y - z1) * f'' z3"
743 finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
745 have A': "y - z1 \<ge> 0"
748 using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
750 then have B': "f'' z3 \<ge> 0"
752 from A' B' have "(y - z1) * f'' z3 \<ge> 0"
754 from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
756 from mult_right_mono_neg[OF this le(2)]
757 have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
758 by (simp add: algebra_simps)
759 then have "f' y * (x - y) - (f x - f y) \<le> 0"
761 then have res: "f' y * (x - y) \<le> f x - f y"
763 have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
765 also have "\<dots> = (z1 - x) * f'' z2"
767 finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
769 have A: "z1 - x \<ge> 0"
772 using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
774 then have B: "f'' z2 \<ge> 0"
776 from A B have "(z1 - x) * f'' z2 \<ge> 0"
778 with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
780 from mult_right_mono[OF this ge(2)]
781 have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
782 by (simp add: algebra_simps)
783 then have "f y - f x - f' x * (y - x) \<ge> 0"
785 then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
787 } note less_imp = this
790 assume "x \<in> C" "y \<in> C" "x \<noteq> y"
791 then have"f y - f x \<ge> f' x * (y - x)"
792 unfolding neq_iff using less_imp by auto
797 assume "x \<in> C" "y \<in> C" "x = y"
798 then have "f y - f x \<ge> f' x * (y - x)" by auto
800 ultimately show ?thesis using assms by blast
803 lemma f''_ge0_imp_convex:
804 fixes f :: "real \<Rightarrow> real"
805 assumes conv: "convex C"
806 and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
807 and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
808 and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
809 shows "convex_on C f"
810 using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
813 lemma minus_log_convex:
816 shows "convex_on {0 <..} (\<lambda> x. - log b x)"
818 have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
819 using DERIV_log by auto
820 then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
821 by (auto simp: DERIV_minus)
822 have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
823 using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
824 from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
825 have "\<And>z :: real. z > 0 \<Longrightarrow>
826 DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
828 then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
829 DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
830 unfolding inverse_eq_divide by (auto simp: mult.assoc)
831 have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
832 using \<open>b > 1\<close> by (auto intro!: less_imp_le)
833 from f''_ge0_imp_convex[OF pos_is_convex,
834 unfolded greaterThan_iff, OF f' f''0 f''_ge0]