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