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