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