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