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