src/HOL/Library/Convex.thy
author huffman
Fri Sep 13 11:16:13 2013 -0700 (2013-09-13)
changeset 53620 3c7f5e7926dc
parent 53596 d29d63460d84
child 53676 476ef9b468d2
permissions -rw-r--r--
generalized and simplified proofs of several theorems about convex sets
     1 (*  Title:      HOL/Library/Convex.thy
     2     Author:     Armin Heller, TU Muenchen
     3     Author:     Johannes Hoelzl, TU Muenchen
     4 *)
     5 
     6 header {* Convexity in real vector spaces *}
     7 
     8 theory Convex
     9 imports Product_Vector
    10 begin
    11 
    12 subsection {* Convexity. *}
    13 
    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)"
    16 
    17 lemma convex_alt:
    18   "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)"
    19   (is "_ \<longleftrightarrow> ?alt")
    20 proof
    21   assume alt[rule_format]: ?alt
    22   { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
    23     assume "0 \<le> u" "0 \<le> v"
    24     moreover assume "u + v = 1" then have "u = 1 - v" by auto
    25     ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
    26   then show "convex s" unfolding convex_def by auto
    27 qed (auto simp: convex_def)
    28 
    29 lemma mem_convex:
    30   assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
    31   shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
    32   using assms unfolding convex_alt by auto
    33 
    34 lemma convex_empty[intro]: "convex {}"
    35   unfolding convex_def by simp
    36 
    37 lemma convex_singleton[intro]: "convex {a}"
    38   unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
    39 
    40 lemma convex_UNIV[intro]: "convex UNIV"
    41   unfolding convex_def by auto
    42 
    43 lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
    44   unfolding convex_def by auto
    45 
    46 lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
    47   unfolding convex_def by auto
    48 
    49 lemma convex_INT: "\<forall>i\<in>A. convex (B i) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
    50   unfolding convex_def by auto
    51 
    52 lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
    53   unfolding convex_def by auto
    54 
    55 lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
    56   unfolding convex_def
    57   by (auto simp: inner_add intro!: convex_bound_le)
    58 
    59 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    60 proof -
    61   have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    62   show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    63 qed
    64 
    65 lemma convex_hyperplane: "convex {x. inner a x = b}"
    66 proof -
    67   have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    68   show ?thesis using convex_halfspace_le convex_halfspace_ge
    69     by (auto intro!: convex_Int simp: *)
    70 qed
    71 
    72 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
    73   unfolding convex_def
    74   by (auto simp: convex_bound_lt inner_add)
    75 
    76 lemma convex_halfspace_gt: "convex {x. inner a x > b}"
    77    using convex_halfspace_lt[of "-a" "-b"] by auto
    78 
    79 lemma convex_real_interval:
    80   fixes a b :: "real"
    81   shows "convex {a..}" and "convex {..b}"
    82     and "convex {a<..}" and "convex {..<b}"
    83     and "convex {a..b}" and "convex {a<..b}"
    84     and "convex {a..<b}" and "convex {a<..<b}"
    85 proof -
    86   have "{a..} = {x. a \<le> inner 1 x}" by auto
    87   then show 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    88   have "{..b} = {x. inner 1 x \<le> b}" by auto
    89   then show 2: "convex {..b}" by (simp only: convex_halfspace_le)
    90   have "{a<..} = {x. a < inner 1 x}" by auto
    91   then show 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
    92   have "{..<b} = {x. inner 1 x < b}" by auto
    93   then show 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
    94   have "{a..b} = {a..} \<inter> {..b}" by auto
    95   then show "convex {a..b}" by (simp only: convex_Int 1 2)
    96   have "{a<..b} = {a<..} \<inter> {..b}" by auto
    97   then show "convex {a<..b}" by (simp only: convex_Int 3 2)
    98   have "{a..<b} = {a..} \<inter> {..<b}" by auto
    99   then show "convex {a..<b}" by (simp only: convex_Int 1 4)
   100   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   101   then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
   102 qed
   103 
   104 
   105 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
   106 
   107 lemma convex_setsum:
   108   fixes C :: "'a::real_vector set"
   109   assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
   110   assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   111   shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   112   using assms
   113 proof (induct s arbitrary:a rule: finite_induct)
   114   case empty
   115   then show ?case by auto
   116 next
   117   case (insert i s) note asms = this
   118   { assume "a i = 1"
   119     then have "(\<Sum> j \<in> s. a j) = 0"
   120       using asms by auto
   121     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   122       using setsum_nonneg_0[where 'b=real] asms by fastforce
   123     then have ?case using asms by auto }
   124   moreover
   125   { assume asm: "a i \<noteq> 1"
   126     from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   127     have fis: "finite (insert i s)" using asms by auto
   128     then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   129     then have "a i < 1" using asm by auto
   130     then have i0: "1 - a i > 0" by auto
   131     let ?a = "\<lambda>j. a j / (1 - a i)"
   132     { fix j assume "j \<in> s"
   133       then have "?a j \<ge> 0"
   134         using i0 asms divide_nonneg_pos
   135         by fastforce
   136     } note a_nonneg = this
   137     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   138     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   139     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   140     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   141     with asms have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastforce
   142     then have "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   143       using asms[unfolded convex_def, rule_format] yai ai1 by auto
   144     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
   145       using scaleR_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j *\<^sub>R y j" s] by auto
   146     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
   147     then have ?case using setsum.insert asms by auto
   148   }
   149   ultimately show ?case by auto
   150 qed
   151 
   152 lemma convex:
   153   "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)
   154       \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   155 proof safe
   156   fix k :: nat
   157   fix u :: "nat \<Rightarrow> real"
   158   fix x
   159   assume "convex s"
   160     "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   161     "setsum u {1..k} = 1"
   162   from this convex_setsum[of "{1 .. k}" s]
   163   show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s" by auto
   164 next
   165   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
   166     \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   167   { fix \<mu> :: real
   168     fix x y :: 'a
   169     assume xy: "x \<in> s" "y \<in> s"
   170     assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   171     let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   172     let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   173     have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
   174     then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   175     then have "setsum ?u {1 .. 2} = 1"
   176       using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   177       by auto
   178     with asm[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   179       using mu xy by auto
   180     have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   181       using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   182     from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   183     have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
   184     then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute)
   185   }
   186   then show "convex s" unfolding convex_alt by auto
   187 qed
   188 
   189 
   190 lemma convex_explicit:
   191   fixes s :: "'a::real_vector set"
   192   shows "convex s \<longleftrightarrow>
   193     (\<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)"
   194 proof safe
   195   fix t
   196   fix u :: "'a \<Rightarrow> real"
   197   assume "convex s" "finite t"
   198     "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   199   then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   200     using convex_setsum[of t s u "\<lambda> x. x"] by auto
   201 next
   202   assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
   203     \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   204   show "convex s"
   205     unfolding convex_alt
   206   proof safe
   207     fix x y
   208     fix \<mu> :: real
   209     assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   210     { assume "x \<noteq> y"
   211       then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   212         using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
   213           asm by auto }
   214     moreover
   215     { assume "x = y"
   216       then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   217         using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
   218           asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
   219     ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
   220   qed
   221 qed
   222 
   223 lemma convex_finite:
   224   assumes "finite s"
   225   shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   226                       \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   227   unfolding convex_explicit
   228 proof safe
   229   fix t u
   230   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"
   231     and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   232   have *: "s \<inter> t = t" using as(2) by auto
   233   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     by simp
   235   show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   236    using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   237    by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   238 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   239 
   240 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   241   where "convex_on s f \<longleftrightarrow>
   242     (\<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)"
   243 
   244 lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   245   unfolding convex_on_def by auto
   246 
   247 lemma convex_on_add [intro]:
   248   assumes "convex_on s f" "convex_on s g"
   249   shows "convex_on s (\<lambda>x. f x + g x)"
   250 proof -
   251   { fix x y
   252     assume "x\<in>s" "y\<in>s"
   253     moreover
   254     fix u v :: real
   255     assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   256     ultimately
   257     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)"
   258       using assms unfolding convex_on_def by (auto simp add: add_mono)
   259     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)"
   260       by (simp add: field_simps)
   261   }
   262   then show ?thesis unfolding convex_on_def by auto
   263 qed
   264 
   265 lemma convex_on_cmul [intro]:
   266   assumes "0 \<le> (c::real)" "convex_on s f"
   267   shows "convex_on s (\<lambda>x. c * f x)"
   268 proof-
   269   have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   270     by (simp add: field_simps)
   271   show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   272     unfolding convex_on_def and * by auto
   273 qed
   274 
   275 lemma convex_lower:
   276   assumes "convex_on s f"  "x\<in>s"  "y \<in> s"  "0 \<le> u"  "0 \<le> v"  "u + v = 1"
   277   shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   278 proof-
   279   let ?m = "max (f x) (f y)"
   280   have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   281     using assms(4,5) by (auto simp add: mult_left_mono add_mono)
   282   also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
   283   finally show ?thesis
   284     using assms unfolding convex_on_def by fastforce
   285 qed
   286 
   287 lemma convex_on_dist [intro]:
   288   fixes s :: "'a::real_normed_vector set"
   289   shows "convex_on s (\<lambda>x. dist a x)"
   290 proof (auto simp add: convex_on_def dist_norm)
   291   fix x y
   292   assume "x\<in>s" "y\<in>s"
   293   fix u v :: real
   294   assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   295   have "a = u *\<^sub>R a + v *\<^sub>R a"
   296     unfolding scaleR_left_distrib[symmetric] and `u+v=1` by simp
   297   then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   298     by (auto simp add: algebra_simps)
   299   show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   300     unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   301     using `0 \<le> u` `0 \<le> v` by auto
   302 qed
   303 
   304 
   305 subsection {* Arithmetic operations on sets preserve convexity. *}
   306 
   307 lemma convex_linear_image:
   308   assumes "linear f" and "convex s" shows "convex (f ` s)"
   309 proof -
   310   interpret f: linear f by fact
   311   from `convex s` show "convex (f ` s)"
   312     by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
   313 qed
   314 
   315 lemma convex_linear_vimage:
   316   assumes "linear f" and "convex s" shows "convex (f -` s)"
   317 proof -
   318   interpret f: linear f by fact
   319   from `convex s` show "convex (f -` s)"
   320     by (simp add: convex_def f.add f.scaleR)
   321 qed
   322 
   323 lemma convex_scaling:
   324   assumes "convex s" shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   325 proof -
   326   have "linear (\<lambda>x. c *\<^sub>R x)" by (simp add: linearI scaleR_add_right)
   327   then show ?thesis using `convex s` by (rule convex_linear_image)
   328 qed
   329 
   330 lemma convex_negations:
   331   assumes "convex s" shows "convex ((\<lambda>x. - x) ` s)"
   332 proof -
   333   have "linear (\<lambda>x. - x)" by (simp add: linearI)
   334   then show ?thesis using `convex s` by (rule convex_linear_image)
   335 qed
   336 
   337 lemma convex_sums:
   338   assumes "convex s" and "convex t"
   339   shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   340 proof -
   341   have "linear (\<lambda>(x, y). x + y)"
   342     by (auto intro: linearI simp add: scaleR_add_right)
   343   with assms have "convex ((\<lambda>(x, y). x + y) ` (s \<times> t))"
   344     by (intro convex_linear_image convex_Times)
   345   also have "((\<lambda>(x, y). x + y) ` (s \<times> t)) = {x + y| x y. x \<in> s \<and> y \<in> t}"
   346     by auto
   347   finally show ?thesis .
   348 qed
   349 
   350 lemma convex_differences:
   351   assumes "convex s" "convex t"
   352   shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
   353 proof -
   354   have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
   355     unfolding diff_def by auto
   356   then show ?thesis
   357     using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   358 qed
   359 
   360 lemma convex_translation:
   361   assumes "convex s"
   362   shows "convex ((\<lambda>x. a + x) ` s)"
   363 proof -
   364   have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   365   then show ?thesis
   366     using convex_sums[OF convex_singleton[of a] assms] by auto
   367 qed
   368 
   369 lemma convex_affinity:
   370   assumes "convex s"
   371   shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   372 proof -
   373   have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   374   then show ?thesis
   375     using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   376 qed
   377 
   378 lemma pos_is_convex: "convex {0 :: real <..}"
   379   unfolding convex_alt
   380 proof safe
   381   fix y x \<mu> :: real
   382   assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   383   { assume "\<mu> = 0"
   384     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   385     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   386   moreover
   387   { assume "\<mu> = 1"
   388     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   389   moreover
   390   { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   391     then have "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   392     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   393       by (auto simp add: add_pos_pos mult_pos_pos) }
   394   ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
   395 qed
   396 
   397 lemma convex_on_setsum:
   398   fixes a :: "'a \<Rightarrow> real"
   399     and y :: "'a \<Rightarrow> 'b::real_vector"
   400     and f :: "'b \<Rightarrow> real"
   401   assumes "finite s" "s \<noteq> {}"
   402     and "convex_on C f"
   403     and "convex C"
   404     and "(\<Sum> i \<in> s. a i) = 1"
   405     and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   406     and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   407   shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   408   using assms
   409 proof (induct s arbitrary: a rule: finite_ne_induct)
   410   case (singleton i)
   411   then have ai: "a i = 1" by auto
   412   then show ?case by auto
   413 next
   414   case (insert i s) note asms = this
   415   then have "convex_on C f" by simp
   416   from this[unfolded convex_on_def, rule_format]
   417   have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1
   418       \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   419     by simp
   420   { assume "a i = 1"
   421     then have "(\<Sum> j \<in> s. a j) = 0"
   422       using asms by auto
   423     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   424       using setsum_nonneg_0[where 'b=real] asms by fastforce
   425     then have ?case using asms by auto }
   426   moreover
   427   { assume asm: "a i \<noteq> 1"
   428     from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   429     have fis: "finite (insert i s)" using asms by auto
   430     then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   431     then have "a i < 1" using asm by auto
   432     then have i0: "1 - a i > 0" by auto
   433     let ?a = "\<lambda>j. a j / (1 - a i)"
   434     { fix j assume "j \<in> s"
   435       then have "?a j \<ge> 0"
   436         using i0 asms divide_nonneg_pos
   437         by fastforce }
   438     note a_nonneg = this
   439     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   440     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   441     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   442     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   443     have "convex C" using asms by auto
   444     then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   445       using asms convex_setsum[OF `finite s`
   446         `convex C` a1 a_nonneg] by auto
   447     have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   448       using a_nonneg a1 asms by blast
   449     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)"
   450       using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF `finite s` `i \<notin> s`] asms
   451       by (auto simp only:add_commute)
   452     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       using i0 by auto
   454     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)"
   455       using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   456       by (auto simp:algebra_simps)
   457     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)"
   458       by (auto simp: divide_inverse)
   459     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)"
   460       using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   461       by (auto simp add:add_commute)
   462     also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   463       using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
   464         OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
   465     also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   466       unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
   467     also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
   468     also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
   469     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       by simp }
   471   ultimately show ?case by auto
   472 qed
   473 
   474 lemma convex_on_alt:
   475   fixes C :: "'a::real_vector set"
   476   assumes "convex C"
   477   shows "convex_on C f =
   478   (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
   479       \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   480 proof safe
   481   fix x y
   482   fix \<mu> :: real
   483   assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   484   from this[unfolded convex_on_def, rule_format]
   485   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
   486   from this[of "\<mu>" "1 - \<mu>", simplified] asms
   487   show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   488 next
   489   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"
   490   { fix x y
   491     fix u v :: real
   492     assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   493     then have[simp]: "1 - u = v" by auto
   494     from asm[rule_format, of x y u]
   495     have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
   496   }
   497   then show "convex_on C f" unfolding convex_on_def by auto
   498 qed
   499 
   500 lemma convex_on_diff:
   501   fixes f :: "real \<Rightarrow> real"
   502   assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
   503   shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   504     "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   505 proof -
   506   def a \<equiv> "(t - y) / (x - y)"
   507   with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
   508   with f `x \<in> I` `y \<in> I` have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   509     by (auto simp: convex_on_def)
   510   have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps)
   511   also have "\<dots> = t" unfolding a_def using `x < t` `t < y` by simp
   512   finally have "f t \<le> a * f x + (1 - a) * f y" using cvx by simp
   513   also have "\<dots> = a * (f x - f y) + f y" by (simp add: field_simps)
   514   finally have "f t - f y \<le> a * (f x - f y)" by simp
   515   with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   516     by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   517   with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   518     by (simp add: le_divide_eq divide_le_eq field_simps)
   519 qed
   520 
   521 lemma pos_convex_function:
   522   fixes f :: "real \<Rightarrow> real"
   523   assumes "convex C"
   524     and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   525   shows "convex_on C f"
   526   unfolding convex_on_alt[OF assms(1)]
   527   using assms
   528 proof safe
   529   fix x y \<mu> :: real
   530   let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   531   assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   532   then have "1 - \<mu> \<ge> 0" by auto
   533   then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
   534   have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
   535             \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   536     using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
   537       mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
   538   then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   539     by (auto simp add: field_simps)
   540   then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   541     using convex_on_alt by auto
   542 qed
   543 
   544 lemma atMostAtLeast_subset_convex:
   545   fixes C :: "real set"
   546   assumes "convex C"
   547     and "x \<in> C" "y \<in> C" "x < y"
   548   shows "{x .. y} \<subseteq> C"
   549 proof safe
   550   fix z assume zasm: "z \<in> {x .. y}"
   551   { assume asm: "x < z" "z < y"
   552     let ?\<mu> = "(y - z) / (y - x)"
   553     have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
   554     then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   555       using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   556       by (simp add: algebra_simps)
   557     have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   558       by (auto simp add: field_simps)
   559     also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   560       using assms unfolding add_divide_distrib by (auto simp: field_simps)
   561     also have "\<dots> = z"
   562       using assms by (auto simp: field_simps)
   563     finally have "z \<in> C"
   564       using comb by auto }
   565   note less = this
   566   show "z \<in> C" using zasm less assms
   567     unfolding atLeastAtMost_iff le_less by auto
   568 qed
   569 
   570 lemma f''_imp_f':
   571   fixes f :: "real \<Rightarrow> real"
   572   assumes "convex C"
   573     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   574     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   575     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   576     and "x \<in> C" "y \<in> C"
   577   shows "f' x * (y - x) \<le> f y - f x"
   578   using assms
   579 proof -
   580   { fix x y :: real
   581     assume asm: "x \<in> C" "y \<in> C" "y > x"
   582     then have ge: "y - x > 0" "y - x \<ge> 0" by auto
   583     from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   584     then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   585       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
   586         THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   587       by auto
   588     then have "z1 \<in> C" using atMostAtLeast_subset_convex
   589       `convex C` `x \<in> C` `y \<in> C` `x < y` by fastforce
   590     from z1 have z1': "f x - f y = (x - y) * f' z1"
   591       by (simp add:field_simps)
   592     obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   593       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `z1 \<in> C` `x < z1`],
   594         THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   595       by auto
   596     obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   597       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `z1 \<in> C` `y \<in> C` `z1 < y`],
   598         THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   599       by auto
   600     have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   601       using asm z1' by auto
   602     also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
   603     finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
   604     have A': "y - z1 \<ge> 0" using z1 by auto
   605     have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
   606       `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastforce
   607     then have B': "f'' z3 \<ge> 0" using assms by auto
   608     from A' B' have "(y - z1) * f'' z3 \<ge> 0" using mult_nonneg_nonneg by auto
   609     from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
   610     from mult_right_mono_neg[OF this le(2)]
   611     have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   612       by (simp add: algebra_simps)
   613     then have "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   614     then have res: "f' y * (x - y) \<le> f x - f y" by auto
   615     have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   616       using asm z1 by auto
   617     also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
   618     finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
   619     have A: "z1 - x \<ge> 0" using z1 by auto
   620     have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
   621       `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastforce
   622     then have B: "f'' z2 \<ge> 0" using assms by auto
   623     from A B have "(z1 - x) * f'' z2 \<ge> 0" using mult_nonneg_nonneg by auto
   624     from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
   625     from mult_right_mono[OF this ge(2)]
   626     have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   627       by (simp add: algebra_simps)
   628     then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   629     then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   630       using res by auto } note less_imp = this
   631   { fix x y :: real
   632     assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   633     then have"f y - f x \<ge> f' x * (y - x)"
   634     unfolding neq_iff using less_imp by auto } note neq_imp = this
   635   moreover
   636   { fix x y :: real
   637     assume asm: "x \<in> C" "y \<in> C" "x = y"
   638     then have "f y - f x \<ge> f' x * (y - x)" by auto }
   639   ultimately show ?thesis using assms by blast
   640 qed
   641 
   642 lemma f''_ge0_imp_convex:
   643   fixes f :: "real \<Rightarrow> real"
   644   assumes conv: "convex C"
   645     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   646     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   647     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   648   shows "convex_on C f"
   649 using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
   650 
   651 lemma minus_log_convex:
   652   fixes b :: real
   653   assumes "b > 1"
   654   shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   655 proof -
   656   have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   657   then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   658     by (auto simp: DERIV_minus)
   659   have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   660     using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   661   from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   662   have "\<And>z :: real. z > 0 \<Longrightarrow>
   663     DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   664     by auto
   665   then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   666     unfolding inverse_eq_divide by (auto simp add: mult_assoc)
   667   have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   668     using `b > 1` by (auto intro!:less_imp_le simp add: divide_pos_pos[of 1] mult_pos_pos)
   669   from f''_ge0_imp_convex[OF pos_is_convex,
   670     unfolded greaterThan_iff, OF f' f''0 f''_ge0]
   671   show ?thesis by auto
   672 qed
   673 
   674 end