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