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