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