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