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