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