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