src/HOL/Library/Convex.thy
 author nipkow Fri Apr 11 22:53:33 2014 +0200 (2014-04-11) changeset 56541 0e3abadbef39 parent 56536 aefb4a8da31f child 56544 b60d5d119489 permissions -rw-r--r--
1 (*  Title:      HOL/Library/Convex.thy
2     Author:     Armin Heller, TU Muenchen
3     Author:     Johannes Hoelzl, TU Muenchen
4 *)
6 header {* Convexity in real vector spaces *}
8 theory Convex
9 imports Product_Vector
10 begin
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)"
17 lemma convexI:
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"
19   shows "convex s"
20   using assms unfolding convex_def by fast
22 lemma convexD:
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
27 lemma convex_alt:
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")
30 proof
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)
39 lemma mem_convex:
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}"
66   unfolding convex_def
67   by (auto simp: inner_add intro!: convex_bound_le)
69 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
70 proof -
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
73 qed
75 lemma convex_hyperplane: "convex {x. inner a x = b}"
76 proof -
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: *)
80 qed
82 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
83   unfolding convex_def
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:
90   fixes a b :: "real"
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}"
95 proof -
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)
112 qed
114 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
116 lemma convex_setsum:
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"
121   using assms(1,3,4,5)
122 proof (induct arbitrary: a set: finite)
123   case empty
124   then show ?case by simp
125 next
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"
131   proof (cases)
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
136   next
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 divide_nonneg_pos 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"
145       by (rule convexD)
146     then show ?thesis by (simp add: scaleR_setsum_right nz)
147   qed
148   then show ?case using `finite s` and `i \<notin> s` by simp
149 qed
151 lemma convex:
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)"
154 proof safe
155   fix k :: nat
156   fix u :: "nat \<Rightarrow> real"
157   fix x
158   assume "convex s"
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
163 next
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"
166   { fix \<mu> :: real
167     fix x y :: 'a
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>"]
176       by auto
177     with asm[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
178       using mu xy by auto
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)
184   }
185   then show "convex s" unfolding convex_alt by auto
186 qed
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)"
193 proof safe
194   fix t
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
200 next
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"
203   show "convex s"
204     unfolding convex_alt
205   proof safe
206     fix x y
207     fix \<mu> :: real
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>"]
212           asm by auto }
213     moreover
214     { assume "x = y"
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
219   qed
220 qed
222 lemma convex_finite:
223   assumes "finite s"
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
227 proof safe
228   fix t u
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)"
233     by simp
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
249   assumes "convex_on s f" "convex_on s g"
250   shows "convex_on s (\<lambda>x. f x + g x)"
251 proof -
252   { fix x y
253     assume "x\<in>s" "y\<in>s"
254     moreover
255     fix u v :: real
256     assume "0 \<le> u" "0 \<le> v" "u + v = 1"
257     ultimately
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)"
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)"
262   }
263   then show ?thesis unfolding convex_on_def by auto
264 qed
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)"
269 proof-
270   have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
272   show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
273     unfolding convex_on_def and * by auto
274 qed
276 lemma convex_lower:
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)"
279 proof-
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)"
283   also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
284   finally show ?thesis
285     using assms unfolding convex_on_def by fastforce
286 qed
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)
292   fix x y
293   assume "x\<in>s" "y\<in>s"
294   fix u v :: real
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
303 qed
306 subsection {* Arithmetic operations on sets preserve convexity. *}
308 lemma convex_linear_image:
309   assumes "linear f" and "convex s" shows "convex (f ` s)"
310 proof -
311   interpret f: linear f by fact
312   from `convex s` show "convex (f ` s)"
314 qed
316 lemma convex_linear_vimage:
317   assumes "linear f" and "convex s" shows "convex (f -` s)"
318 proof -
319   interpret f: linear f by fact
320   from `convex s` show "convex (f -` s)"
322 qed
324 lemma convex_scaling:
325   assumes "convex s" shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
326 proof -
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)
329 qed
331 lemma convex_negations:
332   assumes "convex s" shows "convex ((\<lambda>x. - x) ` s)"
333 proof -
334   have "linear (\<lambda>x. - x)" by (simp add: linearI)
335   then show ?thesis using `convex s` by (rule convex_linear_image)
336 qed
338 lemma convex_sums:
339   assumes "convex s" and "convex t"
340   shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
341 proof -
342   have "linear (\<lambda>(x, y). x + y)"
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}"
347     by auto
348   finally show ?thesis .
349 qed
351 lemma convex_differences:
352   assumes "convex s" "convex t"
353   shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
354 proof -
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}"
357   then show ?thesis
358     using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
359 qed
361 lemma convex_translation:
362   assumes "convex s"
363   shows "convex ((\<lambda>x. a + x) ` s)"
364 proof -
365   have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
366   then show ?thesis
367     using convex_sums[OF convex_singleton[of a] assms] by auto
368 qed
370 lemma convex_affinity:
371   assumes "convex s"
372   shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
373 proof -
374   have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
375   then show ?thesis
376     using convex_translation[OF convex_scaling[OF assms], of a c] by auto
377 qed
379 lemma pos_is_convex: "convex {0 :: real <..}"
380   unfolding convex_alt
381 proof safe
382   fix y x \<mu> :: real
383   assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
384   { assume "\<mu> = 0"
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 }
387   moreover
388   { assume "\<mu> = 1"
389     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
390   moreover
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
395   ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
396 qed
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> {}"
403     and "convex_on C f"
404     and "convex C"
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))"
409   using assms
410 proof (induct s arbitrary: a rule: finite_ne_induct)
411   case (singleton i)
412   then have ai: "a i = 1" by auto
413   then show ?case by auto
414 next
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"
420     by simp
421   { assume "a i = 1"
422     then have "(\<Sum> j \<in> s. a j) = 0"
423       using asms by auto
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 }
427   moreover
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"
436       then have "?a j \<ge> 0"
437         using i0 asms divide_nonneg_pos
438         by fastforce }
439     note a_nonneg = this
440     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
441     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
442     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
443     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
444     have "convex C" using asms by auto
445     then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
446       using asms convex_setsum[OF `finite s`
447         `convex C` a1 a_nonneg] by auto
448     have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
449       using a_nonneg a1 asms by blast
450     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)"
451       using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF `finite s` `i \<notin> s`] asms
453     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)"
454       using i0 by auto
455     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)"
456       using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
457       by (auto simp:algebra_simps)
458     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)"
459       by (auto simp: divide_inverse)
460     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)"
461       using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
463     also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
464       using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
465         OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
466     also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
467       unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
468     also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
469     also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
470     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))"
471       by simp }
472   ultimately show ?case by auto
473 qed
475 lemma convex_on_alt:
476   fixes C :: "'a::real_vector set"
477   assumes "convex C"
478   shows "convex_on C f =
479   (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
480       \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
481 proof safe
482   fix x y
483   fix \<mu> :: real
484   assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
485   from this[unfolded convex_on_def, rule_format]
486   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
487   from this[of "\<mu>" "1 - \<mu>", simplified] asms
488   show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
489 next
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"
491   { fix x y
492     fix u v :: real
493     assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
494     then have[simp]: "1 - u = v" by auto
495     from asm[rule_format, of x y u]
496     have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
497   }
498   then show "convex_on C f" unfolding convex_on_def by auto
499 qed
501 lemma convex_on_diff:
502   fixes f :: "real \<Rightarrow> real"
503   assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
504   shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
505     "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
506 proof -
507   def a \<equiv> "(t - y) / (x - y)"
508   with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
509   with f `x \<in> I` `y \<in> I` have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
510     by (auto simp: convex_on_def)
511   have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps)
512   also have "\<dots> = t" unfolding a_def using `x < t` `t < y` by simp
513   finally have "f t \<le> a * f x + (1 - a) * f y" using cvx by simp
514   also have "\<dots> = a * (f x - f y) + f y" by (simp add: field_simps)
515   finally have "f t - f y \<le> a * (f x - f y)" by simp
516   with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
517     by (simp add: le_divide_eq divide_le_eq field_simps a_def)
518   with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
519     by (simp add: le_divide_eq divide_le_eq field_simps)
520 qed
522 lemma pos_convex_function:
523   fixes f :: "real \<Rightarrow> real"
524   assumes "convex C"
525     and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
526   shows "convex_on C f"
527   unfolding convex_on_alt[OF assms(1)]
528   using assms
529 proof safe
530   fix x y \<mu> :: real
531   let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
532   assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
533   then have "1 - \<mu> \<ge> 0" by auto
534   then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
535   have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
536             \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
537     using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
538       mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
539   then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
540     by (auto simp add: field_simps)
541   then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
542     using convex_on_alt by auto
543 qed
545 lemma atMostAtLeast_subset_convex:
546   fixes C :: "real set"
547   assumes "convex C"
548     and "x \<in> C" "y \<in> C" "x < y"
549   shows "{x .. y} \<subseteq> C"
550 proof safe
551   fix z assume zasm: "z \<in> {x .. y}"
552   { assume asm: "x < z" "z < y"
553     let ?\<mu> = "(y - z) / (y - x)"
554     have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
555     then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
556       using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
558     have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
559       by (auto simp add: field_simps)
560     also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
561       using assms unfolding add_divide_distrib by (auto simp: field_simps)
562     also have "\<dots> = z"
563       using assms by (auto simp: field_simps)
564     finally have "z \<in> C"
565       using comb by auto }
566   note less = this
567   show "z \<in> C" using zasm less assms
568     unfolding atLeastAtMost_iff le_less by auto
569 qed
571 lemma f''_imp_f':
572   fixes f :: "real \<Rightarrow> real"
573   assumes "convex C"
574     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
575     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
576     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
577     and "x \<in> C" "y \<in> C"
578   shows "f' x * (y - x) \<le> f y - f x"
579   using assms
580 proof -
581   { fix x y :: real
582     assume asm: "x \<in> C" "y \<in> C" "y > x"
583     then have ge: "y - x > 0" "y - x \<ge> 0" by auto
584     from asm have le: "x - y < 0" "x - y \<le> 0" by auto
585     then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
586       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
587         THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
588       by auto
589     then have "z1 \<in> C" using atMostAtLeast_subset_convex
590       `convex C` `x \<in> C` `y \<in> C` `x < y` by fastforce
591     from z1 have z1': "f x - f y = (x - y) * f' z1"
593     obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
594       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `z1 \<in> C` `x < z1`],
595         THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
596       by auto
597     obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
598       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `z1 \<in> C` `y \<in> C` `z1 < y`],
599         THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
600       by auto
601     have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
602       using asm z1' by auto
603     also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
604     finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
605     have A': "y - z1 \<ge> 0" using z1 by auto
606     have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
607       `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastforce
608     then have B': "f'' z3 \<ge> 0" using assms by auto
609     from A' B' have "(y - z1) * f'' z3 \<ge> 0" by auto
610     from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
611     from mult_right_mono_neg[OF this le(2)]
612     have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
614     then have "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
615     then have res: "f' y * (x - y) \<le> f x - f y" by auto
616     have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
617       using asm z1 by auto
618     also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
619     finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
620     have A: "z1 - x \<ge> 0" using z1 by auto
621     have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
622       `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastforce
623     then have B: "f'' z2 \<ge> 0" using assms by auto
624     from A B have "(z1 - x) * f'' z2 \<ge> 0" by auto
625     from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
626     from mult_right_mono[OF this ge(2)]
627     have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
629     then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
630     then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
631       using res by auto } note less_imp = this
632   { fix x y :: real
633     assume "x \<in> C" "y \<in> C" "x \<noteq> y"
634     then have"f y - f x \<ge> f' x * (y - x)"
635     unfolding neq_iff using less_imp by auto } note neq_imp = this
636   moreover
637   { fix x y :: real
638     assume asm: "x \<in> C" "y \<in> C" "x = y"
639     then have "f y - f x \<ge> f' x * (y - x)" by auto }
640   ultimately show ?thesis using assms by blast
641 qed
643 lemma f''_ge0_imp_convex:
644   fixes f :: "real \<Rightarrow> real"
645   assumes conv: "convex C"
646     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
647     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
648     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
649   shows "convex_on C f"
650 using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
652 lemma minus_log_convex:
653   fixes b :: real
654   assumes "b > 1"
655   shows "convex_on {0 <..} (\<lambda> x. - log b x)"
656 proof -
657   have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
658   then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
659     by (auto simp: DERIV_minus)
660   have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
661     using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
662   from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
663   have "\<And>z :: real. z > 0 \<Longrightarrow>
664     DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
665     by auto
666   then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
667     unfolding inverse_eq_divide by (auto simp add: mult_assoc)
668   have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
669     using `b > 1` by (auto intro!:less_imp_le simp add: mult_pos_pos)
670   from f''_ge0_imp_convex[OF pos_is_convex,
671     unfolded greaterThan_iff, OF f' f''0 f''_ge0]
672   show ?thesis by auto
673 qed
675 end