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