src/HOL/Library/Convex.thy
changeset 55909 df6133adb63f
parent 54230 b1d955791529
child 56409 36489d77c484
equal deleted inserted replaced
55908:e6d570cb0f5e 55909:df6133adb63f
   109   then show "convex {a..<b}" by (simp only: convex_Int 1 4)
   109   then show "convex {a..<b}" by (simp only: convex_Int 1 4)
   110   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   110   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   111   then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
   111   then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
   112 qed
   112 qed
   113 
   113 
   114 
       
   115 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
   114 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
   116 
   115 
   117 lemma convex_setsum:
   116 lemma convex_setsum:
   118   fixes C :: "'a::real_vector set"
   117   fixes C :: "'a::real_vector set"
   119   assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
   118   assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
   120   assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   119   assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   121   shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   120   shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   122   using assms
   121   using assms(1,3,4,5)
   123 proof (induct s arbitrary:a rule: finite_induct)
   122 proof (induct arbitrary: a set: finite)
   124   case empty
   123   case empty
   125   then show ?case by auto
   124   then show ?case by simp
   126 next
   125 next
   127   case (insert i s) note asms = this
   126   case (insert i s) note IH = this(3)
   128   { assume "a i = 1"
   127   have "a i + setsum a s = 1" and "0 \<le> a i" and "\<forall>j\<in>s. 0 \<le> a j" and "y i \<in> C" and "\<forall>j\<in>s. y j \<in> C"
   129     then have "(\<Sum> j \<in> s. a j) = 0"
   128     using insert.hyps(1,2) insert.prems by simp_all
   130       using asms by auto
   129   then have "0 \<le> setsum a s" by (simp add: setsum_nonneg)
   131     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   130   have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
   132       using setsum_nonneg_0[where 'b=real] asms by fastforce
   131   proof (cases)
   133     then have ?case using asms by auto }
   132     assume z: "setsum a s = 0"
   134   moreover
   133     with `a i + setsum a s = 1` have "a i = 1" by simp
   135   { assume asm: "a i \<noteq> 1"
   134     from setsum_nonneg_0 [OF `finite s` _ z] `\<forall>j\<in>s. 0 \<le> a j` have "\<forall>j\<in>s. a j = 0" by simp
   136     from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   135     show ?thesis using `a i = 1` and `\<forall>j\<in>s. a j = 0` and `y i \<in> C` by simp
   137     have fis: "finite (insert i s)" using asms by auto
   136   next
   138     then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   137     assume nz: "setsum a s \<noteq> 0"
   139     then have "a i < 1" using asm by auto
   138     with `0 \<le> setsum a s` have "0 < setsum a s" by simp
   140     then have i0: "1 - a i > 0" by auto
   139     then have "(\<Sum>j\<in>s. (a j / setsum a s) *\<^sub>R y j) \<in> C"
   141     let ?a = "\<lambda>j. a j / (1 - a i)"
   140       using `\<forall>j\<in>s. 0 \<le> a j` and `\<forall>j\<in>s. y j \<in> C`
   142     { fix j assume "j \<in> s"
   141       by (simp add: IH divide_nonneg_pos setsum_divide_distrib [symmetric])
   143       then have "?a j \<ge> 0"
   142     from `convex C` and `y i \<in> C` and this and `0 \<le> a i`
   144         using i0 asms divide_nonneg_pos
   143       and `0 \<le> setsum a s` and `a i + setsum a s = 1`
   145         by fastforce
   144     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"
   146     } note a_nonneg = this
   145       by (rule convexD)
   147     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   146     then show ?thesis by (simp add: scaleR_setsum_right nz)
   148     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   147   qed
   149     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   148   then show ?case using `finite s` and `i \<notin> s` by simp
   150     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
       
   151     with asms have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastforce
       
   152     then have "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
       
   153       using asms yai ai1 by (auto intro: convexD)
       
   154     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
       
   155       using scaleR_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j *\<^sub>R y j" s] by auto
       
   156     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
       
   157     then have ?case using setsum.insert asms by auto
       
   158   }
       
   159   ultimately show ?case by auto
       
   160 qed
   149 qed
   161 
   150 
   162 lemma convex:
   151 lemma convex:
   163   "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)
   152   "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)
   164       \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   153       \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   244     by simp
   233     by simp
   245   show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   234   show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   246    using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   235    using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   247    by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   236    by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   248 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   237 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
       
   238 
       
   239 subsection {* Functions that are convex on a set *}
   249 
   240 
   250 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   241 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   251   where "convex_on s f \<longleftrightarrow>
   242   where "convex_on s f \<longleftrightarrow>
   252     (\<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)"
   243     (\<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)"
   253 
   244