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