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