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