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