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