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