src/HOL/Analysis/Convex_Euclidean_Space.thy
author immler
Tue Jul 10 09:38:35 2018 +0200 (11 months ago)
changeset 68607 67bb59e49834
parent 68527 2f4e2aab190a
child 69064 5840724b1d71
permissions -rw-r--r--
make theorem, corollary, and proposition %important for HOL-Analysis manual
hoelzl@63969
     1
(* Title:      HOL/Analysis/Convex_Euclidean_Space.thy
hoelzl@63969
     2
   Author:     L C Paulson, University of Cambridge
hoelzl@63969
     3
   Author:     Robert Himmelmann, TU Muenchen
hoelzl@63969
     4
   Author:     Bogdan Grechuk, University of Edinburgh
hoelzl@63969
     5
   Author:     Armin Heller, TU Muenchen
hoelzl@63969
     6
   Author:     Johannes Hoelzl, TU Muenchen
himmelma@33175
     7
*)
himmelma@33175
     8
lp15@63114
     9
section \<open>Convex sets, functions and related things\<close>
himmelma@33175
    10
himmelma@33175
    11
theory Convex_Euclidean_Space
huffman@44132
    12
imports
lp15@66827
    13
  Connected
wenzelm@66453
    14
  "HOL-Library.Set_Algebras"
himmelma@33175
    15
begin
himmelma@33175
    16
lp15@64773
    17
lemma swap_continuous: (*move to Topological_Spaces?*)
lp15@64773
    18
  assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
lp15@64773
    19
    shows "continuous_on (cbox (c,a) (d,b)) (\<lambda>(x, y). f y x)"
lp15@64773
    20
proof -
lp15@64773
    21
  have "(\<lambda>(x, y). f y x) = (\<lambda>(x, y). f x y) \<circ> prod.swap"
lp15@64773
    22
    by auto
lp15@64773
    23
  then show ?thesis
lp15@64773
    24
    apply (rule ssubst)
lp15@64773
    25
    apply (rule continuous_on_compose)
lp15@64773
    26
    apply (simp add: split_def)
lp15@64773
    27
    apply (rule continuous_intros | simp add: assms)+
lp15@64773
    28
    done
lp15@64773
    29
qed
lp15@64773
    30
wenzelm@49529
    31
lemma substdbasis_expansion_unique:
hoelzl@50526
    32
  assumes d: "d \<subseteq> Basis"
wenzelm@53302
    33
  shows "(\<Sum>i\<in>d. f i *\<^sub>R i) = (x::'a::euclidean_space) \<longleftrightarrow>
wenzelm@53302
    34
    (\<forall>i\<in>Basis. (i \<in> d \<longrightarrow> f i = x \<bullet> i) \<and> (i \<notin> d \<longrightarrow> x \<bullet> i = 0))"
wenzelm@49529
    35
proof -
wenzelm@53339
    36
  have *: "\<And>x a b P. x * (if P then a else b) = (if P then x * a else x * b)"
wenzelm@53302
    37
    by auto
wenzelm@53302
    38
  have **: "finite d"
wenzelm@53302
    39
    by (auto intro: finite_subset[OF assms])
hoelzl@50526
    40
  have ***: "\<And>i. i \<in> Basis \<Longrightarrow> (\<Sum>i\<in>d. f i *\<^sub>R i) \<bullet> i = (\<Sum>x\<in>d. if x = i then f x else 0)"
hoelzl@50526
    41
    using d
nipkow@64267
    42
    by (auto intro!: sum.cong simp: inner_Basis inner_sum_left)
nipkow@64267
    43
  show ?thesis
nipkow@64267
    44
    unfolding euclidean_eq_iff[where 'a='a] by (auto simp: sum.delta[OF **] ***)
hoelzl@50526
    45
qed
hoelzl@50526
    46
hoelzl@50526
    47
lemma independent_substdbasis: "d \<subseteq> Basis \<Longrightarrow> independent d"
hoelzl@50526
    48
  by (rule independent_mono[OF independent_Basis])
hoelzl@40377
    49
wenzelm@49531
    50
lemma dim_cball:
wenzelm@53302
    51
  assumes "e > 0"
wenzelm@49529
    52
  shows "dim (cball (0 :: 'n::euclidean_space) e) = DIM('n)"
wenzelm@49529
    53
proof -
wenzelm@53302
    54
  {
wenzelm@53302
    55
    fix x :: "'n::euclidean_space"
wenzelm@63040
    56
    define y where "y = (e / norm x) *\<^sub>R x"
wenzelm@53339
    57
    then have "y \<in> cball 0 e"
lp15@62397
    58
      using assms by auto
wenzelm@53339
    59
    moreover have *: "x = (norm x / e) *\<^sub>R y"
wenzelm@53302
    60
      using y_def assms by simp
wenzelm@53302
    61
    moreover from * have "x = (norm x/e) *\<^sub>R y"
wenzelm@53302
    62
      by auto
wenzelm@53339
    63
    ultimately have "x \<in> span (cball 0 e)"
immler@68072
    64
      using span_scale[of y "cball 0 e" "norm x/e"]
immler@68072
    65
        span_superset[of "cball 0 e"]
immler@68072
    66
      by (simp add: span_base)
wenzelm@53302
    67
  }
wenzelm@53339
    68
  then have "span (cball 0 e) = (UNIV :: 'n::euclidean_space set)"
wenzelm@53302
    69
    by auto
wenzelm@49529
    70
  then show ?thesis
lp15@68031
    71
    using dim_span[of "cball (0 :: 'n::euclidean_space) e"] by (auto simp: dim_UNIV)
hoelzl@40377
    72
qed
hoelzl@40377
    73
nipkow@64267
    74
lemma sum_not_0: "sum f A \<noteq> 0 \<Longrightarrow> \<exists>a \<in> A. f a \<noteq> 0"
wenzelm@49529
    75
  by (rule ccontr) auto
hoelzl@40377
    76
lp15@61694
    77
lemma subset_translation_eq [simp]:
nipkow@67399
    78
    fixes a :: "'a::real_vector" shows "(+) a ` s \<subseteq> (+) a ` t \<longleftrightarrow> s \<subseteq> t"
lp15@61694
    79
  by auto
lp15@61694
    80
wenzelm@49531
    81
lemma translate_inj_on:
wenzelm@53339
    82
  fixes A :: "'a::ab_group_add set"
wenzelm@53339
    83
  shows "inj_on (\<lambda>x. a + x) A"
wenzelm@49529
    84
  unfolding inj_on_def by auto
hoelzl@40377
    85
hoelzl@40377
    86
lemma translation_assoc:
hoelzl@40377
    87
  fixes a b :: "'a::ab_group_add"
wenzelm@53339
    88
  shows "(\<lambda>x. b + x) ` ((\<lambda>x. a + x) ` S) = (\<lambda>x. (a + b) + x) ` S"
wenzelm@49529
    89
  by auto
hoelzl@40377
    90
hoelzl@40377
    91
lemma translation_invert:
hoelzl@40377
    92
  fixes a :: "'a::ab_group_add"
wenzelm@53339
    93
  assumes "(\<lambda>x. a + x) ` A = (\<lambda>x. a + x) ` B"
wenzelm@49529
    94
  shows "A = B"
wenzelm@49529
    95
proof -
wenzelm@53339
    96
  have "(\<lambda>x. -a + x) ` ((\<lambda>x. a + x) ` A) = (\<lambda>x. - a + x) ` ((\<lambda>x. a + x) ` B)"
wenzelm@49529
    97
    using assms by auto
wenzelm@49529
    98
  then show ?thesis
wenzelm@49529
    99
    using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto
hoelzl@40377
   100
qed
hoelzl@40377
   101
hoelzl@40377
   102
lemma translation_galois:
hoelzl@40377
   103
  fixes a :: "'a::ab_group_add"
wenzelm@53339
   104
  shows "T = ((\<lambda>x. a + x) ` S) \<longleftrightarrow> S = ((\<lambda>x. (- a) + x) ` T)"
wenzelm@53333
   105
  using translation_assoc[of "-a" a S]
wenzelm@53333
   106
  apply auto
wenzelm@53333
   107
  using translation_assoc[of a "-a" T]
wenzelm@53333
   108
  apply auto
wenzelm@49529
   109
  done
hoelzl@40377
   110
hoelzl@40377
   111
lemma translation_inverse_subset:
wenzelm@53339
   112
  assumes "((\<lambda>x. - a + x) ` V) \<le> (S :: 'n::ab_group_add set)"
wenzelm@53339
   113
  shows "V \<le> ((\<lambda>x. a + x) ` S)"
wenzelm@49529
   114
proof -
wenzelm@53333
   115
  {
wenzelm@53333
   116
    fix x
wenzelm@53333
   117
    assume "x \<in> V"
wenzelm@53333
   118
    then have "x-a \<in> S" using assms by auto
wenzelm@53333
   119
    then have "x \<in> {a + v |v. v \<in> S}"
wenzelm@49529
   120
      apply auto
lp15@68031
   121
      apply (rule exI[of _ "x-a"], simp)
wenzelm@49529
   122
      done
wenzelm@53333
   123
    then have "x \<in> ((\<lambda>x. a+x) ` S)" by auto
wenzelm@53333
   124
  }
wenzelm@53333
   125
  then show ?thesis by auto
hoelzl@40377
   126
qed
hoelzl@40377
   127
hoelzl@63969
   128
subsection \<open>Convexity\<close>
hoelzl@63969
   129
immler@67962
   130
definition%important convex :: "'a::real_vector set \<Rightarrow> bool"
hoelzl@63969
   131
  where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
hoelzl@63969
   132
hoelzl@63969
   133
lemma convexI:
hoelzl@63969
   134
  assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
hoelzl@63969
   135
  shows "convex s"
hoelzl@63969
   136
  using assms unfolding convex_def by fast
hoelzl@63969
   137
hoelzl@63969
   138
lemma convexD:
hoelzl@63969
   139
  assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
hoelzl@63969
   140
  shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
hoelzl@63969
   141
  using assms unfolding convex_def by fast
hoelzl@63969
   142
hoelzl@63969
   143
lemma convex_alt: "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
hoelzl@63969
   144
  (is "_ \<longleftrightarrow> ?alt")
hoelzl@63969
   145
proof
hoelzl@63969
   146
  show "convex s" if alt: ?alt
hoelzl@63969
   147
  proof -
hoelzl@63969
   148
    {
hoelzl@63969
   149
      fix x y and u v :: real
hoelzl@63969
   150
      assume mem: "x \<in> s" "y \<in> s"
hoelzl@63969
   151
      assume "0 \<le> u" "0 \<le> v"
hoelzl@63969
   152
      moreover
hoelzl@63969
   153
      assume "u + v = 1"
hoelzl@63969
   154
      then have "u = 1 - v" by auto
hoelzl@63969
   155
      ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s"
hoelzl@63969
   156
        using alt [rule_format, OF mem] by auto
hoelzl@63969
   157
    }
hoelzl@63969
   158
    then show ?thesis
hoelzl@63969
   159
      unfolding convex_def by auto
hoelzl@63969
   160
  qed
hoelzl@63969
   161
  show ?alt if "convex s"
hoelzl@63969
   162
    using that by (auto simp: convex_def)
hoelzl@63969
   163
qed
hoelzl@63969
   164
hoelzl@63969
   165
lemma convexD_alt:
hoelzl@63969
   166
  assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
hoelzl@63969
   167
  shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
hoelzl@63969
   168
  using assms unfolding convex_alt by auto
hoelzl@63969
   169
hoelzl@63969
   170
lemma mem_convex_alt:
hoelzl@63969
   171
  assumes "convex S" "x \<in> S" "y \<in> S" "u \<ge> 0" "v \<ge> 0" "u + v > 0"
hoelzl@63969
   172
  shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \<in> S"
hoelzl@63969
   173
  apply (rule convexD)
hoelzl@63969
   174
  using assms
hoelzl@63969
   175
       apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric])
hoelzl@63969
   176
  done
hoelzl@63969
   177
hoelzl@63969
   178
lemma convex_empty[intro,simp]: "convex {}"
hoelzl@63969
   179
  unfolding convex_def by simp
hoelzl@63969
   180
hoelzl@63969
   181
lemma convex_singleton[intro,simp]: "convex {a}"
hoelzl@63969
   182
  unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
hoelzl@63969
   183
hoelzl@63969
   184
lemma convex_UNIV[intro,simp]: "convex UNIV"
hoelzl@63969
   185
  unfolding convex_def by auto
hoelzl@63969
   186
hoelzl@63969
   187
lemma convex_Inter: "(\<And>s. s\<in>f \<Longrightarrow> convex s) \<Longrightarrow> convex(\<Inter>f)"
hoelzl@63969
   188
  unfolding convex_def by auto
hoelzl@63969
   189
hoelzl@63969
   190
lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
hoelzl@63969
   191
  unfolding convex_def by auto
hoelzl@63969
   192
hoelzl@63969
   193
lemma convex_INT: "(\<And>i. i \<in> A \<Longrightarrow> convex (B i)) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
hoelzl@63969
   194
  unfolding convex_def by auto
hoelzl@63969
   195
hoelzl@63969
   196
lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
hoelzl@63969
   197
  unfolding convex_def by auto
hoelzl@63969
   198
hoelzl@63969
   199
lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
hoelzl@63969
   200
  unfolding convex_def
hoelzl@63969
   201
  by (auto simp: inner_add intro!: convex_bound_le)
hoelzl@63969
   202
hoelzl@63969
   203
lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
hoelzl@63969
   204
proof -
hoelzl@63969
   205
  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}"
hoelzl@63969
   206
    by auto
hoelzl@63969
   207
  show ?thesis
hoelzl@63969
   208
    unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
hoelzl@63969
   209
qed
hoelzl@63969
   210
lp15@65583
   211
lemma convex_halfspace_abs_le: "convex {x. \<bar>inner a x\<bar> \<le> b}"
lp15@65583
   212
proof -
lp15@65583
   213
  have *: "{x. \<bar>inner a x\<bar> \<le> b} = {x. inner a x \<le> b} \<inter> {x. -b \<le> inner a x}"
lp15@65583
   214
    by auto
lp15@65583
   215
  show ?thesis
lp15@65583
   216
    unfolding * by (simp add: convex_Int convex_halfspace_ge convex_halfspace_le)
lp15@65583
   217
qed
lp15@65583
   218
hoelzl@63969
   219
lemma convex_hyperplane: "convex {x. inner a x = b}"
hoelzl@63969
   220
proof -
hoelzl@63969
   221
  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}"
hoelzl@63969
   222
    by auto
hoelzl@63969
   223
  show ?thesis using convex_halfspace_le convex_halfspace_ge
hoelzl@63969
   224
    by (auto intro!: convex_Int simp: *)
hoelzl@63969
   225
qed
hoelzl@63969
   226
hoelzl@63969
   227
lemma convex_halfspace_lt: "convex {x. inner a x < b}"
hoelzl@63969
   228
  unfolding convex_def
hoelzl@63969
   229
  by (auto simp: convex_bound_lt inner_add)
hoelzl@63969
   230
hoelzl@63969
   231
lemma convex_halfspace_gt: "convex {x. inner a x > b}"
eberlm@67135
   232
  using convex_halfspace_lt[of "-a" "-b"] by auto
eberlm@67135
   233
eberlm@67135
   234
lemma convex_halfspace_Re_ge: "convex {x. Re x \<ge> b}"
eberlm@67135
   235
  using convex_halfspace_ge[of b "1::complex"] by simp
eberlm@67135
   236
eberlm@67135
   237
lemma convex_halfspace_Re_le: "convex {x. Re x \<le> b}"
eberlm@67135
   238
  using convex_halfspace_le[of "1::complex" b] by simp
eberlm@67135
   239
eberlm@67135
   240
lemma convex_halfspace_Im_ge: "convex {x. Im x \<ge> b}"
eberlm@67135
   241
  using convex_halfspace_ge[of b \<i>] by simp
eberlm@67135
   242
eberlm@67135
   243
lemma convex_halfspace_Im_le: "convex {x. Im x \<le> b}"
eberlm@67135
   244
  using convex_halfspace_le[of \<i> b] by simp
eberlm@67135
   245
eberlm@67135
   246
lemma convex_halfspace_Re_gt: "convex {x. Re x > b}"
eberlm@67135
   247
  using convex_halfspace_gt[of b "1::complex"] by simp
eberlm@67135
   248
eberlm@67135
   249
lemma convex_halfspace_Re_lt: "convex {x. Re x < b}"
eberlm@67135
   250
  using convex_halfspace_lt[of "1::complex" b] by simp
eberlm@67135
   251
eberlm@67135
   252
lemma convex_halfspace_Im_gt: "convex {x. Im x > b}"
eberlm@67135
   253
  using convex_halfspace_gt[of b \<i>] by simp
eberlm@67135
   254
eberlm@67135
   255
lemma convex_halfspace_Im_lt: "convex {x. Im x < b}"
eberlm@67135
   256
  using convex_halfspace_lt[of \<i> b] by simp
hoelzl@63969
   257
hoelzl@63969
   258
lemma convex_real_interval [iff]:
hoelzl@63969
   259
  fixes a b :: "real"
hoelzl@63969
   260
  shows "convex {a..}" and "convex {..b}"
hoelzl@63969
   261
    and "convex {a<..}" and "convex {..<b}"
hoelzl@63969
   262
    and "convex {a..b}" and "convex {a<..b}"
hoelzl@63969
   263
    and "convex {a..<b}" and "convex {a<..<b}"
hoelzl@63969
   264
proof -
hoelzl@63969
   265
  have "{a..} = {x. a \<le> inner 1 x}"
hoelzl@63969
   266
    by auto
hoelzl@63969
   267
  then show 1: "convex {a..}"
hoelzl@63969
   268
    by (simp only: convex_halfspace_ge)
hoelzl@63969
   269
  have "{..b} = {x. inner 1 x \<le> b}"
hoelzl@63969
   270
    by auto
hoelzl@63969
   271
  then show 2: "convex {..b}"
hoelzl@63969
   272
    by (simp only: convex_halfspace_le)
hoelzl@63969
   273
  have "{a<..} = {x. a < inner 1 x}"
hoelzl@63969
   274
    by auto
hoelzl@63969
   275
  then show 3: "convex {a<..}"
hoelzl@63969
   276
    by (simp only: convex_halfspace_gt)
hoelzl@63969
   277
  have "{..<b} = {x. inner 1 x < b}"
hoelzl@63969
   278
    by auto
hoelzl@63969
   279
  then show 4: "convex {..<b}"
hoelzl@63969
   280
    by (simp only: convex_halfspace_lt)
hoelzl@63969
   281
  have "{a..b} = {a..} \<inter> {..b}"
hoelzl@63969
   282
    by auto
hoelzl@63969
   283
  then show "convex {a..b}"
hoelzl@63969
   284
    by (simp only: convex_Int 1 2)
hoelzl@63969
   285
  have "{a<..b} = {a<..} \<inter> {..b}"
hoelzl@63969
   286
    by auto
hoelzl@63969
   287
  then show "convex {a<..b}"
hoelzl@63969
   288
    by (simp only: convex_Int 3 2)
hoelzl@63969
   289
  have "{a..<b} = {a..} \<inter> {..<b}"
hoelzl@63969
   290
    by auto
hoelzl@63969
   291
  then show "convex {a..<b}"
hoelzl@63969
   292
    by (simp only: convex_Int 1 4)
hoelzl@63969
   293
  have "{a<..<b} = {a<..} \<inter> {..<b}"
hoelzl@63969
   294
    by auto
hoelzl@63969
   295
  then show "convex {a<..<b}"
hoelzl@63969
   296
    by (simp only: convex_Int 3 4)
hoelzl@63969
   297
qed
hoelzl@63969
   298
hoelzl@63969
   299
lemma convex_Reals: "convex \<real>"
hoelzl@63969
   300
  by (simp add: convex_def scaleR_conv_of_real)
hoelzl@63969
   301
hoelzl@63969
   302
immler@67962
   303
subsection%unimportant \<open>Explicit expressions for convexity in terms of arbitrary sums\<close>
hoelzl@63969
   304
nipkow@64267
   305
lemma convex_sum:
hoelzl@63969
   306
  fixes C :: "'a::real_vector set"
hoelzl@63969
   307
  assumes "finite s"
hoelzl@63969
   308
    and "convex C"
hoelzl@63969
   309
    and "(\<Sum> i \<in> s. a i) = 1"
hoelzl@63969
   310
  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
hoelzl@63969
   311
    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
hoelzl@63969
   312
  shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
hoelzl@63969
   313
  using assms(1,3,4,5)
hoelzl@63969
   314
proof (induct arbitrary: a set: finite)
hoelzl@63969
   315
  case empty
hoelzl@63969
   316
  then show ?case by simp
hoelzl@63969
   317
next
hoelzl@63969
   318
  case (insert i s) note IH = this(3)
nipkow@64267
   319
  have "a i + sum a s = 1"
hoelzl@63969
   320
    and "0 \<le> a i"
hoelzl@63969
   321
    and "\<forall>j\<in>s. 0 \<le> a j"
hoelzl@63969
   322
    and "y i \<in> C"
hoelzl@63969
   323
    and "\<forall>j\<in>s. y j \<in> C"
hoelzl@63969
   324
    using insert.hyps(1,2) insert.prems by simp_all
nipkow@64267
   325
  then have "0 \<le> sum a s"
nipkow@64267
   326
    by (simp add: sum_nonneg)
hoelzl@63969
   327
  have "a i *\<^sub>R y i + (\<Sum>j\<in>s. a j *\<^sub>R y j) \<in> C"
nipkow@64267
   328
  proof (cases "sum a s = 0")
hoelzl@63969
   329
    case True
nipkow@64267
   330
    with \<open>a i + sum a s = 1\<close> have "a i = 1"
hoelzl@63969
   331
      by simp
nipkow@64267
   332
    from sum_nonneg_0 [OF \<open>finite s\<close> _ True] \<open>\<forall>j\<in>s. 0 \<le> a j\<close> have "\<forall>j\<in>s. a j = 0"
hoelzl@63969
   333
      by simp
hoelzl@63969
   334
    show ?thesis using \<open>a i = 1\<close> and \<open>\<forall>j\<in>s. a j = 0\<close> and \<open>y i \<in> C\<close>
hoelzl@63969
   335
      by simp
hoelzl@63969
   336
  next
hoelzl@63969
   337
    case False
nipkow@64267
   338
    with \<open>0 \<le> sum a s\<close> have "0 < sum a s"
hoelzl@63969
   339
      by simp
nipkow@64267
   340
    then have "(\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
hoelzl@63969
   341
      using \<open>\<forall>j\<in>s. 0 \<le> a j\<close> and \<open>\<forall>j\<in>s. y j \<in> C\<close>
nipkow@64267
   342
      by (simp add: IH sum_divide_distrib [symmetric])
hoelzl@63969
   343
    from \<open>convex C\<close> and \<open>y i \<in> C\<close> and this and \<open>0 \<le> a i\<close>
nipkow@64267
   344
      and \<open>0 \<le> sum a s\<close> and \<open>a i + sum a s = 1\<close>
nipkow@64267
   345
    have "a i *\<^sub>R y i + sum a s *\<^sub>R (\<Sum>j\<in>s. (a j / sum a s) *\<^sub>R y j) \<in> C"
hoelzl@63969
   346
      by (rule convexD)
hoelzl@63969
   347
    then show ?thesis
nipkow@64267
   348
      by (simp add: scaleR_sum_right False)
hoelzl@63969
   349
  qed
hoelzl@63969
   350
  then show ?case using \<open>finite s\<close> and \<open>i \<notin> s\<close>
hoelzl@63969
   351
    by simp
hoelzl@63969
   352
qed
hoelzl@63969
   353
hoelzl@63969
   354
lemma convex:
nipkow@64267
   355
  "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> (sum u {1..k} = 1)
nipkow@64267
   356
      \<longrightarrow> sum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
hoelzl@63969
   357
proof safe
hoelzl@63969
   358
  fix k :: nat
hoelzl@63969
   359
  fix u :: "nat \<Rightarrow> real"
hoelzl@63969
   360
  fix x
hoelzl@63969
   361
  assume "convex s"
hoelzl@63969
   362
    "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
nipkow@64267
   363
    "sum u {1..k} = 1"
nipkow@64267
   364
  with convex_sum[of "{1 .. k}" s] show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s"
nipkow@64267
   365
    by auto
nipkow@64267
   366
next
nipkow@64267
   367
  assume *: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> sum u {1..k} = 1
hoelzl@63969
   368
    \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
hoelzl@63969
   369
  {
hoelzl@63969
   370
    fix \<mu> :: real
hoelzl@63969
   371
    fix x y :: 'a
hoelzl@63969
   372
    assume xy: "x \<in> s" "y \<in> s"
hoelzl@63969
   373
    assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
hoelzl@63969
   374
    let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
hoelzl@63969
   375
    let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
hoelzl@63969
   376
    have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}"
hoelzl@63969
   377
      by auto
hoelzl@63969
   378
    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1"
hoelzl@63969
   379
      by simp
nipkow@64267
   380
    then have "sum ?u {1 .. 2} = 1"
nipkow@64267
   381
      using sum.If_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
hoelzl@63969
   382
      by auto
hoelzl@63969
   383
    with *[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
hoelzl@63969
   384
      using mu xy by auto
hoelzl@63969
   385
    have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
nipkow@64267
   386
      using sum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
nipkow@64267
   387
    from sum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
hoelzl@63969
   388
    have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
hoelzl@63969
   389
      by auto
hoelzl@63969
   390
    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s"
hoelzl@63969
   391
      using s by (auto simp: add.commute)
hoelzl@63969
   392
  }
hoelzl@63969
   393
  then show "convex s"
hoelzl@63969
   394
    unfolding convex_alt by auto
hoelzl@63969
   395
qed
hoelzl@63969
   396
hoelzl@63969
   397
hoelzl@63969
   398
lemma convex_explicit:
hoelzl@63969
   399
  fixes s :: "'a::real_vector set"
hoelzl@63969
   400
  shows "convex s \<longleftrightarrow>
nipkow@64267
   401
    (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> sum u t = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
hoelzl@63969
   402
proof safe
hoelzl@63969
   403
  fix t
hoelzl@63969
   404
  fix u :: "'a \<Rightarrow> real"
hoelzl@63969
   405
  assume "convex s"
hoelzl@63969
   406
    and "finite t"
nipkow@64267
   407
    and "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
hoelzl@63969
   408
  then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
nipkow@64267
   409
    using convex_sum[of t s u "\<lambda> x. x"] by auto
hoelzl@63969
   410
next
hoelzl@63969
   411
  assume *: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and>
nipkow@64267
   412
    sum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
hoelzl@63969
   413
  show "convex s"
hoelzl@63969
   414
    unfolding convex_alt
hoelzl@63969
   415
  proof safe
hoelzl@63969
   416
    fix x y
hoelzl@63969
   417
    fix \<mu> :: real
hoelzl@63969
   418
    assume **: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
hoelzl@63969
   419
    show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
hoelzl@63969
   420
    proof (cases "x = y")
hoelzl@63969
   421
      case False
hoelzl@63969
   422
      then show ?thesis
hoelzl@63969
   423
        using *[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"] **
hoelzl@63969
   424
        by auto
hoelzl@63969
   425
    next
hoelzl@63969
   426
      case True
hoelzl@63969
   427
      then show ?thesis
hoelzl@63969
   428
        using *[rule_format, of "{x, y}" "\<lambda> z. 1"] **
hoelzl@63969
   429
        by (auto simp: field_simps real_vector.scale_left_diff_distrib)
hoelzl@63969
   430
    qed
hoelzl@63969
   431
  qed
hoelzl@63969
   432
qed
hoelzl@63969
   433
hoelzl@63969
   434
lemma convex_finite:
hoelzl@63969
   435
  assumes "finite s"
nipkow@64267
   436
  shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> sum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
hoelzl@63969
   437
  unfolding convex_explicit
hoelzl@63969
   438
  apply safe
hoelzl@63969
   439
  subgoal for u by (erule allE [where x=s], erule allE [where x=u]) auto
hoelzl@63969
   440
  subgoal for t u
hoelzl@63969
   441
  proof -
hoelzl@63969
   442
    have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
hoelzl@63969
   443
      by simp
nipkow@64267
   444
    assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> sum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
nipkow@64267
   445
    assume *: "\<forall>x\<in>t. 0 \<le> u x" "sum u t = 1"
hoelzl@63969
   446
    assume "t \<subseteq> s"
hoelzl@63969
   447
    then have "s \<inter> t = t" by auto
hoelzl@63969
   448
    with sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] * show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
nipkow@64267
   449
      by (auto simp: assms sum.If_cases if_distrib if_distrib_arg)
hoelzl@63969
   450
  qed
hoelzl@63969
   451
  done
hoelzl@63969
   452
hoelzl@63969
   453
hoelzl@63969
   454
subsection \<open>Functions that are convex on a set\<close>
hoelzl@63969
   455
immler@67962
   456
definition%important convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
hoelzl@63969
   457
  where "convex_on s f \<longleftrightarrow>
hoelzl@63969
   458
    (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
hoelzl@63969
   459
hoelzl@63969
   460
lemma convex_onI [intro?]:
hoelzl@63969
   461
  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
hoelzl@63969
   462
    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
hoelzl@63969
   463
  shows "convex_on A f"
hoelzl@63969
   464
  unfolding convex_on_def
hoelzl@63969
   465
proof clarify
hoelzl@63969
   466
  fix x y
hoelzl@63969
   467
  fix u v :: real
hoelzl@63969
   468
  assume A: "x \<in> A" "y \<in> A" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
hoelzl@63969
   469
  from A(5) have [simp]: "v = 1 - u"
hoelzl@63969
   470
    by (simp add: algebra_simps)
hoelzl@63969
   471
  from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
hoelzl@63969
   472
    using assms[of u y x]
hoelzl@63969
   473
    by (cases "u = 0 \<or> u = 1") (auto simp: algebra_simps)
hoelzl@63969
   474
qed
hoelzl@63969
   475
hoelzl@63969
   476
lemma convex_on_linorderI [intro?]:
hoelzl@63969
   477
  fixes A :: "('a::{linorder,real_vector}) set"
hoelzl@63969
   478
  assumes "\<And>t x y. t > 0 \<Longrightarrow> t < 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x < y \<Longrightarrow>
hoelzl@63969
   479
    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
hoelzl@63969
   480
  shows "convex_on A f"
hoelzl@63969
   481
proof
hoelzl@63969
   482
  fix x y
hoelzl@63969
   483
  fix t :: real
hoelzl@63969
   484
  assume A: "x \<in> A" "y \<in> A" "t > 0" "t < 1"
hoelzl@63969
   485
  with assms [of t x y] assms [of "1 - t" y x]
hoelzl@63969
   486
  show "f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
hoelzl@63969
   487
    by (cases x y rule: linorder_cases) (auto simp: algebra_simps)
hoelzl@63969
   488
qed
hoelzl@63969
   489
hoelzl@63969
   490
lemma convex_onD:
hoelzl@63969
   491
  assumes "convex_on A f"
hoelzl@63969
   492
  shows "\<And>t x y. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow>
hoelzl@63969
   493
    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
hoelzl@63969
   494
  using assms by (auto simp: convex_on_def)
hoelzl@63969
   495
hoelzl@63969
   496
lemma convex_onD_Icc:
hoelzl@63969
   497
  assumes "convex_on {x..y} f" "x \<le> (y :: _ :: {real_vector,preorder})"
hoelzl@63969
   498
  shows "\<And>t. t \<ge> 0 \<Longrightarrow> t \<le> 1 \<Longrightarrow>
hoelzl@63969
   499
    f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \<le> (1 - t) * f x + t * f y"
hoelzl@63969
   500
  using assms(2) by (intro convex_onD [OF assms(1)]) simp_all
hoelzl@63969
   501
hoelzl@63969
   502
lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
hoelzl@63969
   503
  unfolding convex_on_def by auto
hoelzl@63969
   504
hoelzl@63969
   505
lemma convex_on_add [intro]:
hoelzl@63969
   506
  assumes "convex_on s f"
hoelzl@63969
   507
    and "convex_on s g"
hoelzl@63969
   508
  shows "convex_on s (\<lambda>x. f x + g x)"
hoelzl@63969
   509
proof -
hoelzl@63969
   510
  {
hoelzl@63969
   511
    fix x y
hoelzl@63969
   512
    assume "x \<in> s" "y \<in> s"
hoelzl@63969
   513
    moreover
hoelzl@63969
   514
    fix u v :: real
hoelzl@63969
   515
    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
hoelzl@63969
   516
    ultimately
hoelzl@63969
   517
    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)"
hoelzl@63969
   518
      using assms unfolding convex_on_def by (auto simp: add_mono)
hoelzl@63969
   519
    then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
hoelzl@63969
   520
      by (simp add: field_simps)
hoelzl@63969
   521
  }
hoelzl@63969
   522
  then show ?thesis
hoelzl@63969
   523
    unfolding convex_on_def by auto
hoelzl@63969
   524
qed
hoelzl@63969
   525
hoelzl@63969
   526
lemma convex_on_cmul [intro]:
hoelzl@63969
   527
  fixes c :: real
hoelzl@63969
   528
  assumes "0 \<le> c"
hoelzl@63969
   529
    and "convex_on s f"
hoelzl@63969
   530
  shows "convex_on s (\<lambda>x. c * f x)"
hoelzl@63969
   531
proof -
hoelzl@63969
   532
  have *: "u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
hoelzl@63969
   533
    for u c fx v fy :: real
hoelzl@63969
   534
    by (simp add: field_simps)
hoelzl@63969
   535
  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
hoelzl@63969
   536
    unfolding convex_on_def and * by auto
hoelzl@63969
   537
qed
hoelzl@63969
   538
hoelzl@63969
   539
lemma convex_lower:
hoelzl@63969
   540
  assumes "convex_on s f"
hoelzl@63969
   541
    and "x \<in> s"
hoelzl@63969
   542
    and "y \<in> s"
hoelzl@63969
   543
    and "0 \<le> u"
hoelzl@63969
   544
    and "0 \<le> v"
hoelzl@63969
   545
    and "u + v = 1"
hoelzl@63969
   546
  shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
hoelzl@63969
   547
proof -
hoelzl@63969
   548
  let ?m = "max (f x) (f y)"
hoelzl@63969
   549
  have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
hoelzl@63969
   550
    using assms(4,5) by (auto simp: mult_left_mono add_mono)
hoelzl@63969
   551
  also have "\<dots> = max (f x) (f y)"
hoelzl@63969
   552
    using assms(6) by (simp add: distrib_right [symmetric])
hoelzl@63969
   553
  finally show ?thesis
hoelzl@63969
   554
    using assms unfolding convex_on_def by fastforce
hoelzl@63969
   555
qed
hoelzl@63969
   556
hoelzl@63969
   557
lemma convex_on_dist [intro]:
hoelzl@63969
   558
  fixes s :: "'a::real_normed_vector set"
hoelzl@63969
   559
  shows "convex_on s (\<lambda>x. dist a x)"
hoelzl@63969
   560
proof (auto simp: convex_on_def dist_norm)
hoelzl@63969
   561
  fix x y
hoelzl@63969
   562
  assume "x \<in> s" "y \<in> s"
hoelzl@63969
   563
  fix u v :: real
hoelzl@63969
   564
  assume "0 \<le> u"
hoelzl@63969
   565
  assume "0 \<le> v"
hoelzl@63969
   566
  assume "u + v = 1"
hoelzl@63969
   567
  have "a = u *\<^sub>R a + v *\<^sub>R a"
hoelzl@63969
   568
    unfolding scaleR_left_distrib[symmetric] and \<open>u + v = 1\<close> by simp
hoelzl@63969
   569
  then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
hoelzl@63969
   570
    by (auto simp: algebra_simps)
hoelzl@63969
   571
  show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
hoelzl@63969
   572
    unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
hoelzl@63969
   573
    using \<open>0 \<le> u\<close> \<open>0 \<le> v\<close> by auto
hoelzl@63969
   574
qed
hoelzl@63969
   575
hoelzl@63969
   576
immler@67962
   577
subsection%unimportant \<open>Arithmetic operations on sets preserve convexity\<close>
hoelzl@63969
   578
hoelzl@63969
   579
lemma convex_linear_image:
hoelzl@63969
   580
  assumes "linear f"
hoelzl@63969
   581
    and "convex s"
hoelzl@63969
   582
  shows "convex (f ` s)"
hoelzl@63969
   583
proof -
hoelzl@63969
   584
  interpret f: linear f by fact
hoelzl@63969
   585
  from \<open>convex s\<close> show "convex (f ` s)"
hoelzl@63969
   586
    by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
hoelzl@63969
   587
qed
hoelzl@63969
   588
hoelzl@63969
   589
lemma convex_linear_vimage:
hoelzl@63969
   590
  assumes "linear f"
hoelzl@63969
   591
    and "convex s"
hoelzl@63969
   592
  shows "convex (f -` s)"
hoelzl@63969
   593
proof -
hoelzl@63969
   594
  interpret f: linear f by fact
hoelzl@63969
   595
  from \<open>convex s\<close> show "convex (f -` s)"
hoelzl@63969
   596
    by (simp add: convex_def f.add f.scaleR)
hoelzl@63969
   597
qed
hoelzl@63969
   598
hoelzl@63969
   599
lemma convex_scaling:
hoelzl@63969
   600
  assumes "convex s"
hoelzl@63969
   601
  shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
hoelzl@63969
   602
proof -
hoelzl@63969
   603
  have "linear (\<lambda>x. c *\<^sub>R x)"
hoelzl@63969
   604
    by (simp add: linearI scaleR_add_right)
hoelzl@63969
   605
  then show ?thesis
hoelzl@63969
   606
    using \<open>convex s\<close> by (rule convex_linear_image)
hoelzl@63969
   607
qed
hoelzl@63969
   608
hoelzl@63969
   609
lemma convex_scaled:
lp15@65038
   610
  assumes "convex S"
lp15@65038
   611
  shows "convex ((\<lambda>x. x *\<^sub>R c) ` S)"
hoelzl@63969
   612
proof -
hoelzl@63969
   613
  have "linear (\<lambda>x. x *\<^sub>R c)"
hoelzl@63969
   614
    by (simp add: linearI scaleR_add_left)
hoelzl@63969
   615
  then show ?thesis
lp15@65038
   616
    using \<open>convex S\<close> by (rule convex_linear_image)
hoelzl@63969
   617
qed
hoelzl@63969
   618
hoelzl@63969
   619
lemma convex_negations:
lp15@65038
   620
  assumes "convex S"
lp15@65038
   621
  shows "convex ((\<lambda>x. - x) ` S)"
hoelzl@63969
   622
proof -
hoelzl@63969
   623
  have "linear (\<lambda>x. - x)"
hoelzl@63969
   624
    by (simp add: linearI)
hoelzl@63969
   625
  then show ?thesis
lp15@65038
   626
    using \<open>convex S\<close> by (rule convex_linear_image)
hoelzl@63969
   627
qed
hoelzl@63969
   628
hoelzl@63969
   629
lemma convex_sums:
lp15@65038
   630
  assumes "convex S"
lp15@65038
   631
    and "convex T"
lp15@65038
   632
  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
hoelzl@63969
   633
proof -
hoelzl@63969
   634
  have "linear (\<lambda>(x, y). x + y)"
hoelzl@63969
   635
    by (auto intro: linearI simp: scaleR_add_right)
lp15@65038
   636
  with assms have "convex ((\<lambda>(x, y). x + y) ` (S \<times> T))"
hoelzl@63969
   637
    by (intro convex_linear_image convex_Times)
lp15@65038
   638
  also have "((\<lambda>(x, y). x + y) ` (S \<times> T)) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
hoelzl@63969
   639
    by auto
hoelzl@63969
   640
  finally show ?thesis .
hoelzl@63969
   641
qed
hoelzl@63969
   642
hoelzl@63969
   643
lemma convex_differences:
lp15@65038
   644
  assumes "convex S" "convex T"
lp15@65038
   645
  shows "convex (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
lp15@65038
   646
proof -
lp15@65038
   647
  have "{x - y| x y. x \<in> S \<and> y \<in> T} = {x + y |x y. x \<in> S \<and> y \<in> uminus ` T}"
hoelzl@63969
   648
    by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff)
hoelzl@63969
   649
  then show ?thesis
hoelzl@63969
   650
    using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
hoelzl@63969
   651
qed
hoelzl@63969
   652
hoelzl@63969
   653
lemma convex_translation:
lp15@65038
   654
  assumes "convex S"
lp15@65038
   655
  shows "convex ((\<lambda>x. a + x) ` S)"
lp15@65038
   656
proof -
lp15@65038
   657
  have "(\<Union> x\<in> {a}. \<Union>y \<in> S. {x + y}) = (\<lambda>x. a + x) ` S"
hoelzl@63969
   658
    by auto
hoelzl@63969
   659
  then show ?thesis
hoelzl@63969
   660
    using convex_sums[OF convex_singleton[of a] assms] by auto
hoelzl@63969
   661
qed
hoelzl@63969
   662
hoelzl@63969
   663
lemma convex_affinity:
lp15@65038
   664
  assumes "convex S"
lp15@65038
   665
  shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` S)"
lp15@65038
   666
proof -
nipkow@67399
   667
  have "(\<lambda>x. a + c *\<^sub>R x) ` S = (+) a ` ( *\<^sub>R) c ` S"
hoelzl@63969
   668
    by auto
hoelzl@63969
   669
  then show ?thesis
hoelzl@63969
   670
    using convex_translation[OF convex_scaling[OF assms], of a c] by auto
hoelzl@63969
   671
qed
hoelzl@63969
   672
hoelzl@63969
   673
lemma pos_is_convex: "convex {0 :: real <..}"
hoelzl@63969
   674
  unfolding convex_alt
hoelzl@63969
   675
proof safe
hoelzl@63969
   676
  fix y x \<mu> :: real
hoelzl@63969
   677
  assume *: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
hoelzl@63969
   678
  {
hoelzl@63969
   679
    assume "\<mu> = 0"
hoelzl@63969
   680
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y"
hoelzl@63969
   681
      by simp
hoelzl@63969
   682
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
hoelzl@63969
   683
      using * by simp
hoelzl@63969
   684
  }
hoelzl@63969
   685
  moreover
hoelzl@63969
   686
  {
hoelzl@63969
   687
    assume "\<mu> = 1"
hoelzl@63969
   688
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
hoelzl@63969
   689
      using * by simp
hoelzl@63969
   690
  }
hoelzl@63969
   691
  moreover
hoelzl@63969
   692
  {
hoelzl@63969
   693
    assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
hoelzl@63969
   694
    then have "\<mu> > 0" "(1 - \<mu>) > 0"
hoelzl@63969
   695
      using * by auto
hoelzl@63969
   696
    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0"
hoelzl@63969
   697
      using * by (auto simp: add_pos_pos)
hoelzl@63969
   698
  }
hoelzl@63969
   699
  ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0"
hoelzl@63969
   700
    by fastforce
hoelzl@63969
   701
qed
hoelzl@63969
   702
nipkow@64267
   703
lemma convex_on_sum:
hoelzl@63969
   704
  fixes a :: "'a \<Rightarrow> real"
hoelzl@63969
   705
    and y :: "'a \<Rightarrow> 'b::real_vector"
hoelzl@63969
   706
    and f :: "'b \<Rightarrow> real"
hoelzl@63969
   707
  assumes "finite s" "s \<noteq> {}"
hoelzl@63969
   708
    and "convex_on C f"
hoelzl@63969
   709
    and "convex C"
hoelzl@63969
   710
    and "(\<Sum> i \<in> s. a i) = 1"
hoelzl@63969
   711
    and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
hoelzl@63969
   712
    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
hoelzl@63969
   713
  shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
hoelzl@63969
   714
  using assms
hoelzl@63969
   715
proof (induct s arbitrary: a rule: finite_ne_induct)
hoelzl@63969
   716
  case (singleton i)
hoelzl@63969
   717
  then have ai: "a i = 1"
hoelzl@63969
   718
    by auto
hoelzl@63969
   719
  then show ?case
hoelzl@63969
   720
    by auto
hoelzl@63969
   721
next
hoelzl@63969
   722
  case (insert i s)
hoelzl@63969
   723
  then have "convex_on C f"
hoelzl@63969
   724
    by simp
hoelzl@63969
   725
  from this[unfolded convex_on_def, rule_format]
hoelzl@63969
   726
  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1 \<Longrightarrow>
hoelzl@63969
   727
      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
hoelzl@63969
   728
    by simp
hoelzl@63969
   729
  show ?case
hoelzl@63969
   730
  proof (cases "a i = 1")
hoelzl@63969
   731
    case True
hoelzl@63969
   732
    then have "(\<Sum> j \<in> s. a j) = 0"
hoelzl@63969
   733
      using insert by auto
hoelzl@63969
   734
    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
nipkow@64267
   735
      using insert by (fastforce simp: sum_nonneg_eq_0_iff)
hoelzl@63969
   736
    then show ?thesis
hoelzl@63969
   737
      using insert by auto
hoelzl@63969
   738
  next
hoelzl@63969
   739
    case False
hoelzl@63969
   740
    from insert have yai: "y i \<in> C" "a i \<ge> 0"
hoelzl@63969
   741
      by auto
hoelzl@63969
   742
    have fis: "finite (insert i s)"
hoelzl@63969
   743
      using insert by auto
hoelzl@63969
   744
    then have ai1: "a i \<le> 1"
nipkow@64267
   745
      using sum_nonneg_leq_bound[of "insert i s" a] insert by simp
hoelzl@63969
   746
    then have "a i < 1"
hoelzl@63969
   747
      using False by auto
hoelzl@63969
   748
    then have i0: "1 - a i > 0"
hoelzl@63969
   749
      by auto
hoelzl@63969
   750
    let ?a = "\<lambda>j. a j / (1 - a i)"
hoelzl@63969
   751
    have a_nonneg: "?a j \<ge> 0" if "j \<in> s" for j
hoelzl@63969
   752
      using i0 insert that by fastforce
hoelzl@63969
   753
    have "(\<Sum> j \<in> insert i s. a j) = 1"
hoelzl@63969
   754
      using insert by auto
hoelzl@63969
   755
    then have "(\<Sum> j \<in> s. a j) = 1 - a i"
nipkow@64267
   756
      using sum.insert insert by fastforce
hoelzl@63969
   757
    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1"
hoelzl@63969
   758
      using i0 by auto
hoelzl@63969
   759
    then have a1: "(\<Sum> j \<in> s. ?a j) = 1"
nipkow@64267
   760
      unfolding sum_divide_distrib by simp
hoelzl@63969
   761
    have "convex C" using insert by auto
hoelzl@63969
   762
    then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
nipkow@64267
   763
      using insert convex_sum [OF \<open>finite s\<close> \<open>convex C\<close> a1 a_nonneg] by auto
hoelzl@63969
   764
    have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
hoelzl@63969
   765
      using a_nonneg a1 insert by blast
hoelzl@63969
   766
    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)"
nipkow@64267
   767
      using sum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF \<open>finite s\<close> \<open>i \<notin> s\<close>] insert
hoelzl@63969
   768
      by (auto simp only: add.commute)
hoelzl@63969
   769
    also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
hoelzl@63969
   770
      using i0 by auto
hoelzl@63969
   771
    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)"
nipkow@64267
   772
      using scaleR_right.sum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
hoelzl@63969
   773
      by (auto simp: algebra_simps)
hoelzl@63969
   774
    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)"
hoelzl@63969
   775
      by (auto simp: divide_inverse)
hoelzl@63969
   776
    also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
hoelzl@63969
   777
      using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
hoelzl@63969
   778
      by (auto simp: add.commute)
hoelzl@63969
   779
    also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
hoelzl@63969
   780
      using add_right_mono [OF mult_left_mono [of _ _ "1 - a i",
hoelzl@63969
   781
            OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"]
hoelzl@63969
   782
      by simp
hoelzl@63969
   783
    also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
nipkow@64267
   784
      unfolding sum_distrib_left[of "1 - a i" "\<lambda> j. ?a j * f (y j)"]
hoelzl@63969
   785
      using i0 by auto
hoelzl@63969
   786
    also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)"
hoelzl@63969
   787
      using i0 by auto
hoelzl@63969
   788
    also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))"
hoelzl@63969
   789
      using insert by auto
hoelzl@63969
   790
    finally show ?thesis
hoelzl@63969
   791
      by simp
hoelzl@63969
   792
  qed
hoelzl@63969
   793
qed
hoelzl@63969
   794
hoelzl@63969
   795
lemma convex_on_alt:
hoelzl@63969
   796
  fixes C :: "'a::real_vector set"
hoelzl@63969
   797
  assumes "convex C"
hoelzl@63969
   798
  shows "convex_on C f \<longleftrightarrow>
hoelzl@63969
   799
    (\<forall>x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1 \<longrightarrow>
hoelzl@63969
   800
      f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
hoelzl@63969
   801
proof safe
hoelzl@63969
   802
  fix x y
hoelzl@63969
   803
  fix \<mu> :: real
hoelzl@63969
   804
  assume *: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
hoelzl@63969
   805
  from this[unfolded convex_on_def, rule_format]
hoelzl@63969
   806
  have "0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" for u v
hoelzl@63969
   807
    by auto
hoelzl@63969
   808
  from this [of "\<mu>" "1 - \<mu>", simplified] *
hoelzl@63969
   809
  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
hoelzl@63969
   810
    by auto
hoelzl@63969
   811
next
hoelzl@63969
   812
  assume *: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow>
hoelzl@63969
   813
    f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
hoelzl@63969
   814
  {
hoelzl@63969
   815
    fix x y
hoelzl@63969
   816
    fix u v :: real
hoelzl@63969
   817
    assume **: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
hoelzl@63969
   818
    then have[simp]: "1 - u = v" by auto
hoelzl@63969
   819
    from *[rule_format, of x y u]
hoelzl@63969
   820
    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y"
hoelzl@63969
   821
      using ** by auto
hoelzl@63969
   822
  }
hoelzl@63969
   823
  then show "convex_on C f"
hoelzl@63969
   824
    unfolding convex_on_def by auto
hoelzl@63969
   825
qed
hoelzl@63969
   826
hoelzl@63969
   827
lemma convex_on_diff:
hoelzl@63969
   828
  fixes f :: "real \<Rightarrow> real"
hoelzl@63969
   829
  assumes f: "convex_on I f"
hoelzl@63969
   830
    and I: "x \<in> I" "y \<in> I"
hoelzl@63969
   831
    and t: "x < t" "t < y"
hoelzl@63969
   832
  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
hoelzl@63969
   833
    and "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
hoelzl@63969
   834
proof -
hoelzl@63969
   835
  define a where "a \<equiv> (t - y) / (x - y)"
hoelzl@63969
   836
  with t have "0 \<le> a" "0 \<le> 1 - a"
hoelzl@63969
   837
    by (auto simp: field_simps)
hoelzl@63969
   838
  with f \<open>x \<in> I\<close> \<open>y \<in> I\<close> have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
hoelzl@63969
   839
    by (auto simp: convex_on_def)
hoelzl@63969
   840
  have "a * x + (1 - a) * y = a * (x - y) + y"
hoelzl@63969
   841
    by (simp add: field_simps)
hoelzl@63969
   842
  also have "\<dots> = t"
hoelzl@63969
   843
    unfolding a_def using \<open>x < t\<close> \<open>t < y\<close> by simp
hoelzl@63969
   844
  finally have "f t \<le> a * f x + (1 - a) * f y"
hoelzl@63969
   845
    using cvx by simp
hoelzl@63969
   846
  also have "\<dots> = a * (f x - f y) + f y"
hoelzl@63969
   847
    by (simp add: field_simps)
hoelzl@63969
   848
  finally have "f t - f y \<le> a * (f x - f y)"
hoelzl@63969
   849
    by simp
hoelzl@63969
   850
  with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
hoelzl@63969
   851
    by (simp add: le_divide_eq divide_le_eq field_simps a_def)
hoelzl@63969
   852
  with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
hoelzl@63969
   853
    by (simp add: le_divide_eq divide_le_eq field_simps)
hoelzl@63969
   854
qed
hoelzl@63969
   855
hoelzl@63969
   856
lemma pos_convex_function:
hoelzl@63969
   857
  fixes f :: "real \<Rightarrow> real"
hoelzl@63969
   858
  assumes "convex C"
hoelzl@63969
   859
    and leq: "\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
hoelzl@63969
   860
  shows "convex_on C f"
hoelzl@63969
   861
  unfolding convex_on_alt[OF assms(1)]
hoelzl@63969
   862
  using assms
hoelzl@63969
   863
proof safe
hoelzl@63969
   864
  fix x y \<mu> :: real
hoelzl@63969
   865
  let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
hoelzl@63969
   866
  assume *: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
hoelzl@63969
   867
  then have "1 - \<mu> \<ge> 0" by auto
hoelzl@63969
   868
  then have xpos: "?x \<in> C"
hoelzl@63969
   869
    using * unfolding convex_alt by fastforce
hoelzl@63969
   870
  have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x) \<ge>
hoelzl@63969
   871
      \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
hoelzl@63969
   872
    using add_mono [OF mult_left_mono [OF leq [OF xpos *(2)] \<open>\<mu> \<ge> 0\<close>]
hoelzl@63969
   873
        mult_left_mono [OF leq [OF xpos *(3)] \<open>1 - \<mu> \<ge> 0\<close>]]
hoelzl@63969
   874
    by auto
hoelzl@63969
   875
  then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
hoelzl@63969
   876
    by (auto simp: field_simps)
hoelzl@63969
   877
  then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
hoelzl@63969
   878
    using convex_on_alt by auto
hoelzl@63969
   879
qed
hoelzl@63969
   880
hoelzl@63969
   881
lemma atMostAtLeast_subset_convex:
hoelzl@63969
   882
  fixes C :: "real set"
hoelzl@63969
   883
  assumes "convex C"
hoelzl@63969
   884
    and "x \<in> C" "y \<in> C" "x < y"
hoelzl@63969
   885
  shows "{x .. y} \<subseteq> C"
hoelzl@63969
   886
proof safe
hoelzl@63969
   887
  fix z assume z: "z \<in> {x .. y}"
hoelzl@63969
   888
  have less: "z \<in> C" if *: "x < z" "z < y"
hoelzl@63969
   889
  proof -
hoelzl@63969
   890
    let ?\<mu> = "(y - z) / (y - x)"
hoelzl@63969
   891
    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1"
hoelzl@63969
   892
      using assms * by (auto simp: field_simps)
hoelzl@63969
   893
    then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
hoelzl@63969
   894
      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
hoelzl@63969
   895
      by (simp add: algebra_simps)
hoelzl@63969
   896
    have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
hoelzl@63969
   897
      by (auto simp: field_simps)
hoelzl@63969
   898
    also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
hoelzl@63969
   899
      using assms by (simp only: add_divide_distrib) (auto simp: field_simps)
hoelzl@63969
   900
    also have "\<dots> = z"
hoelzl@63969
   901
      using assms by (auto simp: field_simps)
hoelzl@63969
   902
    finally show ?thesis
hoelzl@63969
   903
      using comb by auto
hoelzl@63969
   904
  qed
hoelzl@63969
   905
  show "z \<in> C"
hoelzl@63969
   906
    using z less assms by (auto simp: le_less)
hoelzl@63969
   907
qed
hoelzl@63969
   908
hoelzl@63969
   909
lemma f''_imp_f':
hoelzl@63969
   910
  fixes f :: "real \<Rightarrow> real"
hoelzl@63969
   911
  assumes "convex C"
hoelzl@63969
   912
    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
hoelzl@63969
   913
    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
hoelzl@63969
   914
    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
hoelzl@63969
   915
    and x: "x \<in> C"
hoelzl@63969
   916
    and y: "y \<in> C"
hoelzl@63969
   917
  shows "f' x * (y - x) \<le> f y - f x"
hoelzl@63969
   918
  using assms
hoelzl@63969
   919
proof -
hoelzl@63969
   920
  have less_imp: "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
hoelzl@63969
   921
    if *: "x \<in> C" "y \<in> C" "y > x" for x y :: real
hoelzl@63969
   922
  proof -
hoelzl@63969
   923
    from * have ge: "y - x > 0" "y - x \<ge> 0"
hoelzl@63969
   924
      by auto
hoelzl@63969
   925
    from * have le: "x - y < 0" "x - y \<le> 0"
hoelzl@63969
   926
      by auto
hoelzl@63969
   927
    then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
hoelzl@63969
   928
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>],
hoelzl@63969
   929
          THEN f', THEN MVT2[OF \<open>x < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
hoelzl@63969
   930
      by auto
hoelzl@63969
   931
    then have "z1 \<in> C"
hoelzl@63969
   932
      using atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>y \<in> C\<close> \<open>x < y\<close>
hoelzl@63969
   933
      by fastforce
hoelzl@63969
   934
    from z1 have z1': "f x - f y = (x - y) * f' z1"
hoelzl@63969
   935
      by (simp add: field_simps)
hoelzl@63969
   936
    obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
hoelzl@63969
   937
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>],
hoelzl@63969
   938
          THEN f'', THEN MVT2[OF \<open>x < z1\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
hoelzl@63969
   939
      by auto
hoelzl@63969
   940
    obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
hoelzl@63969
   941
      using subsetD[OF atMostAtLeast_subset_convex[OF \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>],
hoelzl@63969
   942
          THEN f'', THEN MVT2[OF \<open>z1 < y\<close>, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
hoelzl@63969
   943
      by auto
hoelzl@63969
   944
    have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
hoelzl@63969
   945
      using * z1' by auto
hoelzl@63969
   946
    also have "\<dots> = (y - z1) * f'' z3"
hoelzl@63969
   947
      using z3 by auto
hoelzl@63969
   948
    finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3"
hoelzl@63969
   949
      by simp
hoelzl@63969
   950
    have A': "y - z1 \<ge> 0"
hoelzl@63969
   951
      using z1 by auto
hoelzl@63969
   952
    have "z3 \<in> C"
hoelzl@63969
   953
      using z3 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>x \<in> C\<close> \<open>z1 \<in> C\<close> \<open>x < z1\<close>
hoelzl@63969
   954
      by fastforce
hoelzl@63969
   955
    then have B': "f'' z3 \<ge> 0"
hoelzl@63969
   956
      using assms by auto
hoelzl@63969
   957
    from A' B' have "(y - z1) * f'' z3 \<ge> 0"
hoelzl@63969
   958
      by auto
hoelzl@63969
   959
    from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0"
hoelzl@63969
   960
      by auto
hoelzl@63969
   961
    from mult_right_mono_neg[OF this le(2)]
hoelzl@63969
   962
    have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
hoelzl@63969
   963
      by (simp add: algebra_simps)
hoelzl@63969
   964
    then have "f' y * (x - y) - (f x - f y) \<le> 0"
hoelzl@63969
   965
      using le by auto
hoelzl@63969
   966
    then have res: "f' y * (x - y) \<le> f x - f y"
hoelzl@63969
   967
      by auto
hoelzl@63969
   968
    have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
hoelzl@63969
   969
      using * z1 by auto
hoelzl@63969
   970
    also have "\<dots> = (z1 - x) * f'' z2"
hoelzl@63969
   971
      using z2 by auto
hoelzl@63969
   972
    finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2"
hoelzl@63969
   973
      by simp
hoelzl@63969
   974
    have A: "z1 - x \<ge> 0"
hoelzl@63969
   975
      using z1 by auto
hoelzl@63969
   976
    have "z2 \<in> C"
hoelzl@63969
   977
      using z2 z1 * atMostAtLeast_subset_convex \<open>convex C\<close> \<open>z1 \<in> C\<close> \<open>y \<in> C\<close> \<open>z1 < y\<close>
hoelzl@63969
   978
      by fastforce
hoelzl@63969
   979
    then have B: "f'' z2 \<ge> 0"
hoelzl@63969
   980
      using assms by auto
hoelzl@63969
   981
    from A B have "(z1 - x) * f'' z2 \<ge> 0"
hoelzl@63969
   982
      by auto
hoelzl@63969
   983
    with cool have "(f y - f x) / (y - x) - f' x \<ge> 0"
hoelzl@63969
   984
      by auto
hoelzl@63969
   985
    from mult_right_mono[OF this ge(2)]
hoelzl@63969
   986
    have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
hoelzl@63969
   987
      by (simp add: algebra_simps)
hoelzl@63969
   988
    then have "f y - f x - f' x * (y - x) \<ge> 0"
hoelzl@63969
   989
      using ge by auto
hoelzl@63969
   990
    then show "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
hoelzl@63969
   991
      using res by auto
hoelzl@63969
   992
  qed
hoelzl@63969
   993
  show ?thesis
hoelzl@63969
   994
  proof (cases "x = y")
hoelzl@63969
   995
    case True
hoelzl@63969
   996
    with x y show ?thesis by auto
hoelzl@63969
   997
  next
hoelzl@63969
   998
    case False
hoelzl@63969
   999
    with less_imp x y show ?thesis
hoelzl@63969
  1000
      by (auto simp: neq_iff)
hoelzl@63969
  1001
  qed
hoelzl@63969
  1002
qed
hoelzl@63969
  1003
hoelzl@63969
  1004
lemma f''_ge0_imp_convex:
hoelzl@63969
  1005
  fixes f :: "real \<Rightarrow> real"
hoelzl@63969
  1006
  assumes conv: "convex C"
hoelzl@63969
  1007
    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
hoelzl@63969
  1008
    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
hoelzl@63969
  1009
    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
hoelzl@63969
  1010
  shows "convex_on C f"
hoelzl@63969
  1011
  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function
hoelzl@63969
  1012
  by fastforce
hoelzl@63969
  1013
hoelzl@63969
  1014
lemma minus_log_convex:
hoelzl@63969
  1015
  fixes b :: real
hoelzl@63969
  1016
  assumes "b > 1"
hoelzl@63969
  1017
  shows "convex_on {0 <..} (\<lambda> x. - log b x)"
hoelzl@63969
  1018
proof -
hoelzl@63969
  1019
  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)"
hoelzl@63969
  1020
    using DERIV_log by auto
hoelzl@63969
  1021
  then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
hoelzl@63969
  1022
    by (auto simp: DERIV_minus)
hoelzl@63969
  1023
  have "\<And>z::real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
hoelzl@63969
  1024
    using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
hoelzl@63969
  1025
  from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
hoelzl@63969
  1026
  have "\<And>z::real. z > 0 \<Longrightarrow>
hoelzl@63969
  1027
    DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
hoelzl@63969
  1028
    by auto
hoelzl@63969
  1029
  then have f''0: "\<And>z::real. z > 0 \<Longrightarrow>
hoelzl@63969
  1030
    DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
hoelzl@63969
  1031
    unfolding inverse_eq_divide by (auto simp: mult.assoc)
hoelzl@63969
  1032
  have f''_ge0: "\<And>z::real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
hoelzl@63969
  1033
    using \<open>b > 1\<close> by (auto intro!: less_imp_le)
hoelzl@63969
  1034
  from f''_ge0_imp_convex[OF pos_is_convex, unfolded greaterThan_iff, OF f' f''0 f''_ge0]
hoelzl@63969
  1035
  show ?thesis
hoelzl@63969
  1036
    by auto
hoelzl@63969
  1037
qed
hoelzl@63969
  1038
hoelzl@63969
  1039
immler@67962
  1040
subsection%unimportant \<open>Convexity of real functions\<close>
hoelzl@63969
  1041
hoelzl@63969
  1042
lemma convex_on_realI:
hoelzl@63969
  1043
  assumes "connected A"
hoelzl@63969
  1044
    and "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
hoelzl@63969
  1045
    and "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f' x \<le> f' y"
hoelzl@63969
  1046
  shows "convex_on A f"
hoelzl@63969
  1047
proof (rule convex_on_linorderI)
hoelzl@63969
  1048
  fix t x y :: real
hoelzl@63969
  1049
  assume t: "t > 0" "t < 1"
hoelzl@63969
  1050
  assume xy: "x \<in> A" "y \<in> A" "x < y"
hoelzl@63969
  1051
  define z where "z = (1 - t) * x + t * y"
hoelzl@63969
  1052
  with \<open>connected A\<close> and xy have ivl: "{x..y} \<subseteq> A"
hoelzl@63969
  1053
    using connected_contains_Icc by blast
hoelzl@63969
  1054
hoelzl@63969
  1055
  from xy t have xz: "z > x"
hoelzl@63969
  1056
    by (simp add: z_def algebra_simps)
hoelzl@63969
  1057
  have "y - z = (1 - t) * (y - x)"
hoelzl@63969
  1058
    by (simp add: z_def algebra_simps)
hoelzl@63969
  1059
  also from xy t have "\<dots> > 0"
hoelzl@63969
  1060
    by (intro mult_pos_pos) simp_all
hoelzl@63969
  1061
  finally have yz: "z < y"
hoelzl@63969
  1062
    by simp
hoelzl@63969
  1063
hoelzl@63969
  1064
  from assms xz yz ivl t have "\<exists>\<xi>. \<xi> > x \<and> \<xi> < z \<and> f z - f x = (z - x) * f' \<xi>"
hoelzl@63969
  1065
    by (intro MVT2) (auto intro!: assms(2))
hoelzl@63969
  1066
  then obtain \<xi> where \<xi>: "\<xi> > x" "\<xi> < z" "f' \<xi> = (f z - f x) / (z - x)"
hoelzl@63969
  1067
    by auto
hoelzl@63969
  1068
  from assms xz yz ivl t have "\<exists>\<eta>. \<eta> > z \<and> \<eta> < y \<and> f y - f z = (y - z) * f' \<eta>"
hoelzl@63969
  1069
    by (intro MVT2) (auto intro!: assms(2))
hoelzl@63969
  1070
  then obtain \<eta> where \<eta>: "\<eta> > z" "\<eta> < y" "f' \<eta> = (f y - f z) / (y - z)"
hoelzl@63969
  1071
    by auto
hoelzl@63969
  1072
hoelzl@63969
  1073
  from \<eta>(3) have "(f y - f z) / (y - z) = f' \<eta>" ..
hoelzl@63969
  1074
  also from \<xi> \<eta> ivl have "\<xi> \<in> A" "\<eta> \<in> A"
hoelzl@63969
  1075
    by auto
hoelzl@63969
  1076
  with \<xi> \<eta> have "f' \<eta> \<ge> f' \<xi>"
hoelzl@63969
  1077
    by (intro assms(3)) auto
hoelzl@63969
  1078
  also from \<xi>(3) have "f' \<xi> = (f z - f x) / (z - x)" .
hoelzl@63969
  1079
  finally have "(f y - f z) * (z - x) \<ge> (f z - f x) * (y - z)"
hoelzl@63969
  1080
    using xz yz by (simp add: field_simps)
hoelzl@63969
  1081
  also have "z - x = t * (y - x)"
hoelzl@63969
  1082
    by (simp add: z_def algebra_simps)
hoelzl@63969
  1083
  also have "y - z = (1 - t) * (y - x)"
hoelzl@63969
  1084
    by (simp add: z_def algebra_simps)
hoelzl@63969
  1085
  finally have "(f y - f z) * t \<ge> (f z - f x) * (1 - t)"
hoelzl@63969
  1086
    using xy by simp
hoelzl@63969
  1087
  then show "(1 - t) * f x + t * f y \<ge> f ((1 - t) *\<^sub>R x + t *\<^sub>R y)"
hoelzl@63969
  1088
    by (simp add: z_def algebra_simps)
hoelzl@63969
  1089
qed
hoelzl@63969
  1090
hoelzl@63969
  1091
lemma convex_on_inverse:
hoelzl@63969
  1092
  assumes "A \<subseteq> {0<..}"
hoelzl@63969
  1093
  shows "convex_on A (inverse :: real \<Rightarrow> real)"
hoelzl@63969
  1094
proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\<lambda>x. -inverse (x^2)"])
hoelzl@63969
  1095
  fix u v :: real
hoelzl@63969
  1096
  assume "u \<in> {0<..}" "v \<in> {0<..}" "u \<le> v"
hoelzl@63969
  1097
  with assms show "-inverse (u^2) \<le> -inverse (v^2)"
hoelzl@63969
  1098
    by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all)
hoelzl@63969
  1099
qed (insert assms, auto intro!: derivative_eq_intros simp: divide_simps power2_eq_square)
hoelzl@63969
  1100
hoelzl@63969
  1101
lemma convex_onD_Icc':
hoelzl@63969
  1102
  assumes "convex_on {x..y} f" "c \<in> {x..y}"
hoelzl@63969
  1103
  defines "d \<equiv> y - x"
hoelzl@63969
  1104
  shows "f c \<le> (f y - f x) / d * (c - x) + f x"
hoelzl@63969
  1105
proof (cases x y rule: linorder_cases)
hoelzl@63969
  1106
  case less
hoelzl@63969
  1107
  then have d: "d > 0"
hoelzl@63969
  1108
    by (simp add: d_def)
hoelzl@63969
  1109
  from assms(2) less have A: "0 \<le> (c - x) / d" "(c - x) / d \<le> 1"
hoelzl@63969
  1110
    by (simp_all add: d_def divide_simps)
hoelzl@63969
  1111
  have "f c = f (x + (c - x) * 1)"
hoelzl@63969
  1112
    by simp
hoelzl@63969
  1113
  also from less have "1 = ((y - x) / d)"
hoelzl@63969
  1114
    by (simp add: d_def)
hoelzl@63969
  1115
  also from d have "x + (c - x) * \<dots> = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y"
hoelzl@63969
  1116
    by (simp add: field_simps)
hoelzl@63969
  1117
  also have "f \<dots> \<le> (1 - (c - x) / d) * f x + (c - x) / d * f y"
hoelzl@63969
  1118
    using assms less by (intro convex_onD_Icc) simp_all
hoelzl@63969
  1119
  also from d have "\<dots> = (f y - f x) / d * (c - x) + f x"
hoelzl@63969
  1120
    by (simp add: field_simps)
hoelzl@63969
  1121
  finally show ?thesis .
hoelzl@63969
  1122
qed (insert assms(2), simp_all)
hoelzl@63969
  1123
hoelzl@63969
  1124
lemma convex_onD_Icc'':
hoelzl@63969
  1125
  assumes "convex_on {x..y} f" "c \<in> {x..y}"
hoelzl@63969
  1126
  defines "d \<equiv> y - x"
hoelzl@63969
  1127
  shows "f c \<le> (f x - f y) / d * (y - c) + f y"
hoelzl@63969
  1128
proof (cases x y rule: linorder_cases)
hoelzl@63969
  1129
  case less
hoelzl@63969
  1130
  then have d: "d > 0"
hoelzl@63969
  1131
    by (simp add: d_def)
hoelzl@63969
  1132
  from assms(2) less have A: "0 \<le> (y - c) / d" "(y - c) / d \<le> 1"
hoelzl@63969
  1133
    by (simp_all add: d_def divide_simps)
hoelzl@63969
  1134
  have "f c = f (y - (y - c) * 1)"
hoelzl@63969
  1135
    by simp
hoelzl@63969
  1136
  also from less have "1 = ((y - x) / d)"
hoelzl@63969
  1137
    by (simp add: d_def)
hoelzl@63969
  1138
  also from d have "y - (y - c) * \<dots> = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y"
hoelzl@63969
  1139
    by (simp add: field_simps)
hoelzl@63969
  1140
  also have "f \<dots> \<le> (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y"
hoelzl@63969
  1141
    using assms less by (intro convex_onD_Icc) (simp_all add: field_simps)
hoelzl@63969
  1142
  also from d have "\<dots> = (f x - f y) / d * (y - c) + f y"
hoelzl@63969
  1143
    by (simp add: field_simps)
hoelzl@63969
  1144
  finally show ?thesis .
hoelzl@63969
  1145
qed (insert assms(2), simp_all)
hoelzl@63969
  1146
nipkow@64267
  1147
lemma convex_supp_sum:
nipkow@64267
  1148
  assumes "convex S" and 1: "supp_sum u I = 1"
hoelzl@63969
  1149
      and "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> u i \<and> (u i = 0 \<or> f i \<in> S)"
nipkow@64267
  1150
    shows "supp_sum (\<lambda>i. u i *\<^sub>R f i) I \<in> S"
hoelzl@63969
  1151
proof -
hoelzl@63969
  1152
  have fin: "finite {i \<in> I. u i \<noteq> 0}"
nipkow@64267
  1153
    using 1 sum.infinite by (force simp: supp_sum_def support_on_def)
nipkow@64267
  1154
  then have eq: "supp_sum (\<lambda>i. u i *\<^sub>R f i) I = sum (\<lambda>i. u i *\<^sub>R f i) {i \<in> I. u i \<noteq> 0}"
nipkow@64267
  1155
    by (force intro: sum.mono_neutral_left simp: supp_sum_def support_on_def)
hoelzl@63969
  1156
  show ?thesis
hoelzl@63969
  1157
    apply (simp add: eq)
nipkow@64267
  1158
    apply (rule convex_sum [OF fin \<open>convex S\<close>])
nipkow@64267
  1159
    using 1 assms apply (auto simp: supp_sum_def support_on_def)
hoelzl@63969
  1160
    done
hoelzl@63969
  1161
qed
hoelzl@63969
  1162
hoelzl@63969
  1163
lemma convex_translation_eq [simp]: "convex ((\<lambda>x. a + x) ` s) \<longleftrightarrow> convex s"
hoelzl@63969
  1164
  by (metis convex_translation translation_galois)
hoelzl@63969
  1165
lp15@61694
  1166
lemma convex_linear_image_eq [simp]:
lp15@61694
  1167
    fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
lp15@61694
  1168
    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> convex (f ` s) \<longleftrightarrow> convex s"
lp15@61694
  1169
    by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq)
lp15@61694
  1170
paulson@61518
  1171
lemma closure_bounded_linear_image_subset:
huffman@44524
  1172
  assumes f: "bounded_linear f"
wenzelm@53333
  1173
  shows "f ` closure S \<subseteq> closure (f ` S)"
huffman@44524
  1174
  using linear_continuous_on [OF f] closed_closure closure_subset
huffman@44524
  1175
  by (rule image_closure_subset)
huffman@44524
  1176
paulson@61518
  1177
lemma closure_linear_image_subset:
wenzelm@53339
  1178
  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::real_normed_vector"
wenzelm@49529
  1179
  assumes "linear f"
paulson@61518
  1180
  shows "f ` (closure S) \<subseteq> closure (f ` S)"
huffman@44524
  1181
  using assms unfolding linear_conv_bounded_linear
paulson@61518
  1182
  by (rule closure_bounded_linear_image_subset)
paulson@61518
  1183
paulson@61518
  1184
lemma closed_injective_linear_image:
paulson@61518
  1185
    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
paulson@61518
  1186
    assumes S: "closed S" and f: "linear f" "inj f"
paulson@61518
  1187
    shows "closed (f ` S)"
paulson@61518
  1188
proof -
paulson@61518
  1189
  obtain g where g: "linear g" "g \<circ> f = id"
paulson@61518
  1190
    using linear_injective_left_inverse [OF f] by blast
paulson@61518
  1191
  then have confg: "continuous_on (range f) g"
paulson@61518
  1192
    using linear_continuous_on linear_conv_bounded_linear by blast
paulson@61518
  1193
  have [simp]: "g ` f ` S = S"
paulson@61518
  1194
    using g by (simp add: image_comp)
paulson@61518
  1195
  have cgf: "closed (g ` f ` S)"
wenzelm@61808
  1196
    by (simp add: \<open>g \<circ> f = id\<close> S image_comp)
lp15@66884
  1197
  have [simp]: "(range f \<inter> g -` S) = f ` S"
lp15@66884
  1198
    using g unfolding o_def id_def image_def by auto metis+
paulson@61518
  1199
  show ?thesis
lp15@66884
  1200
  proof (rule closedin_closed_trans [of "range f"])
lp15@66884
  1201
    show "closedin (subtopology euclidean (range f)) (f ` S)"
lp15@66884
  1202
      using continuous_closedin_preimage [OF confg cgf] by simp
lp15@66884
  1203
    show "closed (range f)"
lp15@66884
  1204
      apply (rule closed_injective_image_subspace)
lp15@66884
  1205
      using f apply (auto simp: linear_linear linear_injective_0)
lp15@66884
  1206
      done
lp15@66884
  1207
  qed
paulson@61518
  1208
qed
paulson@61518
  1209
paulson@61518
  1210
lemma closed_injective_linear_image_eq:
paulson@61518
  1211
    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
paulson@61518
  1212
    assumes f: "linear f" "inj f"
paulson@61518
  1213
      shows "(closed(image f s) \<longleftrightarrow> closed s)"
paulson@61518
  1214
  by (metis closed_injective_linear_image closure_eq closure_linear_image_subset closure_subset_eq f(1) f(2) inj_image_subset_iff)
hoelzl@40377
  1215
hoelzl@40377
  1216
lemma closure_injective_linear_image:
paulson@61518
  1217
    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
paulson@61518
  1218
    shows "\<lbrakk>linear f; inj f\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
paulson@61518
  1219
  apply (rule subset_antisym)
paulson@61518
  1220
  apply (simp add: closure_linear_image_subset)
paulson@61518
  1221
  by (simp add: closure_minimal closed_injective_linear_image closure_subset image_mono)
paulson@61518
  1222
paulson@61518
  1223
lemma closure_bounded_linear_image:
paulson@61518
  1224
    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
paulson@61518
  1225
    shows "\<lbrakk>linear f; bounded S\<rbrakk> \<Longrightarrow> f ` (closure S) = closure (f ` S)"
paulson@61518
  1226
  apply (rule subset_antisym, simp add: closure_linear_image_subset)
paulson@61518
  1227
  apply (rule closure_minimal, simp add: closure_subset image_mono)
paulson@61518
  1228
  by (meson bounded_closure closed_closure compact_continuous_image compact_eq_bounded_closed linear_continuous_on linear_conv_bounded_linear)
hoelzl@40377
  1229
huffman@44524
  1230
lemma closure_scaleR:
wenzelm@53339
  1231
  fixes S :: "'a::real_normed_vector set"
nipkow@67399
  1232
  shows "(( *\<^sub>R) c) ` (closure S) = closure ((( *\<^sub>R) c) ` S)"
huffman@44524
  1233
proof
nipkow@67399
  1234
  show "(( *\<^sub>R) c) ` (closure S) \<subseteq> closure ((( *\<^sub>R) c) ` S)"
wenzelm@53333
  1235
    using bounded_linear_scaleR_right
paulson@61518
  1236
    by (rule closure_bounded_linear_image_subset)
nipkow@67399
  1237
  show "closure ((( *\<^sub>R) c) ` S) \<subseteq> (( *\<^sub>R) c) ` (closure S)"
wenzelm@49529
  1238
    by (intro closure_minimal image_mono closure_subset closed_scaling closed_closure)
wenzelm@49529
  1239
qed
wenzelm@49529
  1240
wenzelm@49529
  1241
lemma fst_linear: "linear fst"
huffman@53600
  1242
  unfolding linear_iff by (simp add: algebra_simps)
wenzelm@49529
  1243
wenzelm@49529
  1244
lemma snd_linear: "linear snd"
huffman@53600
  1245
  unfolding linear_iff by (simp add: algebra_simps)
wenzelm@49529
  1246
wenzelm@54465
  1247
lemma fst_snd_linear: "linear (\<lambda>(x,y). x + y)"
huffman@53600
  1248
  unfolding linear_iff by (simp add: algebra_simps)
hoelzl@40377
  1249
wenzelm@49529
  1250
lemma vector_choose_size:
lp15@62381
  1251
  assumes "0 \<le> c"
lp15@62381
  1252
  obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c"
lp15@62381
  1253
proof -
lp15@62381
  1254
  obtain a::'a where "a \<noteq> 0"
lp15@62381
  1255
    using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce
lp15@62381
  1256
  then show ?thesis
lp15@62381
  1257
    by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms)
lp15@62381
  1258
qed
lp15@62381
  1259
lp15@62381
  1260
lemma vector_choose_dist:
lp15@62381
  1261
  assumes "0 \<le> c"
lp15@62381
  1262
  obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c"
lp15@62381
  1263
by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size)
lp15@62381
  1264
lp15@62381
  1265
lemma sphere_eq_empty [simp]:
lp15@62381
  1266
  fixes a :: "'a::{real_normed_vector, perfect_space}"
lp15@62381
  1267
  shows "sphere a r = {} \<longleftrightarrow> r < 0"
lp15@62381
  1268
by (auto simp: sphere_def dist_norm) (metis dist_norm le_less_linear vector_choose_dist)
wenzelm@49529
  1269
nipkow@64267
  1270
lemma sum_delta_notmem:
wenzelm@49529
  1271
  assumes "x \<notin> s"
nipkow@64267
  1272
  shows "sum (\<lambda>y. if (y = x) then P x else Q y) s = sum Q s"
nipkow@64267
  1273
    and "sum (\<lambda>y. if (x = y) then P x else Q y) s = sum Q s"
nipkow@64267
  1274
    and "sum (\<lambda>y. if (y = x) then P y else Q y) s = sum Q s"
nipkow@64267
  1275
    and "sum (\<lambda>y. if (x = y) then P y else Q y) s = sum Q s"
nipkow@64267
  1276
  apply (rule_tac [!] sum.cong)
wenzelm@53333
  1277
  using assms
wenzelm@53333
  1278
  apply auto
wenzelm@49529
  1279
  done
himmelma@33175
  1280
nipkow@64267
  1281
lemma sum_delta'':
wenzelm@49529
  1282
  fixes s::"'a::real_vector set"
wenzelm@49529
  1283
  assumes "finite s"
himmelma@33175
  1284
  shows "(\<Sum>x\<in>s. (if y = x then f x else 0) *\<^sub>R x) = (if y\<in>s then (f y) *\<^sub>R y else 0)"
wenzelm@49529
  1285
proof -
wenzelm@49529
  1286
  have *: "\<And>x y. (if y = x then f x else (0::real)) *\<^sub>R x = (if x=y then (f x) *\<^sub>R x else 0)"
wenzelm@49529
  1287
    by auto
wenzelm@49529
  1288
  show ?thesis
nipkow@64267
  1289
    unfolding * using sum.delta[OF assms, of y "\<lambda>x. f x *\<^sub>R x"] by auto
himmelma@33175
  1290
qed
himmelma@33175
  1291
wenzelm@53333
  1292
lemma if_smult: "(if P then x else (y::real)) *\<^sub>R v = (if P then x *\<^sub>R v else y *\<^sub>R v)"
haftmann@57418
  1293
  by (fact if_distrib)
himmelma@33175
  1294
himmelma@33175
  1295
lemma dist_triangle_eq:
huffman@44361
  1296
  fixes x y z :: "'a::real_inner"
wenzelm@53333
  1297
  shows "dist x z = dist x y + dist y z \<longleftrightarrow>
wenzelm@53333
  1298
    norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)"
wenzelm@49529
  1299
proof -
wenzelm@49529
  1300
  have *: "x - y + (y - z) = x - z" by auto
hoelzl@37489
  1301
  show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *]
lp15@68031
  1302
    by (auto simp:norm_minus_commute)
wenzelm@49529
  1303
qed
himmelma@33175
  1304
hoelzl@37489
  1305
wenzelm@60420
  1306
subsection \<open>Affine set and affine hull\<close>
himmelma@33175
  1307
immler@67962
  1308
definition%important affine :: "'a::real_vector set \<Rightarrow> bool"
wenzelm@49529
  1309
  where "affine s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u v. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
himmelma@33175
  1310
himmelma@33175
  1311
lemma affine_alt: "affine s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u::real. (1 - u) *\<^sub>R x + u *\<^sub>R y \<in> s)"
wenzelm@49529
  1312
  unfolding affine_def by (metis eq_diff_eq')
himmelma@33175
  1313
lp15@62948
  1314
lemma affine_empty [iff]: "affine {}"
himmelma@33175
  1315
  unfolding affine_def by auto
himmelma@33175
  1316
lp15@62948
  1317
lemma affine_sing [iff]: "affine {x}"
lp15@68031
  1318
  unfolding affine_alt by (auto simp: scaleR_left_distrib [symmetric])
himmelma@33175
  1319
lp15@62948
  1320
lemma affine_UNIV [iff]: "affine UNIV"
himmelma@33175
  1321
  unfolding affine_def by auto
himmelma@33175
  1322
lp15@63007
  1323
lemma affine_Inter [intro]: "(\<And>s. s\<in>f \<Longrightarrow> affine s) \<Longrightarrow> affine (\<Inter>f)"
wenzelm@49531
  1324
  unfolding affine_def by auto
himmelma@33175
  1325
paulson@60303
  1326
lemma affine_Int[intro]: "affine s \<Longrightarrow> affine t \<Longrightarrow> affine (s \<inter> t)"
himmelma@33175
  1327
  unfolding affine_def by auto
himmelma@33175
  1328
lp15@63114
  1329
lemma affine_scaling: "affine s \<Longrightarrow> affine (image (\<lambda>x. c *\<^sub>R x) s)"
lp15@63114
  1330
  apply (clarsimp simp add: affine_def)
lp15@63114
  1331
  apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI)
lp15@63114
  1332
  apply (auto simp: algebra_simps)
lp15@63114
  1333
  done
lp15@63114
  1334
paulson@60303
  1335
lemma affine_affine_hull [simp]: "affine(affine hull s)"
wenzelm@49529
  1336
  unfolding hull_def
wenzelm@49529
  1337
  using affine_Inter[of "{t. affine t \<and> s \<subseteq> t}"] by auto
himmelma@33175
  1338
himmelma@33175
  1339
lemma affine_hull_eq[simp]: "(affine hull s = s) \<longleftrightarrow> affine s"
wenzelm@49529
  1340
  by (metis affine_affine_hull hull_same)
wenzelm@49529
  1341
lp15@62948
  1342
lemma affine_hyperplane: "affine {x. a \<bullet> x = b}"
lp15@62948
  1343
  by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
lp15@62948
  1344
himmelma@33175
  1345
immler@67962
  1346
subsubsection%unimportant \<open>Some explicit formulations (from Lars Schewe)\<close>
himmelma@33175
  1347
wenzelm@49529
  1348
lemma affine:
wenzelm@49529
  1349
  fixes V::"'a::real_vector set"
wenzelm@49529
  1350
  shows "affine V \<longleftrightarrow>
lp15@68024
  1351
         (\<forall>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> V \<and> sum u S = 1 \<longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V)"
lp15@68024
  1352
proof -
lp15@68024
  1353
  have "u *\<^sub>R x + v *\<^sub>R y \<in> V" if "x \<in> V" "y \<in> V" "u + v = (1::real)"
lp15@68024
  1354
    and *: "\<And>S u. \<lbrakk>finite S; S \<noteq> {}; S \<subseteq> V; sum u S = 1\<rbrakk> \<Longrightarrow> (\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V" for x y u v
lp15@68024
  1355
  proof (cases "x = y")
lp15@68024
  1356
    case True
lp15@68024
  1357
    then show ?thesis
lp15@68024
  1358
      using that by (metis scaleR_add_left scaleR_one)
lp15@68024
  1359
  next
lp15@68024
  1360
    case False
wenzelm@49529
  1361
    then show ?thesis
lp15@68024
  1362
      using that *[of "{x,y}" "\<lambda>w. if w = x then u else v"] by auto
lp15@68024
  1363
  qed
lp15@68024
  1364
  moreover have "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
lp15@68024
  1365
                if *: "\<And>x y u v. \<lbrakk>x\<in>V; y\<in>V; u + v = 1\<rbrakk> \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> V"
lp15@68024
  1366
                  and "finite S" "S \<noteq> {}" "S \<subseteq> V" "sum u S = 1" for S u
lp15@68024
  1367
  proof -
lp15@68024
  1368
    define n where "n = card S"
lp15@68024
  1369
    consider "card S = 0" | "card S = 1" | "card S = 2" | "card S > 2" by linarith
lp15@68024
  1370
    then show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
lp15@68024
  1371
    proof cases
lp15@68024
  1372
      assume "card S = 1"
lp15@68024
  1373
      then obtain a where "S={a}"
lp15@68031
  1374
        by (auto simp: card_Suc_eq)
lp15@68024
  1375
      then show ?thesis
lp15@68024
  1376
        using that by simp
lp15@68024
  1377
    next
lp15@68024
  1378
      assume "card S = 2"
lp15@68024
  1379
      then obtain a b where "S = {a, b}"
lp15@68024
  1380
        by (metis Suc_1 card_1_singletonE card_Suc_eq)
lp15@68024
  1381
      then show ?thesis
lp15@68024
  1382
        using *[of a b] that
lp15@68031
  1383
        by (auto simp: sum_clauses(2))
wenzelm@49529
  1384
    next
lp15@68024
  1385
      assume "card S > 2"
lp15@68024
  1386
      then show ?thesis using that n_def
lp15@68024
  1387
      proof (induct n arbitrary: u S)
lp15@68024
  1388
        case 0
lp15@68024
  1389
        then show ?case by auto
lp15@68024
  1390
      next
lp15@68024
  1391
        case (Suc n u S)
lp15@68024
  1392
        have "sum u S = card S" if "\<not> (\<exists>x\<in>S. u x \<noteq> 1)"
lp15@68024
  1393
          using that unfolding card_eq_sum by auto
lp15@68024
  1394
        with Suc.prems obtain x where "x \<in> S" and x: "u x \<noteq> 1" by force
lp15@68024
  1395
        have c: "card (S - {x}) = card S - 1"
lp15@68024
  1396
          by (simp add: Suc.prems(3) \<open>x \<in> S\<close>)
lp15@68024
  1397
        have "sum u (S - {x}) = 1 - u x"
lp15@68024
  1398
          by (simp add: Suc.prems sum_diff1_ring \<open>x \<in> S\<close>)
lp15@68024
  1399
        with x have eq1: "inverse (1 - u x) * sum u (S - {x}) = 1"
lp15@68024
  1400
          by auto
lp15@68024
  1401
        have inV: "(\<Sum>y\<in>S - {x}. (inverse (1 - u x) * u y) *\<^sub>R y) \<in> V"
lp15@68024
  1402
        proof (cases "card (S - {x}) > 2")
lp15@68024
  1403
          case True
lp15@68024
  1404
          then have S: "S - {x} \<noteq> {}" "card (S - {x}) = n"
lp15@68024
  1405
            using Suc.prems c by force+
lp15@68024
  1406
          show ?thesis
lp15@68024
  1407
          proof (rule Suc.hyps)
lp15@68024
  1408
            show "(\<Sum>a\<in>S - {x}. inverse (1 - u x) * u a) = 1"
lp15@68024
  1409
              by (auto simp: eq1 sum_distrib_left[symmetric])
lp15@68024
  1410
          qed (use S Suc.prems True in auto)
lp15@68024
  1411
        next
lp15@68024
  1412
          case False
lp15@68024
  1413
          then have "card (S - {x}) = Suc (Suc 0)"
lp15@68024
  1414
            using Suc.prems c by auto
lp15@68024
  1415
          then obtain a b where ab: "(S - {x}) = {a, b}" "a\<noteq>b"
lp15@68024
  1416
            unfolding card_Suc_eq by auto
lp15@68024
  1417
          then show ?thesis
lp15@68024
  1418
            using eq1 \<open>S \<subseteq> V\<close>
lp15@68031
  1419
            by (auto simp: sum_distrib_left distrib_left intro!: Suc.prems(2)[of a b])
lp15@68024
  1420
        qed
lp15@68024
  1421
        have "u x + (1 - u x) = 1 \<Longrightarrow>
lp15@68024
  1422
          u x *\<^sub>R x + (1 - u x) *\<^sub>R ((\<Sum>y\<in>S - {x}. u y *\<^sub>R y) /\<^sub>R (1 - u x)) \<in> V"
lp15@68024
  1423
          by (rule Suc.prems) (use \<open>x \<in> S\<close> Suc.prems inV in \<open>auto simp: scaleR_right.sum\<close>)
lp15@68024
  1424
        moreover have "(\<Sum>a\<in>S. u a *\<^sub>R a) = u x *\<^sub>R x + (\<Sum>a\<in>S - {x}. u a *\<^sub>R a)"
lp15@68024
  1425
          by (meson Suc.prems(3) sum.remove \<open>x \<in> S\<close>)
lp15@68024
  1426
        ultimately show "(\<Sum>x\<in>S. u x *\<^sub>R x) \<in> V"
lp15@68024
  1427
          by (simp add: x)
huffman@45498
  1428
      qed
lp15@68024
  1429
    qed (use \<open>S\<noteq>{}\<close> \<open>finite S\<close> in auto)
lp15@68024
  1430
  qed
lp15@68024
  1431
  ultimately show ?thesis
lp15@68024
  1432
    unfolding affine_def by meson
lp15@68024
  1433
qed
lp15@68024
  1434
himmelma@33175
  1435
himmelma@33175
  1436
lemma affine_hull_explicit:
lp15@68024
  1437
  "affine hull p = {y. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and> sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
lp15@68024
  1438
  (is "_ = ?rhs")
lp15@68024
  1439
proof (rule hull_unique)
lp15@68024
  1440
  show "p \<subseteq> ?rhs"
lp15@68024
  1441
  proof (intro subsetI CollectI exI conjI)
lp15@68024
  1442
    show "\<And>x. sum (\<lambda>z. 1) {x} = 1"
lp15@68024
  1443
      by auto
lp15@68024
  1444
  qed auto
lp15@68024
  1445
  show "?rhs \<subseteq> T" if "p \<subseteq> T" "affine T" for T
lp15@68024
  1446
    using that unfolding affine by blast
lp15@68024
  1447
  show "affine ?rhs"
wenzelm@49529
  1448
    unfolding affine_def
lp15@68024
  1449
  proof clarify
lp15@68024
  1450
    fix u v :: real and sx ux sy uy
wenzelm@49529
  1451
    assume uv: "u + v = 1"
lp15@68024
  1452
      and x: "finite sx" "sx \<noteq> {}" "sx \<subseteq> p" "sum ux sx = (1::real)"
lp15@68024
  1453
      and y: "finite sy" "sy \<noteq> {}" "sy \<subseteq> p" "sum uy sy = (1::real)" 
wenzelm@53333
  1454
    have **: "(sx \<union> sy) \<inter> sx = sx" "(sx \<union> sy) \<inter> sy = sy"
wenzelm@53333
  1455
      by auto
lp15@68024
  1456
    show "\<exists>S w. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p \<and>
lp15@68024
  1457
        sum w S = 1 \<and> (\<Sum>v\<in>S. w v *\<^sub>R v) = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
lp15@68024
  1458
    proof (intro exI conjI)
lp15@68024
  1459
      show "finite (sx \<union> sy)"
lp15@68024
  1460
        using x y by auto
lp15@68024
  1461
      show "sum (\<lambda>i. (if i\<in>sx then u * ux i else 0) + (if i\<in>sy then v * uy i else 0)) (sx \<union> sy) = 1"
lp15@68024
  1462
        using x y uv
lp15@68024
  1463
        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric] sum_distrib_left [symmetric] **)
lp15@68024
  1464
      have "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i)
lp15@68024
  1465
          = (\<Sum>i\<in>sx. (u * ux i) *\<^sub>R i) + (\<Sum>i\<in>sy. (v * uy i) *\<^sub>R i)"
lp15@68024
  1466
        using x y
lp15@68024
  1467
        unfolding scaleR_left_distrib scaleR_zero_left if_smult
lp15@68024
  1468
        by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric]  **)
lp15@68031
  1469
      also have "\<dots> = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)"
lp15@68024
  1470
        unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] by blast
lp15@68024
  1471
      finally show "(\<Sum>i\<in>sx \<union> sy. ((if i \<in> sx then u * ux i else 0) + (if i \<in> sy then v * uy i else 0)) *\<^sub>R i) 
lp15@68024
  1472
                  = u *\<^sub>R (\<Sum>v\<in>sx. ux v *\<^sub>R v) + v *\<^sub>R (\<Sum>v\<in>sy. uy v *\<^sub>R v)" .
lp15@68024
  1473
    qed (use x y in auto)
wenzelm@49529
  1474
  qed
wenzelm@49529
  1475
qed
himmelma@33175
  1476
himmelma@33175
  1477
lemma affine_hull_finite:
lp15@68024
  1478
  assumes "finite S"
lp15@68024
  1479
  shows "affine hull S = {y. \<exists>u. sum u S = 1 \<and> sum (\<lambda>v. u v *\<^sub>R v) S = y}"
lp15@68031
  1480
proof -
lp15@68031
  1481
  have *: "\<exists>h. sum h S = 1 \<and> (\<Sum>v\<in>S. h v *\<^sub>R v) = x" 
lp15@68031
  1482
    if "F \<subseteq> S" "finite F" "F \<noteq> {}" and sum: "sum u F = 1" and x: "(\<Sum>v\<in>F. u v *\<^sub>R v) = x" for x F u
lp15@68031
  1483
  proof -
lp15@68031
  1484
    have "S \<inter> F = F"
lp15@68031
  1485
      using that by auto
lp15@68031
  1486
    show ?thesis
lp15@68031
  1487
    proof (intro exI conjI)
lp15@68031
  1488
      show "(\<Sum>x\<in>S. if x \<in> F then u x else 0) = 1"
lp15@68031
  1489
        by (metis (mono_tags, lifting) \<open>S \<inter> F = F\<close> assms sum.inter_restrict sum)
lp15@68031
  1490
      show "(\<Sum>v\<in>S. (if v \<in> F then u v else 0) *\<^sub>R v) = x"
lp15@68031
  1491
        by (simp add: if_smult cong: if_cong) (metis (no_types) \<open>S \<inter> F = F\<close> assms sum.inter_restrict x)
lp15@68031
  1492
    qed
lp15@68031
  1493
  qed
lp15@68031
  1494
  show ?thesis
lp15@68031
  1495
    unfolding affine_hull_explicit using assms
lp15@68031
  1496
    by (fastforce dest: *)
wenzelm@49529
  1497
qed
wenzelm@49529
  1498
himmelma@33175
  1499
immler@67962
  1500
subsubsection%unimportant \<open>Stepping theorems and hence small special cases\<close>
himmelma@33175
  1501
himmelma@33175
  1502
lemma affine_hull_empty[simp]: "affine hull {} = {}"
lp15@68031
  1503
  by simp
lp15@68031
  1504
himmelma@33175
  1505
lemma affine_hull_finite_step:
himmelma@33175
  1506
  fixes y :: "'a::real_vector"
lp15@68031
  1507
  shows "finite S \<Longrightarrow>
lp15@68024
  1508
      (\<exists>u. sum u (insert a S) = w \<and> sum (\<lambda>x. u x *\<^sub>R x) (insert a S) = y) \<longleftrightarrow>
lp15@68024
  1509
      (\<exists>v u. sum u S = w - v \<and> sum (\<lambda>x. u x *\<^sub>R x) S = y - v *\<^sub>R a)" (is "_ \<Longrightarrow> ?lhs = ?rhs")
wenzelm@49529
  1510
proof -
lp15@68024
  1511
  assume fin: "finite S"
wenzelm@53347
  1512
  show "?lhs = ?rhs"
wenzelm@53347
  1513
  proof
wenzelm@53302
  1514
    assume ?lhs
lp15@68024
  1515
    then obtain u where u: "sum u (insert a S) = w \<and> (\<Sum>x\<in>insert a S. u x *\<^sub>R x) = y"
wenzelm@53302
  1516
      by auto
wenzelm@53347
  1517
    show ?rhs
lp15@68024
  1518
    proof (cases "a \<in> S")
wenzelm@49529
  1519
      case True
lp15@68031
  1520
      then show ?thesis
lp15@68031
  1521
        using u by (simp add: insert_absorb) (metis diff_zero real_vector.scale_zero_left)
himmelma@33175
  1522
    next
wenzelm@49529
  1523
      case False
lp15@68031
  1524
      show ?thesis
lp15@68031
  1525
        by (rule exI [where x="u a"]) (use u fin False in auto)
wenzelm@53302
  1526
    qed
wenzelm@53347
  1527
  next
wenzelm@53302
  1528
    assume ?rhs
lp15@68024
  1529
    then obtain v u where vu: "sum u S = w - v"  "(\<Sum>x\<in>S. u x *\<^sub>R x) = y - v *\<^sub>R a"
wenzelm@53302
  1530
      by auto
wenzelm@53302
  1531
    have *: "\<And>x M. (if x = a then v else M) *\<^sub>R x = (if x = a then v *\<^sub>R x else M *\<^sub>R x)"
wenzelm@53302
  1532
      by auto
wenzelm@53347
  1533
    show ?lhs
lp15@68024
  1534
    proof (cases "a \<in> S")
wenzelm@49529
  1535
      case True
lp15@68031
  1536
      show ?thesis
lp15@68031
  1537
        by (rule exI [where x="\<lambda>x. (if x=a then v else 0) + u x"])
lp15@68031
  1538
           (simp add: True scaleR_left_distrib sum.distrib sum_clauses fin vu * cong: if_cong)
himmelma@33175
  1539
    next
wenzelm@49531
  1540
      case False
lp15@68031
  1541
      then show ?thesis
lp15@68031
  1542
        apply (rule_tac x="\<lambda>x. if x=a then v else u x" in exI) 
lp15@68031
  1543
        apply (simp add: vu sum_clauses(2)[OF fin] *)
lp15@68031
  1544
        by (simp add: sum_delta_notmem(3) vu)
wenzelm@49529
  1545
    qed
wenzelm@53347
  1546
  qed
himmelma@33175
  1547
qed
himmelma@33175
  1548
himmelma@33175
  1549
lemma affine_hull_2:
himmelma@33175
  1550
  fixes a b :: "'a::real_vector"
wenzelm@53302
  1551
  shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}"
wenzelm@53302
  1552
  (is "?lhs = ?rhs")
wenzelm@49529
  1553
proof -
wenzelm@49529
  1554
  have *:
wenzelm@49531
  1555
    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
wenzelm@49529
  1556
    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
nipkow@64267
  1557
  have "?lhs = {y. \<exists>u. sum u {a, b} = 1 \<and> (\<Sum>v\<in>{a, b}. u v *\<^sub>R v) = y}"
himmelma@33175
  1558
    using affine_hull_finite[of "{a,b}"] by auto
himmelma@33175
  1559
  also have "\<dots> = {y. \<exists>v u. u b = 1 - v \<and> u b *\<^sub>R b = y - v *\<^sub>R a}"
lp15@68031
  1560
    by (simp add: affine_hull_finite_step[of "{b}" a])
himmelma@33175
  1561
  also have "\<dots> = ?rhs" unfolding * by auto
himmelma@33175
  1562
  finally show ?thesis by auto
himmelma@33175
  1563
qed
himmelma@33175
  1564
himmelma@33175
  1565
lemma affine_hull_3:
himmelma@33175
  1566
  fixes a b c :: "'a::real_vector"
wenzelm@53302
  1567
  shows "affine hull {a,b,c} = { u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c| u v w. u + v + w = 1}"
wenzelm@49529
  1568
proof -
wenzelm@49529
  1569
  have *:
wenzelm@49531
  1570
    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::real)"
wenzelm@49529
  1571
    "\<And>x y z. z = x - y \<longleftrightarrow> y + z = (x::'a)" by auto
wenzelm@49529
  1572
  show ?thesis
wenzelm@49529
  1573
    apply (simp add: affine_hull_finite affine_hull_finite_step)
wenzelm@49529
  1574
    unfolding *
lp15@68031
  1575
    apply safe
lp15@68031
  1576
     apply (metis add.assoc)
lp15@68031
  1577
    apply (rule_tac x=u in exI, force)
wenzelm@49529
  1578
    done
himmelma@33175
  1579
qed
himmelma@33175
  1580
hoelzl@40377
  1581
lemma mem_affine:
wenzelm@53333
  1582
  assumes "affine S" "x \<in> S" "y \<in> S" "u + v = 1"
wenzelm@53347
  1583
  shows "u *\<^sub>R x + v *\<^sub>R y \<in> S"
hoelzl@40377
  1584
  using assms affine_def[of S] by auto
hoelzl@40377
  1585
hoelzl@40377
  1586
lemma mem_affine_3:
wenzelm@53333
  1587
  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S" "u + v + w = 1"
wenzelm@53347
  1588
  shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> S"
wenzelm@49529
  1589
proof -
wenzelm@53347
  1590
  have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \<in> affine hull {x, y, z}"
wenzelm@49529
  1591
    using affine_hull_3[of x y z] assms by auto
wenzelm@49529
  1592
  moreover
wenzelm@53347
  1593
  have "affine hull {x, y, z} \<subseteq> affine hull S"
wenzelm@49529
  1594
    using hull_mono[of "{x, y, z}" "S"] assms by auto
wenzelm@49529
  1595
  moreover
wenzelm@53347
  1596
  have "affine hull S = S"
wenzelm@53347
  1597
    using assms affine_hull_eq[of S] by auto
wenzelm@49531
  1598
  ultimately show ?thesis by auto
hoelzl@40377
  1599
qed
hoelzl@40377
  1600
hoelzl@40377
  1601
lemma mem_affine_3_minus:
wenzelm@53333
  1602
  assumes "affine S" "x \<in> S" "y \<in> S" "z \<in> S"
wenzelm@53333
  1603
  shows "x + v *\<^sub>R (y-z) \<in> S"
wenzelm@53333
  1604
  using mem_affine_3[of S x y z 1 v "-v"] assms
wenzelm@53333
  1605
  by (simp add: algebra_simps)
hoelzl@40377
  1606
lp15@60307
  1607
corollary mem_affine_3_minus2:
lp15@60307
  1608
    "\<lbrakk>affine S; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> x - v *\<^sub>R (y-z) \<in> S"
lp15@60307
  1609
  by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left)
lp15@60307
  1610
hoelzl@40377
  1611
immler@67962
  1612
subsubsection%unimportant \<open>Some relations between affine hull and subspaces\<close>
himmelma@33175
  1613
himmelma@33175
  1614
lemma affine_hull_insert_subset_span:
lp15@68031
  1615
  "affine hull (insert a S) \<subseteq> {a + v| v . v \<in> span {x - a | x . x \<in> S}}"
lp15@68031
  1616
proof -
lp15@68031
  1617
  have "\<exists>v T u. x = a + v \<and> (finite T \<and> T \<subseteq> {x - a |x. x \<in> S} \<and> (\<Sum>v\<in>T. u v *\<^sub>R v) = v)"
lp15@68031
  1618
    if "finite F" "F \<noteq> {}" "F \<subseteq> insert a S" "sum u F = 1" "(\<Sum>v\<in>F. u v *\<^sub>R v) = x"
lp15@68031
  1619
    for x F u
lp15@68031
  1620
  proof -
lp15@68031
  1621
    have *: "(\<lambda>x. x - a) ` (F - {a}) \<subseteq> {x - a |x. x \<in> S}"
lp15@68031
  1622
      using that by auto
lp15@68031
  1623
    show ?thesis
lp15@68031
  1624
    proof (intro exI conjI)
lp15@68031
  1625
      show "finite ((\<lambda>x. x - a) ` (F - {a}))"
lp15@68031
  1626
        by (simp add: that(1))
lp15@68031
  1627
      show "(\<Sum>v\<in>(\<lambda>x. x - a) ` (F - {a}). u(v+a) *\<^sub>R v) = x-a"
lp15@68031
  1628
        by (simp add: sum.reindex[unfolded inj_on_def] algebra_simps
lp15@68031
  1629
            sum_subtractf scaleR_left.sum[symmetric] sum_diff1 that)
lp15@68031
  1630
    qed (use \<open>F \<subseteq> insert a S\<close> in auto)
lp15@68031
  1631
  qed
lp15@68031
  1632
  then show ?thesis
immler@68074
  1633
    unfolding affine_hull_explicit span_explicit by blast
wenzelm@49529
  1634
qed
himmelma@33175
  1635
himmelma@33175
  1636
lemma affine_hull_insert_span:
lp15@68031
  1637
  assumes "a \<notin> S"
lp15@68031
  1638
  shows "affine hull (insert a S) = {a + v | v . v \<in> span {x - a | x.  x \<in> S}}"
lp15@68031
  1639
proof -
lp15@68031
  1640
  have *: "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
lp15@68031
  1641
    if "v \<in> span {x - a |x. x \<in> S}" "y = a + v" for y v
lp15@68031
  1642
  proof -
lp15@68031
  1643
    from that
lp15@68031
  1644
    obtain T u where u: "finite T" "T \<subseteq> {x - a |x. x \<in> S}" "a + (\<Sum>v\<in>T. u v *\<^sub>R v) = y"
lp15@68031
  1645
      unfolding span_explicit by auto
lp15@68031
  1646
    define F where "F = (\<lambda>x. x + a) ` T"
lp15@68031
  1647
    have F: "finite F" "F \<subseteq> S" "(\<Sum>v\<in>F. u (v - a) *\<^sub>R (v - a)) = y - a"
lp15@68031
  1648
      unfolding F_def using u by (auto simp: sum.reindex[unfolded inj_on_def])
lp15@68031
  1649
    have *: "F \<inter> {a} = {}" "F \<inter> - {a} = F"
lp15@68031
  1650
      using F assms by auto
lp15@68031
  1651
    show "\<exists>G u. finite G \<and> G \<noteq> {} \<and> G \<subseteq> insert a S \<and> sum u G = 1 \<and> (\<Sum>v\<in>G. u v *\<^sub>R v) = y"
lp15@68031
  1652
      apply (rule_tac x = "insert a F" in exI)
lp15@68031
  1653
      apply (rule_tac x = "\<lambda>x. if x=a then 1 - sum (\<lambda>x. u (x - a)) F else u (x - a)" in exI)
lp15@68031
  1654
      using assms F
lp15@68031
  1655
      apply (auto simp:  sum_clauses sum.If_cases if_smult sum_subtractf scaleR_left.sum algebra_simps *)
lp15@68031
  1656
      done
lp15@68031
  1657
  qed
lp15@68031
  1658
  show ?thesis
lp15@68031
  1659
    by (intro subset_antisym affine_hull_insert_subset_span) (auto simp: affine_hull_explicit dest!: *)
wenzelm@49529
  1660
qed
himmelma@33175
  1661
himmelma@33175
  1662
lemma affine_hull_span:
lp15@68031
  1663
  assumes "a \<in> S"
lp15@68031
  1664
  shows "affine hull S = {a + v | v. v \<in> span {x - a | x. x \<in> S - {a}}}"
lp15@68031
  1665
  using affine_hull_insert_span[of a "S - {a}", unfolded insert_Diff[OF assms]] by auto
himmelma@33175
  1666
wenzelm@49529
  1667
immler@67962
  1668
subsubsection%unimportant \<open>Parallel affine sets\<close>
hoelzl@40377
  1669
wenzelm@53347
  1670
definition affine_parallel :: "'a::real_vector set \<Rightarrow> 'a::real_vector set \<Rightarrow> bool"
wenzelm@53339
  1671
  where "affine_parallel S T \<longleftrightarrow> (\<exists>a. T = (\<lambda>x. a + x) ` S)"
hoelzl@40377
  1672
hoelzl@40377
  1673
lemma affine_parallel_expl_aux:
wenzelm@49529
  1674
  fixes S T :: "'a::real_vector set"
lp15@68031
  1675
  assumes "\<And>x. x \<in> S \<longleftrightarrow> a + x \<in> T"
wenzelm@53339
  1676
  shows "T = (\<lambda>x. a + x) ` S"
wenzelm@49529
  1677
proof -
lp15@68031
  1678
  have "x \<in> ((\<lambda>x. a + x) ` S)" if "x \<in> T" for x
lp15@68031
  1679
    using that
lp15@68031
  1680
    by (simp add: image_iff) (metis add.commute diff_add_cancel assms)
wenzelm@53339
  1681
  moreover have "T \<ge> (\<lambda>x. a + x) ` S"
wenzelm@53333
  1682
    using assms by auto
wenzelm@49529
  1683
  ultimately show ?thesis by auto
wenzelm@49529
  1684
qed
wenzelm@49529
  1685
wenzelm@53339
  1686
lemma affine_parallel_expl: "affine_parallel S T \<longleftrightarrow> (\<exists>a. \<forall>x. x \<in> S \<longleftrightarrow> a + x \<in> T)"
wenzelm@49529
  1687
  unfolding affine_parallel_def
wenzelm@49529
  1688
  using affine_parallel_expl_aux[of S _ T] by auto
wenzelm@49529
  1689
wenzelm@49529
  1690
lemma affine_parallel_reflex: "affine_parallel S S"
wenzelm@53302
  1691
  unfolding affine_parallel_def
lp15@68031
  1692
  using image_add_0 by blast
hoelzl@40377
  1693
hoelzl@40377
  1694
lemma affine_parallel_commut:
wenzelm@49529
  1695
  assumes "affine_parallel A B"
wenzelm@49529
  1696
  shows "affine_parallel B A"
wenzelm@49529
  1697
proof -
haftmann@54230
  1698
  from assms obtain a where B: "B = (\<lambda>x. a + x) ` A"
wenzelm@49529
  1699
    unfolding affine_parallel_def by auto
haftmann@54230
  1700
  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
haftmann@54230
  1701
  from B show ?thesis
wenzelm@53333
  1702
    using translation_galois [of B a A]
wenzelm@53333
  1703
    unfolding affine_parallel_def by auto
hoelzl@40377
  1704
qed
hoelzl@40377
  1705
hoelzl@40377
  1706
lemma affine_parallel_assoc:
wenzelm@53339
  1707
  assumes "affine_parallel A B"
wenzelm@53339
  1708
    and "affine_parallel B C"
wenzelm@49531
  1709
  shows "affine_parallel A C"
wenzelm@49529
  1710
proof -
wenzelm@53333
  1711
  from assms obtain ab where "B = (\<lambda>x. ab + x) ` A"
wenzelm@49531
  1712
    unfolding affine_parallel_def by auto
wenzelm@49531
  1713
  moreover
wenzelm@53333
  1714
  from assms obtain bc where "C = (\<lambda>x. bc + x) ` B"
wenzelm@49529
  1715
    unfolding affine_parallel_def by auto
wenzelm@49529
  1716
  ultimately show ?thesis
wenzelm@49529
  1717
    using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto
hoelzl@40377
  1718
qed
hoelzl@40377
  1719
hoelzl@40377
  1720
lemma affine_translation_aux:
hoelzl@40377
  1721
  fixes a :: "'a::real_vector"
wenzelm@53333
  1722
  assumes "affine ((\<lambda>x. a + x) ` S)"
wenzelm@53333
  1723
  shows "affine S"
wenzelm@53302
  1724
proof -
wenzelm@53302
  1725
  {
wenzelm@53302
  1726
    fix x y u v
wenzelm@53333
  1727
    assume xy: "x \<in> S" "y \<in> S" "(u :: real) + v = 1"
wenzelm@53333
  1728
    then have "(a + x) \<in> ((\<lambda>x. a + x) ` S)" "(a + y) \<in> ((\<lambda>x. a + x) ` S)"
wenzelm@53333
  1729
      by auto
wenzelm@53339
  1730
    then have h1: "u *\<^sub>R  (a + x) + v *\<^sub>R (a + y) \<in> (\<lambda>x. a + x) ` S"
wenzelm@49529
  1731
      using xy assms unfolding affine_def by auto
wenzelm@53339
  1732
    have "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) = (u + v) *\<^sub>R a + (u *\<^sub>R x + v *\<^sub>R y)"
wenzelm@49529
  1733
      by (simp add: algebra_simps)
wenzelm@53339
  1734
    also have "\<dots> = a + (u *\<^sub>R x + v *\<^sub>R y)"
wenzelm@60420
  1735
      using \<open>u + v = 1\<close> by auto
wenzelm@53339
  1736
    ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \<in> (\<lambda>x. a + x) ` S"
wenzelm@53333
  1737
      using h1 by auto
wenzelm@67613
  1738
    then have "u *\<^sub>R x + v *\<^sub>R y \<in> S" by auto
wenzelm@49529
  1739
  }
wenzelm@49529
  1740
  then show ?thesis unfolding affine_def by auto
hoelzl@40377
  1741
qed
hoelzl@40377
  1742
hoelzl@40377
  1743
lemma affine_translation:
hoelzl@40377
  1744
  fixes a :: "'a::real_vector"
wenzelm@53339
  1745
  shows "affine S \<longleftrightarrow> affine ((\<lambda>x. a + x) ` S)"
wenzelm@49529
  1746
proof -
wenzelm@53339
  1747
  have "affine S \<Longrightarrow> affine ((\<lambda>x. a + x) ` S)"
wenzelm@53339
  1748
    using affine_translation_aux[of "-a" "((\<lambda>x. a + x) ` S)"]
wenzelm@49529
  1749
    using translation_assoc[of "-a" a S] by auto
wenzelm@49529
  1750
  then show ?thesis using affine_translation_aux by auto
hoelzl@40377
  1751
qed
hoelzl@40377
  1752
hoelzl@40377
  1753
lemma parallel_is_affine:
wenzelm@49529
  1754
  fixes S T :: "'a::real_vector set"
wenzelm@49529
  1755
  assumes "affine S" "affine_parallel S T"
wenzelm@49529
  1756
  shows "affine T"
wenzelm@49529
  1757
proof -
wenzelm@53339
  1758
  from assms obtain a where "T = (\<lambda>x. a + x) ` S"
wenzelm@49531
  1759
    unfolding affine_parallel_def by auto
wenzelm@53339
  1760
  then show ?thesis
wenzelm@53339
  1761
    using affine_translation assms by auto
hoelzl@40377
  1762
qed
hoelzl@40377
  1763
huffman@44361
  1764
lemma subspace_imp_affine: "subspace s \<Longrightarrow> affine s"
hoelzl@40377
  1765
  unfolding subspace_def affine_def by auto
hoelzl@40377
  1766
wenzelm@49529
  1767
immler@67962
  1768
subsubsection%unimportant \<open>Subspace parallel to an affine set\<close>
hoelzl@40377
  1769
wenzelm@53339
  1770
lemma subspace_affine: "subspace S \<longleftrightarrow> affine S \<and> 0 \<in> S"
wenzelm@49529
  1771
proof -
wenzelm@53333
  1772
  have h0: "subspace S \<Longrightarrow> affine S \<and> 0 \<in> S"
wenzelm@49529
  1773
    using subspace_imp_affine[of S] subspace_0 by auto
wenzelm@53302
  1774
  {
wenzelm@53333
  1775
    assume assm: "affine S \<and> 0 \<in> S"
wenzelm@53302
  1776
    {
wenzelm@53302
  1777
      fix c :: real
wenzelm@54465
  1778
      fix x
wenzelm@54465
  1779
      assume x: "x \<in> S"
wenzelm@49529
  1780
      have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto
wenzelm@49529
  1781
      moreover
wenzelm@53339
  1782
      have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \<in> S"
wenzelm@54465
  1783
        using affine_alt[of S] assm x by auto
wenzelm@53333
  1784
      ultimately have "c *\<^sub>R x \<in> S" by auto
wenzelm@49529
  1785
    }
wenzelm@53333
  1786
    then have h1: "\<forall>c. \<forall>x \<in> S. c *\<^sub>R x \<in> S" by auto
wenzelm@49529
  1787
wenzelm@53302
  1788
    {
wenzelm@53302
  1789
      fix x y
wenzelm@54465
  1790
      assume xy: "x \<in> S" "y \<in> S"
wenzelm@63040
  1791
      define u where "u = (1 :: real)/2"
wenzelm@53302
  1792
      have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)"
wenzelm@53302
  1793
        by auto
wenzelm@49529
  1794
      moreover
wenzelm@53302
  1795
      have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y"
wenzelm@53302
  1796
        by (simp add: algebra_simps)
wenzelm@49529
  1797
      moreover
wenzelm@54465
  1798
      have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> S"
wenzelm@54465
  1799
        using affine_alt[of S] assm xy by auto
wenzelm@49529
  1800
      ultimately
wenzelm@53333
  1801
      have "(1/2) *\<^sub>R (x+y) \<in> S"
wenzelm@53302
  1802
        using u_def by auto
wenzelm@49529
  1803
      moreover
wenzelm@54465
  1804
      have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))"
wenzelm@53302
  1805
        by auto
wenzelm@49529
  1806
      ultimately
wenzelm@54465
  1807
      have "x + y \<in> S"
wenzelm@53302
  1808
        using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto
wenzelm@49529
  1809
    }
wenzelm@53302
  1810
    then have "\<forall>x \<in> S. \<forall>y \<in> S. x + y \<in> S"
wenzelm@53302
  1811
      by auto
wenzelm@53302
  1812
    then have "subspace S"
wenzelm@53302
  1813
      using h1 assm unfolding subspace_def by auto
wenzelm@49529
  1814
  }
wenzelm@49529
  1815
  then show ?thesis using h0 by metis
hoelzl@40377
  1816
qed
hoelzl@40377
  1817
hoelzl@40377
  1818
lemma affine_diffs_subspace:
wenzelm@53333
  1819
  assumes "affine S" "a \<in> S"
wenzelm@53302
  1820
  shows "subspace ((\<lambda>x. (-a)+x) ` S)"
wenzelm@49529
  1821
proof -
haftmann@54230
  1822
  have [simp]: "(\<lambda>x. x - a) = plus (- a)" by (simp add: fun_eq_iff)
wenzelm@53302
  1823
  have "affine ((\<lambda>x. (-a)+x) ` S)"
wenzelm@49531
  1824
    using  affine_translation assms by auto
wenzelm@67613
  1825
  moreover have "0 \<in> ((\<lambda>x. (-a)+x) ` S)"
wenzelm@53333
  1826
    using assms exI[of "(\<lambda>x. x\<in>S \<and> -a+x = 0)" a] by auto
wenzelm@49531
  1827
  ultimately show ?thesis using subspace_affine by auto
hoelzl@40377
  1828
qed
hoelzl@40377
  1829
hoelzl@40377
  1830
lemma parallel_subspace_explicit:
wenzelm@54465
  1831
  assumes "affine S"
wenzelm@54465
  1832
    and "a \<in> S"
wenzelm@54465
  1833
  assumes "L \<equiv> {y. \<exists>x \<in> S. (-a) + x = y}"
wenzelm@54465
  1834
  shows "subspace L \<and> affine_parallel S L"
wenzelm@49529
  1835
proof -
haftmann@54230
  1836
  from assms have "L = plus (- a) ` S" by auto
haftmann@54230
  1837
  then have par: "affine_parallel S L"
wenzelm@54465
  1838
    unfolding affine_parallel_def ..
wenzelm@49531
  1839
  then have "affine L" using assms parallel_is_affine by auto
wenzelm@53302
  1840
  moreover have "0 \<in> L"
haftmann@54230
  1841
    using assms by auto
wenzelm@53302
  1842
  ultimately show ?thesis
wenzelm@53302
  1843
    using subspace_affine par by auto
hoelzl@40377
  1844
qed
hoelzl@40377
  1845
hoelzl@40377
  1846
lemma parallel_subspace_aux:
wenzelm@53302
  1847
  assumes "subspace A"
wenzelm@53302
  1848
    and "subspace B"
wenzelm@53302
  1849
    and "affine_parallel A B"
wenzelm@53302
  1850
  shows "A \<supseteq> B"
wenzelm@49529
  1851
proof -
wenzelm@54465
  1852
  from assms obtain a where a: "\<forall>x. x \<in> A \<longleftrightarrow> a + x \<in> B"
wenzelm@49529
  1853
    using affine_parallel_expl[of A B] by auto
wenzelm@53302
  1854
  then have "-a \<in> A"
wenzelm@53302
  1855
    using assms subspace_0[of B] by auto
wenzelm@53302
  1856
  then have "a \<in> A"
wenzelm@53302
  1857
    using assms subspace_neg[of A "-a"] by auto
wenzelm@53302
  1858
  then show ?thesis
wenzelm@54465
  1859
    using assms a unfolding subspace_def by auto
hoelzl@40377
  1860
qed
hoelzl@40377
  1861
hoelzl@40377
  1862
lemma parallel_subspace:
wenzelm@53302
  1863
  assumes "subspace A"
wenzelm@53302
  1864
    and "subspace B"
wenzelm@53302
  1865
    and "affine_parallel A B"
wenzelm@49529
  1866
  shows "A = B"
wenzelm@49529
  1867
proof
wenzelm@53302
  1868
  show "A \<supseteq> B"
wenzelm@49529
  1869
    using assms parallel_subspace_aux by auto
wenzelm@53302
  1870
  show "A \<subseteq> B"
wenzelm@49529
  1871
    using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto
hoelzl@40377
  1872
qed
hoelzl@40377
  1873
hoelzl@40377
  1874
lemma affine_parallel_subspace:
wenzelm@53302
  1875
  assumes "affine S" "S \<noteq> {}"
wenzelm@53339
  1876
  shows "\<exists>!L. subspace L \<and> affine_parallel S L"
wenzelm@49529
  1877
proof -
wenzelm@53339
  1878
  have ex: "\<exists>L. subspace L \<and> affine_parallel S L"
wenzelm@49531
  1879
    using assms parallel_subspace_explicit by auto
wenzelm@53302
  1880
  {
wenzelm@53302
  1881
    fix L1 L2
wenzelm@53339
  1882
    assume ass: "subspace L1 \<and> affine_parallel S L1" "subspace L2 \<and> affine_parallel S L2"
wenzelm@49529
  1883
    then have "affine_parallel L1 L2"
wenzelm@49529
  1884
      using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto
wenzelm@49529
  1885
    then have "L1 = L2"
wenzelm@49529
  1886
      using ass parallel_subspace by auto
wenzelm@49529
  1887
  }
wenzelm@49529
  1888
  then show ?thesis using ex by auto
wenzelm@49529
  1889
qed
wenzelm@49529
  1890
hoelzl@40377
  1891
wenzelm@60420
  1892
subsection \<open>Cones\<close>
himmelma@33175
  1893
immler@67962
  1894
definition%important cone :: "'a::real_vector set \<Rightarrow> bool"
wenzelm@53339
  1895
  where "cone s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>c\<ge>0. c *\<^sub>R x \<in> s)"
himmelma@33175
  1896
himmelma@33175
  1897
lemma cone_empty[intro, simp]: "cone {}"
himmelma@33175
  1898
  unfolding cone_def by auto
himmelma@33175
  1899
himmelma@33175
  1900
lemma cone_univ[intro, simp]: "cone UNIV"
himmelma@33175
  1901
  unfolding cone_def by auto
himmelma@33175
  1902
wenzelm@53339
  1903
lemma cone_Inter[intro]: "\<forall>s\<in>f. cone s \<Longrightarrow> cone (\<Inter>f)"
himmelma@33175
  1904
  unfolding cone_def by auto
himmelma@33175
  1905
lp15@63469
  1906
lemma subspace_imp_cone: "subspace S \<Longrightarrow> cone S"
immler@68072
  1907
  by (simp add: cone_def subspace_scale)
lp15@63469
  1908
wenzelm@49529
  1909
wenzelm@60420
  1910
subsubsection \<open>Conic hull\<close>
himmelma@33175
  1911
himmelma@33175
  1912
lemma cone_cone_hull: "cone (cone hull s)"
huffman@44170
  1913
  unfolding hull_def by auto
himmelma@33175
  1914
wenzelm@53302
  1915
lemma cone_hull_eq: "cone hull s = s \<longleftrightarrow> cone s"
wenzelm@49529
  1916
  apply (rule hull_eq)
wenzelm@53302
  1917
  using cone_Inter
wenzelm@53302
  1918
  unfolding subset_eq
wenzelm@53302
  1919
  apply auto
wenzelm@49529
  1920
  done
himmelma@33175
  1921
hoelzl@40377
  1922
lemma mem_cone:
wenzelm@53302
  1923
  assumes "cone S" "x \<in> S" "c \<ge> 0"
wenzelm@67613
  1924
  shows "c *\<^sub>R x \<in> S"
hoelzl@40377
  1925
  using assms cone_def[of S] by auto
hoelzl@40377
  1926
hoelzl@40377
  1927
lemma cone_contains_0:
wenzelm@49529
  1928
  assumes "cone S"
wenzelm@53302
  1929
  shows "S \<noteq> {} \<longleftrightarrow> 0 \<in> S"
wenzelm@49529
  1930
proof -
wenzelm@53302
  1931
  {
wenzelm@53302
  1932
    assume "S \<noteq> {}"
wenzelm@53302
  1933
    then obtain a where "a \<in> S" by auto
wenzelm@53302
  1934
    then have "0 \<in> S"
wenzelm@53302
  1935
      using assms mem_cone[of S a 0] by auto
wenzelm@49529
  1936
  }
wenzelm@49529
  1937
  then show ?thesis by auto
hoelzl@40377
  1938
qed
hoelzl@40377
  1939
huffman@44361
  1940
lemma cone_0: "cone {0}"
wenzelm@49529
  1941
  unfolding cone_def by auto
hoelzl@40377
  1942
wenzelm@61952
  1943
lemma cone_Union[intro]: "(\<forall>s\<in>f. cone s) \<longrightarrow> cone (\<Union>f)"
hoelzl@40377
  1944
  unfolding cone_def by blast
hoelzl@40377
  1945
hoelzl@40377
  1946
lemma cone_iff:
wenzelm@53347
  1947
  assumes "S \<noteq> {}"
nipkow@67399
  1948
  shows "cone S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (( *\<^sub>R) c) ` S = S)"
wenzelm@49529
  1949
proof -
wenzelm@53302
  1950
  {
wenzelm@53302
  1951
    assume "cone S"
wenzelm@53302
  1952
    {
wenzelm@53347
  1953
      fix c :: real
wenzelm@53347
  1954
      assume "c > 0"
wenzelm@53302
  1955
      {
wenzelm@53302
  1956
        fix x
wenzelm@53347
  1957
        assume "x \<in> S"
nipkow@67399
  1958
        then have "x \<in> (( *\<^sub>R) c) ` S"
wenzelm@49529
  1959
          unfolding image_def
wenzelm@60420
  1960
          using \<open>cone S\<close> \<open>c>0\<close> mem_cone[of S x "1/c"]
wenzelm@54465
  1961
            exI[of "(\<lambda>t. t \<in> S \<and> x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"]
wenzelm@53347
  1962
          by auto
wenzelm@49529
  1963
      }
wenzelm@49529
  1964
      moreover
wenzelm@53302
  1965
      {
wenzelm@53302
  1966
        fix x
nipkow@67399
  1967
        assume "x \<in> (( *\<^sub>R) c) ` S"
wenzelm@53347
  1968
        then have "x \<in> S"
wenzelm@60420
  1969
          using \<open>cone S\<close> \<open>c > 0\<close>
wenzelm@60420
  1970
          unfolding cone_def image_def \<open>c > 0\<close> by auto
wenzelm@49529
  1971
      }
nipkow@67399
  1972
      ultimately have "(( *\<^sub>R) c) ` S = S" by auto
hoelzl@40377
  1973
    }
nipkow@67399
  1974
    then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (( *\<^sub>R) c) ` S = S)"
wenzelm@60420
  1975
      using \<open>cone S\<close> cone_contains_0[of S] assms by auto
wenzelm@49529
  1976
  }
wenzelm@49529
  1977
  moreover
wenzelm@53302
  1978
  {
nipkow@67399
  1979
    assume a: "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> (( *\<^sub>R) c) ` S = S)"
wenzelm@53302
  1980
    {
wenzelm@53302
  1981
      fix x
wenzelm@53302
  1982
      assume "x \<in> S"
wenzelm@53347
  1983
      fix c1 :: real
wenzelm@53347
  1984
      assume "c1 \<ge> 0"
wenzelm@53347
  1985
      then have "c1 = 0 \<or> c1 > 0" by auto
wenzelm@60420
  1986
      then have "c1 *\<^sub>R x \<in> S" using a \<open>x \<in> S\<close> by auto
wenzelm@49529
  1987
    }
wenzelm@49529
  1988
    then have "cone S" unfolding cone_def by auto
hoelzl@40377
  1989
  }
wenzelm@49529
  1990
  ultimately show ?thesis by blast
wenzelm@49529
  1991
qed
wenzelm@49529
  1992
wenzelm@49529
  1993
lemma cone_hull_empty: "cone hull {} = {}"
wenzelm@49529
  1994
  by (metis cone_empty cone_hull_eq)
wenzelm@49529
  1995
wenzelm@53302
  1996
lemma cone_hull_empty_iff: "S = {} \<longleftrightarrow> cone hull S = {}"
wenzelm@49529
  1997
  by (metis bot_least cone_hull_empty hull_subset xtrans(5))
wenzelm@49529
  1998
wenzelm@53302
  1999
lemma cone_hull_contains_0: "S \<noteq> {} \<longleftrightarrow> 0 \<in> cone hull S"
wenzelm@49529
  2000
  using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S]
wenzelm@49529
  2001
  by auto
hoelzl@40377
  2002
hoelzl@40377
  2003
lemma mem_cone_hull:
wenzelm@53347
  2004
  assumes "x \<in> S" "c \<ge> 0"
wenzelm@53302
  2005
  shows "c *\<^sub>R x \<in> cone hull S"
wenzelm@49529
  2006
  by (metis assms cone_cone_hull hull_inc mem_cone)
wenzelm@49529
  2007
immler@68607
  2008
proposition cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \<ge> 0 \<and> x \<in> S}"
wenzelm@53339
  2009
  (is "?lhs = ?rhs")
immler@68607
  2010
proof -
wenzelm@53302
  2011
  {
wenzelm@53302
  2012
    fix x
wenzelm@53302
  2013
    assume "x \<in> ?rhs"
wenzelm@54465
  2014
    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
wenzelm@49529
  2015
      by auto
wenzelm@53347
  2016
    fix c :: real
wenzelm@53347
  2017
    assume c: "c \<ge> 0"
wenzelm@53339
  2018
    then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx"
wenzelm@54465
  2019
      using x by (simp add: algebra_simps)
wenzelm@49529
  2020
    moreover
nipkow@56536
  2021
    have "c * cx \<ge> 0" using c x by auto
wenzelm@49529
  2022
    ultimately
wenzelm@54465
  2023
    have "c *\<^sub>R x \<in> ?rhs" using x by auto
wenzelm@53302
  2024
  }
wenzelm@53347
  2025
  then have "cone ?rhs"
wenzelm@53347
  2026
    unfolding cone_def by auto
wenzelm@53347
  2027
  then have "?rhs \<in> Collect cone"
wenzelm@53347
  2028
    unfolding mem_Collect_eq by auto
wenzelm@53302
  2029
  {
wenzelm@53302
  2030
    fix x
wenzelm@53302
  2031
    assume "x \<in> S"
wenzelm@53302
  2032
    then have "1 *\<^sub>R x \<in> ?rhs"
wenzelm@49531
  2033
      apply auto
lp15@68031
  2034
      apply (rule_tac x = 1 in exI, auto)
wenzelm@49529
  2035
      done
wenzelm@53302
  2036
    then have "x \<in> ?rhs" by auto
wenzelm@53347
  2037
  }
wenzelm@53347
  2038
  then have "S \<subseteq> ?rhs" by auto
wenzelm@53302
  2039
  then have "?lhs \<subseteq> ?rhs"
wenzelm@60420
  2040
    using \<open>?rhs \<in> Collect cone\<close> hull_minimal[of S "?rhs" "cone"] by auto
wenzelm@49529
  2041
  moreover
wenzelm@53302
  2042
  {
wenzelm@53302
  2043
    fix x
wenzelm@53302
  2044
    assume "x \<in> ?rhs"
wenzelm@54465
  2045
    then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \<ge> 0" "xx \<in> S"
wenzelm@53339
  2046
      by auto
wenzelm@53339
  2047
    then have "xx \<in> cone hull S"
wenzelm@53339
  2048
      using hull_subset[of S] by auto
wenzelm@53302
  2049
    then have "x \<in> ?lhs"
wenzelm@54465
  2050
      using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto
wenzelm@49529
  2051
  }
wenzelm@49529
  2052
  ultimately show ?thesis by auto
hoelzl@40377
  2053
qed
hoelzl@40377
  2054
hoelzl@40377
  2055
lemma cone_closure:
wenzelm@53347
  2056
  fixes S :: "'a::real_normed_vector set"
wenzelm@49529
  2057
  assumes "cone S"
wenzelm@49529
  2058
  shows "cone (closure S)"
wenzelm@49529
  2059
proof (cases "S = {}")
wenzelm@49529
  2060
  case True
wenzelm@49529
  2061
  then show ?thesis by auto
wenzelm@49529
  2062
next
wenzelm@49529
  2063
  case False
nipkow@67399
  2064
  then have "0 \<in> S \<and> (\<forall>c. c > 0 \<longrightarrow> ( *\<^sub>R) c ` S = S)"
wenzelm@49529
  2065
    using cone_iff[of S] assms by auto
nipkow@67399
  2066
  then have "0 \<in> closure S \<and> (\<forall>c. c > 0 \<longrightarrow> ( *\<^sub>R) c ` closure S = closure S)"
lp15@68031
  2067
    using closure_subset by (auto simp: closure_scaleR)
wenzelm@53339
  2068
  then show ?thesis
lp15@60974
  2069
    using False cone_iff[of "closure S"] by auto
wenzelm@49529
  2070
qed
wenzelm@49529
  2071
hoelzl@40377
  2072
wenzelm@60420
  2073
subsection \<open>Affine dependence and consequential theorems (from Lars Schewe)\<close>
himmelma@33175
  2074
immler@67962
  2075
definition%important affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
wenzelm@53339
  2076
  where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
himmelma@33175
  2077
lp15@63007
  2078
lemma affine_dependent_subset:
lp15@63007
  2079
   "\<lbrakk>affine_dependent s; s \<subseteq> t\<rbrakk> \<Longrightarrow> affine_dependent t"
lp15@63007
  2080
apply (simp add: affine_dependent_def Bex_def)
lp15@63007
  2081
apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]])
lp15@63007
  2082
done
lp15@63007
  2083
lp15@63007
  2084
lemma affine_independent_subset:
lp15@63007
  2085
  shows "\<lbrakk>~ affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> ~ affine_dependent s"
lp15@63007
  2086
by (metis affine_dependent_subset)
lp15@63007
  2087
lp15@63007
  2088
lemma affine_independent_Diff:
lp15@63007
  2089
   "~ affine_dependent s \<Longrightarrow> ~ affine_dependent(s - t)"
lp15@63007
  2090
by (meson Diff_subset affine_dependent_subset)
lp15@63007
  2091
immler@68607
  2092
proposition affine_dependent_explicit:
himmelma@33175
  2093
  "affine_dependent p \<longleftrightarrow>
lp15@68041
  2094
    (\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
lp15@68041
  2095
proof -
lp15@68041
  2096
  have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> (\<Sum>w\<in>S. u w *\<^sub>R w) = 0"
lp15@68041
  2097
    if "(\<Sum>w\<in>S. u w *\<^sub>R w) = x" "x \<in> p" "finite S" "S \<noteq> {}" "S \<subseteq> p - {x}" "sum u S = 1" for x S u
lp15@68041
  2098
  proof (intro exI conjI)
lp15@68041
  2099
    have "x \<notin> S" 
lp15@68041
  2100
      using that by auto
lp15@68041
  2101
    then show "(\<Sum>v \<in> insert x S. if v = x then - 1 else u v) = 0"
lp15@68041
  2102
      using that by (simp add: sum_delta_notmem)
lp15@68041
  2103
    show "(\<Sum>w \<in> insert x S. (if w = x then - 1 else u w) *\<^sub>R w) = 0"
lp15@68041
  2104
      using that \<open>x \<notin> S\<close> by (simp add: if_smult sum_delta_notmem cong: if_cong)
lp15@68041
  2105
  qed (use that in auto)
lp15@68041
  2106
  moreover have "\<exists>x\<in>p. \<exists>S u. finite S \<and> S \<noteq> {} \<and> S \<subseteq> p - {x} \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
lp15@68041
  2107
    if "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0" "finite S" "S \<subseteq> p" "sum u S = 0" "v \<in> S" "u v \<noteq> 0" for S u v
lp15@68041
  2108
  proof (intro bexI exI conjI)
lp15@68041
  2109
    have "S \<noteq> {v}"
lp15@68041
  2110
      using that by auto
lp15@68041
  2111
    then show "S - {v} \<noteq> {}"
lp15@68041
  2112
      using that by auto
lp15@68041
  2113
    show "(\<Sum>x \<in> S - {v}. - (1 / u v) * u x) = 1"
lp15@68041
  2114
      unfolding sum_distrib_left[symmetric] sum_diff1[OF \<open>finite S\<close>] by (simp add: that)
lp15@68041
  2115
    show "(\<Sum>x\<in>S - {v}. (- (1 / u v) * u x) *\<^sub>R x) = v"
lp15@68041
  2116
      unfolding sum_distrib_left [symmetric] scaleR_scaleR[symmetric]
lp15@68041
  2117
                scaleR_right.sum [symmetric] sum_diff1[OF \<open>finite S\<close>] 
lp15@68041
  2118
      using that by auto
lp15@68041
  2119
    show "S - {v} \<subseteq> p - {v}"
lp15@68041
  2120
      using that by auto
lp15@68041
  2121
  qed (use that in auto)
lp15@68041
  2122
  ultimately show ?thesis
lp15@68041
  2123
    unfolding affine_dependent_def affine_hull_explicit by auto
himmelma@33175
  2124
qed
himmelma@33175
  2125
himmelma@33175
  2126
lemma affine_dependent_explicit_finite:
lp15@68041
  2127
  fixes S :: "'a::real_vector set"
lp15@68041
  2128
  assumes "finite S"
lp15@68041
  2129
  shows "affine_dependent S \<longleftrightarrow>
lp15@68041
  2130
    (\<exists>u. sum u S = 0 \<and> (\<exists>v\<in>S. u v \<noteq> 0) \<and> sum (\<lambda>v. u v *\<^sub>R v) S = 0)"
himmelma@33175
  2131
  (is "?lhs = ?rhs")
himmelma@33175
  2132
proof
wenzelm@53347
  2133
  have *: "\<And>vt u v. (if vt then u v else 0) *\<^sub>R v = (if vt then (u v) *\<^sub>R v else 0::'a)"
wenzelm@49529
  2134
    by auto
himmelma@33175
  2135
  assume ?lhs
wenzelm@49529
  2136
  then obtain t u v where
lp15@68041
  2137
    "finite t" "t \<subseteq> S" "sum u t = 0" "v\<in>t" "u v \<noteq> 0"  "(\<Sum>v\<in>t. u v *\<^sub>R v) = 0"
himmelma@33175
  2138
    unfolding affine_dependent_explicit by auto
wenzelm@49529
  2139
  then show ?rhs
wenzelm@49529
  2140
    apply (rule_tac x="\<lambda>x. if x\<in>t then u x else 0" in exI)
lp15@68041
  2141
    apply (auto simp: * sum.inter_restrict[OF assms, symmetric] Int_absorb1[OF \<open>t\<subseteq>S\<close>])
wenzelm@49529
  2142
    done
himmelma@33175
  2143
next
himmelma@33175
  2144
  assume ?rhs
lp15@68041
  2145
  then obtain u v where "sum u S = 0"  "v\<in>S" "u v \<noteq> 0" "(\<Sum>v\<in>S. u v *\<^sub>R v) = 0"
wenzelm@53339
  2146
    by auto
wenzelm@49529
  2147
  then show ?lhs unfolding affine_dependent_explicit
wenzelm@49529
  2148
    using assms by auto
wenzelm@49529
  2149
qed
wenzelm@49529
  2150
himmelma@33175
  2151
immler@67962
  2152
subsection%unimportant \<open>Connectedness of convex sets\<close>
huffman@44465
  2153
hoelzl@51480
  2154
lemma connectedD:
hoelzl@51480
  2155
  "connected S \<Longrightarrow> open A \<Longrightarrow> open B \<Longrightarrow> S \<subseteq> A \<union> B \<Longrightarrow> A \<inter> B \<inter> S = {} \<Longrightarrow> A \<inter> S = {} \<or> B \<inter> S = {}"
lp15@61426
  2156
  by (rule Topological_Spaces.topological_space_class.connectedD)
himmelma@33175
  2157
himmelma@33175
  2158
lemma convex_connected:
lp15@68041
  2159
  fixes S :: "'a::real_normed_vector set"
lp15@68041
  2160
  assumes "convex S"
lp15@68041
  2161
  shows "connected S"
hoelzl@51480
  2162
proof (rule connectedI)
hoelzl@51480
  2163
  fix A B
lp15@68041
  2164
  assume "open A" "open B" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
hoelzl@51480
  2165
  moreover
lp15@68041
  2166
  assume "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}"
lp15@68041
  2167
  then obtain a b where a: "a \<in> A" "a \<in> S" and b: "b \<in> B" "b \<in> S" by auto
wenzelm@63040
  2168
  define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u
hoelzl@51480
  2169
  then have "continuous_on {0 .. 1} f"
hoelzl@56371
  2170
    by (auto intro!: continuous_intros)
hoelzl@51480
  2171
  then have "connected (f ` {0 .. 1})"
hoelzl@51480
  2172
    by (auto intro!: connected_continuous_image)
hoelzl@51480
  2173
  note connectedD[OF this, of A B]
hoelzl@51480
  2174
  moreover have "a \<in> A \<inter> f ` {0 .. 1}"
hoelzl@51480
  2175
    using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def)
hoelzl@51480
  2176
  moreover have "b \<in> B \<inter> f ` {0 .. 1}"
hoelzl@51480
  2177
    using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def)
lp15@68041
  2178
  moreover have "f ` {0 .. 1} \<subseteq> S"
lp15@68041
  2179
    using \<open>convex S\<close> a b unfolding convex_def f_def by auto
hoelzl@51480
  2180
  ultimately show False by auto
himmelma@33175
  2181
qed
himmelma@33175
  2182
lp15@61426
  2183
corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)"
lp15@66939
  2184
  by (simp add: convex_connected)
lp15@66939
  2185
lp15@66939
  2186
corollary component_complement_connected:
lp15@66939
  2187
  fixes S :: "'a::real_normed_vector set"
lp15@66939
  2188
  assumes "connected S" "C \<in> components (-S)"
lp15@66939
  2189
  shows "connected(-C)"
lp15@66939
  2190
  using component_diff_connected [of S UNIV] assms
lp15@66939
  2191
  by (auto simp: Compl_eq_Diff_UNIV)
himmelma@33175
  2192
paulson@62131
  2193
proposition clopen:
lp15@66884
  2194
  fixes S :: "'a :: real_normed_vector set"
lp15@66884
  2195
  shows "closed S \<and> open S \<longleftrightarrow> S = {} \<or> S = UNIV"
lp15@66884
  2196
    by (force intro!: connected_UNIV [unfolded connected_clopen, rule_format])
paulson@62131
  2197
paulson@62131
  2198
corollary compact_open:
lp15@66884
  2199
  fixes S :: "'a :: euclidean_space set"
lp15@66884
  2200
  shows "compact S \<and> open S \<longleftrightarrow> S = {}"
paulson@62131
  2201
  by (auto simp: compact_eq_bounded_closed clopen)
paulson@62131
  2202
lp15@62948
  2203
corollary finite_imp_not_open:
lp15@62948
  2204
    fixes S :: "'a::{real_normed_vector, perfect_space} set"
lp15@62948
  2205
    shows "\<lbrakk>finite S; open S\<rbrakk> \<Longrightarrow> S={}"
lp15@62948
  2206
  using clopen [of S] finite_imp_closed not_bounded_UNIV by blast
lp15@62948
  2207
lp15@63007
  2208
corollary empty_interior_finite:
lp15@63007
  2209
    fixes S :: "'a::{real_normed_vector, perfect_space} set"
lp15@63007
  2210
    shows "finite S \<Longrightarrow> interior S = {}"
lp15@63007
  2211
  by (metis interior_subset finite_subset open_interior [of S] finite_imp_not_open)
lp15@63007
  2212
wenzelm@60420
  2213
text \<open>Balls, being convex, are connected.\<close>
himmelma@33175
  2214
immler@56188
  2215
lemma convex_prod:
wenzelm@53347
  2216
  assumes "\<And>i. i \<in> Basis \<Longrightarrow> convex {x. P i x}"
hoelzl@50526
  2217
  shows "convex {x. \<forall>i\<in>Basis. P i (x\<bullet>i)}"
hoelzl@50526
  2218
  using assms unfolding convex_def
hoelzl@50526
  2219
  by (auto simp: inner_add_left)
hoelzl@50526
  2220
hoelzl@50526
  2221
lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\<forall>i\<in>Basis. 0 \<le> x\<bullet>i)}"
immler@56188
  2222
  by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval)
himmelma@33175
  2223
himmelma@33175
  2224
lemma convex_local_global_minimum:
himmelma@33175
  2225
  fixes s :: "'a::real_normed_vector set"
wenzelm@53347
  2226
  assumes "e > 0"
wenzelm@53347
  2227
    and "convex_on s f"
wenzelm@53347
  2228
    and "ball x e \<subseteq> s"
wenzelm@53347
  2229
    and "\<forall>y\<in>ball x e. f x \<le> f y"
himmelma@33175
  2230
  shows "\<forall>y\<in>s. f x \<le> f y"
wenzelm@53302
  2231
proof (rule ccontr)
wenzelm@53302
  2232
  have "x \<in> s" using assms(1,3) by auto
wenzelm@53302
  2233
  assume "\<not> ?thesis"
wenzelm@53302
  2234
  then obtain y where "y\<in>s" and y: "f x > f y" by auto
paulson@62087
  2235
  then have xy: "0 < dist x y"  by auto
wenzelm@53347
  2236
  then obtain u where "0 < u" "u \<le> 1" and u: "u < e / dist x y"
lp15@68527
  2237
    using field_lbound_gt_zero[of 1 "e / dist x y"] xy \<open>e>0\<close> by auto
wenzelm@53302
  2238
  then have "f ((1-u) *\<^sub>R x + u *\<^sub>R y) \<le> (1-u) * f x + u * f y"
wenzelm@60420
  2239
    using \<open>x\<in>s\<close> \<open>y\<in>s\<close>
wenzelm@53302
  2240
    using assms(2)[unfolded convex_on_def,
wenzelm@53302
  2241
      THEN bspec[where x=x], THEN bspec[where x=y], THEN spec[where x="1-u"]]
wenzelm@50804
  2242
    by auto
himmelma@33175
  2243
  moreover
wenzelm@50804
  2244
  have *: "x - ((1 - u) *\<^sub>R x + u *\<^sub>R y) = u *\<^sub>R (x - y)"
wenzelm@50804
  2245
    by (simp add: algebra_simps)
wenzelm@50804
  2246
  have "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> ball x e"
wenzelm@53302
  2247
    unfolding mem_ball dist_norm
wenzelm@60420
  2248
    unfolding * and norm_scaleR and abs_of_pos[OF \<open>0<u\<close>]
wenzelm@50804
  2249
    unfolding dist_norm[symmetric]
wenzelm@53302
  2250
    using u
wenzelm@53302
  2251
    unfolding pos_less_divide_eq[OF xy]
wenzelm@53302
  2252
    by auto
wenzelm@53302
  2253
  then have "f x \<le> f ((1 - u) *\<^sub>R x + u *\<^sub>R y)"
wenzelm@53302
  2254
    using assms(4) by auto
wenzelm@50804
  2255
  ultimately show False
wenzelm@60420
  2256
    using mult_strict_left_mono[OF y \<open>u>0\<close>]
wenzelm@53302
  2257
    unfolding left_diff_distrib
wenzelm@53302
  2258
    by auto
himmelma@33175
  2259
qed
himmelma@33175
  2260
lp15@60800
  2261
lemma convex_ball [iff]:
himmelma@33175
  2262
  fixes x :: "'a::real_normed_vector"
wenzelm@49531
  2263
  shows "convex (ball x e)"
lp15@68031
  2264
proof (auto simp: convex_def)
wenzelm@50804
  2265
  fix y z
wenzelm@50804
  2266
  assume yz: "dist x y < e" "dist x z < e"
wenzelm@50804
  2267
  fix u v :: real
wenzelm@50804
  2268
  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
wenzelm@50804
  2269
  have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
wenzelm@50804
  2270
    using uv yz
huffman@53620
  2271
    using convex_on_dist [of "ball x e" x, unfolded convex_on_def,
wenzelm@53302
  2272
      THEN bspec[where x=y], THEN bspec[where x=z]]
wenzelm@50804
  2273
    by auto
wenzelm@50804
  2274
  then show "dist x (u *\<^sub>R y + v *\<^sub>R z) < e"
wenzelm@50804
  2275
    using convex_bound_lt[OF yz uv] by auto
himmelma@33175
  2276
qed
himmelma@33175
  2277
lp15@60800
  2278
lemma convex_cball [iff]:
himmelma@33175
  2279
  fixes x :: "'a::real_normed_vector"
wenzelm@53347
  2280
  shows "convex (cball x e)"
wenzelm@53347
  2281
proof -
wenzelm@53347
  2282
  {
wenzelm@53347
  2283
    fix y z
wenzelm@53347
  2284
    assume yz: "dist x y \<le> e" "dist x z \<le> e"
wenzelm@53347
  2285
    fix u v :: real
wenzelm@53347
  2286
    assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
wenzelm@53347
  2287
    have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> u * dist x y + v * dist x z"
wenzelm@53347
  2288
      using uv yz
huffman@53620
  2289
      using convex_on_dist [of "cball x e" x, unfolded convex_on_def,
wenzelm@53347
  2290
        THEN bspec[where x=y], THEN bspec[where x=z]]
wenzelm@53347
  2291
      by auto
wenzelm@53347
  2292
    then have "dist x (u *\<^sub>R y + v *\<^sub>R z) \<le> e"
wenzelm@53347
  2293
      using convex_bound_le[OF yz uv] by auto
wenzelm@53347
  2294
  }
lp15@68031
  2295
  then show ?thesis by (auto simp: convex_def Ball_def)
himmelma@33175
  2296
qed
himmelma@33175
  2297
paulson@61518
  2298
lemma connected_ball [iff]:
himmelma@33175
  2299
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  2300
  shows "connected (ball x e)"
himmelma@33175
  2301
  using convex_connected convex_ball by auto
himmelma@33175
  2302
paulson@61518
  2303
lemma connected_cball [iff]:
himmelma@33175
  2304
  fixes x :: "'a::real_normed_vector"
wenzelm@53302
  2305
  shows "connected (cball x e)"
himmelma@33175
  2306
  using convex_connected convex_cball by auto
himmelma@33175
  2307
wenzelm@50804
  2308
wenzelm@60420
  2309
subsection \<open>Convex hull\<close>
himmelma@33175
  2310
paulson@60762
  2311
lemma convex_convex_hull [iff]: "convex (convex hull s)"
wenzelm@53302
  2312
  unfolding hull_def
wenzelm@53302
  2313
  using convex_Inter[of "{t. convex t \<and> s \<subseteq> t}"]
huffman@44170
  2314
  by auto
himmelma@33175
  2315
lp15@63016
  2316
lemma convex_hull_subset:
lp15@63016
  2317
    "s \<subseteq> convex hull t \<Longrightarrow> convex hull s \<subseteq> convex hull t"
lp15@63016
  2318
  by (simp add: convex_convex_hull subset_hull)
lp15@63016
  2319
haftmann@34064
  2320
lemma convex_hull_eq: "convex hull s = s \<longleftrightarrow> convex s"
wenzelm@50804
  2321
  by (metis convex_convex_hull hull_same)
himmelma@33175
  2322
himmelma@33175
  2323
lemma bounded_convex_hull:
himmelma@33175
  2324
  fixes s :: "'a::real_normed_vector set"
wenzelm@53347
  2325
  assumes "bounded s"
wenzelm@53347
  2326
  shows "bounded (convex hull s)"
wenzelm@50804
  2327
proof -
wenzelm@50804
  2328
  from assms obtain B where B: "\<forall>x\<in>s. norm x \<le> B"
wenzelm@50804
  2329
    unfolding bounded_iff by auto
wenzelm@50804
  2330
  show ?thesis
wenzelm@50804
  2331
    apply (rule bounded_subset[OF bounded_cball, of _ 0 B])
huffman@44170
  2332
    unfolding subset_hull[of convex, OF convex_cball]
wenzelm@53302
  2333
    unfolding subset_eq mem_cball dist_norm using B
wenzelm@53302
  2334
    apply auto
wenzelm@50804
  2335
    done
wenzelm@50804
  2336
qed
himmelma@33175
  2337
himmelma@33175
  2338
lemma finite_imp_bounded_convex_hull:
himmelma@33175
  2339
  fixes s :: "'a::real_normed_vector set"
wenzelm@53302
  2340
  shows "finite s \<Longrightarrow> bounded (convex hull s)"
wenzelm@53302
  2341
  using bounded_convex_hull finite_imp_bounded
wenzelm@53302
  2342
  by auto
himmelma@33175
  2343
wenzelm@50804
  2344
immler@67962
  2345
subsubsection%unimportant \<open>Convex hull is "preserved" by a linear function\<close>
hoelzl@40377
  2346
hoelzl@40377
  2347
lemma convex_hull_linear_image:
huffman@53620
  2348
  assumes f: "linear f"
hoelzl@40377
  2349
  shows "f ` (convex hull s) = convex hull (f ` s)"
huffman@53620
  2350
proof
huffman@53620
  2351
  show "convex hull (f ` s) \<subseteq> f ` (convex hull s)"
huffman@53620
  2352
    by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull)
huffman@53620
  2353
  show "f ` (convex hull s) \<subseteq> convex hull (f ` s)"
huffman@53620
  2354
  proof (unfold image_subset_iff_subset_vimage, rule hull_minimal)
huffman@53620
  2355
    show "s \<subseteq> f -` (convex hull (f ` s))"
huffman@53620
  2356
      by (fast intro: hull_inc)
huffman@53620
  2357
    show "convex (f -` (convex hull (f ` s)))"
huffman@53620
  2358
      by (intro convex_linear_vimage [OF f] convex_convex_hull)
huffman@53620
  2359
  qed
huffman@53620
  2360
qed
hoelzl@40377
  2361
hoelzl@40377
  2362
lemma in_convex_hull_linear_image:
huffman@53620
  2363
  assumes "linear f"
wenzelm@53347
  2364
    and "x \<in> convex hull s"
wenzelm@53339
  2365
  shows "f x \<in> convex hull (f ` s)"
wenzelm@50804
  2366
  using convex_hull_linear_image[OF assms(1)] assms(2) by auto
wenzelm@50804
  2367
huffman@53620
  2368
lemma convex_hull_Times:
huffman@53620
  2369
  "convex hull (s \<times> t) = (convex hull s) \<times> (convex hull t)"
huffman@53620
  2370
proof
huffman@53620
  2371
  show "convex hull (s \<times> t) \<subseteq> (convex hull s) \<times> (convex hull t)"
huffman@53620
  2372
    by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull)
lp15@68058
  2373
  have "(x, y) \<in> convex hull (s \<times> t)" if x: "x \<in> convex hull s" and y: "y \<in> convex hull t" for x y
lp15@68058
  2374
  proof (rule hull_induct [OF x], rule hull_induct [OF y])
huffman@53620
  2375
    fix x y assume "x \<in> s" and "y \<in> t"
huffman@53620
  2376
    then show "(x, y) \<in> convex hull (s \<times> t)"
huffman@53620
  2377
      by (simp add: hull_inc)
huffman@53620
  2378
  next
huffman@53620
  2379
    fix x let ?S = "((\<lambda