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