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