src/HOL/Library/Convex.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62376 85f38d5f8807
child 62418 f1b0908028cf
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
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@60423
     6
section \<open>Convexity in real vector spaces\<close>
hoelzl@36648
     7
hoelzl@36623
     8
theory Convex
hoelzl@36623
     9
imports Product_Vector
hoelzl@36623
    10
begin
hoelzl@36623
    11
wenzelm@60423
    12
subsection \<open>Convexity\<close>
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
lp15@61426
    46
lemma convexD_alt:
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
lp15@61520
    51
lemma mem_convex_alt:
lp15@61520
    52
  assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
lp15@61520
    53
  shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
lp15@61520
    54
  apply (rule convexD)
lp15@61520
    55
  using assms
lp15@61520
    56
  apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
lp15@61520
    57
  done
lp15@61520
    58
paulson@60303
    59
lemma convex_empty[intro,simp]: "convex {}"
hoelzl@36623
    60
  unfolding convex_def by simp
hoelzl@36623
    61
paulson@60303
    62
lemma convex_singleton[intro,simp]: "convex {a}"
hoelzl@36623
    63
  unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
hoelzl@36623
    64
paulson@60303
    65
lemma convex_UNIV[intro,simp]: "convex UNIV"
hoelzl@36623
    66
  unfolding convex_def by auto
hoelzl@36623
    67
wenzelm@60423
    68
lemma convex_Inter: "(\<forall>s\<in>f. convex s) \<Longrightarrow> convex(\<Inter>f)"
hoelzl@36623
    69
  unfolding convex_def by auto
hoelzl@36623
    70
hoelzl@36623
    71
lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
hoelzl@36623
    72
  unfolding convex_def by auto
hoelzl@36623
    73
huffman@53596
    74
lemma convex_INT: "\<forall>i\<in>A. convex (B i) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
huffman@53596
    75
  unfolding convex_def by auto
huffman@53596
    76
huffman@53596
    77
lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
huffman@53596
    78
  unfolding convex_def by auto
huffman@53596
    79
hoelzl@36623
    80
lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
hoelzl@36623
    81
  unfolding convex_def
huffman@44142
    82
  by (auto simp: inner_add intro!: convex_bound_le)
hoelzl@36623
    83
hoelzl@36623
    84
lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
hoelzl@36623
    85
proof -
wenzelm@56796
    86
  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
wenzelm@56796
    87
    by auto
wenzelm@56796
    88
  show ?thesis
wenzelm@56796
    89
    unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
hoelzl@36623
    90
qed
hoelzl@36623
    91
hoelzl@36623
    92
lemma convex_hyperplane: "convex {x. inner a x = b}"
wenzelm@49609
    93
proof -
wenzelm@56796
    94
  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
wenzelm@56796
    95
    by auto
hoelzl@36623
    96
  show ?thesis using convex_halfspace_le convex_halfspace_ge
hoelzl@36623
    97
    by (auto intro!: convex_Int simp: *)
hoelzl@36623
    98
qed
hoelzl@36623
    99
hoelzl@36623
   100
lemma convex_halfspace_lt: "convex {x. inner a x < b}"
hoelzl@36623
   101
  unfolding convex_def
hoelzl@36623
   102
  by (auto simp: convex_bound_lt inner_add)
hoelzl@36623
   103
hoelzl@36623
   104
lemma convex_halfspace_gt: "convex {x. inner a x > b}"
hoelzl@36623
   105
   using convex_halfspace_lt[of "-a" "-b"] by auto
hoelzl@36623
   106
paulson@61518
   107
lemma convex_real_interval [iff]:
hoelzl@36623
   108
  fixes a b :: "real"
hoelzl@36623
   109
  shows "convex {a..}" and "convex {..b}"
wenzelm@49609
   110
    and "convex {a<..}" and "convex {..<b}"
wenzelm@49609
   111
    and "convex {a..b}" and "convex {a<..b}"
wenzelm@49609
   112
    and "convex {a..<b}" and "convex {a<..<b}"
hoelzl@36623
   113
proof -
wenzelm@60423
   114
  have "{a..} = {x. a \<le> inner 1 x}"
wenzelm@60423
   115
    by auto
wenzelm@60423
   116
  then show 1: "convex {a..}"
wenzelm@60423
   117
    by (simp only: convex_halfspace_ge)
wenzelm@60423
   118
  have "{..b} = {x. inner 1 x \<le> b}"
wenzelm@60423
   119
    by auto
wenzelm@60423
   120
  then show 2: "convex {..b}"
wenzelm@60423
   121
    by (simp only: convex_halfspace_le)
wenzelm@60423
   122
  have "{a<..} = {x. a < inner 1 x}"
wenzelm@60423
   123
    by auto
wenzelm@60423
   124
  then show 3: "convex {a<..}"
wenzelm@60423
   125
    by (simp only: convex_halfspace_gt)
wenzelm@60423
   126
  have "{..<b} = {x. inner 1 x < b}"
wenzelm@60423
   127
    by auto
wenzelm@60423
   128
  then show 4: "convex {..<b}"
wenzelm@60423
   129
    by (simp only: convex_halfspace_lt)
wenzelm@60423
   130
  have "{a..b} = {a..} \<inter> {..b}"
wenzelm@60423
   131
    by auto
wenzelm@60423
   132
  then show "convex {a..b}"
wenzelm@60423
   133
    by (simp only: convex_Int 1 2)
wenzelm@60423
   134
  have "{a<..b} = {a<..} \<inter> {..b}"
wenzelm@60423
   135
    by auto
wenzelm@60423
   136
  then show "convex {a<..b}"
wenzelm@60423
   137
    by (simp only: convex_Int 3 2)
wenzelm@60423
   138
  have "{a..<b} = {a..} \<inter> {..<b}"
wenzelm@60423
   139
    by auto
wenzelm@60423
   140
  then show "convex {a..<b}"
wenzelm@60423
   141
    by (simp only: convex_Int 1 4)
wenzelm@60423
   142
  have "{a<..<b} = {a<..} \<inter> {..<b}"
wenzelm@60423
   143
    by auto
wenzelm@60423
   144
  then show "convex {a<..<b}"
wenzelm@60423
   145
    by (simp only: convex_Int 3 4)
hoelzl@36623
   146
qed
hoelzl@36623
   147
wenzelm@61070
   148
lemma convex_Reals: "convex \<real>"
lp15@59862
   149
  by (simp add: convex_def scaleR_conv_of_real)
wenzelm@60423
   150
wenzelm@60423
   151
wenzelm@60423
   152
subsection \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
hoelzl@36623
   153
hoelzl@36623
   154
lemma convex_setsum:
hoelzl@36623
   155
  fixes C :: "'a::real_vector set"
wenzelm@56796
   156
  assumes "finite s"
wenzelm@56796
   157
    and "convex C"
wenzelm@56796
   158
    and "(\<Sum> i \<in> s. a i) = 1"
wenzelm@56796
   159
  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
wenzelm@56796
   160
    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
hoelzl@36623
   161
  shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
huffman@55909
   162
  using assms(1,3,4,5)
huffman@55909
   163
proof (induct arbitrary: a set: finite)
wenzelm@49609
   164
  case empty
huffman@55909
   165
  then show ?case by simp
hoelzl@36623
   166
next
huffman@55909
   167
  case (insert i s) note IH = this(3)
wenzelm@56796
   168
  have "a i + setsum a s = 1"
wenzelm@56796
   169
    and "0 \<le> a i"
wenzelm@56796
   170
    and "\<forall>j\<in>s. 0 \<le> a j"
wenzelm@56796
   171
    and "y i \<in> C"
wenzelm@56796
   172
    and "\<forall>j\<in>s. y j \<in> C"
huffman@55909
   173
    using insert.hyps(1,2) insert.prems by simp_all
wenzelm@56796
   174
  then have "0 \<le> setsum a s"
wenzelm@56796
   175
    by (simp add: setsum_nonneg)
huffman@55909
   176
  have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
huffman@55909
   177
  proof (cases)
huffman@55909
   178
    assume z: "setsum a s = 0"
wenzelm@60423
   179
    with \<open>a i + setsum a s = 1\<close> have "a i = 1"
wenzelm@56796
   180
      by simp
wenzelm@60423
   181
    from setsum_nonneg_0 [OF \<open>finite s\<close> _ z] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
wenzelm@56796
   182
      by simp
wenzelm@60423
   183
    show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
wenzelm@56796
   184
      by simp
huffman@55909
   185
  next
huffman@55909
   186
    assume nz: "setsum a s \<noteq> 0"
wenzelm@60423
   187
    with \<open>0 \<le> setsum a s\<close> have "0 < setsum a s"
wenzelm@56796
   188
      by simp
huffman@55909
   189
    then have "(\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
wenzelm@60423
   190
      using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
hoelzl@56571
   191
      by (simp add: IH setsum_divide_distrib [symmetric])
wenzelm@60423
   192
    from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
wenzelm@60423
   193
      and \<open>0 \<le> setsum a s\<close> and \<open>a i + setsum a s = 1\<close>
huffman@55909
   194
    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
   195
      by (rule convexD)
wenzelm@56796
   196
    then show ?thesis
wenzelm@56796
   197
      by (simp add: scaleR_setsum_right nz)
huffman@55909
   198
  qed
wenzelm@60423
   199
  then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
wenzelm@56796
   200
    by simp
hoelzl@36623
   201
qed
hoelzl@36623
   202
hoelzl@36623
   203
lemma convex:
wenzelm@49609
   204
  "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
   205
      \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
hoelzl@36623
   206
proof safe
wenzelm@49609
   207
  fix k :: nat
wenzelm@49609
   208
  fix u :: "nat \<Rightarrow> real"
wenzelm@49609
   209
  fix x
hoelzl@36623
   210
  assume "convex s"
hoelzl@36623
   211
    "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
hoelzl@36623
   212
    "setsum u {1..k} = 1"
wenzelm@60423
   213
  with convex_setsum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
wenzelm@56796
   214
    by auto
hoelzl@36623
   215
next
wenzelm@60423
   216
  assume *: "\<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
   217
    \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
wenzelm@56796
   218
  {
wenzelm@56796
   219
    fix \<mu> :: real
wenzelm@49609
   220
    fix x y :: 'a
wenzelm@49609
   221
    assume xy: "x \<in> s" "y \<in> s"
wenzelm@49609
   222
    assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
wenzelm@49609
   223
    let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
wenzelm@49609
   224
    let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
wenzelm@56796
   225
    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
wenzelm@56796
   226
      by auto
wenzelm@56796
   227
    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
wenzelm@56796
   228
      by simp
wenzelm@49609
   229
    then have "setsum ?u {1 .. 2} = 1"
haftmann@57418
   230
      using setsum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
hoelzl@36623
   231
      by auto
wenzelm@60423
   232
    with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
hoelzl@36623
   233
      using mu xy by auto
hoelzl@36623
   234
    have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
hoelzl@36623
   235
      using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
hoelzl@36623
   236
    from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
wenzelm@56796
   237
    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
wenzelm@56796
   238
      by auto
wenzelm@56796
   239
    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
wenzelm@60423
   240
      using s by (auto simp: add.commute)
wenzelm@49609
   241
  }
wenzelm@56796
   242
  then show "convex s"
wenzelm@56796
   243
    unfolding convex_alt by auto
hoelzl@36623
   244
qed
hoelzl@36623
   245
hoelzl@36623
   246
hoelzl@36623
   247
lemma convex_explicit:
hoelzl@36623
   248
  fixes s :: "'a::real_vector set"
hoelzl@36623
   249
  shows "convex s \<longleftrightarrow>
wenzelm@49609
   250
    (\<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
   251
proof safe
wenzelm@49609
   252
  fix t
wenzelm@49609
   253
  fix u :: "'a \<Rightarrow> real"
wenzelm@56796
   254
  assume "convex s"
wenzelm@56796
   255
    and "finite t"
wenzelm@56796
   256
    and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
wenzelm@49609
   257
  then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
hoelzl@36623
   258
    using convex_setsum[of t s u "\<lambda> x. x"] by auto
hoelzl@36623
   259
next
wenzelm@60423
   260
  assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
wenzelm@56796
   261
    setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
hoelzl@36623
   262
  show "convex s"
hoelzl@36623
   263
    unfolding convex_alt
hoelzl@36623
   264
  proof safe
wenzelm@49609
   265
    fix x y
wenzelm@49609
   266
    fix \<mu> :: real
wenzelm@60423
   267
    assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
wenzelm@60423
   268
    show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
wenzelm@60423
   269
    proof (cases "x = y")
wenzelm@60423
   270
      case False
wenzelm@60423
   271
      then show ?thesis
wenzelm@60423
   272
        using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
wenzelm@60423
   273
          by auto
wenzelm@60423
   274
    next
wenzelm@60423
   275
      case True
wenzelm@60423
   276
      then show ?thesis
wenzelm@60423
   277
        using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
wenzelm@60423
   278
          by (auto simp: field_simps real_vector.scale_left_diff_distrib)
wenzelm@60423
   279
    qed
hoelzl@36623
   280
  qed
hoelzl@36623
   281
qed
hoelzl@36623
   282
wenzelm@49609
   283
lemma convex_finite:
wenzelm@49609
   284
  assumes "finite s"
wenzelm@56796
   285
  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
   286
  unfolding convex_explicit
wenzelm@49609
   287
proof safe
wenzelm@49609
   288
  fix t u
wenzelm@49609
   289
  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
   290
    and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
wenzelm@56796
   291
  have *: "s \<inter> t = t"
wenzelm@56796
   292
    using as(2) by auto
wenzelm@49609
   293
  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
   294
    by simp
hoelzl@36623
   295
  show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
hoelzl@36623
   296
   using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
haftmann@57418
   297
   by (auto simp: assms setsum.If_cases if_distrib if_distrib_arg)
hoelzl@36623
   298
qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
hoelzl@36623
   299
wenzelm@56796
   300
wenzelm@60423
   301
subsection \<open>Functions that are convex on a set\<close>
huffman@55909
   302
wenzelm@49609
   303
definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
wenzelm@49609
   304
  where "convex_on s f \<longleftrightarrow>
wenzelm@49609
   305
    (\<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
   306
eberlm@61531
   307
lemma convex_onI [intro?]:
eberlm@61531
   308
  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
eberlm@61531
   309
             f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
eberlm@61531
   310
  shows   "convex_on A f"
eberlm@61531
   311
  unfolding convex_on_def
eberlm@61531
   312
proof clarify
eberlm@61531
   313
  fix x y u v assume A: "x \<in> A" "y \<in> A" "(u::real) \<ge> 0" "v \<ge> 0" "u + v = 1"
eberlm@61531
   314
  from A(5) have [simp]: "v = 1 - u" by (simp add: algebra_simps)
eberlm@61531
   315
  from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using assms[of u y x]
eberlm@61531
   316
    by (cases "u = 0 \<or> u = 1") (auto simp: algebra_simps)
eberlm@61531
   317
qed
eberlm@61531
   318
eberlm@61531
   319
lemma convex_on_linorderI [intro?]:
eberlm@61531
   320
  fixes A :: "('a::{linorder,real_vector}) set"
eberlm@61531
   321
  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x < y \<Longrightarrow>
eberlm@61531
   322
             f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
eberlm@61531
   323
  shows   "convex_on A f"
eberlm@61531
   324
proof
eberlm@61531
   325
  fix t x y assume A: "x \<in> A" "y \<in> A" "(t::real) > 0" "t < 1"
eberlm@61531
   326
  case (goal1 t x y)
eberlm@61531
   327
  with goal1 assms[of t x y] assms[of "1 - t" y x]
eberlm@61531
   328
    show ?case by (cases x y rule: linorder_cases) (auto simp: algebra_simps)
eberlm@61531
   329
qed
eberlm@61531
   330
eberlm@61531
   331
lemma convex_onD:
eberlm@61531
   332
  assumes "convex_on A f"
eberlm@61531
   333
  shows   "\<And>t x y. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
eberlm@61531
   334
             f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
eberlm@61531
   335
  using assms unfolding convex_on_def by auto
eberlm@61531
   336
eberlm@61531
   337
lemma convex_onD_Icc:
eberlm@61531
   338
  assumes "convex_on {x..y} f" "x \<le> (y :: _ :: {real_vector,preorder})"
eberlm@61531
   339
  shows   "\<And>t. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow>
eberlm@61531
   340
             f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
eberlm@61531
   341
  using assms(2) by (intro convex_onD[OF assms(1)]) simp_all
eberlm@61531
   342
hoelzl@36623
   343
lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
hoelzl@36623
   344
  unfolding convex_on_def by auto
hoelzl@36623
   345
huffman@53620
   346
lemma convex_on_add [intro]:
wenzelm@56796
   347
  assumes "convex_on s f"
wenzelm@56796
   348
    and "convex_on s g"
hoelzl@36623
   349
  shows "convex_on s (\<lambda>x. f x + g x)"
wenzelm@49609
   350
proof -
wenzelm@56796
   351
  {
wenzelm@56796
   352
    fix x y
wenzelm@56796
   353
    assume "x \<in> s" "y \<in> s"
wenzelm@49609
   354
    moreover
wenzelm@49609
   355
    fix u v :: real
wenzelm@49609
   356
    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
wenzelm@49609
   357
    ultimately
wenzelm@49609
   358
    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@60423
   359
      using assms unfolding convex_on_def by (auto simp: add_mono)
wenzelm@49609
   360
    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
   361
      by (simp add: field_simps)
wenzelm@49609
   362
  }
wenzelm@56796
   363
  then show ?thesis
wenzelm@56796
   364
    unfolding convex_on_def by auto
hoelzl@36623
   365
qed
hoelzl@36623
   366
huffman@53620
   367
lemma convex_on_cmul [intro]:
wenzelm@56796
   368
  fixes c :: real
wenzelm@56796
   369
  assumes "0 \<le> c"
wenzelm@56796
   370
    and "convex_on s f"
hoelzl@36623
   371
  shows "convex_on s (\<lambda>x. c * f x)"
wenzelm@56796
   372
proof -
wenzelm@60423
   373
  have *: "\<And>u c fx v fy :: real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
wenzelm@49609
   374
    by (simp add: field_simps)
wenzelm@49609
   375
  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
wenzelm@49609
   376
    unfolding convex_on_def and * by auto
hoelzl@36623
   377
qed
hoelzl@36623
   378
hoelzl@36623
   379
lemma convex_lower:
wenzelm@56796
   380
  assumes "convex_on s f"
wenzelm@56796
   381
    and "x \<in> s"
wenzelm@56796
   382
    and "y \<in> s"
wenzelm@56796
   383
    and "0 \<le> u"
wenzelm@56796
   384
    and "0 \<le> v"
wenzelm@56796
   385
    and "u + v = 1"
hoelzl@36623
   386
  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
wenzelm@56796
   387
proof -
hoelzl@36623
   388
  let ?m = "max (f x) (f y)"
hoelzl@36623
   389
  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
wenzelm@60423
   390
    using assms(4,5) by (auto simp: mult_left_mono add_mono)
wenzelm@56796
   391
  also have "\<dots> = max (f x) (f y)"
wenzelm@60423
   392
    using assms(6) by (simp add: distrib_right [symmetric])
hoelzl@36623
   393
  finally show ?thesis
nipkow@44890
   394
    using assms unfolding convex_on_def by fastforce
hoelzl@36623
   395
qed
hoelzl@36623
   396
huffman@53620
   397
lemma convex_on_dist [intro]:
hoelzl@36623
   398
  fixes s :: "'a::real_normed_vector set"
hoelzl@36623
   399
  shows "convex_on s (\<lambda>x. dist a x)"
wenzelm@60423
   400
proof (auto simp: convex_on_def dist_norm)
wenzelm@49609
   401
  fix x y
wenzelm@56796
   402
  assume "x \<in> s" "y \<in> s"
wenzelm@49609
   403
  fix u v :: real
wenzelm@56796
   404
  assume "0 \<le> u"
wenzelm@56796
   405
  assume "0 \<le> v"
wenzelm@56796
   406
  assume "u + v = 1"
wenzelm@49609
   407
  have "a = u *\<^sub>R a + v *\<^sub>R a"
wenzelm@60423
   408
    unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
wenzelm@49609
   409
  then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
wenzelm@60423
   410
    by (auto simp: algebra_simps)
hoelzl@36623
   411
  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
hoelzl@36623
   412
    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
wenzelm@60423
   413
    using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
hoelzl@36623
   414
qed
hoelzl@36623
   415
wenzelm@49609
   416
wenzelm@60423
   417
subsection \<open>Arithmetic operations on sets preserve convexity\<close>
wenzelm@49609
   418
huffman@53620
   419
lemma convex_linear_image:
wenzelm@56796
   420
  assumes "linear f"
wenzelm@56796
   421
    and "convex s"
wenzelm@56796
   422
  shows "convex (f ` s)"
huffman@53620
   423
proof -
huffman@53620
   424
  interpret f: linear f by fact
wenzelm@60423
   425
  from \<open>convex s\<close> show "convex (f ` s)"
huffman@53620
   426
    by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
hoelzl@36623
   427
qed
hoelzl@36623
   428
huffman@53620
   429
lemma convex_linear_vimage:
wenzelm@56796
   430
  assumes "linear f"
wenzelm@56796
   431
    and "convex s"
wenzelm@56796
   432
  shows "convex (f -` s)"
huffman@53620
   433
proof -
huffman@53620
   434
  interpret f: linear f by fact
wenzelm@60423
   435
  from \<open>convex s\<close> show "convex (f -` s)"
huffman@53620
   436
    by (simp add: convex_def f.add f.scaleR)
huffman@53620
   437
qed
huffman@53620
   438
huffman@53620
   439
lemma convex_scaling:
wenzelm@56796
   440
  assumes "convex s"
wenzelm@56796
   441
  shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
huffman@53620
   442
proof -
wenzelm@56796
   443
  have "linear (\<lambda>x. c *\<^sub>R x)"
wenzelm@56796
   444
    by (simp add: linearI scaleR_add_right)
wenzelm@56796
   445
  then show ?thesis
wenzelm@60423
   446
    using \<open>convex s\<close> by (rule convex_linear_image)
huffman@53620
   447
qed
huffman@53620
   448
immler@60178
   449
lemma convex_scaled:
immler@60178
   450
  assumes "convex s"
immler@60178
   451
  shows "convex ((\<lambda>x. x *\<^sub>R c) ` s)"
immler@60178
   452
proof -
immler@60178
   453
  have "linear (\<lambda>x. x *\<^sub>R c)"
immler@60178
   454
    by (simp add: linearI scaleR_add_left)
immler@60178
   455
  then show ?thesis
wenzelm@60423
   456
    using \<open>convex s\<close> by (rule convex_linear_image)
immler@60178
   457
qed
immler@60178
   458
huffman@53620
   459
lemma convex_negations:
wenzelm@56796
   460
  assumes "convex s"
wenzelm@56796
   461
  shows "convex ((\<lambda>x. - x) ` s)"
huffman@53620
   462
proof -
wenzelm@56796
   463
  have "linear (\<lambda>x. - x)"
wenzelm@56796
   464
    by (simp add: linearI)
wenzelm@56796
   465
  then show ?thesis
wenzelm@60423
   466
    using \<open>convex s\<close> by (rule convex_linear_image)
hoelzl@36623
   467
qed
hoelzl@36623
   468
hoelzl@36623
   469
lemma convex_sums:
wenzelm@56796
   470
  assumes "convex s"
wenzelm@56796
   471
    and "convex t"
hoelzl@36623
   472
  shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
huffman@53620
   473
proof -
huffman@53620
   474
  have "linear (\<lambda>(x, y). x + y)"
wenzelm@60423
   475
    by (auto intro: linearI simp: scaleR_add_right)
huffman@53620
   476
  with assms have "convex ((\<lambda>(x, y). x + y) ` (s \<times> t))"
huffman@53620
   477
    by (intro convex_linear_image convex_Times)
huffman@53620
   478
  also have "((\<lambda>(x, y). x + y) ` (s \<times> t)) = {x + y| x y. x \<in> s \<and> y \<in> t}"
huffman@53620
   479
    by auto
huffman@53620
   480
  finally show ?thesis .
hoelzl@36623
   481
qed
hoelzl@36623
   482
hoelzl@36623
   483
lemma convex_differences:
hoelzl@36623
   484
  assumes "convex s" "convex t"
hoelzl@36623
   485
  shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
hoelzl@36623
   486
proof -
hoelzl@36623
   487
  have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
wenzelm@60423
   488
    by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
wenzelm@49609
   489
  then show ?thesis
wenzelm@49609
   490
    using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
hoelzl@36623
   491
qed
hoelzl@36623
   492
wenzelm@49609
   493
lemma convex_translation:
wenzelm@49609
   494
  assumes "convex s"
wenzelm@49609
   495
  shows "convex ((\<lambda>x. a + x) ` s)"
wenzelm@49609
   496
proof -
wenzelm@56796
   497
  have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s"
wenzelm@56796
   498
    by auto
wenzelm@49609
   499
  then show ?thesis
wenzelm@49609
   500
    using convex_sums[OF convex_singleton[of a] assms] by auto
wenzelm@49609
   501
qed
hoelzl@36623
   502
wenzelm@49609
   503
lemma convex_affinity:
wenzelm@49609
   504
  assumes "convex s"
wenzelm@49609
   505
  shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
wenzelm@49609
   506
proof -
wenzelm@56796
   507
  have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s"
wenzelm@56796
   508
    by auto
wenzelm@49609
   509
  then show ?thesis
wenzelm@49609
   510
    using convex_translation[OF convex_scaling[OF assms], of a c] by auto
wenzelm@49609
   511
qed
hoelzl@36623
   512
wenzelm@49609
   513
lemma pos_is_convex: "convex {0 :: real <..}"
wenzelm@49609
   514
  unfolding convex_alt
hoelzl@36623
   515
proof safe
hoelzl@36623
   516
  fix y x \<mu> :: real
wenzelm@60423
   517
  assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
wenzelm@56796
   518
  {
wenzelm@56796
   519
    assume "\<mu> = 0"
wenzelm@49609
   520
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
wenzelm@60423
   521
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using * by simp
wenzelm@56796
   522
  }
hoelzl@36623
   523
  moreover
wenzelm@56796
   524
  {
wenzelm@56796
   525
    assume "\<mu> = 1"
wenzelm@60423
   526
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using * by simp
wenzelm@56796
   527
  }
hoelzl@36623
   528
  moreover
wenzelm@56796
   529
  {
wenzelm@56796
   530
    assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
wenzelm@60423
   531
    then have "\<mu> > 0" "(1 - \<mu>) > 0" using * by auto
wenzelm@60423
   532
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using *
wenzelm@60423
   533
      by (auto simp: add_pos_pos)
wenzelm@56796
   534
  }
wenzelm@56796
   535
  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
wenzelm@56796
   536
    using assms by fastforce
hoelzl@36623
   537
qed
hoelzl@36623
   538
hoelzl@36623
   539
lemma convex_on_setsum:
hoelzl@36623
   540
  fixes a :: "'a \<Rightarrow> real"
wenzelm@49609
   541
    and y :: "'a \<Rightarrow> 'b::real_vector"
wenzelm@49609
   542
    and f :: "'b \<Rightarrow> real"
hoelzl@36623
   543
  assumes "finite s" "s \<noteq> {}"
wenzelm@49609
   544
    and "convex_on C f"
wenzelm@49609
   545
    and "convex C"
wenzelm@49609
   546
    and "(\<Sum> i \<in> s. a i) = 1"
wenzelm@49609
   547
    and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
wenzelm@49609
   548
    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
hoelzl@36623
   549
  shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
wenzelm@49609
   550
  using assms
wenzelm@49609
   551
proof (induct s arbitrary: a rule: finite_ne_induct)
hoelzl@36623
   552
  case (singleton i)
wenzelm@49609
   553
  then have ai: "a i = 1" by auto
wenzelm@49609
   554
  then show ?case by auto
hoelzl@36623
   555
next
wenzelm@60423
   556
  case (insert i s)
wenzelm@49609
   557
  then have "convex_on C f" by simp
hoelzl@36623
   558
  from this[unfolded convex_on_def, rule_format]
wenzelm@56796
   559
  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<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
    by simp
wenzelm@60423
   562
  show ?case
wenzelm@60423
   563
  proof (cases "a i = 1")
wenzelm@60423
   564
    case True
wenzelm@49609
   565
    then have "(\<Sum> j \<in> s. a j) = 0"
wenzelm@60423
   566
      using insert by auto
wenzelm@49609
   567
    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
hoelzl@62376
   568
      using insert by (fastforce simp: setsum_nonneg_eq_0_iff)
wenzelm@60423
   569
    then show ?thesis
wenzelm@60423
   570
      using insert by auto
wenzelm@60423
   571
  next
wenzelm@60423
   572
    case False
wenzelm@60423
   573
    from insert have yai: "y i \<in> C" "a i \<ge> 0"
wenzelm@60423
   574
      by auto
wenzelm@60423
   575
    have fis: "finite (insert i s)"
wenzelm@60423
   576
      using insert by auto
wenzelm@60423
   577
    then have ai1: "a i \<le> 1"
wenzelm@60423
   578
      using setsum_nonneg_leq_bound[of "insert i s" a] insert by simp
wenzelm@60423
   579
    then have "a i < 1"
wenzelm@60423
   580
      using False by auto
wenzelm@60423
   581
    then have i0: "1 - a i > 0"
wenzelm@60423
   582
      by auto
wenzelm@49609
   583
    let ?a = "\<lambda>j. a j / (1 - a i)"
wenzelm@60423
   584
    have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
wenzelm@60449
   585
      using i0 insert that by fastforce
wenzelm@60423
   586
    have "(\<Sum> j \<in> insert i s. a j) = 1"
wenzelm@60423
   587
      using insert by auto
wenzelm@60423
   588
    then have "(\<Sum> j \<in> s. a j) = 1 - a i"
wenzelm@60423
   589
      using setsum.insert insert by fastforce
wenzelm@60423
   590
    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
wenzelm@60423
   591
      using i0 by auto
wenzelm@60423
   592
    then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
wenzelm@60423
   593
      unfolding setsum_divide_distrib by simp
wenzelm@60423
   594
    have "convex C" using insert by auto
wenzelm@49609
   595
    then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
wenzelm@60423
   596
      using insert convex_setsum[OF \<open>finite s\<close>
wenzelm@60423
   597
        \<open>convex C\<close> a1 a_nonneg] by auto
hoelzl@36623
   598
    have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
wenzelm@60423
   599
      using a_nonneg a1 insert by blast
hoelzl@36623
   600
    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)"
wenzelm@60423
   601
      using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
wenzelm@60423
   602
      by (auto simp only: add.commute)
hoelzl@36623
   603
    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
   604
      using i0 by auto
hoelzl@36623
   605
    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
   606
      using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
wenzelm@60423
   607
      by (auto simp: algebra_simps)
hoelzl@36623
   608
    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
   609
      by (auto simp: divide_inverse)
hoelzl@36623
   610
    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
   611
      using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
wenzelm@60423
   612
      by (auto simp: add.commute)
hoelzl@36623
   613
    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
hoelzl@36623
   614
      using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
hoelzl@36623
   615
        OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
hoelzl@36623
   616
    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
huffman@44282
   617
      unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
wenzelm@60423
   618
    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
wenzelm@60423
   619
      using i0 by auto
wenzelm@60423
   620
    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
wenzelm@60423
   621
      using insert by auto
wenzelm@60423
   622
    finally show ?thesis
wenzelm@56796
   623
      by simp
wenzelm@60423
   624
  qed
hoelzl@36623
   625
qed
hoelzl@36623
   626
hoelzl@36623
   627
lemma convex_on_alt:
hoelzl@36623
   628
  fixes C :: "'a::real_vector set"
hoelzl@36623
   629
  assumes "convex C"
wenzelm@56796
   630
  shows "convex_on C f \<longleftrightarrow>
wenzelm@56796
   631
    (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
wenzelm@56796
   632
      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
hoelzl@36623
   633
proof safe
wenzelm@49609
   634
  fix x y
wenzelm@49609
   635
  fix \<mu> :: real
wenzelm@60423
   636
  assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
hoelzl@36623
   637
  from this[unfolded convex_on_def, rule_format]
wenzelm@56796
   638
  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
   639
    by auto
wenzelm@60423
   640
  from this[of "\<mu>" "1 - \<mu>", simplified] *
wenzelm@56796
   641
  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
wenzelm@56796
   642
    by auto
hoelzl@36623
   643
next
wenzelm@60423
   644
  assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
wenzelm@56796
   645
    f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
wenzelm@56796
   646
  {
wenzelm@56796
   647
    fix x y
wenzelm@49609
   648
    fix u v :: real
wenzelm@60423
   649
    assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
wenzelm@49609
   650
    then have[simp]: "1 - u = v" by auto
wenzelm@60423
   651
    from *[rule_format, of x y u]
wenzelm@56796
   652
    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
wenzelm@60423
   653
      using ** by auto
wenzelm@49609
   654
  }
wenzelm@56796
   655
  then show "convex_on C f"
wenzelm@56796
   656
    unfolding convex_on_def by auto
hoelzl@36623
   657
qed
hoelzl@36623
   658
hoelzl@43337
   659
lemma convex_on_diff:
hoelzl@43337
   660
  fixes f :: "real \<Rightarrow> real"
wenzelm@56796
   661
  assumes f: "convex_on I f"
wenzelm@56796
   662
    and I: "x \<in> I" "y \<in> I"
wenzelm@56796
   663
    and t: "x < t" "t < y"
wenzelm@49609
   664
  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
wenzelm@56796
   665
    and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
hoelzl@43337
   666
proof -
hoelzl@43337
   667
  def a \<equiv> "(t - y) / (x - y)"
wenzelm@56796
   668
  with t have "0 \<le> a" "0 \<le> 1 - a"
wenzelm@56796
   669
    by (auto simp: field_simps)
wenzelm@60423
   670
  with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
hoelzl@43337
   671
    by (auto simp: convex_on_def)
wenzelm@56796
   672
  have "a * x + (1 - a) * y = a * (x - y) + y"
wenzelm@56796
   673
    by (simp add: field_simps)
wenzelm@56796
   674
  also have "\<dots> = t"
wenzelm@60423
   675
    unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
wenzelm@56796
   676
  finally have "f t \<le> a * f x + (1 - a) * f y"
wenzelm@56796
   677
    using cvx by simp
wenzelm@56796
   678
  also have "\<dots> = a * (f x - f y) + f y"
wenzelm@56796
   679
    by (simp add: field_simps)
wenzelm@56796
   680
  finally have "f t - f y \<le> a * (f x - f y)"
wenzelm@56796
   681
    by simp
hoelzl@43337
   682
  with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
huffman@44142
   683
    by (simp add: le_divide_eq divide_le_eq field_simps a_def)
hoelzl@43337
   684
  with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
huffman@44142
   685
    by (simp add: le_divide_eq divide_le_eq field_simps)
hoelzl@43337
   686
qed
hoelzl@36623
   687
hoelzl@36623
   688
lemma pos_convex_function:
hoelzl@36623
   689
  fixes f :: "real \<Rightarrow> real"
hoelzl@36623
   690
  assumes "convex C"
wenzelm@56796
   691
    and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
hoelzl@36623
   692
  shows "convex_on C f"
wenzelm@49609
   693
  unfolding convex_on_alt[OF assms(1)]
wenzelm@49609
   694
  using assms
hoelzl@36623
   695
proof safe
hoelzl@36623
   696
  fix x y \<mu> :: real
hoelzl@36623
   697
  let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
wenzelm@60423
   698
  assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
wenzelm@49609
   699
  then have "1 - \<mu> \<ge> 0" by auto
wenzelm@56796
   700
  then have xpos: "?x \<in> C"
wenzelm@60423
   701
    using * unfolding convex_alt by fastforce
wenzelm@56796
   702
  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
wenzelm@56796
   703
      \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
wenzelm@60423
   704
    using add_mono[OF mult_left_mono[OF leq[OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
wenzelm@60423
   705
      mult_left_mono[OF leq[OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
wenzelm@56796
   706
    by auto
wenzelm@49609
   707
  then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
wenzelm@60423
   708
    by (auto simp: field_simps)
wenzelm@49609
   709
  then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
hoelzl@36623
   710
    using convex_on_alt by auto
hoelzl@36623
   711
qed
hoelzl@36623
   712
hoelzl@36623
   713
lemma atMostAtLeast_subset_convex:
hoelzl@36623
   714
  fixes C :: "real set"
hoelzl@36623
   715
  assumes "convex C"
wenzelm@49609
   716
    and "x \<in> C" "y \<in> C" "x < y"
hoelzl@36623
   717
  shows "{x .. y} \<subseteq> C"
hoelzl@36623
   718
proof safe
wenzelm@60423
   719
  fix z assume z: "z \<in> {x .. y}"
wenzelm@60423
   720
  have less: "z \<in> C" if *: "x < z" "z < y"
wenzelm@60423
   721
  proof -
wenzelm@49609
   722
    let ?\<mu> = "(y - z) / (y - x)"
wenzelm@56796
   723
    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
wenzelm@60423
   724
      using assms * by (auto simp: field_simps)
wenzelm@49609
   725
    then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
wenzelm@49609
   726
      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
wenzelm@49609
   727
      by (simp add: algebra_simps)
hoelzl@36623
   728
    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
wenzelm@60423
   729
      by (auto simp: field_simps)
hoelzl@36623
   730
    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
wenzelm@49609
   731
      using assms unfolding add_divide_distrib by (auto simp: field_simps)
hoelzl@36623
   732
    also have "\<dots> = z"
wenzelm@49609
   733
      using assms by (auto simp: field_simps)
wenzelm@60423
   734
    finally show ?thesis
wenzelm@56796
   735
      using comb by auto
wenzelm@60423
   736
  qed
wenzelm@60423
   737
  show "z \<in> C" using z less assms
hoelzl@36623
   738
    unfolding atLeastAtMost_iff le_less by auto
hoelzl@36623
   739
qed
hoelzl@36623
   740
hoelzl@36623
   741
lemma f''_imp_f':
hoelzl@36623
   742
  fixes f :: "real \<Rightarrow> real"
hoelzl@36623
   743
  assumes "convex C"
wenzelm@49609
   744
    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
wenzelm@49609
   745
    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
wenzelm@49609
   746
    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
wenzelm@49609
   747
    and "x \<in> C" "y \<in> C"
hoelzl@36623
   748
  shows "f' x * (y - x) \<le> f y - f x"
wenzelm@49609
   749
  using assms
hoelzl@36623
   750
proof -
wenzelm@56796
   751
  {
wenzelm@56796
   752
    fix x y :: real
wenzelm@60423
   753
    assume *: "x \<in> C" "y \<in> C" "y > x"
wenzelm@60423
   754
    then have ge: "y - x > 0" "y - x \<ge> 0"
wenzelm@60423
   755
      by auto
wenzelm@60423
   756
    from * have le: "x - y < 0" "x - y \<le> 0"
wenzelm@60423
   757
      by auto
hoelzl@36623
   758
    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
wenzelm@60423
   759
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
wenzelm@60423
   760
        THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
hoelzl@36623
   761
      by auto
wenzelm@60423
   762
    then have "z1 \<in> C"
wenzelm@60423
   763
      using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
wenzelm@60423
   764
      by fastforce
hoelzl@36623
   765
    from z1 have z1': "f x - f y = (x - y) * f' z1"
wenzelm@60423
   766
      by (simp add: field_simps)
hoelzl@36623
   767
    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
wenzelm@60423
   768
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
wenzelm@60423
   769
        THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
hoelzl@36623
   770
      by auto
hoelzl@36623
   771
    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
wenzelm@60423
   772
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
wenzelm@60423
   773
        THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
hoelzl@36623
   774
      by auto
hoelzl@36623
   775
    have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
wenzelm@60423
   776
      using * z1' by auto
wenzelm@60423
   777
    also have "\<dots> = (y - z1) * f'' z3"
wenzelm@60423
   778
      using z3 by auto
wenzelm@60423
   779
    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
wenzelm@60423
   780
      by simp
wenzelm@60423
   781
    have A': "y - z1 \<ge> 0"
wenzelm@60423
   782
      using z1 by auto
wenzelm@60423
   783
    have "z3 \<in> C"
wenzelm@60423
   784
      using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
wenzelm@60423
   785
      by fastforce
wenzelm@60423
   786
    then have B': "f'' z3 \<ge> 0"
wenzelm@60423
   787
      using assms by auto
wenzelm@60423
   788
    from A' B' have "(y - z1) * f'' z3 \<ge> 0"
wenzelm@60423
   789
      by auto
wenzelm@60423
   790
    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
wenzelm@60423
   791
      by auto
hoelzl@36623
   792
    from mult_right_mono_neg[OF this le(2)]
hoelzl@36623
   793
    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
huffman@36778
   794
      by (simp add: algebra_simps)
wenzelm@60423
   795
    then have "f' y * (x - y) - (f x - f y) \<le> 0"
wenzelm@60423
   796
      using le by auto
wenzelm@60423
   797
    then have res: "f' y * (x - y) \<le> f x - f y"
wenzelm@60423
   798
      by auto
hoelzl@36623
   799
    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
wenzelm@60423
   800
      using * z1 by auto
wenzelm@60423
   801
    also have "\<dots> = (z1 - x) * f'' z2"
wenzelm@60423
   802
      using z2 by auto
wenzelm@60423
   803
    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
wenzelm@60423
   804
      by simp
wenzelm@60423
   805
    have A: "z1 - x \<ge> 0"
wenzelm@60423
   806
      using z1 by auto
wenzelm@60423
   807
    have "z2 \<in> C"
wenzelm@60423
   808
      using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
wenzelm@60423
   809
      by fastforce
wenzelm@60423
   810
    then have B: "f'' z2 \<ge> 0"
wenzelm@60423
   811
      using assms by auto
wenzelm@60423
   812
    from A B have "(z1 - x) * f'' z2 \<ge> 0"
wenzelm@60423
   813
      by auto
wenzelm@60423
   814
    with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
wenzelm@60423
   815
      by auto
hoelzl@36623
   816
    from mult_right_mono[OF this ge(2)]
hoelzl@36623
   817
    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
huffman@36778
   818
      by (simp add: algebra_simps)
wenzelm@60423
   819
    then have "f y - f x - f' x * (y - x) \<ge> 0"
wenzelm@60423
   820
      using ge by auto
wenzelm@49609
   821
    then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
wenzelm@60423
   822
      using res by auto
wenzelm@60423
   823
  } note less_imp = this
wenzelm@56796
   824
  {
wenzelm@56796
   825
    fix x y :: real
wenzelm@49609
   826
    assume "x \<in> C" "y \<in> C" "x \<noteq> y"
wenzelm@49609
   827
    then have"f y - f x \<ge> f' x * (y - x)"
wenzelm@56796
   828
    unfolding neq_iff using less_imp by auto
wenzelm@56796
   829
  }
hoelzl@36623
   830
  moreover
wenzelm@56796
   831
  {
wenzelm@56796
   832
    fix x y :: real
wenzelm@60423
   833
    assume "x \<in> C" "y \<in> C" "x = y"
wenzelm@56796
   834
    then have "f y - f x \<ge> f' x * (y - x)" by auto
wenzelm@56796
   835
  }
hoelzl@36623
   836
  ultimately show ?thesis using assms by blast
hoelzl@36623
   837
qed
hoelzl@36623
   838
hoelzl@36623
   839
lemma f''_ge0_imp_convex:
hoelzl@36623
   840
  fixes f :: "real \<Rightarrow> real"
hoelzl@36623
   841
  assumes conv: "convex C"
wenzelm@49609
   842
    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
wenzelm@49609
   843
    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
wenzelm@49609
   844
    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
hoelzl@36623
   845
  shows "convex_on C f"
wenzelm@56796
   846
  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
wenzelm@56796
   847
  by fastforce
hoelzl@36623
   848
hoelzl@36623
   849
lemma minus_log_convex:
hoelzl@36623
   850
  fixes b :: real
hoelzl@36623
   851
  assumes "b > 1"
hoelzl@36623
   852
  shows "convex_on {0 <..} (\<lambda> x. - log b x)"
hoelzl@36623
   853
proof -
wenzelm@56796
   854
  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
wenzelm@56796
   855
    using DERIV_log by auto
wenzelm@49609
   856
  then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
hoelzl@56479
   857
    by (auto simp: DERIV_minus)
wenzelm@49609
   858
  have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
hoelzl@36623
   859
    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
hoelzl@36623
   860
  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
wenzelm@49609
   861
  have "\<And>z :: real. z > 0 \<Longrightarrow>
wenzelm@49609
   862
    DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
hoelzl@36623
   863
    by auto
wenzelm@56796
   864
  then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
wenzelm@56796
   865
    DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
wenzelm@60423
   866
    unfolding inverse_eq_divide by (auto simp: mult.assoc)
wenzelm@56796
   867
  have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
wenzelm@60423
   868
    using \<open>b > 1\<close> by (auto intro!: less_imp_le)
hoelzl@36623
   869
  from f''_ge0_imp_convex[OF pos_is_convex,
hoelzl@36623
   870
    unfolded greaterThan_iff, OF f' f''0 f''_ge0]
hoelzl@36623
   871
  show ?thesis by auto
hoelzl@36623
   872
qed
hoelzl@36623
   873
eberlm@61531
   874
eberlm@61531
   875
subsection \<open>Convexity of real functions\<close>
eberlm@61531
   876
eberlm@61531
   877
lemma convex_on_realI:
eberlm@61531
   878
  assumes "connected A"
eberlm@61531
   879
  assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
eberlm@61531
   880
  assumes "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f' x \<le> f' y"
eberlm@61531
   881
  shows   "convex_on A f"
eberlm@61531
   882
proof (rule convex_on_linorderI)
eberlm@61531
   883
  fix t x y :: real
eberlm@61531
   884
  assume t: "t > 0" "t < 1" and xy: "x \<in> A" "y \<in> A" "x < y"
eberlm@61531
   885
  def z \<equiv> "(1 - t) * x + t * y"
wenzelm@61585
   886
  with \<open>connected A\<close> and xy have ivl: "{x..y} \<subseteq> A" using connected_contains_Icc by blast
lp15@61694
   887
eberlm@61531
   888
  from xy t have xz: "z > x" by (simp add: z_def algebra_simps)
eberlm@61531
   889
  have "y - z = (1 - t) * (y - x)" by (simp add: z_def algebra_simps)
eberlm@61531
   890
  also from xy t have "... > 0" by (intro mult_pos_pos) simp_all
eberlm@61531
   891
  finally have yz: "z < y" by simp
lp15@61694
   892
eberlm@61531
   893
  from assms xz yz ivl t have "\<exists>\<xi>. \<xi> > x \<and> \<xi> < z \<and> f z - f x = (z - x) * f' \<xi>"
eberlm@61531
   894
    by (intro MVT2) (auto intro!: assms(2))
eberlm@61531
   895
  then obtain \<xi> where \<xi>: "\<xi> > x" "\<xi> < z" "f' \<xi> = (f z - f x) / (z - x)" by auto
eberlm@61531
   896
  from assms xz yz ivl t have "\<exists>\<eta>. \<eta> > z \<and> \<eta> < y \<and> f y - f z = (y - z) * f' \<eta>"
eberlm@61531
   897
    by (intro MVT2) (auto intro!: assms(2))
eberlm@61531
   898
  then obtain \<eta> where \<eta>: "\<eta> > z" "\<eta> < y" "f' \<eta> = (f y - f z) / (y - z)" by auto
lp15@61694
   899
eberlm@61531
   900
  from \<eta>(3) have "(f y - f z) / (y - z) = f' \<eta>" ..
eberlm@61531
   901
  also from \<xi> \<eta> ivl have "\<xi> \<in> A" "\<eta> \<in> A" by auto
eberlm@61531
   902
  with \<xi> \<eta> have "f' \<eta> \<ge> f' \<xi>" by (intro assms(3)) auto
eberlm@61531
   903
  also from \<xi>(3) have "f' \<xi> = (f z - f x) / (z - x)" .
eberlm@61531
   904
  finally have "(f y - f z) * (z - x) \<ge> (f z - f x) * (y - z)"
eberlm@61531
   905
    using xz yz by (simp add: field_simps)
eberlm@61531
   906
  also have "z - x = t * (y - x)" by (simp add: z_def algebra_simps)
eberlm@61531
   907
  also have "y - z = (1 - t) * (y - x)" by (simp add: z_def algebra_simps)
eberlm@61531
   908
  finally have "(f y - f z) * t \<ge> (f z - f x) * (1 - t)" using xy by simp
eberlm@61531
   909
  thus "(1 - t) * f x + t * f y \<ge> f ((1 - t) *\<^sub>R x + t *\<^sub>R y)"
eberlm@61531
   910
    by (simp add: z_def algebra_simps)
eberlm@61531
   911
qed
eberlm@61531
   912
eberlm@61531
   913
lemma convex_on_inverse:
eberlm@61531
   914
  assumes "A \<subseteq> {0<..}"
eberlm@61531
   915
  shows   "convex_on A (inverse :: real \<Rightarrow> real)"
eberlm@61531
   916
proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\<lambda>x. -inverse (x^2)"])
eberlm@61531
   917
  fix u v :: real assume "u \<in> {0<..}" "v \<in> {0<..}" "u \<le> v"
eberlm@61531
   918
  with assms show "-inverse (u^2) \<le> -inverse (v^2)"
eberlm@61531
   919
    by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all)
eberlm@61531
   920
qed (insert assms, auto intro!: derivative_eq_intros simp: divide_simps power2_eq_square)
eberlm@61531
   921
eberlm@61531
   922
lemma convex_onD_Icc':
eberlm@61531
   923
  assumes "convex_on {x..y} f" "c \<in> {x..y}"
eberlm@61531
   924
  defines "d \<equiv> y - x"
eberlm@61531
   925
  shows   "f c \<le> (f y - f x) / d * (c - x) + f x"
eberlm@61531
   926
proof (cases y x rule: linorder_cases)
eberlm@61531
   927
  assume less: "x < y"
eberlm@61531
   928
  hence d: "d > 0" by (simp add: d_def)
lp15@61694
   929
  from assms(2) less have A: "0 \<le> (c - x) / d" "(c - x) / d \<le> 1"
eberlm@61531
   930
    by (simp_all add: d_def divide_simps)
eberlm@61531
   931
  have "f c = f (x + (c - x) * 1)" by simp
eberlm@61531
   932
  also from less have "1 = ((y - x) / d)" by (simp add: d_def)
lp15@61694
   933
  also from d have "x + (c - x) * \<dots> = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y"
eberlm@61531
   934
    by (simp add: field_simps)
eberlm@61531
   935
  also have "f \<dots> \<le> (1 - (c - x) / d) * f x + (c - x) / d * f y" using assms less
eberlm@61531
   936
    by (intro convex_onD_Icc) simp_all
eberlm@61531
   937
  also from d have "\<dots> = (f y - f x) / d * (c - x) + f x" by (simp add: field_simps)
eberlm@61531
   938
  finally show ?thesis .
eberlm@61531
   939
qed (insert assms(2), simp_all)
eberlm@61531
   940
eberlm@61531
   941
lemma convex_onD_Icc'':
eberlm@61531
   942
  assumes "convex_on {x..y} f" "c \<in> {x..y}"
eberlm@61531
   943
  defines "d \<equiv> y - x"
eberlm@61531
   944
  shows   "f c \<le> (f x - f y) / d * (y - c) + f y"
eberlm@61531
   945
proof (cases y x rule: linorder_cases)
eberlm@61531
   946
  assume less: "x < y"
eberlm@61531
   947
  hence d: "d > 0" by (simp add: d_def)
lp15@61694
   948
  from assms(2) less have A: "0 \<le> (y - c) / d" "(y - c) / d \<le> 1"
eberlm@61531
   949
    by (simp_all add: d_def divide_simps)
eberlm@61531
   950
  have "f c = f (y - (y - c) * 1)" by simp
eberlm@61531
   951
  also from less have "1 = ((y - x) / d)" by (simp add: d_def)
lp15@61694
   952
  also from d have "y - (y - c) * \<dots> = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y"
eberlm@61531
   953
    by (simp add: field_simps)
eberlm@61531
   954
  also have "f \<dots> \<le> (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y" using assms less
eberlm@61531
   955
    by (intro convex_onD_Icc) (simp_all add: field_simps)
eberlm@61531
   956
  also from d have "\<dots> = (f x - f y) / d * (y - c) + f y" by (simp add: field_simps)
eberlm@61531
   957
  finally show ?thesis .
eberlm@61531
   958
qed (insert assms(2), simp_all)
eberlm@61531
   959
eberlm@61531
   960
hoelzl@36623
   961
end