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