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