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