src/HOL/Multivariate_Analysis/Linear_Algebra.thy
author huffman
Fri Aug 12 20:55:22 2011 -0700 (2011-08-12)
changeset 44176 eda112e9cdee
parent 44170 510ac30f44c0
child 44282 f0de18b62d63
permissions -rw-r--r--
remove redundant lemma setsum_norm in favor of norm_setsum;
remove finiteness assumption from setsum_norm_le;
huffman@44133
     1
(*  Title:      HOL/Multivariate_Analysis/Linear_Algebra.thy
huffman@44133
     2
    Author:     Amine Chaieb, University of Cambridge
huffman@44133
     3
*)
huffman@44133
     4
huffman@44133
     5
header {* Elementary linear algebra on Euclidean spaces *}
huffman@44133
     6
huffman@44133
     7
theory Linear_Algebra
huffman@44133
     8
imports
huffman@44133
     9
  Euclidean_Space
huffman@44133
    10
  "~~/src/HOL/Library/Infinite_Set"
huffman@44133
    11
  L2_Norm
huffman@44133
    12
  "~~/src/HOL/Library/Convex"
huffman@44133
    13
uses
huffman@44133
    14
  "~~/src/HOL/Library/positivstellensatz.ML"  (* FIXME duplicate use!? *)
huffman@44133
    15
  ("normarith.ML")
huffman@44133
    16
begin
huffman@44133
    17
huffman@44133
    18
lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
huffman@44133
    19
  by auto
huffman@44133
    20
huffman@44133
    21
notation inner (infix "\<bullet>" 70)
huffman@44133
    22
huffman@44133
    23
subsection {* A connectedness or intermediate value lemma with several applications. *}
huffman@44133
    24
huffman@44133
    25
lemma connected_real_lemma:
huffman@44133
    26
  fixes f :: "real \<Rightarrow> 'a::metric_space"
huffman@44133
    27
  assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
huffman@44133
    28
  and dst: "\<And>e x. a <= x \<Longrightarrow> x <= b \<Longrightarrow> 0 < e ==> \<exists>d > 0. \<forall>y. abs(y - x) < d \<longrightarrow> dist(f y) (f x) < e"
huffman@44133
    29
  and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
huffman@44133
    30
  and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
huffman@44133
    31
  and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
huffman@44133
    32
  shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
huffman@44133
    33
proof-
huffman@44133
    34
  let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
huffman@44133
    35
  have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa)
huffman@44133
    36
  have Sub: "\<exists>y. isUb UNIV ?S y"
huffman@44133
    37
    apply (rule exI[where x= b])
huffman@44133
    38
    using ab fb e12 by (auto simp add: isUb_def setle_def)
huffman@44133
    39
  from reals_complete[OF Se Sub] obtain l where
huffman@44133
    40
    l: "isLub UNIV ?S l"by blast
huffman@44133
    41
  have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
huffman@44133
    42
    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
huffman@44133
    43
    by (metis linorder_linear)
huffman@44133
    44
  have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
huffman@44133
    45
    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
huffman@44133
    46
    by (metis linorder_linear not_le)
huffman@44133
    47
    have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
huffman@44133
    48
    have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
huffman@44133
    49
    have "\<And>d::real. 0 < d \<Longrightarrow> 0 < d/2 \<and> d/2 < d" by simp
huffman@44133
    50
    then have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by blast
huffman@44133
    51
    {assume le2: "f l \<in> e2"
huffman@44133
    52
      from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
huffman@44133
    53
      hence lap: "l - a > 0" using alb by arith
huffman@44133
    54
      from e2[rule_format, OF le2] obtain e where
huffman@44133
    55
        e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
huffman@44133
    56
      from dst[OF alb e(1)] obtain d where
huffman@44133
    57
        d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
huffman@44133
    58
      let ?d' = "min (d/2) ((l - a)/2)"
huffman@44133
    59
      have "?d' < d \<and> 0 < ?d' \<and> ?d' < l - a" using lap d(1)
huffman@44133
    60
        by (simp add: min_max.less_infI2)
huffman@44133
    61
      then have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" by auto
huffman@44133
    62
      then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
huffman@44133
    63
      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
huffman@44133
    64
      from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
huffman@44133
    65
      moreover
huffman@44133
    66
      have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
huffman@44133
    67
      ultimately have False using e12 alb d' by auto}
huffman@44133
    68
    moreover
huffman@44133
    69
    {assume le1: "f l \<in> e1"
huffman@44133
    70
    from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
huffman@44133
    71
      hence blp: "b - l > 0" using alb by arith
huffman@44133
    72
      from e1[rule_format, OF le1] obtain e where
huffman@44133
    73
        e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
huffman@44133
    74
      from dst[OF alb e(1)] obtain d where
huffman@44133
    75
        d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
huffman@44133
    76
      have "\<And>d::real. 0 < d \<Longrightarrow> d/2 < d \<and> 0 < d/2" by simp
huffman@44133
    77
      then have "\<exists>d'. d' < d \<and> d' >0" using d(1) by blast
huffman@44133
    78
      then obtain d' where d': "d' > 0" "d' < d" by metis
huffman@44133
    79
      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
huffman@44133
    80
      hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
huffman@44133
    81
      with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
huffman@44133
    82
      with l d' have False
huffman@44133
    83
        by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
huffman@44133
    84
    ultimately show ?thesis using alb by metis
huffman@44133
    85
qed
huffman@44133
    86
huffman@44133
    87
text{* One immediately useful corollary is the existence of square roots! --- Should help to get rid of all the development of square-root for reals as a special case *}
huffman@44133
    88
huffman@44133
    89
lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
huffman@44133
    90
proof-
huffman@44133
    91
  have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith
huffman@44133
    92
  thus ?thesis by (simp add: field_simps power2_eq_square)
huffman@44133
    93
qed
huffman@44133
    94
huffman@44133
    95
lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
huffman@44133
    96
  using isCont_power[OF isCont_ident, of 2, unfolded isCont_def LIM_eq, rule_format, of e x] apply (auto simp add: power2_eq_square)
huffman@44133
    97
  apply (rule_tac x="s" in exI)
huffman@44133
    98
  apply auto
huffman@44133
    99
  apply (erule_tac x=y in allE)
huffman@44133
   100
  apply auto
huffman@44133
   101
  done
huffman@44133
   102
huffman@44133
   103
lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
huffman@44133
   104
  using real_sqrt_le_iff[of x "y^2"] by simp
huffman@44133
   105
huffman@44133
   106
lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
huffman@44133
   107
  using real_sqrt_le_mono[of "x^2" y] by simp
huffman@44133
   108
huffman@44133
   109
lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
huffman@44133
   110
  using real_sqrt_less_mono[of "x^2" y] by simp
huffman@44133
   111
huffman@44133
   112
lemma sqrt_even_pow2: assumes n: "even n"
huffman@44133
   113
  shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
huffman@44133
   114
proof-
huffman@44133
   115
  from n obtain m where m: "n = 2*m" unfolding even_mult_two_ex ..
huffman@44133
   116
  from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
huffman@44133
   117
    by (simp only: power_mult[symmetric] mult_commute)
huffman@44133
   118
  then show ?thesis  using m by simp
huffman@44133
   119
qed
huffman@44133
   120
huffman@44133
   121
lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
huffman@44133
   122
  apply (cases "x = 0", simp_all)
huffman@44133
   123
  using sqrt_divide_self_eq[of x]
huffman@44133
   124
  apply (simp add: inverse_eq_divide field_simps)
huffman@44133
   125
  done
huffman@44133
   126
huffman@44133
   127
text{* Hence derive more interesting properties of the norm. *}
huffman@44133
   128
huffman@44133
   129
(* FIXME: same as norm_scaleR
huffman@44133
   130
lemma norm_mul[simp]: "norm(a *\<^sub>R x) = abs(a) * norm x"
huffman@44133
   131
  by (simp add: norm_vector_def setL2_right_distrib abs_mult)
huffman@44133
   132
*)
huffman@44133
   133
huffman@44133
   134
lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (inner x x = (0::real))"
huffman@44133
   135
  by (simp add: setL2_def power2_eq_square)
huffman@44133
   136
huffman@44133
   137
lemma norm_cauchy_schwarz:
huffman@44133
   138
  shows "inner x y <= norm x * norm y"
huffman@44133
   139
  using Cauchy_Schwarz_ineq2[of x y] by auto
huffman@44133
   140
huffman@44133
   141
lemma norm_cauchy_schwarz_abs:
huffman@44133
   142
  shows "\<bar>inner x y\<bar> \<le> norm x * norm y"
huffman@44133
   143
  by (rule Cauchy_Schwarz_ineq2)
huffman@44133
   144
huffman@44133
   145
lemma norm_triangle_sub:
huffman@44133
   146
  fixes x y :: "'a::real_normed_vector"
huffman@44133
   147
  shows "norm x \<le> norm y  + norm (x - y)"
huffman@44133
   148
  using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)
huffman@44133
   149
huffman@44133
   150
lemma real_abs_norm: "\<bar>norm x\<bar> = norm x"
huffman@44133
   151
  by (rule abs_norm_cancel)
huffman@44133
   152
lemma real_abs_sub_norm: "\<bar>norm x - norm y\<bar> <= norm(x - y)"
huffman@44133
   153
  by (rule norm_triangle_ineq3)
huffman@44133
   154
lemma norm_le: "norm(x) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
huffman@44133
   155
  by (simp add: norm_eq_sqrt_inner) 
huffman@44133
   156
lemma norm_lt: "norm(x) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
huffman@44133
   157
  by (simp add: norm_eq_sqrt_inner)
huffman@44133
   158
lemma norm_eq: "norm(x) = norm (y) \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
huffman@44133
   159
  apply(subst order_eq_iff) unfolding norm_le by auto
huffman@44133
   160
lemma norm_eq_1: "norm(x) = 1 \<longleftrightarrow> x \<bullet> x = 1"
huffman@44133
   161
  unfolding norm_eq_sqrt_inner by auto
huffman@44133
   162
huffman@44133
   163
text{* Squaring equations and inequalities involving norms.  *}
huffman@44133
   164
huffman@44133
   165
lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
huffman@44133
   166
  by (simp add: norm_eq_sqrt_inner)
huffman@44133
   167
huffman@44133
   168
lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
huffman@44133
   169
  by (auto simp add: norm_eq_sqrt_inner)
huffman@44133
   170
huffman@44133
   171
lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
huffman@44133
   172
proof
huffman@44133
   173
  assume "\<bar>x\<bar> \<le> \<bar>y\<bar>"
huffman@44133
   174
  then have "\<bar>x\<bar>\<twosuperior> \<le> \<bar>y\<bar>\<twosuperior>" by (rule power_mono, simp)
huffman@44133
   175
  then show "x\<twosuperior> \<le> y\<twosuperior>" by simp
huffman@44133
   176
next
huffman@44133
   177
  assume "x\<twosuperior> \<le> y\<twosuperior>"
huffman@44133
   178
  then have "sqrt (x\<twosuperior>) \<le> sqrt (y\<twosuperior>)" by (rule real_sqrt_le_mono)
huffman@44133
   179
  then show "\<bar>x\<bar> \<le> \<bar>y\<bar>" by simp
huffman@44133
   180
qed
huffman@44133
   181
huffman@44133
   182
lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
huffman@44133
   183
  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
huffman@44133
   184
  using norm_ge_zero[of x]
huffman@44133
   185
  apply arith
huffman@44133
   186
  done
huffman@44133
   187
huffman@44133
   188
lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2"
huffman@44133
   189
  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
huffman@44133
   190
  using norm_ge_zero[of x]
huffman@44133
   191
  apply arith
huffman@44133
   192
  done
huffman@44133
   193
huffman@44133
   194
lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
huffman@44133
   195
  by (metis not_le norm_ge_square)
huffman@44133
   196
lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
huffman@44133
   197
  by (metis norm_le_square not_less)
huffman@44133
   198
huffman@44133
   199
text{* Dot product in terms of the norm rather than conversely. *}
huffman@44133
   200
huffman@44133
   201
lemmas inner_simps = inner.add_left inner.add_right inner.diff_right inner.diff_left 
huffman@44133
   202
inner.scaleR_left inner.scaleR_right
huffman@44133
   203
huffman@44133
   204
lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
huffman@44133
   205
  unfolding power2_norm_eq_inner inner_simps inner_commute by auto 
huffman@44133
   206
huffman@44133
   207
lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
huffman@44133
   208
  unfolding power2_norm_eq_inner inner_simps inner_commute by(auto simp add:algebra_simps)
huffman@44133
   209
huffman@44133
   210
text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
huffman@44133
   211
huffman@44133
   212
lemma vector_eq: "x = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y \<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
huffman@44133
   213
proof
huffman@44133
   214
  assume ?lhs then show ?rhs by simp
huffman@44133
   215
next
huffman@44133
   216
  assume ?rhs
huffman@44133
   217
  then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y \<bullet> y = 0" by simp
huffman@44133
   218
  hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0" by (simp add: inner_simps inner_commute)
huffman@44133
   219
  then have "(x - y) \<bullet> (x - y) = 0" by (simp add: field_simps inner_simps inner_commute)
huffman@44133
   220
  then show "x = y" by (simp)
huffman@44133
   221
qed
huffman@44133
   222
huffman@44133
   223
subsection{* General linear decision procedure for normed spaces. *}
huffman@44133
   224
huffman@44133
   225
lemma norm_cmul_rule_thm:
huffman@44133
   226
  fixes x :: "'a::real_normed_vector"
huffman@44133
   227
  shows "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(scaleR c x)"
huffman@44133
   228
  unfolding norm_scaleR
huffman@44133
   229
  apply (erule mult_left_mono)
huffman@44133
   230
  apply simp
huffman@44133
   231
  done
huffman@44133
   232
huffman@44133
   233
  (* FIXME: Move all these theorems into the ML code using lemma antiquotation *)
huffman@44133
   234
lemma norm_add_rule_thm:
huffman@44133
   235
  fixes x1 x2 :: "'a::real_normed_vector"
huffman@44133
   236
  shows "norm x1 \<le> b1 \<Longrightarrow> norm x2 \<le> b2 \<Longrightarrow> norm (x1 + x2) \<le> b1 + b2"
huffman@44133
   237
  by (rule order_trans [OF norm_triangle_ineq add_mono])
huffman@44133
   238
huffman@44133
   239
lemma ge_iff_diff_ge_0: "(a::'a::linordered_ring) \<ge> b == a - b \<ge> 0"
huffman@44133
   240
  by (simp add: field_simps)
huffman@44133
   241
huffman@44133
   242
lemma pth_1:
huffman@44133
   243
  fixes x :: "'a::real_normed_vector"
huffman@44133
   244
  shows "x == scaleR 1 x" by simp
huffman@44133
   245
huffman@44133
   246
lemma pth_2:
huffman@44133
   247
  fixes x :: "'a::real_normed_vector"
huffman@44133
   248
  shows "x - y == x + -y" by (atomize (full)) simp
huffman@44133
   249
huffman@44133
   250
lemma pth_3:
huffman@44133
   251
  fixes x :: "'a::real_normed_vector"
huffman@44133
   252
  shows "- x == scaleR (-1) x" by simp
huffman@44133
   253
huffman@44133
   254
lemma pth_4:
huffman@44133
   255
  fixes x :: "'a::real_normed_vector"
huffman@44133
   256
  shows "scaleR 0 x == 0" and "scaleR c 0 = (0::'a)" by simp_all
huffman@44133
   257
huffman@44133
   258
lemma pth_5:
huffman@44133
   259
  fixes x :: "'a::real_normed_vector"
huffman@44133
   260
  shows "scaleR c (scaleR d x) == scaleR (c * d) x" by simp
huffman@44133
   261
huffman@44133
   262
lemma pth_6:
huffman@44133
   263
  fixes x :: "'a::real_normed_vector"
huffman@44133
   264
  shows "scaleR c (x + y) == scaleR c x + scaleR c y"
huffman@44133
   265
  by (simp add: scaleR_right_distrib)
huffman@44133
   266
huffman@44133
   267
lemma pth_7:
huffman@44133
   268
  fixes x :: "'a::real_normed_vector"
huffman@44133
   269
  shows "0 + x == x" and "x + 0 == x" by simp_all
huffman@44133
   270
huffman@44133
   271
lemma pth_8:
huffman@44133
   272
  fixes x :: "'a::real_normed_vector"
huffman@44133
   273
  shows "scaleR c x + scaleR d x == scaleR (c + d) x"
huffman@44133
   274
  by (simp add: scaleR_left_distrib)
huffman@44133
   275
huffman@44133
   276
lemma pth_9:
huffman@44133
   277
  fixes x :: "'a::real_normed_vector" shows
huffman@44133
   278
  "(scaleR c x + z) + scaleR d x == scaleR (c + d) x + z"
huffman@44133
   279
  "scaleR c x + (scaleR d x + z) == scaleR (c + d) x + z"
huffman@44133
   280
  "(scaleR c x + w) + (scaleR d x + z) == scaleR (c + d) x + (w + z)"
huffman@44133
   281
  by (simp_all add: algebra_simps)
huffman@44133
   282
huffman@44133
   283
lemma pth_a:
huffman@44133
   284
  fixes x :: "'a::real_normed_vector"
huffman@44133
   285
  shows "scaleR 0 x + y == y" by simp
huffman@44133
   286
huffman@44133
   287
lemma pth_b:
huffman@44133
   288
  fixes x :: "'a::real_normed_vector" shows
huffman@44133
   289
  "scaleR c x + scaleR d y == scaleR c x + scaleR d y"
huffman@44133
   290
  "(scaleR c x + z) + scaleR d y == scaleR c x + (z + scaleR d y)"
huffman@44133
   291
  "scaleR c x + (scaleR d y + z) == scaleR c x + (scaleR d y + z)"
huffman@44133
   292
  "(scaleR c x + w) + (scaleR d y + z) == scaleR c x + (w + (scaleR d y + z))"
huffman@44133
   293
  by (simp_all add: algebra_simps)
huffman@44133
   294
huffman@44133
   295
lemma pth_c:
huffman@44133
   296
  fixes x :: "'a::real_normed_vector" shows
huffman@44133
   297
  "scaleR c x + scaleR d y == scaleR d y + scaleR c x"
huffman@44133
   298
  "(scaleR c x + z) + scaleR d y == scaleR d y + (scaleR c x + z)"
huffman@44133
   299
  "scaleR c x + (scaleR d y + z) == scaleR d y + (scaleR c x + z)"
huffman@44133
   300
  "(scaleR c x + w) + (scaleR d y + z) == scaleR d y + ((scaleR c x + w) + z)"
huffman@44133
   301
  by (simp_all add: algebra_simps)
huffman@44133
   302
huffman@44133
   303
lemma pth_d:
huffman@44133
   304
  fixes x :: "'a::real_normed_vector"
huffman@44133
   305
  shows "x + 0 == x" by simp
huffman@44133
   306
huffman@44133
   307
lemma norm_imp_pos_and_ge:
huffman@44133
   308
  fixes x :: "'a::real_normed_vector"
huffman@44133
   309
  shows "norm x == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
huffman@44133
   310
  by atomize auto
huffman@44133
   311
huffman@44133
   312
lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
huffman@44133
   313
huffman@44133
   314
lemma norm_pths:
huffman@44133
   315
  fixes x :: "'a::real_normed_vector" shows
huffman@44133
   316
  "x = y \<longleftrightarrow> norm (x - y) \<le> 0"
huffman@44133
   317
  "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
huffman@44133
   318
  using norm_ge_zero[of "x - y"] by auto
huffman@44133
   319
huffman@44133
   320
use "normarith.ML"
huffman@44133
   321
huffman@44133
   322
method_setup norm = {* Scan.succeed (SIMPLE_METHOD' o NormArith.norm_arith_tac)
huffman@44133
   323
*} "prove simple linear statements about vector norms"
huffman@44133
   324
huffman@44133
   325
huffman@44133
   326
text{* Hence more metric properties. *}
huffman@44133
   327
huffman@44133
   328
lemma norm_triangle_half_r:
huffman@44133
   329
  shows "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
huffman@44133
   330
  using dist_triangle_half_r unfolding dist_norm[THEN sym] by auto
huffman@44133
   331
huffman@44133
   332
lemma norm_triangle_half_l: assumes "norm (x - y) < e / 2" "norm (x' - (y)) < e / 2" 
huffman@44133
   333
  shows "norm (x - x') < e"
huffman@44133
   334
  using dist_triangle_half_l[OF assms[unfolded dist_norm[THEN sym]]]
huffman@44133
   335
  unfolding dist_norm[THEN sym] .
huffman@44133
   336
huffman@44133
   337
lemma norm_triangle_le: "norm(x) + norm y <= e ==> norm(x + y) <= e"
huffman@44133
   338
  by (metis order_trans norm_triangle_ineq)
huffman@44133
   339
huffman@44133
   340
lemma norm_triangle_lt: "norm(x) + norm(y) < e ==> norm(x + y) < e"
huffman@44133
   341
  by (metis basic_trans_rules(21) norm_triangle_ineq)
huffman@44133
   342
huffman@44133
   343
lemma dist_triangle_add:
huffman@44133
   344
  fixes x y x' y' :: "'a::real_normed_vector"
huffman@44133
   345
  shows "dist (x + y) (x' + y') <= dist x x' + dist y y'"
huffman@44133
   346
  by norm
huffman@44133
   347
huffman@44133
   348
lemma dist_triangle_add_half:
huffman@44133
   349
  fixes x x' y y' :: "'a::real_normed_vector"
huffman@44133
   350
  shows "dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 \<Longrightarrow> dist(x + y) (x' + y') < e"
huffman@44133
   351
  by norm
huffman@44133
   352
huffman@44133
   353
lemma setsum_clauses:
huffman@44133
   354
  shows "setsum f {} = 0"
huffman@44133
   355
  and "finite S \<Longrightarrow> setsum f (insert x S) =
huffman@44133
   356
                 (if x \<in> S then setsum f S else f x + setsum f S)"
huffman@44133
   357
  by (auto simp add: insert_absorb)
huffman@44133
   358
huffman@44133
   359
lemma setsum_norm_le:
huffman@44133
   360
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@44176
   361
  assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
huffman@44133
   362
  shows "norm (setsum f S) \<le> setsum g S"
huffman@44176
   363
  by (rule order_trans [OF norm_setsum setsum_mono], simp add: fg)
huffman@44133
   364
huffman@44133
   365
lemma setsum_norm_bound:
huffman@44133
   366
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@44133
   367
  assumes fS: "finite S"
huffman@44133
   368
  and K: "\<forall>x \<in> S. norm (f x) \<le> K"
huffman@44133
   369
  shows "norm (setsum f S) \<le> of_nat (card S) * K"
huffman@44176
   370
  using setsum_norm_le[OF K] setsum_constant[symmetric]
huffman@44133
   371
  by simp
huffman@44133
   372
huffman@44133
   373
lemma setsum_group:
huffman@44133
   374
  assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
huffman@44133
   375
  shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
huffman@44133
   376
  apply (subst setsum_image_gen[OF fS, of g f])
huffman@44133
   377
  apply (rule setsum_mono_zero_right[OF fT fST])
huffman@44133
   378
  by (auto intro: setsum_0')
huffman@44133
   379
huffman@44133
   380
lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> y = setsum (\<lambda>x. f x \<bullet> y) S "
huffman@44133
   381
  apply(induct rule: finite_induct) by(auto simp add: inner_simps)
huffman@44133
   382
huffman@44133
   383
lemma dot_rsum: "finite S \<Longrightarrow> y \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
huffman@44133
   384
  apply(induct rule: finite_induct) by(auto simp add: inner_simps)
huffman@44133
   385
huffman@44133
   386
lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
huffman@44133
   387
proof
huffman@44133
   388
  assume "\<forall>x. x \<bullet> y = x \<bullet> z"
huffman@44133
   389
  hence "\<forall>x. x \<bullet> (y - z) = 0" by (simp add: inner_simps)
huffman@44133
   390
  hence "(y - z) \<bullet> (y - z) = 0" ..
huffman@44133
   391
  thus "y = z" by simp
huffman@44133
   392
qed simp
huffman@44133
   393
huffman@44133
   394
lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = y"
huffman@44133
   395
proof
huffman@44133
   396
  assume "\<forall>z. x \<bullet> z = y \<bullet> z"
huffman@44133
   397
  hence "\<forall>z. (x - y) \<bullet> z = 0" by (simp add: inner_simps)
huffman@44133
   398
  hence "(x - y) \<bullet> (x - y) = 0" ..
huffman@44133
   399
  thus "x = y" by simp
huffman@44133
   400
qed simp
huffman@44133
   401
huffman@44133
   402
subsection{* Orthogonality. *}
huffman@44133
   403
huffman@44133
   404
context real_inner
huffman@44133
   405
begin
huffman@44133
   406
huffman@44133
   407
definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
huffman@44133
   408
huffman@44133
   409
lemma orthogonal_clauses:
huffman@44133
   410
  "orthogonal a 0"
huffman@44133
   411
  "orthogonal a x \<Longrightarrow> orthogonal a (c *\<^sub>R x)"
huffman@44133
   412
  "orthogonal a x \<Longrightarrow> orthogonal a (-x)"
huffman@44133
   413
  "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x + y)"
huffman@44133
   414
  "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x - y)"
huffman@44133
   415
  "orthogonal 0 a"
huffman@44133
   416
  "orthogonal x a \<Longrightarrow> orthogonal (c *\<^sub>R x) a"
huffman@44133
   417
  "orthogonal x a \<Longrightarrow> orthogonal (-x) a"
huffman@44133
   418
  "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x + y) a"
huffman@44133
   419
  "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x - y) a"
huffman@44133
   420
  unfolding orthogonal_def inner_simps inner_add_left inner_add_right inner_diff_left inner_diff_right (*FIXME*) by auto
huffman@44133
   421
 
huffman@44133
   422
end
huffman@44133
   423
huffman@44133
   424
lemma orthogonal_commute: "orthogonal x y \<longleftrightarrow> orthogonal y x"
huffman@44133
   425
  by (simp add: orthogonal_def inner_commute)
huffman@44133
   426
huffman@44133
   427
subsection{* Linear functions. *}
huffman@44133
   428
huffman@44133
   429
definition
huffman@44133
   430
  linear :: "('a::real_vector \<Rightarrow> 'b::real_vector) \<Rightarrow> bool" where
huffman@44133
   431
  "linear f \<longleftrightarrow> (\<forall>x y. f(x + y) = f x + f y) \<and> (\<forall>c x. f(c *\<^sub>R x) = c *\<^sub>R f x)"
huffman@44133
   432
huffman@44133
   433
lemma linearI: assumes "\<And>x y. f (x + y) = f x + f y" "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
huffman@44133
   434
  shows "linear f" using assms unfolding linear_def by auto
huffman@44133
   435
huffman@44133
   436
lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. c *\<^sub>R f x)"
huffman@44133
   437
  by (simp add: linear_def algebra_simps)
huffman@44133
   438
huffman@44133
   439
lemma linear_compose_neg: "linear f ==> linear (\<lambda>x. -(f(x)))"
huffman@44133
   440
  by (simp add: linear_def)
huffman@44133
   441
huffman@44133
   442
lemma linear_compose_add: "linear f \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
huffman@44133
   443
  by (simp add: linear_def algebra_simps)
huffman@44133
   444
huffman@44133
   445
lemma linear_compose_sub: "linear f \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
huffman@44133
   446
  by (simp add: linear_def algebra_simps)
huffman@44133
   447
huffman@44133
   448
lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
huffman@44133
   449
  by (simp add: linear_def)
huffman@44133
   450
huffman@44133
   451
lemma linear_id: "linear id" by (simp add: linear_def id_def)
huffman@44133
   452
huffman@44133
   453
lemma linear_zero: "linear (\<lambda>x. 0)" by (simp add: linear_def)
huffman@44133
   454
huffman@44133
   455
lemma linear_compose_setsum:
huffman@44133
   456
  assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a)"
huffman@44133
   457
  shows "linear(\<lambda>x. setsum (\<lambda>a. f a x) S)"
huffman@44133
   458
  using lS
huffman@44133
   459
  apply (induct rule: finite_induct[OF fS])
huffman@44133
   460
  by (auto simp add: linear_zero intro: linear_compose_add)
huffman@44133
   461
huffman@44133
   462
lemma linear_0: "linear f \<Longrightarrow> f 0 = 0"
huffman@44133
   463
  unfolding linear_def
huffman@44133
   464
  apply clarsimp
huffman@44133
   465
  apply (erule allE[where x="0::'a"])
huffman@44133
   466
  apply simp
huffman@44133
   467
  done
huffman@44133
   468
huffman@44133
   469
lemma linear_cmul: "linear f ==> f(c *\<^sub>R x) = c *\<^sub>R f x" by (simp add: linear_def)
huffman@44133
   470
huffman@44133
   471
lemma linear_neg: "linear f ==> f (-x) = - f x"
huffman@44133
   472
  using linear_cmul [where c="-1"] by simp
huffman@44133
   473
huffman@44133
   474
lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def)
huffman@44133
   475
huffman@44133
   476
lemma linear_sub: "linear f ==> f(x - y) = f x - f y"
huffman@44133
   477
  by (simp add: diff_minus linear_add linear_neg)
huffman@44133
   478
huffman@44133
   479
lemma linear_setsum:
huffman@44133
   480
  assumes lf: "linear f" and fS: "finite S"
huffman@44133
   481
  shows "f (setsum g S) = setsum (f o g) S"
huffman@44133
   482
proof (induct rule: finite_induct[OF fS])
huffman@44133
   483
  case 1 thus ?case by (simp add: linear_0[OF lf])
huffman@44133
   484
next
huffman@44133
   485
  case (2 x F)
huffman@44133
   486
  have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
huffman@44133
   487
    by simp
huffman@44133
   488
  also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
huffman@44133
   489
  also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
huffman@44133
   490
  finally show ?case .
huffman@44133
   491
qed
huffman@44133
   492
huffman@44133
   493
lemma linear_setsum_mul:
huffman@44133
   494
  assumes lf: "linear f" and fS: "finite S"
huffman@44133
   495
  shows "f (setsum (\<lambda>i. c i *\<^sub>R v i) S) = setsum (\<lambda>i. c i *\<^sub>R f (v i)) S"
huffman@44133
   496
  using linear_setsum[OF lf fS, of "\<lambda>i. c i *\<^sub>R v i" , unfolded o_def]
huffman@44133
   497
  linear_cmul[OF lf] by simp
huffman@44133
   498
huffman@44133
   499
lemma linear_injective_0:
huffman@44133
   500
  assumes lf: "linear f"
huffman@44133
   501
  shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
huffman@44133
   502
proof-
huffman@44133
   503
  have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
huffman@44133
   504
  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
huffman@44133
   505
  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
huffman@44133
   506
    by (simp add: linear_sub[OF lf])
huffman@44133
   507
  also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
huffman@44133
   508
  finally show ?thesis .
huffman@44133
   509
qed
huffman@44133
   510
huffman@44133
   511
subsection{* Bilinear functions. *}
huffman@44133
   512
huffman@44133
   513
definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
huffman@44133
   514
huffman@44133
   515
lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
huffman@44133
   516
  by (simp add: bilinear_def linear_def)
huffman@44133
   517
lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
huffman@44133
   518
  by (simp add: bilinear_def linear_def)
huffman@44133
   519
huffman@44133
   520
lemma bilinear_lmul: "bilinear h ==> h (c *\<^sub>R x) y = c *\<^sub>R (h x y)"
huffman@44133
   521
  by (simp add: bilinear_def linear_def)
huffman@44133
   522
huffman@44133
   523
lemma bilinear_rmul: "bilinear h ==> h x (c *\<^sub>R y) = c *\<^sub>R (h x y)"
huffman@44133
   524
  by (simp add: bilinear_def linear_def)
huffman@44133
   525
huffman@44133
   526
lemma bilinear_lneg: "bilinear h ==> h (- x) y = -(h x y)"
huffman@44133
   527
  by (simp only: scaleR_minus1_left [symmetric] bilinear_lmul)
huffman@44133
   528
huffman@44133
   529
lemma bilinear_rneg: "bilinear h ==> h x (- y) = - h x y"
huffman@44133
   530
  by (simp only: scaleR_minus1_left [symmetric] bilinear_rmul)
huffman@44133
   531
huffman@44133
   532
lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
huffman@44133
   533
  using add_imp_eq[of x y 0] by auto
huffman@44133
   534
huffman@44133
   535
lemma bilinear_lzero:
huffman@44133
   536
  assumes bh: "bilinear h" shows "h 0 x = 0"
huffman@44133
   537
  using bilinear_ladd[OF bh, of 0 0 x]
huffman@44133
   538
    by (simp add: eq_add_iff field_simps)
huffman@44133
   539
huffman@44133
   540
lemma bilinear_rzero:
huffman@44133
   541
  assumes bh: "bilinear h" shows "h x 0 = 0"
huffman@44133
   542
  using bilinear_radd[OF bh, of x 0 0 ]
huffman@44133
   543
    by (simp add: eq_add_iff field_simps)
huffman@44133
   544
huffman@44133
   545
lemma bilinear_lsub: "bilinear h ==> h (x - y) z = h x z - h y z"
huffman@44133
   546
  by (simp  add: diff_minus bilinear_ladd bilinear_lneg)
huffman@44133
   547
huffman@44133
   548
lemma bilinear_rsub: "bilinear h ==> h z (x - y) = h z x - h z y"
huffman@44133
   549
  by (simp  add: diff_minus bilinear_radd bilinear_rneg)
huffman@44133
   550
huffman@44133
   551
lemma bilinear_setsum:
huffman@44133
   552
  assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
huffman@44133
   553
  shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
huffman@44133
   554
proof-
huffman@44133
   555
  have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
huffman@44133
   556
    apply (rule linear_setsum[unfolded o_def])
huffman@44133
   557
    using bh fS by (auto simp add: bilinear_def)
huffman@44133
   558
  also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
huffman@44133
   559
    apply (rule setsum_cong, simp)
huffman@44133
   560
    apply (rule linear_setsum[unfolded o_def])
huffman@44133
   561
    using bh fT by (auto simp add: bilinear_def)
huffman@44133
   562
  finally show ?thesis unfolding setsum_cartesian_product .
huffman@44133
   563
qed
huffman@44133
   564
huffman@44133
   565
subsection{* Adjoints. *}
huffman@44133
   566
huffman@44133
   567
definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
huffman@44133
   568
huffman@44133
   569
lemma adjoint_unique:
huffman@44133
   570
  assumes "\<forall>x y. inner (f x) y = inner x (g y)"
huffman@44133
   571
  shows "adjoint f = g"
huffman@44133
   572
unfolding adjoint_def
huffman@44133
   573
proof (rule some_equality)
huffman@44133
   574
  show "\<forall>x y. inner (f x) y = inner x (g y)" using assms .
huffman@44133
   575
next
huffman@44133
   576
  fix h assume "\<forall>x y. inner (f x) y = inner x (h y)"
huffman@44133
   577
  hence "\<forall>x y. inner x (g y) = inner x (h y)" using assms by simp
huffman@44133
   578
  hence "\<forall>x y. inner x (g y - h y) = 0" by (simp add: inner_diff_right)
huffman@44133
   579
  hence "\<forall>y. inner (g y - h y) (g y - h y) = 0" by simp
huffman@44133
   580
  hence "\<forall>y. h y = g y" by simp
huffman@44133
   581
  thus "h = g" by (simp add: ext)
huffman@44133
   582
qed
huffman@44133
   583
huffman@44133
   584
lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
huffman@44133
   585
huffman@44133
   586
subsection{* Interlude: Some properties of real sets *}
huffman@44133
   587
huffman@44133
   588
lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
huffman@44133
   589
  shows "\<forall>n \<ge> m. d n < e m"
huffman@44133
   590
  using assms apply auto
huffman@44133
   591
  apply (erule_tac x="n" in allE)
huffman@44133
   592
  apply (erule_tac x="n" in allE)
huffman@44133
   593
  apply auto
huffman@44133
   594
  done
huffman@44133
   595
huffman@44133
   596
huffman@44133
   597
lemma infinite_enumerate: assumes fS: "infinite S"
huffman@44133
   598
  shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
huffman@44133
   599
unfolding subseq_def
huffman@44133
   600
using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
huffman@44133
   601
huffman@44133
   602
lemma approachable_lt_le: "(\<exists>(d::real)>0. \<forall>x. f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> P x)"
huffman@44133
   603
apply auto
huffman@44133
   604
apply (rule_tac x="d/2" in exI)
huffman@44133
   605
apply auto
huffman@44133
   606
done
huffman@44133
   607
huffman@44133
   608
huffman@44133
   609
lemma triangle_lemma:
huffman@44133
   610
  assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
huffman@44133
   611
  shows "x <= y + z"
huffman@44133
   612
proof-
huffman@44133
   613
  have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y by (simp add: mult_nonneg_nonneg)
huffman@44133
   614
  with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square field_simps)
huffman@44133
   615
  from y z have yz: "y + z \<ge> 0" by arith
huffman@44133
   616
  from power2_le_imp_le[OF th yz] show ?thesis .
huffman@44133
   617
qed
huffman@44133
   618
huffman@44133
   619
text {* TODO: move to NthRoot *}
huffman@44133
   620
lemma sqrt_add_le_add_sqrt:
huffman@44133
   621
  assumes x: "0 \<le> x" and y: "0 \<le> y"
huffman@44133
   622
  shows "sqrt (x + y) \<le> sqrt x + sqrt y"
huffman@44133
   623
apply (rule power2_le_imp_le)
huffman@44142
   624
apply (simp add: real_sum_squared_expand x y)
huffman@44133
   625
apply (simp add: mult_nonneg_nonneg x y)
huffman@44142
   626
apply (simp add: x y)
huffman@44133
   627
done
huffman@44133
   628
huffman@44133
   629
subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
huffman@44133
   630
huffman@44170
   631
definition hull :: "('a set \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "hull" 75) where
huffman@44170
   632
  "S hull s = Inter {t. S t \<and> s \<subseteq> t}"
huffman@44170
   633
huffman@44170
   634
lemma hull_same: "S s \<Longrightarrow> S hull s = s"
huffman@44133
   635
  unfolding hull_def by auto
huffman@44133
   636
huffman@44170
   637
lemma hull_in: "(\<And>T. Ball T S ==> S (Inter T)) ==> S (S hull s)"
huffman@44170
   638
unfolding hull_def Ball_def by auto
huffman@44170
   639
huffman@44170
   640
lemma hull_eq: "(\<And>T. Ball T S ==> S (Inter T)) ==> (S hull s) = s \<longleftrightarrow> S s"
huffman@44170
   641
using hull_same[of S s] hull_in[of S s] by metis
huffman@44133
   642
huffman@44133
   643
huffman@44133
   644
lemma hull_hull: "S hull (S hull s) = S hull s"
huffman@44133
   645
  unfolding hull_def by blast
huffman@44133
   646
huffman@44133
   647
lemma hull_subset[intro]: "s \<subseteq> (S hull s)"
huffman@44133
   648
  unfolding hull_def by blast
huffman@44133
   649
huffman@44133
   650
lemma hull_mono: " s \<subseteq> t ==> (S hull s) \<subseteq> (S hull t)"
huffman@44133
   651
  unfolding hull_def by blast
huffman@44133
   652
huffman@44170
   653
lemma hull_antimono: "\<forall>x. S x \<longrightarrow> T x ==> (T hull s) \<subseteq> (S hull s)"
huffman@44133
   654
  unfolding hull_def by blast
huffman@44133
   655
huffman@44170
   656
lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> S t ==> (S hull s) \<subseteq> t"
huffman@44133
   657
  unfolding hull_def by blast
huffman@44133
   658
huffman@44170
   659
lemma subset_hull: "S t ==> S hull s \<subseteq> t \<longleftrightarrow>  s \<subseteq> t"
huffman@44133
   660
  unfolding hull_def by blast
huffman@44133
   661
huffman@44170
   662
lemma hull_unique: "s \<subseteq> t \<Longrightarrow> S t \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> S t' ==> t \<subseteq> t')
huffman@44133
   663
           ==> (S hull s = t)"
huffman@44133
   664
unfolding hull_def by auto
huffman@44133
   665
huffman@44133
   666
lemma hull_induct: "(\<And>x. x\<in> S \<Longrightarrow> P x) \<Longrightarrow> Q {x. P x} \<Longrightarrow> \<forall>x\<in> Q hull S. P x"
huffman@44133
   667
  using hull_minimal[of S "{x. P x}" Q]
huffman@44170
   668
  by (auto simp add: subset_eq)
huffman@44133
   669
huffman@44133
   670
lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S" by (metis hull_subset subset_eq)
huffman@44133
   671
huffman@44133
   672
lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
huffman@44133
   673
unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
huffman@44133
   674
huffman@44170
   675
lemma hull_union: assumes T: "\<And>T. Ball T S ==> S (Inter T)"
huffman@44133
   676
  shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
huffman@44133
   677
apply rule
huffman@44133
   678
apply (rule hull_mono)
huffman@44133
   679
unfolding Un_subset_iff
huffman@44133
   680
apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
huffman@44133
   681
apply (rule hull_minimal)
huffman@44133
   682
apply (metis hull_union_subset)
huffman@44133
   683
apply (metis hull_in T)
huffman@44133
   684
done
huffman@44133
   685
huffman@44133
   686
lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
huffman@44133
   687
  unfolding hull_def by blast
huffman@44133
   688
huffman@44133
   689
lemma hull_redundant: "a \<in> (S hull s) ==> (S hull (insert a s) = S hull s)"
huffman@44133
   690
by (metis hull_redundant_eq)
huffman@44133
   691
huffman@44133
   692
text{* Archimedian properties and useful consequences. *}
huffman@44133
   693
huffman@44133
   694
lemma real_arch_simple: "\<exists>n. x <= real (n::nat)"
huffman@44133
   695
  using reals_Archimedean2[of x] apply auto by (rule_tac x="Suc n" in exI, auto)
huffman@44133
   696
lemmas real_arch_lt = reals_Archimedean2
huffman@44133
   697
huffman@44133
   698
lemmas real_arch = reals_Archimedean3
huffman@44133
   699
huffman@44133
   700
lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
huffman@44133
   701
  using reals_Archimedean
huffman@44133
   702
  apply (auto simp add: field_simps)
huffman@44133
   703
  apply (subgoal_tac "inverse (real n) > 0")
huffman@44133
   704
  apply arith
huffman@44133
   705
  apply simp
huffman@44133
   706
  done
huffman@44133
   707
huffman@44133
   708
lemma real_pow_lbound: "0 <= x ==> 1 + real n * x <= (1 + x) ^ n"
huffman@44133
   709
proof(induct n)
huffman@44133
   710
  case 0 thus ?case by simp
huffman@44133
   711
next
huffman@44133
   712
  case (Suc n)
huffman@44133
   713
  hence h: "1 + real n * x \<le> (1 + x) ^ n" by simp
huffman@44133
   714
  from h have p: "1 \<le> (1 + x) ^ n" using Suc.prems by simp
huffman@44133
   715
  from h have "1 + real n * x + x \<le> (1 + x) ^ n + x" by simp
huffman@44133
   716
  also have "\<dots> \<le> (1 + x) ^ Suc n" apply (subst diff_le_0_iff_le[symmetric])
huffman@44133
   717
    apply (simp add: field_simps)
huffman@44133
   718
    using mult_left_mono[OF p Suc.prems] by simp
huffman@44133
   719
  finally show ?case  by (simp add: real_of_nat_Suc field_simps)
huffman@44133
   720
qed
huffman@44133
   721
huffman@44133
   722
lemma real_arch_pow: assumes x: "1 < (x::real)" shows "\<exists>n. y < x^n"
huffman@44133
   723
proof-
huffman@44133
   724
  from x have x0: "x - 1 > 0" by arith
huffman@44133
   725
  from real_arch[OF x0, rule_format, of y]
huffman@44133
   726
  obtain n::nat where n:"y < real n * (x - 1)" by metis
huffman@44133
   727
  from x0 have x00: "x- 1 \<ge> 0" by arith
huffman@44133
   728
  from real_pow_lbound[OF x00, of n] n
huffman@44133
   729
  have "y < x^n" by auto
huffman@44133
   730
  then show ?thesis by metis
huffman@44133
   731
qed
huffman@44133
   732
huffman@44133
   733
lemma real_arch_pow2: "\<exists>n. (x::real) < 2^ n"
huffman@44133
   734
  using real_arch_pow[of 2 x] by simp
huffman@44133
   735
huffman@44133
   736
lemma real_arch_pow_inv: assumes y: "(y::real) > 0" and x1: "x < 1"
huffman@44133
   737
  shows "\<exists>n. x^n < y"
huffman@44133
   738
proof-
huffman@44133
   739
  {assume x0: "x > 0"
huffman@44133
   740
    from x0 x1 have ix: "1 < 1/x" by (simp add: field_simps)
huffman@44133
   741
    from real_arch_pow[OF ix, of "1/y"]
huffman@44133
   742
    obtain n where n: "1/y < (1/x)^n" by blast
huffman@44133
   743
    then
huffman@44133
   744
    have ?thesis using y x0 by (auto simp add: field_simps power_divide) }
huffman@44133
   745
  moreover
huffman@44133
   746
  {assume "\<not> x > 0" with y x1 have ?thesis apply auto by (rule exI[where x=1], auto)}
huffman@44133
   747
  ultimately show ?thesis by metis
huffman@44133
   748
qed
huffman@44133
   749
huffman@44133
   750
lemma forall_pos_mono: "(\<And>d e::real. d < e \<Longrightarrow> P d ==> P e) \<Longrightarrow> (\<And>n::nat. n \<noteq> 0 ==> P(inverse(real n))) \<Longrightarrow> (\<And>e. 0 < e ==> P e)"
huffman@44133
   751
  by (metis real_arch_inv)
huffman@44133
   752
huffman@44133
   753
lemma forall_pos_mono_1: "(\<And>d e::real. d < e \<Longrightarrow> P d ==> P e) \<Longrightarrow> (\<And>n. P(inverse(real (Suc n)))) ==> 0 < e ==> P e"
huffman@44133
   754
  apply (rule forall_pos_mono)
huffman@44133
   755
  apply auto
huffman@44133
   756
  apply (atomize)
huffman@44133
   757
  apply (erule_tac x="n - 1" in allE)
huffman@44133
   758
  apply auto
huffman@44133
   759
  done
huffman@44133
   760
huffman@44133
   761
lemma real_archimedian_rdiv_eq_0: assumes x0: "x \<ge> 0" and c: "c \<ge> 0" and xc: "\<forall>(m::nat)>0. real m * x \<le> c"
huffman@44133
   762
  shows "x = 0"
huffman@44133
   763
proof-
huffman@44133
   764
  {assume "x \<noteq> 0" with x0 have xp: "x > 0" by arith
huffman@44133
   765
    from real_arch[OF xp, rule_format, of c] obtain n::nat where n: "c < real n * x"  by blast
huffman@44133
   766
    with xc[rule_format, of n] have "n = 0" by arith
huffman@44133
   767
    with n c have False by simp}
huffman@44133
   768
  then show ?thesis by blast
huffman@44133
   769
qed
huffman@44133
   770
huffman@44133
   771
subsection {* Geometric progression *}
huffman@44133
   772
huffman@44133
   773
lemma sum_gp_basic: "((1::'a::{field}) - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
huffman@44133
   774
  (is "?lhs = ?rhs")
huffman@44133
   775
proof-
huffman@44133
   776
  {assume x1: "x = 1" hence ?thesis by simp}
huffman@44133
   777
  moreover
huffman@44133
   778
  {assume x1: "x\<noteq>1"
huffman@44133
   779
    hence x1': "x - 1 \<noteq> 0" "1 - x \<noteq> 0" "x - 1 = - (1 - x)" "- (1 - x) \<noteq> 0" by auto
huffman@44133
   780
    from geometric_sum[OF x1, of "Suc n", unfolded x1']
huffman@44133
   781
    have "(- (1 - x)) * setsum (\<lambda>i. x^i) {0 .. n} = - (1 - x^(Suc n))"
huffman@44133
   782
      unfolding atLeastLessThanSuc_atLeastAtMost
huffman@44133
   783
      using x1' apply (auto simp only: field_simps)
huffman@44133
   784
      apply (simp add: field_simps)
huffman@44133
   785
      done
huffman@44133
   786
    then have ?thesis by (simp add: field_simps) }
huffman@44133
   787
  ultimately show ?thesis by metis
huffman@44133
   788
qed
huffman@44133
   789
huffman@44133
   790
lemma sum_gp_multiplied: assumes mn: "m <= n"
huffman@44133
   791
  shows "((1::'a::{field}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
huffman@44133
   792
  (is "?lhs = ?rhs")
huffman@44133
   793
proof-
huffman@44133
   794
  let ?S = "{0..(n - m)}"
huffman@44133
   795
  from mn have mn': "n - m \<ge> 0" by arith
huffman@44133
   796
  let ?f = "op + m"
huffman@44133
   797
  have i: "inj_on ?f ?S" unfolding inj_on_def by auto
huffman@44133
   798
  have f: "?f ` ?S = {m..n}"
huffman@44133
   799
    using mn apply (auto simp add: image_iff Bex_def) by arith
huffman@44133
   800
  have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)"
huffman@44133
   801
    by (rule ext, simp add: power_add power_mult)
huffman@44133
   802
  from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
huffman@44133
   803
  have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
huffman@44133
   804
  then show ?thesis unfolding sum_gp_basic using mn
huffman@44133
   805
    by (simp add: field_simps power_add[symmetric])
huffman@44133
   806
qed
huffman@44133
   807
huffman@44133
   808
lemma sum_gp: "setsum (op ^ (x::'a::{field})) {m .. n} =
huffman@44133
   809
   (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m)
huffman@44133
   810
                    else (x^ m - x^ (Suc n)) / (1 - x))"
huffman@44133
   811
proof-
huffman@44133
   812
  {assume nm: "n < m" hence ?thesis by simp}
huffman@44133
   813
  moreover
huffman@44133
   814
  {assume "\<not> n < m" hence nm: "m \<le> n" by arith
huffman@44133
   815
    {assume x: "x = 1"  hence ?thesis by simp}
huffman@44133
   816
    moreover
huffman@44133
   817
    {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
huffman@44133
   818
      from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
huffman@44133
   819
    ultimately have ?thesis by metis
huffman@44133
   820
  }
huffman@44133
   821
  ultimately show ?thesis by metis
huffman@44133
   822
qed
huffman@44133
   823
huffman@44133
   824
lemma sum_gp_offset: "setsum (op ^ (x::'a::{field})) {m .. m+n} =
huffman@44133
   825
  (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
huffman@44133
   826
  unfolding sum_gp[of x m "m + n"] power_Suc
huffman@44133
   827
  by (simp add: field_simps power_add)
huffman@44133
   828
huffman@44133
   829
huffman@44133
   830
subsection{* A bit of linear algebra. *}
huffman@44133
   831
huffman@44133
   832
definition (in real_vector)
huffman@44133
   833
  subspace :: "'a set \<Rightarrow> bool" where
huffman@44133
   834
  "subspace S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>x\<in> S. \<forall>y \<in>S. x + y \<in> S) \<and> (\<forall>c. \<forall>x \<in>S. c *\<^sub>R x \<in>S )"
huffman@44133
   835
huffman@44133
   836
definition (in real_vector) "span S = (subspace hull S)"
huffman@44133
   837
definition (in real_vector) "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span(S - {a}))"
huffman@44133
   838
abbreviation (in real_vector) "independent s == ~(dependent s)"
huffman@44133
   839
huffman@44133
   840
text {* Closure properties of subspaces. *}
huffman@44133
   841
huffman@44133
   842
lemma subspace_UNIV[simp]: "subspace(UNIV)" by (simp add: subspace_def)
huffman@44133
   843
huffman@44133
   844
lemma (in real_vector) subspace_0: "subspace S ==> 0 \<in> S" by (metis subspace_def)
huffman@44133
   845
huffman@44133
   846
lemma (in real_vector) subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S ==> x + y \<in> S"
huffman@44133
   847
  by (metis subspace_def)
huffman@44133
   848
huffman@44133
   849
lemma (in real_vector) subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *\<^sub>R x \<in> S"
huffman@44133
   850
  by (metis subspace_def)
huffman@44133
   851
huffman@44133
   852
lemma subspace_neg: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> - x \<in> S"
huffman@44133
   853
  by (metis scaleR_minus1_left subspace_mul)
huffman@44133
   854
huffman@44133
   855
lemma subspace_sub: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
huffman@44133
   856
  by (metis diff_minus subspace_add subspace_neg)
huffman@44133
   857
huffman@44133
   858
lemma (in real_vector) subspace_setsum:
huffman@44133
   859
  assumes sA: "subspace A" and fB: "finite B"
huffman@44133
   860
  and f: "\<forall>x\<in> B. f x \<in> A"
huffman@44133
   861
  shows "setsum f B \<in> A"
huffman@44133
   862
  using  fB f sA
huffman@44133
   863
  apply(induct rule: finite_induct[OF fB])
huffman@44133
   864
  by (simp add: subspace_def sA, auto simp add: sA subspace_add)
huffman@44133
   865
huffman@44133
   866
lemma subspace_linear_image:
huffman@44133
   867
  assumes lf: "linear f" and sS: "subspace S"
huffman@44133
   868
  shows "subspace(f ` S)"
huffman@44133
   869
  using lf sS linear_0[OF lf]
huffman@44133
   870
  unfolding linear_def subspace_def
huffman@44133
   871
  apply (auto simp add: image_iff)
huffman@44133
   872
  apply (rule_tac x="x + y" in bexI, auto)
huffman@44133
   873
  apply (rule_tac x="c *\<^sub>R x" in bexI, auto)
huffman@44133
   874
  done
huffman@44133
   875
huffman@44133
   876
lemma subspace_linear_preimage: "linear f ==> subspace S ==> subspace {x. f x \<in> S}"
huffman@44133
   877
  by (auto simp add: subspace_def linear_def linear_0[of f])
huffman@44133
   878
huffman@44133
   879
lemma subspace_trivial: "subspace {0}"
huffman@44133
   880
  by (simp add: subspace_def)
huffman@44133
   881
huffman@44133
   882
lemma (in real_vector) subspace_inter: "subspace A \<Longrightarrow> subspace B ==> subspace (A \<inter> B)"
huffman@44133
   883
  by (simp add: subspace_def)
huffman@44133
   884
huffman@44133
   885
lemma (in real_vector) span_mono: "A \<subseteq> B ==> span A \<subseteq> span B"
huffman@44133
   886
  by (metis span_def hull_mono)
huffman@44133
   887
huffman@44133
   888
lemma (in real_vector) subspace_span: "subspace(span S)"
huffman@44133
   889
  unfolding span_def
huffman@44170
   890
  apply (rule hull_in)
huffman@44133
   891
  apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
huffman@44133
   892
  apply auto
huffman@44133
   893
  done
huffman@44133
   894
huffman@44133
   895
lemma (in real_vector) span_clauses:
huffman@44133
   896
  "a \<in> S ==> a \<in> span S"
huffman@44133
   897
  "0 \<in> span S"
huffman@44133
   898
  "x\<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
huffman@44133
   899
  "x \<in> span S \<Longrightarrow> c *\<^sub>R x \<in> span S"
huffman@44133
   900
  by (metis span_def hull_subset subset_eq)
huffman@44133
   901
     (metis subspace_span subspace_def)+
huffman@44133
   902
huffman@44170
   903
lemma (in real_vector) span_induct: assumes SP: "\<And>x. x \<in> S ==> x \<in> P"
huffman@44170
   904
  and P: "subspace P" and x: "x \<in> span S" shows "x \<in> P"
huffman@44133
   905
proof-
huffman@44170
   906
  from SP have SP': "S \<subseteq> P" by (simp add: subset_eq)
huffman@44170
   907
  from x hull_minimal[where S=subspace, OF SP' P, unfolded span_def[symmetric]]
huffman@44170
   908
  show "x \<in> P" by (metis subset_eq)
huffman@44133
   909
qed
huffman@44133
   910
huffman@44133
   911
lemma span_empty[simp]: "span {} = {0}"
huffman@44133
   912
  apply (simp add: span_def)
huffman@44133
   913
  apply (rule hull_unique)
huffman@44170
   914
  apply (auto simp add: subspace_def)
huffman@44133
   915
  done
huffman@44133
   916
huffman@44133
   917
lemma (in real_vector) independent_empty[intro]: "independent {}"
huffman@44133
   918
  by (simp add: dependent_def)
huffman@44133
   919
huffman@44133
   920
lemma dependent_single[simp]:
huffman@44133
   921
  "dependent {x} \<longleftrightarrow> x = 0"
huffman@44133
   922
  unfolding dependent_def by auto
huffman@44133
   923
huffman@44133
   924
lemma (in real_vector) independent_mono: "independent A \<Longrightarrow> B \<subseteq> A ==> independent B"
huffman@44133
   925
  apply (clarsimp simp add: dependent_def span_mono)
huffman@44133
   926
  apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
huffman@44133
   927
  apply force
huffman@44133
   928
  apply (rule span_mono)
huffman@44133
   929
  apply auto
huffman@44133
   930
  done
huffman@44133
   931
huffman@44133
   932
lemma (in real_vector) span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
huffman@44170
   933
  by (metis order_antisym span_def hull_minimal)
huffman@44133
   934
huffman@44133
   935
lemma (in real_vector) span_induct': assumes SP: "\<forall>x \<in> S. P x"
huffman@44170
   936
  and P: "subspace {x. P x}" shows "\<forall>x \<in> span S. P x"
huffman@44133
   937
  using span_induct SP P by blast
huffman@44133
   938
huffman@44170
   939
inductive_set (in real_vector) span_induct_alt_help for S:: "'a set"
huffman@44133
   940
  where
huffman@44170
   941
  span_induct_alt_help_0: "0 \<in> span_induct_alt_help S"
huffman@44170
   942
  | span_induct_alt_help_S: "x \<in> S \<Longrightarrow> z \<in> span_induct_alt_help S \<Longrightarrow> (c *\<^sub>R x + z) \<in> span_induct_alt_help S"
huffman@44133
   943
huffman@44133
   944
lemma span_induct_alt':
huffman@44133
   945
  assumes h0: "h 0" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)" shows "\<forall>x \<in> span S. h x"
huffman@44133
   946
proof-
huffman@44170
   947
  {fix x:: "'a" assume x: "x \<in> span_induct_alt_help S"
huffman@44133
   948
    have "h x"
huffman@44133
   949
      apply (rule span_induct_alt_help.induct[OF x])
huffman@44133
   950
      apply (rule h0)
huffman@44133
   951
      apply (rule hS, assumption, assumption)
huffman@44133
   952
      done}
huffman@44133
   953
  note th0 = this
huffman@44133
   954
  {fix x assume x: "x \<in> span S"
huffman@44133
   955
huffman@44170
   956
    have "x \<in> span_induct_alt_help S"
huffman@44133
   957
      proof(rule span_induct[where x=x and S=S])
huffman@44133
   958
        show "x \<in> span S" using x .
huffman@44133
   959
      next
huffman@44133
   960
        fix x assume xS : "x \<in> S"
huffman@44133
   961
          from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
huffman@44170
   962
          show "x \<in> span_induct_alt_help S" by simp
huffman@44133
   963
        next
huffman@44170
   964
        have "0 \<in> span_induct_alt_help S" by (rule span_induct_alt_help_0)
huffman@44133
   965
        moreover
huffman@44170
   966
        {fix x y assume h: "x \<in> span_induct_alt_help S" "y \<in> span_induct_alt_help S"
huffman@44133
   967
          from h
huffman@44170
   968
          have "(x + y) \<in> span_induct_alt_help S"
huffman@44133
   969
            apply (induct rule: span_induct_alt_help.induct)
huffman@44133
   970
            apply simp
huffman@44133
   971
            unfolding add_assoc
huffman@44133
   972
            apply (rule span_induct_alt_help_S)
huffman@44133
   973
            apply assumption
huffman@44133
   974
            apply simp
huffman@44133
   975
            done}
huffman@44133
   976
        moreover
huffman@44170
   977
        {fix c x assume xt: "x \<in> span_induct_alt_help S"
huffman@44170
   978
          then have "(c *\<^sub>R x) \<in> span_induct_alt_help S"
huffman@44133
   979
            apply (induct rule: span_induct_alt_help.induct)
huffman@44133
   980
            apply (simp add: span_induct_alt_help_0)
huffman@44133
   981
            apply (simp add: scaleR_right_distrib)
huffman@44133
   982
            apply (rule span_induct_alt_help_S)
huffman@44133
   983
            apply assumption
huffman@44133
   984
            apply simp
huffman@44133
   985
            done
huffman@44133
   986
        }
huffman@44133
   987
        ultimately show "subspace (span_induct_alt_help S)"
huffman@44170
   988
          unfolding subspace_def Ball_def by blast
huffman@44133
   989
      qed}
huffman@44133
   990
  with th0 show ?thesis by blast
huffman@44133
   991
qed
huffman@44133
   992
huffman@44133
   993
lemma span_induct_alt:
huffman@44133
   994
  assumes h0: "h 0" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)" and x: "x \<in> span S"
huffman@44133
   995
  shows "h x"
huffman@44133
   996
using span_induct_alt'[of h S] h0 hS x by blast
huffman@44133
   997
huffman@44133
   998
text {* Individual closure properties. *}
huffman@44133
   999
huffman@44133
  1000
lemma span_span: "span (span A) = span A"
huffman@44133
  1001
  unfolding span_def hull_hull ..
huffman@44133
  1002
huffman@44133
  1003
lemma (in real_vector) span_superset: "x \<in> S ==> x \<in> span S" by (metis span_clauses(1))
huffman@44133
  1004
huffman@44133
  1005
lemma (in real_vector) span_0: "0 \<in> span S" by (metis subspace_span subspace_0)
huffman@44133
  1006
huffman@44133
  1007
lemma span_inc: "S \<subseteq> span S"
huffman@44133
  1008
  by (metis subset_eq span_superset)
huffman@44133
  1009
huffman@44133
  1010
lemma (in real_vector) dependent_0: assumes "0\<in>A" shows "dependent A"
huffman@44133
  1011
  unfolding dependent_def apply(rule_tac x=0 in bexI)
huffman@44133
  1012
  using assms span_0 by auto
huffman@44133
  1013
huffman@44133
  1014
lemma (in real_vector) span_add: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
huffman@44133
  1015
  by (metis subspace_add subspace_span)
huffman@44133
  1016
huffman@44133
  1017
lemma (in real_vector) span_mul: "x \<in> span S ==> (c *\<^sub>R x) \<in> span S"
huffman@44133
  1018
  by (metis subspace_span subspace_mul)
huffman@44133
  1019
huffman@44133
  1020
lemma span_neg: "x \<in> span S ==> - x \<in> span S"
huffman@44133
  1021
  by (metis subspace_neg subspace_span)
huffman@44133
  1022
huffman@44133
  1023
lemma span_sub: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x - y \<in> span S"
huffman@44133
  1024
  by (metis subspace_span subspace_sub)
huffman@44133
  1025
huffman@44133
  1026
lemma (in real_vector) span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S ==> setsum f A \<in> span S"
huffman@44133
  1027
  by (rule subspace_setsum, rule subspace_span)
huffman@44133
  1028
huffman@44133
  1029
lemma span_add_eq: "x \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
huffman@44133
  1030
  apply (auto simp only: span_add span_sub)
huffman@44133
  1031
  apply (subgoal_tac "(x + y) - x \<in> span S", simp)
huffman@44133
  1032
  by (simp only: span_add span_sub)
huffman@44133
  1033
huffman@44133
  1034
text {* Mapping under linear image. *}
huffman@44133
  1035
huffman@44133
  1036
lemma span_linear_image: assumes lf: "linear f"
huffman@44133
  1037
  shows "span (f ` S) = f ` (span S)"
huffman@44133
  1038
proof-
huffman@44133
  1039
  {fix x
huffman@44133
  1040
    assume x: "x \<in> span (f ` S)"
huffman@44133
  1041
    have "x \<in> f ` span S"
huffman@44133
  1042
      apply (rule span_induct[where x=x and S = "f ` S"])
huffman@44133
  1043
      apply (clarsimp simp add: image_iff)
huffman@44133
  1044
      apply (frule span_superset)
huffman@44133
  1045
      apply blast
huffman@44133
  1046
      apply (rule subspace_linear_image[OF lf])
huffman@44133
  1047
      apply (rule subspace_span)
huffman@44133
  1048
      apply (rule x)
huffman@44133
  1049
      done}
huffman@44133
  1050
  moreover
huffman@44133
  1051
  {fix x assume x: "x \<in> span S"
huffman@44170
  1052
    have "x \<in> {x. f x \<in> span (f ` S)}"
huffman@44133
  1053
      apply (rule span_induct[where S=S])
huffman@44170
  1054
      apply simp
huffman@44133
  1055
      apply (rule span_superset)
huffman@44133
  1056
      apply simp
huffman@44133
  1057
      apply (rule subspace_linear_preimage[OF lf subspace_span, of "f ` S"])
huffman@44133
  1058
      apply (rule x)
huffman@44170
  1059
      done
huffman@44170
  1060
    hence "f x \<in> span (f ` S)" by simp
huffman@44170
  1061
  }
huffman@44133
  1062
  ultimately show ?thesis by blast
huffman@44133
  1063
qed
huffman@44133
  1064
huffman@44133
  1065
text {* The key breakdown property. *}
huffman@44133
  1066
huffman@44133
  1067
lemma span_breakdown:
huffman@44133
  1068
  assumes bS: "b \<in> S" and aS: "a \<in> span S"
huffman@44133
  1069
  shows "\<exists>k. a - k *\<^sub>R b \<in> span (S - {b})" (is "?P a")
huffman@44133
  1070
proof-
huffman@44133
  1071
  {fix x assume xS: "x \<in> S"
huffman@44133
  1072
    {assume ab: "x = b"
huffman@44133
  1073
      then have "?P x"
huffman@44133
  1074
        apply simp
huffman@44133
  1075
        apply (rule exI[where x="1"], simp)
huffman@44133
  1076
        by (rule span_0)}
huffman@44133
  1077
    moreover
huffman@44133
  1078
    {assume ab: "x \<noteq> b"
huffman@44133
  1079
      then have "?P x"  using xS
huffman@44133
  1080
        apply -
huffman@44133
  1081
        apply (rule exI[where x=0])
huffman@44133
  1082
        apply (rule span_superset)
huffman@44133
  1083
        by simp}
huffman@44170
  1084
    ultimately have "x \<in> Collect ?P" by blast}
huffman@44170
  1085
  moreover have "subspace (Collect ?P)"
huffman@44133
  1086
    unfolding subspace_def
huffman@44133
  1087
    apply auto
huffman@44133
  1088
    apply (rule exI[where x=0])
huffman@44133
  1089
    using span_0[of "S - {b}"]
huffman@44170
  1090
    apply simp
huffman@44133
  1091
    apply (rule_tac x="k + ka" in exI)
huffman@44133
  1092
    apply (subgoal_tac "x + y - (k + ka) *\<^sub>R b = (x - k*\<^sub>R b) + (y - ka *\<^sub>R b)")
huffman@44133
  1093
    apply (simp only: )
huffman@44170
  1094
    apply (rule span_add)
huffman@44133
  1095
    apply assumption+
huffman@44133
  1096
    apply (simp add: algebra_simps)
huffman@44133
  1097
    apply (rule_tac x= "c*k" in exI)
huffman@44133
  1098
    apply (subgoal_tac "c *\<^sub>R x - (c * k) *\<^sub>R b = c*\<^sub>R (x - k*\<^sub>R b)")
huffman@44133
  1099
    apply (simp only: )
huffman@44170
  1100
    apply (rule span_mul)
huffman@44133
  1101
    apply assumption
huffman@44133
  1102
    by (simp add: algebra_simps)
huffman@44170
  1103
  ultimately have "a \<in> Collect ?P" using aS by (rule span_induct)
huffman@44170
  1104
  thus "?P a" by simp
huffman@44133
  1105
qed
huffman@44133
  1106
huffman@44133
  1107
lemma span_breakdown_eq:
huffman@44133
  1108
  "x \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. (x - k *\<^sub>R a) \<in> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
huffman@44133
  1109
proof-
huffman@44133
  1110
  {assume x: "x \<in> span (insert a S)"
huffman@44133
  1111
    from x span_breakdown[of "a" "insert a S" "x"]
huffman@44133
  1112
    have ?rhs apply clarsimp
huffman@44133
  1113
      apply (rule_tac x= "k" in exI)
huffman@44133
  1114
      apply (rule set_rev_mp[of _ "span (S - {a})" _])
huffman@44133
  1115
      apply assumption
huffman@44133
  1116
      apply (rule span_mono)
huffman@44133
  1117
      apply blast
huffman@44133
  1118
      done}
huffman@44133
  1119
  moreover
huffman@44133
  1120
  { fix k assume k: "x - k *\<^sub>R a \<in> span S"
huffman@44133
  1121
    have eq: "x = (x - k *\<^sub>R a) + k *\<^sub>R a" by simp
huffman@44133
  1122
    have "(x - k *\<^sub>R a) + k *\<^sub>R a \<in> span (insert a S)"
huffman@44133
  1123
      apply (rule span_add)
huffman@44133
  1124
      apply (rule set_rev_mp[of _ "span S" _])
huffman@44133
  1125
      apply (rule k)
huffman@44133
  1126
      apply (rule span_mono)
huffman@44133
  1127
      apply blast
huffman@44133
  1128
      apply (rule span_mul)
huffman@44133
  1129
      apply (rule span_superset)
huffman@44133
  1130
      apply blast
huffman@44133
  1131
      done
huffman@44133
  1132
    then have ?lhs using eq by metis}
huffman@44133
  1133
  ultimately show ?thesis by blast
huffman@44133
  1134
qed
huffman@44133
  1135
huffman@44133
  1136
text {* Hence some "reversal" results. *}
huffman@44133
  1137
huffman@44133
  1138
lemma in_span_insert:
huffman@44133
  1139
  assumes a: "a \<in> span (insert b S)" and na: "a \<notin> span S"
huffman@44133
  1140
  shows "b \<in> span (insert a S)"
huffman@44133
  1141
proof-
huffman@44133
  1142
  from span_breakdown[of b "insert b S" a, OF insertI1 a]
huffman@44133
  1143
  obtain k where k: "a - k*\<^sub>R b \<in> span (S - {b})" by auto
huffman@44133
  1144
  {assume k0: "k = 0"
huffman@44133
  1145
    with k have "a \<in> span S"
huffman@44133
  1146
      apply (simp)
huffman@44133
  1147
      apply (rule set_rev_mp)
huffman@44133
  1148
      apply assumption
huffman@44133
  1149
      apply (rule span_mono)
huffman@44133
  1150
      apply blast
huffman@44133
  1151
      done
huffman@44133
  1152
    with na  have ?thesis by blast}
huffman@44133
  1153
  moreover
huffman@44133
  1154
  {assume k0: "k \<noteq> 0"
huffman@44133
  1155
    have eq: "b = (1/k) *\<^sub>R a - ((1/k) *\<^sub>R a - b)" by simp
huffman@44133
  1156
    from k0 have eq': "(1/k) *\<^sub>R (a - k*\<^sub>R b) = (1/k) *\<^sub>R a - b"
huffman@44133
  1157
      by (simp add: algebra_simps)
huffman@44133
  1158
    from k have "(1/k) *\<^sub>R (a - k*\<^sub>R b) \<in> span (S - {b})"
huffman@44133
  1159
      by (rule span_mul)
huffman@44133
  1160
    hence th: "(1/k) *\<^sub>R a - b \<in> span (S - {b})"
huffman@44133
  1161
      unfolding eq' .
huffman@44133
  1162
huffman@44133
  1163
    from k
huffman@44133
  1164
    have ?thesis
huffman@44133
  1165
      apply (subst eq)
huffman@44133
  1166
      apply (rule span_sub)
huffman@44133
  1167
      apply (rule span_mul)
huffman@44133
  1168
      apply (rule span_superset)
huffman@44133
  1169
      apply blast
huffman@44133
  1170
      apply (rule set_rev_mp)
huffman@44133
  1171
      apply (rule th)
huffman@44133
  1172
      apply (rule span_mono)
huffman@44133
  1173
      using na by blast}
huffman@44133
  1174
  ultimately show ?thesis by blast
huffman@44133
  1175
qed
huffman@44133
  1176
huffman@44133
  1177
lemma in_span_delete:
huffman@44133
  1178
  assumes a: "a \<in> span S"
huffman@44133
  1179
  and na: "a \<notin> span (S-{b})"
huffman@44133
  1180
  shows "b \<in> span (insert a (S - {b}))"
huffman@44133
  1181
  apply (rule in_span_insert)
huffman@44133
  1182
  apply (rule set_rev_mp)
huffman@44133
  1183
  apply (rule a)
huffman@44133
  1184
  apply (rule span_mono)
huffman@44133
  1185
  apply blast
huffman@44133
  1186
  apply (rule na)
huffman@44133
  1187
  done
huffman@44133
  1188
huffman@44133
  1189
text {* Transitivity property. *}
huffman@44133
  1190
huffman@44133
  1191
lemma span_trans:
huffman@44133
  1192
  assumes x: "x \<in> span S" and y: "y \<in> span (insert x S)"
huffman@44133
  1193
  shows "y \<in> span S"
huffman@44133
  1194
proof-
huffman@44133
  1195
  from span_breakdown[of x "insert x S" y, OF insertI1 y]
huffman@44133
  1196
  obtain k where k: "y -k*\<^sub>R x \<in> span (S - {x})" by auto
huffman@44133
  1197
  have eq: "y = (y - k *\<^sub>R x) + k *\<^sub>R x" by simp
huffman@44133
  1198
  show ?thesis
huffman@44133
  1199
    apply (subst eq)
huffman@44133
  1200
    apply (rule span_add)
huffman@44133
  1201
    apply (rule set_rev_mp)
huffman@44133
  1202
    apply (rule k)
huffman@44133
  1203
    apply (rule span_mono)
huffman@44133
  1204
    apply blast
huffman@44133
  1205
    apply (rule span_mul)
huffman@44133
  1206
    by (rule x)
huffman@44133
  1207
qed
huffman@44133
  1208
huffman@44133
  1209
lemma span_insert_0[simp]: "span (insert 0 S) = span S"
huffman@44133
  1210
  using span_mono[of S "insert 0 S"] by (auto intro: span_trans span_0)
huffman@44133
  1211
huffman@44133
  1212
text {* An explicit expansion is sometimes needed. *}
huffman@44133
  1213
huffman@44133
  1214
lemma span_explicit:
huffman@44133
  1215
  "span P = {y. \<exists>S u. finite S \<and> S \<subseteq> P \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
huffman@44133
  1216
  (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
huffman@44133
  1217
proof-
huffman@44133
  1218
  {fix x assume x: "x \<in> ?E"
huffman@44133
  1219
    then obtain S u where fS: "finite S" and SP: "S\<subseteq>P" and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = x"
huffman@44133
  1220
      by blast
huffman@44133
  1221
    have "x \<in> span P"
huffman@44133
  1222
      unfolding u[symmetric]
huffman@44133
  1223
      apply (rule span_setsum[OF fS])
huffman@44133
  1224
      using span_mono[OF SP]
huffman@44133
  1225
      by (auto intro: span_superset span_mul)}
huffman@44133
  1226
  moreover
huffman@44133
  1227
  have "\<forall>x \<in> span P. x \<in> ?E"
huffman@44133
  1228
  proof(rule span_induct_alt')
huffman@44170
  1229
    show "0 \<in> Collect ?h"
huffman@44170
  1230
      unfolding mem_Collect_eq
huffman@44133
  1231
      apply (rule exI[where x="{}"]) by simp
huffman@44133
  1232
  next
huffman@44133
  1233
    fix c x y
huffman@44170
  1234
    assume x: "x \<in> P" and hy: "y \<in> Collect ?h"
huffman@44133
  1235
    from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P"
huffman@44133
  1236
      and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y" by blast
huffman@44133
  1237
    let ?S = "insert x S"
huffman@44133
  1238
    let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c)
huffman@44133
  1239
                  else u y"
huffman@44133
  1240
    from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P" by blast+
huffman@44133
  1241
    {assume xS: "x \<in> S"
huffman@44133
  1242
      have S1: "S = (S - {x}) \<union> {x}"
huffman@44133
  1243
        and Sss:"finite (S - {x})" "finite {x}" "(S -{x}) \<inter> {x} = {}" using xS fS by auto
huffman@44133
  1244
      have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S =(\<Sum>v\<in>S - {x}. u v *\<^sub>R v) + (u x + c) *\<^sub>R x"
huffman@44133
  1245
        using xS
huffman@44133
  1246
        by (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]]
huffman@44133
  1247
          setsum_clauses(2)[OF fS] cong del: if_weak_cong)
huffman@44133
  1248
      also have "\<dots> = (\<Sum>v\<in>S. u v *\<^sub>R v) + c *\<^sub>R x"
huffman@44133
  1249
        apply (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]])
huffman@44133
  1250
        by (simp add: algebra_simps)
huffman@44133
  1251
      also have "\<dots> = c*\<^sub>R x + y"
huffman@44133
  1252
        by (simp add: add_commute u)
huffman@44133
  1253
      finally have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = c*\<^sub>R x + y" .
huffman@44133
  1254
    then have "?Q ?S ?u (c*\<^sub>R x + y)" using th0 by blast}
huffman@44133
  1255
  moreover
huffman@44133
  1256
  {assume xS: "x \<notin> S"
huffman@44133
  1257
    have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *\<^sub>R v) = y"
huffman@44133
  1258
      unfolding u[symmetric]
huffman@44133
  1259
      apply (rule setsum_cong2)
huffman@44133
  1260
      using xS by auto
huffman@44133
  1261
    have "?Q ?S ?u (c*\<^sub>R x + y)" using fS xS th0
huffman@44133
  1262
      by (simp add: th00 setsum_clauses add_commute cong del: if_weak_cong)}
huffman@44133
  1263
  ultimately have "?Q ?S ?u (c*\<^sub>R x + y)"
huffman@44133
  1264
    by (cases "x \<in> S", simp, simp)
huffman@44170
  1265
    then show "(c*\<^sub>R x + y) \<in> Collect ?h"
huffman@44170
  1266
      unfolding mem_Collect_eq
huffman@44133
  1267
      apply -
huffman@44133
  1268
      apply (rule exI[where x="?S"])
huffman@44133
  1269
      apply (rule exI[where x="?u"]) by metis
huffman@44133
  1270
  qed
huffman@44133
  1271
  ultimately show ?thesis by blast
huffman@44133
  1272
qed
huffman@44133
  1273
huffman@44133
  1274
lemma dependent_explicit:
huffman@44133
  1275
  "dependent P \<longleftrightarrow> (\<exists>S u. finite S \<and> S \<subseteq> P \<and> (\<exists>v\<in>S. u v \<noteq> 0 \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = 0))" (is "?lhs = ?rhs")
huffman@44133
  1276
proof-
huffman@44133
  1277
  {assume dP: "dependent P"
huffman@44133
  1278
    then obtain a S u where aP: "a \<in> P" and fS: "finite S"
huffman@44133
  1279
      and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *\<^sub>R v) S = a"
huffman@44133
  1280
      unfolding dependent_def span_explicit by blast
huffman@44133
  1281
    let ?S = "insert a S"
huffman@44133
  1282
    let ?u = "\<lambda>y. if y = a then - 1 else u y"
huffman@44133
  1283
    let ?v = a
huffman@44133
  1284
    from aP SP have aS: "a \<notin> S" by blast
huffman@44133
  1285
    from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0" by auto
huffman@44133
  1286
    have s0: "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = 0"
huffman@44133
  1287
      using fS aS
huffman@44133
  1288
      apply (simp add: setsum_clauses field_simps)
huffman@44133
  1289
      apply (subst (2) ua[symmetric])
huffman@44133
  1290
      apply (rule setsum_cong2)
huffman@44133
  1291
      by auto
huffman@44133
  1292
    with th0 have ?rhs
huffman@44133
  1293
      apply -
huffman@44133
  1294
      apply (rule exI[where x= "?S"])
huffman@44133
  1295
      apply (rule exI[where x= "?u"])
huffman@44133
  1296
      by clarsimp}
huffman@44133
  1297
  moreover
huffman@44133
  1298
  {fix S u v assume fS: "finite S"
huffman@44133
  1299
      and SP: "S \<subseteq> P" and vS: "v \<in> S" and uv: "u v \<noteq> 0"
huffman@44133
  1300
    and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = 0"
huffman@44133
  1301
    let ?a = v
huffman@44133
  1302
    let ?S = "S - {v}"
huffman@44133
  1303
    let ?u = "\<lambda>i. (- u i) / u v"
huffman@44133
  1304
    have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"       using fS SP vS by auto
huffman@44133
  1305
    have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = setsum (\<lambda>v. (- (inverse (u ?a))) *\<^sub>R (u v *\<^sub>R v)) S - ?u v *\<^sub>R v"
huffman@44133
  1306
      using fS vS uv
huffman@44133
  1307
      by (simp add: setsum_diff1 divide_inverse field_simps)
huffman@44133
  1308
    also have "\<dots> = ?a"
huffman@44133
  1309
      unfolding scaleR_right.setsum [symmetric] u
huffman@44133
  1310
      using uv by simp
huffman@44133
  1311
    finally  have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = ?a" .
huffman@44133
  1312
    with th0 have ?lhs
huffman@44133
  1313
      unfolding dependent_def span_explicit
huffman@44133
  1314
      apply -
huffman@44133
  1315
      apply (rule bexI[where x= "?a"])
huffman@44133
  1316
      apply (simp_all del: scaleR_minus_left)
huffman@44133
  1317
      apply (rule exI[where x= "?S"])
huffman@44133
  1318
      by (auto simp del: scaleR_minus_left)}
huffman@44133
  1319
  ultimately show ?thesis by blast
huffman@44133
  1320
qed
huffman@44133
  1321
huffman@44133
  1322
huffman@44133
  1323
lemma span_finite:
huffman@44133
  1324
  assumes fS: "finite S"
huffman@44133
  1325
  shows "span S = {y. \<exists>u. setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
huffman@44133
  1326
  (is "_ = ?rhs")
huffman@44133
  1327
proof-
huffman@44133
  1328
  {fix y assume y: "y \<in> span S"
huffman@44133
  1329
    from y obtain S' u where fS': "finite S'" and SS': "S' \<subseteq> S" and
huffman@44133
  1330
      u: "setsum (\<lambda>v. u v *\<^sub>R v) S' = y" unfolding span_explicit by blast
huffman@44133
  1331
    let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
huffman@44133
  1332
    have "setsum (\<lambda>v. ?u v *\<^sub>R v) S = setsum (\<lambda>v. u v *\<^sub>R v) S'"
huffman@44133
  1333
      using SS' fS by (auto intro!: setsum_mono_zero_cong_right)
huffman@44133
  1334
    hence "setsum (\<lambda>v. ?u v *\<^sub>R v) S = y" by (metis u)
huffman@44133
  1335
    hence "y \<in> ?rhs" by auto}
huffman@44133
  1336
  moreover
huffman@44133
  1337
  {fix y u assume u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y"
huffman@44133
  1338
    then have "y \<in> span S" using fS unfolding span_explicit by auto}
huffman@44133
  1339
  ultimately show ?thesis by blast
huffman@44133
  1340
qed
huffman@44133
  1341
huffman@44133
  1342
lemma Int_Un_cancel: "(A \<union> B) \<inter> A = A" "(A \<union> B) \<inter> B = B" by auto
huffman@44133
  1343
huffman@44133
  1344
lemma span_union: "span (A \<union> B) = (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
huffman@44133
  1345
proof safe
huffman@44133
  1346
  fix x assume "x \<in> span (A \<union> B)"
huffman@44133
  1347
  then obtain S u where S: "finite S" "S \<subseteq> A \<union> B" and x: "x = (\<Sum>v\<in>S. u v *\<^sub>R v)"
huffman@44133
  1348
    unfolding span_explicit by auto
huffman@44133
  1349
huffman@44133
  1350
  let ?Sa = "\<Sum>v\<in>S\<inter>A. u v *\<^sub>R v"
huffman@44133
  1351
  let ?Sb = "(\<Sum>v\<in>S\<inter>(B - A). u v *\<^sub>R v)"
huffman@44133
  1352
  show "x \<in> (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
huffman@44133
  1353
  proof
huffman@44133
  1354
    show "x = (case (?Sa, ?Sb) of (a, b) \<Rightarrow> a + b)"
huffman@44133
  1355
      unfolding x using S
huffman@44133
  1356
      by (simp, subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
huffman@44133
  1357
huffman@44133
  1358
    from S have "?Sa \<in> span A" unfolding span_explicit
huffman@44133
  1359
      by (auto intro!: exI[of _ "S \<inter> A"])
huffman@44133
  1360
    moreover from S have "?Sb \<in> span B" unfolding span_explicit
huffman@44133
  1361
      by (auto intro!: exI[of _ "S \<inter> (B - A)"])
huffman@44133
  1362
    ultimately show "(?Sa, ?Sb) \<in> span A \<times> span B" by simp
huffman@44133
  1363
  qed
huffman@44133
  1364
next
huffman@44133
  1365
  fix a b assume "a \<in> span A" and "b \<in> span B"
huffman@44133
  1366
  then obtain Sa ua Sb ub where span:
huffman@44133
  1367
    "finite Sa" "Sa \<subseteq> A" "a = (\<Sum>v\<in>Sa. ua v *\<^sub>R v)"
huffman@44133
  1368
    "finite Sb" "Sb \<subseteq> B" "b = (\<Sum>v\<in>Sb. ub v *\<^sub>R v)"
huffman@44133
  1369
    unfolding span_explicit by auto
huffman@44133
  1370
  let "?u v" = "(if v \<in> Sa then ua v else 0) + (if v \<in> Sb then ub v else 0)"
huffman@44133
  1371
  from span have "finite (Sa \<union> Sb)" "Sa \<union> Sb \<subseteq> A \<union> B"
huffman@44133
  1372
    and "a + b = (\<Sum>v\<in>(Sa\<union>Sb). ?u v *\<^sub>R v)"
huffman@44133
  1373
    unfolding setsum_addf scaleR_left_distrib
huffman@44133
  1374
    by (auto simp add: if_distrib cond_application_beta setsum_cases Int_Un_cancel)
huffman@44133
  1375
  thus "a + b \<in> span (A \<union> B)"
huffman@44133
  1376
    unfolding span_explicit by (auto intro!: exI[of _ ?u])
huffman@44133
  1377
qed
huffman@44133
  1378
huffman@44133
  1379
text {* This is useful for building a basis step-by-step. *}
huffman@44133
  1380
huffman@44133
  1381
lemma independent_insert:
huffman@44133
  1382
  "independent(insert a S) \<longleftrightarrow>
huffman@44133
  1383
      (if a \<in> S then independent S
huffman@44133
  1384
                else independent S \<and> a \<notin> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
huffman@44133
  1385
proof-
huffman@44133
  1386
  {assume aS: "a \<in> S"
huffman@44133
  1387
    hence ?thesis using insert_absorb[OF aS] by simp}
huffman@44133
  1388
  moreover
huffman@44133
  1389
  {assume aS: "a \<notin> S"
huffman@44133
  1390
    {assume i: ?lhs
huffman@44133
  1391
      then have ?rhs using aS
huffman@44133
  1392
        apply simp
huffman@44133
  1393
        apply (rule conjI)
huffman@44133
  1394
        apply (rule independent_mono)
huffman@44133
  1395
        apply assumption
huffman@44133
  1396
        apply blast
huffman@44133
  1397
        by (simp add: dependent_def)}
huffman@44133
  1398
    moreover
huffman@44133
  1399
    {assume i: ?rhs
huffman@44133
  1400
      have ?lhs using i aS
huffman@44133
  1401
        apply simp
huffman@44133
  1402
        apply (auto simp add: dependent_def)
huffman@44133
  1403
        apply (case_tac "aa = a", auto)
huffman@44133
  1404
        apply (subgoal_tac "insert a S - {aa} = insert a (S - {aa})")
huffman@44133
  1405
        apply simp
huffman@44133
  1406
        apply (subgoal_tac "a \<in> span (insert aa (S - {aa}))")
huffman@44133
  1407
        apply (subgoal_tac "insert aa (S - {aa}) = S")
huffman@44133
  1408
        apply simp
huffman@44133
  1409
        apply blast
huffman@44133
  1410
        apply (rule in_span_insert)
huffman@44133
  1411
        apply assumption
huffman@44133
  1412
        apply blast
huffman@44133
  1413
        apply blast
huffman@44133
  1414
        done}
huffman@44133
  1415
    ultimately have ?thesis by blast}
huffman@44133
  1416
  ultimately show ?thesis by blast
huffman@44133
  1417
qed
huffman@44133
  1418
huffman@44133
  1419
text {* The degenerate case of the Exchange Lemma. *}
huffman@44133
  1420
huffman@44133
  1421
lemma mem_delete: "x \<in> (A - {a}) \<longleftrightarrow> x \<noteq> a \<and> x \<in> A"
huffman@44133
  1422
  by blast
huffman@44133
  1423
huffman@44133
  1424
lemma spanning_subset_independent:
huffman@44133
  1425
  assumes BA: "B \<subseteq> A" and iA: "independent A"
huffman@44133
  1426
  and AsB: "A \<subseteq> span B"
huffman@44133
  1427
  shows "A = B"
huffman@44133
  1428
proof
huffman@44133
  1429
  from BA show "B \<subseteq> A" .
huffman@44133
  1430
next
huffman@44133
  1431
  from span_mono[OF BA] span_mono[OF AsB]
huffman@44133
  1432
  have sAB: "span A = span B" unfolding span_span by blast
huffman@44133
  1433
huffman@44133
  1434
  {fix x assume x: "x \<in> A"
huffman@44133
  1435
    from iA have th0: "x \<notin> span (A - {x})"
huffman@44133
  1436
      unfolding dependent_def using x by blast
huffman@44133
  1437
    from x have xsA: "x \<in> span A" by (blast intro: span_superset)
huffman@44133
  1438
    have "A - {x} \<subseteq> A" by blast
huffman@44133
  1439
    hence th1:"span (A - {x}) \<subseteq> span A" by (metis span_mono)
huffman@44133
  1440
    {assume xB: "x \<notin> B"
huffman@44133
  1441
      from xB BA have "B \<subseteq> A -{x}" by blast
huffman@44133
  1442
      hence "span B \<subseteq> span (A - {x})" by (metis span_mono)
huffman@44133
  1443
      with th1 th0 sAB have "x \<notin> span A" by blast
huffman@44133
  1444
      with x have False by (metis span_superset)}
huffman@44133
  1445
    then have "x \<in> B" by blast}
huffman@44133
  1446
  then show "A \<subseteq> B" by blast
huffman@44133
  1447
qed
huffman@44133
  1448
huffman@44133
  1449
text {* The general case of the Exchange Lemma, the key to what follows. *}
huffman@44133
  1450
huffman@44133
  1451
lemma exchange_lemma:
huffman@44133
  1452
  assumes f:"finite t" and i: "independent s"
huffman@44133
  1453
  and sp:"s \<subseteq> span t"
huffman@44133
  1454
  shows "\<exists>t'. (card t' = card t) \<and> finite t' \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
huffman@44133
  1455
using f i sp
huffman@44133
  1456
proof(induct "card (t - s)" arbitrary: s t rule: less_induct)
huffman@44133
  1457
  case less
huffman@44133
  1458
  note ft = `finite t` and s = `independent s` and sp = `s \<subseteq> span t`
huffman@44133
  1459
  let ?P = "\<lambda>t'. (card t' = card t) \<and> finite t' \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
huffman@44133
  1460
  let ?ths = "\<exists>t'. ?P t'"
huffman@44133
  1461
  {assume st: "s \<subseteq> t"
huffman@44133
  1462
    from st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
huffman@44133
  1463
      by (auto intro: span_superset)}
huffman@44133
  1464
  moreover
huffman@44133
  1465
  {assume st: "t \<subseteq> s"
huffman@44133
  1466
huffman@44133
  1467
    from spanning_subset_independent[OF st s sp]
huffman@44133
  1468
      st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
huffman@44133
  1469
      by (auto intro: span_superset)}
huffman@44133
  1470
  moreover
huffman@44133
  1471
  {assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
huffman@44133
  1472
    from st(2) obtain b where b: "b \<in> t" "b \<notin> s" by blast
huffman@44133
  1473
      from b have "t - {b} - s \<subset> t - s" by blast
huffman@44133
  1474
      then have cardlt: "card (t - {b} - s) < card (t - s)" using ft
huffman@44133
  1475
        by (auto intro: psubset_card_mono)
huffman@44133
  1476
      from b ft have ct0: "card t \<noteq> 0" by auto
huffman@44133
  1477
    {assume stb: "s \<subseteq> span(t -{b})"
huffman@44133
  1478
      from ft have ftb: "finite (t -{b})" by auto
huffman@44133
  1479
      from less(1)[OF cardlt ftb s stb]
huffman@44133
  1480
      obtain u where u: "card u = card (t-{b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u" and fu: "finite u" by blast
huffman@44133
  1481
      let ?w = "insert b u"
huffman@44133
  1482
      have th0: "s \<subseteq> insert b u" using u by blast
huffman@44133
  1483
      from u(3) b have "u \<subseteq> s \<union> t" by blast
huffman@44133
  1484
      then have th1: "insert b u \<subseteq> s \<union> t" using u b by blast
huffman@44133
  1485
      have bu: "b \<notin> u" using b u by blast
huffman@44133
  1486
      from u(1) ft b have "card u = (card t - 1)" by auto
huffman@44133
  1487
      then
huffman@44133
  1488
      have th2: "card (insert b u) = card t"
huffman@44133
  1489
        using card_insert_disjoint[OF fu bu] ct0 by auto
huffman@44133
  1490
      from u(4) have "s \<subseteq> span u" .
huffman@44133
  1491
      also have "\<dots> \<subseteq> span (insert b u)" apply (rule span_mono) by blast
huffman@44133
  1492
      finally have th3: "s \<subseteq> span (insert b u)" .
huffman@44133
  1493
      from th0 th1 th2 th3 fu have th: "?P ?w"  by blast
huffman@44133
  1494
      from th have ?ths by blast}
huffman@44133
  1495
    moreover
huffman@44133
  1496
    {assume stb: "\<not> s \<subseteq> span(t -{b})"
huffman@44133
  1497
      from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})" by blast
huffman@44133
  1498
      have ab: "a \<noteq> b" using a b by blast
huffman@44133
  1499
      have at: "a \<notin> t" using a ab span_superset[of a "t- {b}"] by auto
huffman@44133
  1500
      have mlt: "card ((insert a (t - {b})) - s) < card (t - s)"
huffman@44133
  1501
        using cardlt ft a b by auto
huffman@44133
  1502
      have ft': "finite (insert a (t - {b}))" using ft by auto
huffman@44133
  1503
      {fix x assume xs: "x \<in> s"
huffman@44133
  1504
        have t: "t \<subseteq> (insert b (insert a (t -{b})))" using b by auto
huffman@44133
  1505
        from b(1) have "b \<in> span t" by (simp add: span_superset)
huffman@44133
  1506
        have bs: "b \<in> span (insert a (t - {b}))" apply(rule in_span_delete)
huffman@44133
  1507
          using  a sp unfolding subset_eq by auto
huffman@44133
  1508
        from xs sp have "x \<in> span t" by blast
huffman@44133
  1509
        with span_mono[OF t]
huffman@44133
  1510
        have x: "x \<in> span (insert b (insert a (t - {b})))" ..
huffman@44133
  1511
        from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))"  .}
huffman@44133
  1512
      then have sp': "s \<subseteq> span (insert a (t - {b}))" by blast
huffman@44133
  1513
huffman@44133
  1514
      from less(1)[OF mlt ft' s sp'] obtain u where
huffman@44133
  1515
        u: "card u = card (insert a (t -{b}))" "finite u" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t -{b})"
huffman@44133
  1516
        "s \<subseteq> span u" by blast
huffman@44133
  1517
      from u a b ft at ct0 have "?P u" by auto
huffman@44133
  1518
      then have ?ths by blast }
huffman@44133
  1519
    ultimately have ?ths by blast
huffman@44133
  1520
  }
huffman@44133
  1521
  ultimately
huffman@44133
  1522
  show ?ths  by blast
huffman@44133
  1523
qed
huffman@44133
  1524
huffman@44133
  1525
text {* This implies corresponding size bounds. *}
huffman@44133
  1526
huffman@44133
  1527
lemma independent_span_bound:
huffman@44133
  1528
  assumes f: "finite t" and i: "independent s" and sp:"s \<subseteq> span t"
huffman@44133
  1529
  shows "finite s \<and> card s \<le> card t"
huffman@44133
  1530
  by (metis exchange_lemma[OF f i sp] finite_subset card_mono)
huffman@44133
  1531
huffman@44133
  1532
huffman@44133
  1533
lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> (UNIV::'a::finite set)}"
huffman@44133
  1534
proof-
huffman@44133
  1535
  have eq: "{f x |x. x\<in> UNIV} = f ` UNIV" by auto
huffman@44133
  1536
  show ?thesis unfolding eq
huffman@44133
  1537
    apply (rule finite_imageI)
huffman@44133
  1538
    apply (rule finite)
huffman@44133
  1539
    done
huffman@44133
  1540
qed
huffman@44133
  1541
huffman@44133
  1542
subsection{* Euclidean Spaces as Typeclass*}
huffman@44133
  1543
huffman@44133
  1544
lemma independent_eq_inj_on:
huffman@44133
  1545
  fixes D :: nat and f :: "nat \<Rightarrow> 'c::real_vector" assumes *: "inj_on f {..<D}"
huffman@44133
  1546
  shows "independent (f ` {..<D}) \<longleftrightarrow> (\<forall>a u. a < D \<longrightarrow> (\<Sum>i\<in>{..<D}-{a}. u (f i) *\<^sub>R f i) \<noteq> f a)"
huffman@44133
  1547
proof -
huffman@44133
  1548
  from * have eq: "\<And>i. i < D \<Longrightarrow> f ` {..<D} - {f i} = f`({..<D} - {i})"
huffman@44133
  1549
    and inj: "\<And>i. inj_on f ({..<D} - {i})"
huffman@44133
  1550
    by (auto simp: inj_on_def)
huffman@44133
  1551
  have *: "\<And>i. finite (f ` {..<D} - {i})" by simp
huffman@44133
  1552
  show ?thesis unfolding dependent_def span_finite[OF *]
huffman@44133
  1553
    by (auto simp: eq setsum_reindex[OF inj])
huffman@44133
  1554
qed
huffman@44133
  1555
huffman@44133
  1556
lemma independent_basis:
huffman@44133
  1557
  "independent (basis ` {..<DIM('a)} :: 'a::euclidean_space set)"
huffman@44133
  1558
  unfolding independent_eq_inj_on [OF basis_inj]
huffman@44133
  1559
  apply clarify
huffman@44133
  1560
  apply (drule_tac f="inner (basis a)" in arg_cong)
huffman@44133
  1561
  apply (simp add: inner_right.setsum dot_basis)
huffman@44133
  1562
  done
huffman@44133
  1563
huffman@44133
  1564
lemma dimensionI:
huffman@44133
  1565
  assumes "\<And>d. \<lbrakk> 0 < d; basis ` {d..} = {0::'a::euclidean_space};
huffman@44133
  1566
    independent (basis ` {..<d} :: 'a set);
huffman@44133
  1567
    inj_on (basis :: nat \<Rightarrow> 'a) {..<d} \<rbrakk> \<Longrightarrow> P d"
huffman@44133
  1568
  shows "P DIM('a::euclidean_space)"
huffman@44133
  1569
  using DIM_positive basis_finite independent_basis basis_inj
huffman@44133
  1570
  by (rule assms)
huffman@44133
  1571
huffman@44133
  1572
lemma (in euclidean_space) dimension_eq:
huffman@44133
  1573
  assumes "\<And>i. i < d \<Longrightarrow> basis i \<noteq> 0"
huffman@44133
  1574
  assumes "\<And>i. d \<le> i \<Longrightarrow> basis i = 0"
huffman@44133
  1575
  shows "DIM('a) = d"
huffman@44133
  1576
proof (rule linorder_cases [of "DIM('a)" d])
huffman@44133
  1577
  assume "DIM('a) < d"
huffman@44133
  1578
  hence "basis DIM('a) \<noteq> 0" by (rule assms)
huffman@44133
  1579
  thus ?thesis by simp
huffman@44133
  1580
next
huffman@44133
  1581
  assume "d < DIM('a)"
huffman@44133
  1582
  hence "basis d \<noteq> 0" by simp
huffman@44133
  1583
  thus ?thesis by (simp add: assms)
huffman@44133
  1584
next
huffman@44133
  1585
  assume "DIM('a) = d" thus ?thesis .
huffman@44133
  1586
qed
huffman@44133
  1587
huffman@44133
  1588
lemma (in euclidean_space) range_basis:
huffman@44133
  1589
    "range basis = insert 0 (basis ` {..<DIM('a)})"
huffman@44133
  1590
proof -
huffman@44133
  1591
  have *: "UNIV = {..<DIM('a)} \<union> {DIM('a)..}" by auto
huffman@44133
  1592
  show ?thesis unfolding * image_Un basis_finite by auto
huffman@44133
  1593
qed
huffman@44133
  1594
huffman@44133
  1595
lemma (in euclidean_space) range_basis_finite[intro]:
huffman@44133
  1596
    "finite (range basis)"
huffman@44133
  1597
  unfolding range_basis by auto
huffman@44133
  1598
huffman@44133
  1599
lemma span_basis: "span (range basis) = (UNIV :: 'a::euclidean_space set)"
huffman@44133
  1600
proof -
huffman@44133
  1601
  { fix x :: 'a
huffman@44133
  1602
    have "(\<Sum>i<DIM('a). (x $$ i) *\<^sub>R basis i) \<in> span (range basis :: 'a set)"
huffman@44133
  1603
      by (simp add: span_setsum span_mul span_superset)
huffman@44133
  1604
    hence "x \<in> span (range basis)"
huffman@44133
  1605
      by (simp only: euclidean_representation [symmetric])
huffman@44133
  1606
  } thus ?thesis by auto
huffman@44133
  1607
qed
huffman@44133
  1608
huffman@44133
  1609
lemma basis_representation:
huffman@44133
  1610
  "\<exists>u. x = (\<Sum>v\<in>basis ` {..<DIM('a)}. u v *\<^sub>R (v\<Colon>'a\<Colon>euclidean_space))"
huffman@44133
  1611
proof -
huffman@44133
  1612
  have "x\<in>UNIV" by auto from this[unfolded span_basis[THEN sym]]
huffman@44133
  1613
  have "\<exists>u. (\<Sum>v\<in>basis ` {..<DIM('a)}. u v *\<^sub>R v) = x"
huffman@44133
  1614
    unfolding range_basis span_insert_0 apply(subst (asm) span_finite) by auto
huffman@44133
  1615
  thus ?thesis by fastsimp
huffman@44133
  1616
qed
huffman@44133
  1617
huffman@44133
  1618
lemma span_basis'[simp]:"span ((basis::nat=>'a) ` {..<DIM('a::euclidean_space)}) = UNIV"
huffman@44133
  1619
  apply(subst span_basis[symmetric]) unfolding range_basis by auto
huffman@44133
  1620
huffman@44133
  1621
lemma card_basis[simp]:"card ((basis::nat=>'a) ` {..<DIM('a::euclidean_space)}) = DIM('a)"
huffman@44133
  1622
  apply(subst card_image) using basis_inj by auto
huffman@44133
  1623
huffman@44133
  1624
lemma in_span_basis: "(x::'a::euclidean_space) \<in> span (basis ` {..<DIM('a)})"
huffman@44133
  1625
  unfolding span_basis' ..
huffman@44133
  1626
huffman@44133
  1627
lemma component_le_norm: "\<bar>x$$i\<bar> \<le> norm (x::'a::euclidean_space)"
huffman@44133
  1628
  unfolding euclidean_component_def
huffman@44133
  1629
  apply(rule order_trans[OF real_inner_class.Cauchy_Schwarz_ineq2]) by auto
huffman@44133
  1630
huffman@44133
  1631
lemma norm_bound_component_le: "norm (x::'a::euclidean_space) \<le> e \<Longrightarrow> \<bar>x$$i\<bar> <= e"
huffman@44133
  1632
  by (metis component_le_norm order_trans)
huffman@44133
  1633
huffman@44133
  1634
lemma norm_bound_component_lt: "norm (x::'a::euclidean_space) < e \<Longrightarrow> \<bar>x$$i\<bar> < e"
huffman@44133
  1635
  by (metis component_le_norm basic_trans_rules(21))
huffman@44133
  1636
huffman@44133
  1637
lemma norm_le_l1: "norm (x::'a::euclidean_space) \<le> (\<Sum>i<DIM('a). \<bar>x $$ i\<bar>)"
huffman@44133
  1638
  apply (subst euclidean_representation[of x])
huffman@44176
  1639
  apply (rule order_trans[OF norm_setsum])
huffman@44133
  1640
  by (auto intro!: setsum_mono)
huffman@44133
  1641
huffman@44133
  1642
lemma setsum_norm_allsubsets_bound:
huffman@44133
  1643
  fixes f:: "'a \<Rightarrow> 'n::euclidean_space"
huffman@44133
  1644
  assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
huffman@44133
  1645
  shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real DIM('n) *  e"
huffman@44133
  1646
proof-
huffman@44133
  1647
  let ?d = "real DIM('n)"
huffman@44133
  1648
  let ?nf = "\<lambda>x. norm (f x)"
huffman@44133
  1649
  let ?U = "{..<DIM('n)}"
huffman@44133
  1650
  have th0: "setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $$ i\<bar>) ?U) P = setsum (\<lambda>i. setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P) ?U"
huffman@44133
  1651
    by (rule setsum_commute)
huffman@44133
  1652
  have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
huffman@44133
  1653
  have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $$ i\<bar>) ?U) P"
huffman@44133
  1654
    apply (rule setsum_mono)    by (rule norm_le_l1)
huffman@44133
  1655
  also have "\<dots> \<le> 2 * ?d * e"
huffman@44133
  1656
    unfolding th0 th1
huffman@44133
  1657
  proof(rule setsum_bounded)
huffman@44133
  1658
    fix i assume i: "i \<in> ?U"
huffman@44133
  1659
    let ?Pp = "{x. x\<in> P \<and> f x $$ i \<ge> 0}"
huffman@44133
  1660
    let ?Pn = "{x. x \<in> P \<and> f x $$ i < 0}"
huffman@44133
  1661
    have thp: "P = ?Pp \<union> ?Pn" by auto
huffman@44133
  1662
    have thp0: "?Pp \<inter> ?Pn ={}" by auto
huffman@44133
  1663
    have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
huffman@44133
  1664
    have Ppe:"setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pp \<le> e"
huffman@44133
  1665
      using component_le_norm[of "setsum (\<lambda>x. f x) ?Pp" i]  fPs[OF PpP]
huffman@44133
  1666
      unfolding euclidean_component.setsum by(auto intro: abs_le_D1)
huffman@44133
  1667
    have Pne: "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pn \<le> e"
huffman@44133
  1668
      using component_le_norm[of "setsum (\<lambda>x. - f x) ?Pn" i]  fPs[OF PnP]
huffman@44133
  1669
      unfolding euclidean_component.setsum euclidean_component.minus
huffman@44133
  1670
      by(auto simp add: setsum_negf intro: abs_le_D1)
huffman@44133
  1671
    have "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P = setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pp + setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pn"
huffman@44133
  1672
      apply (subst thp)
huffman@44133
  1673
      apply (rule setsum_Un_zero)
huffman@44133
  1674
      using fP thp0 by auto
huffman@44133
  1675
    also have "\<dots> \<le> 2*e" using Pne Ppe by arith
huffman@44133
  1676
    finally show "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P \<le> 2*e" .
huffman@44133
  1677
  qed
huffman@44133
  1678
  finally show ?thesis .
huffman@44133
  1679
qed
huffman@44133
  1680
huffman@44133
  1681
lemma choice_iff': "(\<forall>x<d. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x<d. P x (f x))" by metis
huffman@44133
  1682
huffman@44133
  1683
lemma lambda_skolem': "(\<forall>i<DIM('a::euclidean_space). \<exists>x. P i x) \<longleftrightarrow>
huffman@44133
  1684
   (\<exists>x::'a. \<forall>i<DIM('a). P i (x$$i))" (is "?lhs \<longleftrightarrow> ?rhs")
huffman@44133
  1685
proof-
huffman@44133
  1686
  let ?S = "{..<DIM('a)}"
huffman@44133
  1687
  {assume H: "?rhs"
huffman@44133
  1688
    then have ?lhs by auto}
huffman@44133
  1689
  moreover
huffman@44133
  1690
  {assume H: "?lhs"
huffman@44133
  1691
    then obtain f where f:"\<forall>i<DIM('a). P i (f i)" unfolding choice_iff' by metis
huffman@44133
  1692
    let ?x = "(\<chi>\<chi> i. (f i)) :: 'a"
huffman@44133
  1693
    {fix i assume i:"i<DIM('a)"
huffman@44133
  1694
      with f have "P i (f i)" by metis
huffman@44133
  1695
      then have "P i (?x$$i)" using i by auto
huffman@44133
  1696
    }
huffman@44133
  1697
    hence "\<forall>i<DIM('a). P i (?x$$i)" by metis
huffman@44133
  1698
    hence ?rhs by metis }
huffman@44133
  1699
  ultimately show ?thesis by metis
huffman@44133
  1700
qed
huffman@44133
  1701
huffman@44133
  1702
subsection {* An ordering on euclidean spaces that will allow us to talk about intervals *}
huffman@44133
  1703
huffman@44133
  1704
class ordered_euclidean_space = ord + euclidean_space +
huffman@44133
  1705
  assumes eucl_le: "x \<le> y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i \<le> y $$ i)"
huffman@44133
  1706
  and eucl_less: "x < y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i < y $$ i)"
huffman@44133
  1707
huffman@44133
  1708
lemma eucl_less_not_refl[simp, intro!]: "\<not> x < (x::'a::ordered_euclidean_space)"
huffman@44133
  1709
  unfolding eucl_less[where 'a='a] by auto
huffman@44133
  1710
huffman@44133
  1711
lemma euclidean_trans[trans]:
huffman@44133
  1712
  fixes x y z :: "'a::ordered_euclidean_space"
huffman@44133
  1713
  shows "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
huffman@44133
  1714
  and "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
huffman@44133
  1715
  and "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
huffman@44133
  1716
  by (force simp: eucl_less[where 'a='a] eucl_le[where 'a='a])+
huffman@44133
  1717
huffman@44133
  1718
subsection {* Linearity and Bilinearity continued *}
huffman@44133
  1719
huffman@44133
  1720
lemma linear_bounded:
huffman@44133
  1721
  fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
  1722
  assumes lf: "linear f"
huffman@44133
  1723
  shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
huffman@44133
  1724
proof-
huffman@44133
  1725
  let ?S = "{..<DIM('a)}"
huffman@44133
  1726
  let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
huffman@44133
  1727
  have fS: "finite ?S" by simp
huffman@44133
  1728
  {fix x:: "'a"
huffman@44133
  1729
    let ?g = "(\<lambda> i. (x$$i) *\<^sub>R (basis i) :: 'a)"
huffman@44133
  1730
    have "norm (f x) = norm (f (setsum (\<lambda>i. (x$$i) *\<^sub>R (basis i)) ?S))"
huffman@44133
  1731
      apply(subst euclidean_representation[of x]) ..
huffman@44133
  1732
    also have "\<dots> = norm (setsum (\<lambda> i. (x$$i) *\<^sub>R f (basis i)) ?S)"
huffman@44133
  1733
      using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf] by auto
huffman@44133
  1734
    finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x$$i) *\<^sub>R f (basis i))?S)" .
huffman@44133
  1735
    {fix i assume i: "i \<in> ?S"
huffman@44133
  1736
      from component_le_norm[of x i]
huffman@44133
  1737
      have "norm ((x$$i) *\<^sub>R f (basis i :: 'a)) \<le> norm (f (basis i)) * norm x"
huffman@44133
  1738
      unfolding norm_scaleR
huffman@44133
  1739
      apply (simp only: mult_commute)
huffman@44133
  1740
      apply (rule mult_mono)
huffman@44133
  1741
      by (auto simp add: field_simps) }
huffman@44133
  1742
    then have th: "\<forall>i\<in> ?S. norm ((x$$i) *\<^sub>R f (basis i :: 'a)) \<le> norm (f (basis i)) * norm x" by metis
huffman@44176
  1743
    from setsum_norm_le[of _ "\<lambda>i. (x$$i) *\<^sub>R (f (basis i))", OF th]
huffman@44133
  1744
    have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
huffman@44133
  1745
  then show ?thesis by blast
huffman@44133
  1746
qed
huffman@44133
  1747
huffman@44133
  1748
lemma linear_bounded_pos:
huffman@44133
  1749
  fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
  1750
  assumes lf: "linear f"
huffman@44133
  1751
  shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
huffman@44133
  1752
proof-
huffman@44133
  1753
  from linear_bounded[OF lf] obtain B where
huffman@44133
  1754
    B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
huffman@44133
  1755
  let ?K = "\<bar>B\<bar> + 1"
huffman@44133
  1756
  have Kp: "?K > 0" by arith
huffman@44133
  1757
    { assume C: "B < 0"
huffman@44133
  1758
      have "((\<chi>\<chi> i. 1)::'a) \<noteq> 0" unfolding euclidean_eq[where 'a='a]
huffman@44133
  1759
        by(auto intro!:exI[where x=0] simp add:euclidean_component.zero)
huffman@44133
  1760
      hence "norm ((\<chi>\<chi> i. 1)::'a) > 0" by auto
huffman@44133
  1761
      with C have "B * norm ((\<chi>\<chi> i. 1)::'a) < 0"
huffman@44133
  1762
        by (simp add: mult_less_0_iff)
huffman@44133
  1763
      with B[rule_format, of "(\<chi>\<chi> i. 1)::'a"] norm_ge_zero[of "f ((\<chi>\<chi> i. 1)::'a)"] have False by simp
huffman@44133
  1764
    }
huffman@44133
  1765
    then have Bp: "B \<ge> 0" by (metis not_leE)
huffman@44133
  1766
    {fix x::"'a"
huffman@44133
  1767
      have "norm (f x) \<le> ?K *  norm x"
huffman@44133
  1768
      using B[rule_format, of x] norm_ge_zero[of x] norm_ge_zero[of "f x"] Bp
huffman@44133
  1769
      apply (auto simp add: field_simps split add: abs_split)
huffman@44133
  1770
      apply (erule order_trans, simp)
huffman@44133
  1771
      done
huffman@44133
  1772
  }
huffman@44133
  1773
  then show ?thesis using Kp by blast
huffman@44133
  1774
qed
huffman@44133
  1775
huffman@44133
  1776
lemma linear_conv_bounded_linear:
huffman@44133
  1777
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
  1778
  shows "linear f \<longleftrightarrow> bounded_linear f"
huffman@44133
  1779
proof
huffman@44133
  1780
  assume "linear f"
huffman@44133
  1781
  show "bounded_linear f"
huffman@44133
  1782
  proof
huffman@44133
  1783
    fix x y show "f (x + y) = f x + f y"
huffman@44133
  1784
      using `linear f` unfolding linear_def by simp
huffman@44133
  1785
  next
huffman@44133
  1786
    fix r x show "f (scaleR r x) = scaleR r (f x)"
huffman@44133
  1787
      using `linear f` unfolding linear_def by simp
huffman@44133
  1788
  next
huffman@44133
  1789
    have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
huffman@44133
  1790
      using `linear f` by (rule linear_bounded)
huffman@44133
  1791
    thus "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
huffman@44133
  1792
      by (simp add: mult_commute)
huffman@44133
  1793
  qed
huffman@44133
  1794
next
huffman@44133
  1795
  assume "bounded_linear f"
huffman@44133
  1796
  then interpret f: bounded_linear f .
huffman@44133
  1797
  show "linear f"
huffman@44133
  1798
    by (simp add: f.add f.scaleR linear_def)
huffman@44133
  1799
qed
huffman@44133
  1800
huffman@44133
  1801
lemma bounded_linearI': fixes f::"'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
  1802
  assumes "\<And>x y. f (x + y) = f x + f y" "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
huffman@44133
  1803
  shows "bounded_linear f" unfolding linear_conv_bounded_linear[THEN sym]
huffman@44133
  1804
  by(rule linearI[OF assms])
huffman@44133
  1805
huffman@44133
  1806
huffman@44133
  1807
lemma bilinear_bounded:
huffman@44133
  1808
  fixes h:: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'k::real_normed_vector"
huffman@44133
  1809
  assumes bh: "bilinear h"
huffman@44133
  1810
  shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@44133
  1811
proof-
huffman@44133
  1812
  let ?M = "{..<DIM('m)}"
huffman@44133
  1813
  let ?N = "{..<DIM('n)}"
huffman@44133
  1814
  let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
huffman@44133
  1815
  have fM: "finite ?M" and fN: "finite ?N" by simp_all
huffman@44133
  1816
  {fix x:: "'m" and  y :: "'n"
huffman@44133
  1817
    have "norm (h x y) = norm (h (setsum (\<lambda>i. (x$$i) *\<^sub>R basis i) ?M) (setsum (\<lambda>i. (y$$i) *\<^sub>R basis i) ?N))" 
huffman@44133
  1818
      apply(subst euclidean_representation[where 'a='m])
huffman@44133
  1819
      apply(subst euclidean_representation[where 'a='n]) ..
huffman@44133
  1820
    also have "\<dots> = norm (setsum (\<lambda> (i,j). h ((x$$i) *\<^sub>R basis i) ((y$$j) *\<^sub>R basis j)) (?M \<times> ?N))"  
huffman@44133
  1821
      unfolding bilinear_setsum[OF bh fM fN] ..
huffman@44133
  1822
    finally have th: "norm (h x y) = \<dots>" .
huffman@44133
  1823
    have "norm (h x y) \<le> ?B * norm x * norm y"
huffman@44133
  1824
      apply (simp add: setsum_left_distrib th)
huffman@44133
  1825
      apply (rule setsum_norm_le)
huffman@44133
  1826
      using fN fM
huffman@44133
  1827
      apply simp
huffman@44133
  1828
      apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] field_simps simp del: scaleR_scaleR)
huffman@44133
  1829
      apply (rule mult_mono)
huffman@44133
  1830
      apply (auto simp add: zero_le_mult_iff component_le_norm)
huffman@44133
  1831
      apply (rule mult_mono)
huffman@44133
  1832
      apply (auto simp add: zero_le_mult_iff component_le_norm)
huffman@44133
  1833
      done}
huffman@44133
  1834
  then show ?thesis by metis
huffman@44133
  1835
qed
huffman@44133
  1836
huffman@44133
  1837
lemma bilinear_bounded_pos:
huffman@44133
  1838
  fixes h:: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
huffman@44133
  1839
  assumes bh: "bilinear h"
huffman@44133
  1840
  shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@44133
  1841
proof-
huffman@44133
  1842
  from bilinear_bounded[OF bh] obtain B where
huffman@44133
  1843
    B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
huffman@44133
  1844
  let ?K = "\<bar>B\<bar> + 1"
huffman@44133
  1845
  have Kp: "?K > 0" by arith
huffman@44133
  1846
  have KB: "B < ?K" by arith
huffman@44133
  1847
  {fix x::'a and y::'b
huffman@44133
  1848
    from KB Kp
huffman@44133
  1849
    have "B * norm x * norm y \<le> ?K * norm x * norm y"
huffman@44133
  1850
      apply -
huffman@44133
  1851
      apply (rule mult_right_mono, rule mult_right_mono)
huffman@44133
  1852
      by auto
huffman@44133
  1853
    then have "norm (h x y) \<le> ?K * norm x * norm y"
huffman@44133
  1854
      using B[rule_format, of x y] by simp}
huffman@44133
  1855
  with Kp show ?thesis by blast
huffman@44133
  1856
qed
huffman@44133
  1857
huffman@44133
  1858
lemma bilinear_conv_bounded_bilinear:
huffman@44133
  1859
  fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
huffman@44133
  1860
  shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
huffman@44133
  1861
proof
huffman@44133
  1862
  assume "bilinear h"
huffman@44133
  1863
  show "bounded_bilinear h"
huffman@44133
  1864
  proof
huffman@44133
  1865
    fix x y z show "h (x + y) z = h x z + h y z"
huffman@44133
  1866
      using `bilinear h` unfolding bilinear_def linear_def by simp
huffman@44133
  1867
  next
huffman@44133
  1868
    fix x y z show "h x (y + z) = h x y + h x z"
huffman@44133
  1869
      using `bilinear h` unfolding bilinear_def linear_def by simp
huffman@44133
  1870
  next
huffman@44133
  1871
    fix r x y show "h (scaleR r x) y = scaleR r (h x y)"
huffman@44133
  1872
      using `bilinear h` unfolding bilinear_def linear_def
huffman@44133
  1873
      by simp
huffman@44133
  1874
  next
huffman@44133
  1875
    fix r x y show "h x (scaleR r y) = scaleR r (h x y)"
huffman@44133
  1876
      using `bilinear h` unfolding bilinear_def linear_def
huffman@44133
  1877
      by simp
huffman@44133
  1878
  next
huffman@44133
  1879
    have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@44133
  1880
      using `bilinear h` by (rule bilinear_bounded)
huffman@44133
  1881
    thus "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
huffman@44133
  1882
      by (simp add: mult_ac)
huffman@44133
  1883
  qed
huffman@44133
  1884
next
huffman@44133
  1885
  assume "bounded_bilinear h"
huffman@44133
  1886
  then interpret h: bounded_bilinear h .
huffman@44133
  1887
  show "bilinear h"
huffman@44133
  1888
    unfolding bilinear_def linear_conv_bounded_linear
huffman@44133
  1889
    using h.bounded_linear_left h.bounded_linear_right
huffman@44133
  1890
    by simp
huffman@44133
  1891
qed
huffman@44133
  1892
huffman@44133
  1893
subsection {* We continue. *}
huffman@44133
  1894
huffman@44133
  1895
lemma independent_bound:
huffman@44133
  1896
  fixes S:: "('a::euclidean_space) set"
huffman@44133
  1897
  shows "independent S \<Longrightarrow> finite S \<and> card S <= DIM('a::euclidean_space)"
huffman@44133
  1898
  using independent_span_bound[of "(basis::nat=>'a) ` {..<DIM('a)}" S] by auto
huffman@44133
  1899
huffman@44133
  1900
lemma dependent_biggerset: "(finite (S::('a::euclidean_space) set) ==> card S > DIM('a)) ==> dependent S"
huffman@44133
  1901
  by (metis independent_bound not_less)
huffman@44133
  1902
huffman@44133
  1903
text {* Hence we can create a maximal independent subset. *}
huffman@44133
  1904
huffman@44133
  1905
lemma maximal_independent_subset_extend:
huffman@44133
  1906
  assumes sv: "(S::('a::euclidean_space) set) \<subseteq> V" and iS: "independent S"
huffman@44133
  1907
  shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
huffman@44133
  1908
  using sv iS
huffman@44133
  1909
proof(induct "DIM('a) - card S" arbitrary: S rule: less_induct)
huffman@44133
  1910
  case less
huffman@44133
  1911
  note sv = `S \<subseteq> V` and i = `independent S`
huffman@44133
  1912
  let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
huffman@44133
  1913
  let ?ths = "\<exists>x. ?P x"
huffman@44133
  1914
  let ?d = "DIM('a)"
huffman@44133
  1915
  {assume "V \<subseteq> span S"
huffman@44133
  1916
    then have ?ths  using sv i by blast }
huffman@44133
  1917
  moreover
huffman@44133
  1918
  {assume VS: "\<not> V \<subseteq> span S"
huffman@44133
  1919
    from VS obtain a where a: "a \<in> V" "a \<notin> span S" by blast
huffman@44133
  1920
    from a have aS: "a \<notin> S" by (auto simp add: span_superset)
huffman@44133
  1921
    have th0: "insert a S \<subseteq> V" using a sv by blast
huffman@44133
  1922
    from independent_insert[of a S]  i a
huffman@44133
  1923
    have th1: "independent (insert a S)" by auto
huffman@44133
  1924
    have mlt: "?d - card (insert a S) < ?d - card S"
huffman@44133
  1925
      using aS a independent_bound[OF th1]
huffman@44133
  1926
      by auto
huffman@44133
  1927
huffman@44133
  1928
    from less(1)[OF mlt th0 th1]
huffman@44133
  1929
    obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B"
huffman@44133
  1930
      by blast
huffman@44133
  1931
    from B have "?P B" by auto
huffman@44133
  1932
    then have ?ths by blast}
huffman@44133
  1933
  ultimately show ?ths by blast
huffman@44133
  1934
qed
huffman@44133
  1935
huffman@44133
  1936
lemma maximal_independent_subset:
huffman@44133
  1937
  "\<exists>(B:: ('a::euclidean_space) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
huffman@44133
  1938
  by (metis maximal_independent_subset_extend[of "{}:: ('a::euclidean_space) set"] empty_subsetI independent_empty)
huffman@44133
  1939
huffman@44133
  1940
huffman@44133
  1941
text {* Notion of dimension. *}
huffman@44133
  1942
huffman@44133
  1943
definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = n))"
huffman@44133
  1944
huffman@44133
  1945
lemma basis_exists:  "\<exists>B. (B :: ('a::euclidean_space) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = dim V)"
huffman@44133
  1946
unfolding dim_def some_eq_ex[of "\<lambda>n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = n)"]
huffman@44133
  1947
using maximal_independent_subset[of V] independent_bound
huffman@44133
  1948
by auto
huffman@44133
  1949
huffman@44133
  1950
text {* Consequences of independence or spanning for cardinality. *}
huffman@44133
  1951
huffman@44133
  1952
lemma independent_card_le_dim: 
huffman@44133
  1953
  assumes "(B::('a::euclidean_space) set) \<subseteq> V" and "independent B" shows "card B \<le> dim V"
huffman@44133
  1954
proof -
huffman@44133
  1955
  from basis_exists[of V] `B \<subseteq> V`
huffman@44133
  1956
  obtain B' where "independent B'" and "B \<subseteq> span B'" and "card B' = dim V" by blast
huffman@44133
  1957
  with independent_span_bound[OF _ `independent B` `B \<subseteq> span B'`] independent_bound[of B']
huffman@44133
  1958
  show ?thesis by auto
huffman@44133
  1959
qed
huffman@44133
  1960
huffman@44133
  1961
lemma span_card_ge_dim:  "(B::('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
huffman@44133
  1962
  by (metis basis_exists[of V] independent_span_bound subset_trans)
huffman@44133
  1963
huffman@44133
  1964
lemma basis_card_eq_dim:
huffman@44133
  1965
  "B \<subseteq> (V:: ('a::euclidean_space) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
huffman@44133
  1966
  by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_bound)
huffman@44133
  1967
huffman@44133
  1968
lemma dim_unique: "(B::('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> card B = n \<Longrightarrow> dim V = n"
huffman@44133
  1969
  by (metis basis_card_eq_dim)
huffman@44133
  1970
huffman@44133
  1971
text {* More lemmas about dimension. *}
huffman@44133
  1972
huffman@44133
  1973
lemma dim_UNIV: "dim (UNIV :: ('a::euclidean_space) set) = DIM('a)"
huffman@44133
  1974
  apply (rule dim_unique[of "(basis::nat=>'a) ` {..<DIM('a)}"])
huffman@44133
  1975
  using independent_basis by auto
huffman@44133
  1976
huffman@44133
  1977
lemma dim_subset:
huffman@44133
  1978
  "(S:: ('a::euclidean_space) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
huffman@44133
  1979
  using basis_exists[of T] basis_exists[of S]
huffman@44133
  1980
  by (metis independent_card_le_dim subset_trans)
huffman@44133
  1981
huffman@44133
  1982
lemma dim_subset_UNIV: "dim (S:: ('a::euclidean_space) set) \<le> DIM('a)"
huffman@44133
  1983
  by (metis dim_subset subset_UNIV dim_UNIV)
huffman@44133
  1984
huffman@44133
  1985
text {* Converses to those. *}
huffman@44133
  1986
huffman@44133
  1987
lemma card_ge_dim_independent:
huffman@44133
  1988
  assumes BV:"(B::('a::euclidean_space) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
huffman@44133
  1989
  shows "V \<subseteq> span B"
huffman@44133
  1990
proof-
huffman@44133
  1991
  {fix a assume aV: "a \<in> V"
huffman@44133
  1992
    {assume aB: "a \<notin> span B"
huffman@44133
  1993
      then have iaB: "independent (insert a B)" using iB aV  BV by (simp add: independent_insert)
huffman@44133
  1994
      from aV BV have th0: "insert a B \<subseteq> V" by blast
huffman@44133
  1995
      from aB have "a \<notin>B" by (auto simp add: span_superset)
huffman@44133
  1996
      with independent_card_le_dim[OF th0 iaB] dVB independent_bound[OF iB] have False by auto }
huffman@44133
  1997
    then have "a \<in> span B"  by blast}
huffman@44133
  1998
  then show ?thesis by blast
huffman@44133
  1999
qed
huffman@44133
  2000
huffman@44133
  2001
lemma card_le_dim_spanning:
huffman@44133
  2002
  assumes BV: "(B:: ('a::euclidean_space) set) \<subseteq> V" and VB: "V \<subseteq> span B"
huffman@44133
  2003
  and fB: "finite B" and dVB: "dim V \<ge> card B"
huffman@44133
  2004
  shows "independent B"
huffman@44133
  2005
proof-
huffman@44133
  2006
  {fix a assume a: "a \<in> B" "a \<in> span (B -{a})"
huffman@44133
  2007
    from a fB have c0: "card B \<noteq> 0" by auto
huffman@44133
  2008
    from a fB have cb: "card (B -{a}) = card B - 1" by auto
huffman@44133
  2009
    from BV a have th0: "B -{a} \<subseteq> V" by blast
huffman@44133
  2010
    {fix x assume x: "x \<in> V"
huffman@44133
  2011
      from a have eq: "insert a (B -{a}) = B" by blast
huffman@44133
  2012
      from x VB have x': "x \<in> span B" by blast
huffman@44133
  2013
      from span_trans[OF a(2), unfolded eq, OF x']
huffman@44133
  2014
      have "x \<in> span (B -{a})" . }
huffman@44133
  2015
    then have th1: "V \<subseteq> span (B -{a})" by blast
huffman@44133
  2016
    have th2: "finite (B -{a})" using fB by auto
huffman@44133
  2017
    from span_card_ge_dim[OF th0 th1 th2]
huffman@44133
  2018
    have c: "dim V \<le> card (B -{a})" .
huffman@44133
  2019
    from c c0 dVB cb have False by simp}
huffman@44133
  2020
  then show ?thesis unfolding dependent_def by blast
huffman@44133
  2021
qed
huffman@44133
  2022
huffman@44133
  2023
lemma card_eq_dim: "(B:: ('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> card B = dim V \<Longrightarrow> finite B \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
huffman@44133
  2024
  by (metis order_eq_iff card_le_dim_spanning
huffman@44133
  2025
    card_ge_dim_independent)
huffman@44133
  2026
huffman@44133
  2027
text {* More general size bound lemmas. *}
huffman@44133
  2028
huffman@44133
  2029
lemma independent_bound_general:
huffman@44133
  2030
  "independent (S:: ('a::euclidean_space) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
huffman@44133
  2031
  by (metis independent_card_le_dim independent_bound subset_refl)
huffman@44133
  2032
huffman@44133
  2033
lemma dependent_biggerset_general: "(finite (S:: ('a::euclidean_space) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
huffman@44133
  2034
  using independent_bound_general[of S] by (metis linorder_not_le)
huffman@44133
  2035
huffman@44133
  2036
lemma dim_span: "dim (span (S:: ('a::euclidean_space) set)) = dim S"
huffman@44133
  2037
proof-
huffman@44133
  2038
  have th0: "dim S \<le> dim (span S)"
huffman@44133
  2039
    by (auto simp add: subset_eq intro: dim_subset span_superset)
huffman@44133
  2040
  from basis_exists[of S]
huffman@44133
  2041
  obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" by blast
huffman@44133
  2042
  from B have fB: "finite B" "card B = dim S" using independent_bound by blast+
huffman@44133
  2043
  have bSS: "B \<subseteq> span S" using B(1) by (metis subset_eq span_inc)
huffman@44133
  2044
  have sssB: "span S \<subseteq> span B" using span_mono[OF B(3)] by (simp add: span_span)
huffman@44133
  2045
  from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
huffman@44133
  2046
    using fB(2)  by arith
huffman@44133
  2047
qed
huffman@44133
  2048
huffman@44133
  2049
lemma subset_le_dim: "(S:: ('a::euclidean_space) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
huffman@44133
  2050
  by (metis dim_span dim_subset)
huffman@44133
  2051
huffman@44133
  2052
lemma span_eq_dim: "span (S:: ('a::euclidean_space) set) = span T ==> dim S = dim T"
huffman@44133
  2053
  by (metis dim_span)
huffman@44133
  2054
huffman@44133
  2055
lemma spans_image:
huffman@44133
  2056
  assumes lf: "linear f" and VB: "V \<subseteq> span B"
huffman@44133
  2057
  shows "f ` V \<subseteq> span (f ` B)"
huffman@44133
  2058
  unfolding span_linear_image[OF lf]
huffman@44133
  2059
  by (metis VB image_mono)
huffman@44133
  2060
huffman@44133
  2061
lemma dim_image_le:
huffman@44133
  2062
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
huffman@44133
  2063
  assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S)"
huffman@44133
  2064
proof-
huffman@44133
  2065
  from basis_exists[of S] obtain B where
huffman@44133
  2066
    B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" by blast
huffman@44133
  2067
  from B have fB: "finite B" "card B = dim S" using independent_bound by blast+
huffman@44133
  2068
  have "dim (f ` S) \<le> card (f ` B)"
huffman@44133
  2069
    apply (rule span_card_ge_dim)
huffman@44133
  2070
    using lf B fB by (auto simp add: span_linear_image spans_image subset_image_iff)
huffman@44133
  2071
  also have "\<dots> \<le> dim S" using card_image_le[OF fB(1)] fB by simp
huffman@44133
  2072
  finally show ?thesis .
huffman@44133
  2073
qed
huffman@44133
  2074
huffman@44133
  2075
text {* Relation between bases and injectivity/surjectivity of map. *}
huffman@44133
  2076
huffman@44133
  2077
lemma spanning_surjective_image:
huffman@44133
  2078
  assumes us: "UNIV \<subseteq> span S"
huffman@44133
  2079
  and lf: "linear f" and sf: "surj f"
huffman@44133
  2080
  shows "UNIV \<subseteq> span (f ` S)"
huffman@44133
  2081
proof-
huffman@44133
  2082
  have "UNIV \<subseteq> f ` UNIV" using sf by (auto simp add: surj_def)
huffman@44133
  2083
  also have " \<dots> \<subseteq> span (f ` S)" using spans_image[OF lf us] .
huffman@44133
  2084
finally show ?thesis .
huffman@44133
  2085
qed
huffman@44133
  2086
huffman@44133
  2087
lemma independent_injective_image:
huffman@44133
  2088
  assumes iS: "independent S" and lf: "linear f" and fi: "inj f"
huffman@44133
  2089
  shows "independent (f ` S)"
huffman@44133
  2090
proof-
huffman@44133
  2091
  {fix a assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
huffman@44133
  2092
    have eq: "f ` S - {f a} = f ` (S - {a})" using fi
huffman@44133
  2093
      by (auto simp add: inj_on_def)
huffman@44133
  2094
    from a have "f a \<in> f ` span (S -{a})"
huffman@44133
  2095
      unfolding eq span_linear_image[OF lf, of "S - {a}"]  by blast
huffman@44133
  2096
    hence "a \<in> span (S -{a})" using fi by (auto simp add: inj_on_def)
huffman@44133
  2097
    with a(1) iS  have False by (simp add: dependent_def) }
huffman@44133
  2098
  then show ?thesis unfolding dependent_def by blast
huffman@44133
  2099
qed
huffman@44133
  2100
huffman@44133
  2101
text {* Picking an orthogonal replacement for a spanning set. *}
huffman@44133
  2102
huffman@44133
  2103
    (* FIXME : Move to some general theory ?*)
huffman@44133
  2104
definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
huffman@44133
  2105
huffman@44133
  2106
lemma vector_sub_project_orthogonal: "(b::'a::euclidean_space) \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *\<^sub>R b) = 0"
huffman@44133
  2107
  unfolding inner_simps by auto
huffman@44133
  2108
huffman@44133
  2109
lemma basis_orthogonal:
huffman@44133
  2110
  fixes B :: "('a::euclidean_space) set"
huffman@44133
  2111
  assumes fB: "finite B"
huffman@44133
  2112
  shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
huffman@44133
  2113
  (is " \<exists>C. ?P B C")
huffman@44133
  2114
proof(induct rule: finite_induct[OF fB])
huffman@44133
  2115
  case 1 thus ?case apply (rule exI[where x="{}"]) by (auto simp add: pairwise_def)
huffman@44133
  2116
next
huffman@44133
  2117
  case (2 a B)
huffman@44133
  2118
  note fB = `finite B` and aB = `a \<notin> B`
huffman@44133
  2119
  from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C`
huffman@44133
  2120
  obtain C where C: "finite C" "card C \<le> card B"
huffman@44133
  2121
    "span C = span B" "pairwise orthogonal C" by blast
huffman@44133
  2122
  let ?a = "a - setsum (\<lambda>x. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x) C"
huffman@44133
  2123
  let ?C = "insert ?a C"
huffman@44133
  2124
  from C(1) have fC: "finite ?C" by simp
huffman@44133
  2125
  from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)" by (simp add: card_insert_if)
huffman@44133
  2126
  {fix x k
huffman@44133
  2127
    have th0: "\<And>(a::'a) b c. a - (b - c) = c + (a - b)" by (simp add: field_simps)
huffman@44133
  2128
    have "x - k *\<^sub>R (a - (\<Sum>x\<in>C. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x)) \<in> span C \<longleftrightarrow> x - k *\<^sub>R a \<in> span C"
huffman@44133
  2129
      apply (simp only: scaleR_right_diff_distrib th0)
huffman@44133
  2130
      apply (rule span_add_eq)
huffman@44133
  2131
      apply (rule span_mul)
huffman@44133
  2132
      apply (rule span_setsum[OF C(1)])
huffman@44133
  2133
      apply clarify
huffman@44133
  2134
      apply (rule span_mul)
huffman@44133
  2135
      by (rule span_superset)}
huffman@44133
  2136
  then have SC: "span ?C = span (insert a B)"
huffman@44133
  2137
    unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
huffman@44133
  2138
  thm pairwise_def
huffman@44133
  2139
  {fix x y assume xC: "x \<in> ?C" and yC: "y \<in> ?C" and xy: "x \<noteq> y"
huffman@44133
  2140
    {assume xa: "x = ?a" and ya: "y = ?a"
huffman@44133
  2141
      have "orthogonal x y" using xa ya xy by blast}
huffman@44133
  2142
    moreover
huffman@44133
  2143
    {assume xa: "x = ?a" and ya: "y \<noteq> ?a" "y \<in> C"
huffman@44133
  2144
      from ya have Cy: "C = insert y (C - {y})" by blast
huffman@44133
  2145
      have fth: "finite (C - {y})" using C by simp
huffman@44133
  2146
      have "orthogonal x y"
huffman@44133
  2147
        using xa ya
huffman@44133
  2148
        unfolding orthogonal_def xa inner_simps diff_eq_0_iff_eq
huffman@44133
  2149
        apply simp
huffman@44133
  2150
        apply (subst Cy)
huffman@44133
  2151
        using C(1) fth
huffman@44133
  2152
        apply (simp only: setsum_clauses)
huffman@44133
  2153
        apply (auto simp add: inner_simps inner_commute[of y a] dot_lsum[OF fth])
huffman@44133
  2154
        apply (rule setsum_0')
huffman@44133
  2155
        apply clarsimp
huffman@44133
  2156
        apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
huffman@44133
  2157
        by auto}
huffman@44133
  2158
    moreover
huffman@44133
  2159
    {assume xa: "x \<noteq> ?a" "x \<in> C" and ya: "y = ?a"
huffman@44133
  2160
      from xa have Cx: "C = insert x (C - {x})" by blast
huffman@44133
  2161
      have fth: "finite (C - {x})" using C by simp
huffman@44133
  2162
      have "orthogonal x y"
huffman@44133
  2163
        using xa ya
huffman@44133
  2164
        unfolding orthogonal_def ya inner_simps diff_eq_0_iff_eq
huffman@44133
  2165
        apply simp
huffman@44133
  2166
        apply (subst Cx)
huffman@44133
  2167
        using C(1) fth
huffman@44133
  2168
        apply (simp only: setsum_clauses)
huffman@44133
  2169
        apply (subst inner_commute[of x])
huffman@44133
  2170
        apply (auto simp add: inner_simps inner_commute[of x a] dot_rsum[OF fth])
huffman@44133
  2171
        apply (rule setsum_0')
huffman@44133
  2172
        apply clarsimp
huffman@44133
  2173
        apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
huffman@44133
  2174
        by auto}
huffman@44133
  2175
    moreover
huffman@44133
  2176
    {assume xa: "x \<in> C" and ya: "y \<in> C"
huffman@44133
  2177
      have "orthogonal x y" using xa ya xy C(4) unfolding pairwise_def by blast}
huffman@44133
  2178
    ultimately have "orthogonal x y" using xC yC by blast}
huffman@44133
  2179
  then have CPO: "pairwise orthogonal ?C" unfolding pairwise_def by blast
huffman@44133
  2180
  from fC cC SC CPO have "?P (insert a B) ?C" by blast
huffman@44133
  2181
  then show ?case by blast
huffman@44133
  2182
qed
huffman@44133
  2183
huffman@44133
  2184
lemma orthogonal_basis_exists:
huffman@44133
  2185
  fixes V :: "('a::euclidean_space) set"
huffman@44133
  2186
  shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (card B = dim V) \<and> pairwise orthogonal B"
huffman@44133
  2187
proof-
huffman@44133
  2188
  from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "card B = dim V" by blast
huffman@44133
  2189
  from B have fB: "finite B" "card B = dim V" using independent_bound by auto
huffman@44133
  2190
  from basis_orthogonal[OF fB(1)] obtain C where
huffman@44133
  2191
    C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C" by blast
huffman@44133
  2192
  from C B
huffman@44133
  2193
  have CSV: "C \<subseteq> span V" by (metis span_inc span_mono subset_trans)
huffman@44133
  2194
  from span_mono[OF B(3)]  C have SVC: "span V \<subseteq> span C" by (simp add: span_span)
huffman@44133
  2195
  from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
huffman@44133
  2196
  have iC: "independent C" by (simp add: dim_span)
huffman@44133
  2197
  from C fB have "card C \<le> dim V" by simp
huffman@44133
  2198
  moreover have "dim V \<le> card C" using span_card_ge_dim[OF CSV SVC C(1)]
huffman@44133
  2199
    by (simp add: dim_span)
huffman@44133
  2200
  ultimately have CdV: "card C = dim V" using C(1) by simp
huffman@44133
  2201
  from C B CSV CdV iC show ?thesis by auto
huffman@44133
  2202
qed
huffman@44133
  2203
huffman@44133
  2204
lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
huffman@44133
  2205
  using span_inc[unfolded subset_eq] using span_mono[of T "span S"] span_mono[of S "span T"]
huffman@44133
  2206
  by(auto simp add: span_span)
huffman@44133
  2207
huffman@44133
  2208
text {* Low-dimensional subset is in a hyperplane (weak orthogonal complement). *}
huffman@44133
  2209
huffman@44133
  2210
lemma span_not_univ_orthogonal: fixes S::"('a::euclidean_space) set"
huffman@44133
  2211
  assumes sU: "span S \<noteq> UNIV"
huffman@44133
  2212
  shows "\<exists>(a::'a). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
huffman@44133
  2213
proof-
huffman@44133
  2214
  from sU obtain a where a: "a \<notin> span S" by blast
huffman@44133
  2215
  from orthogonal_basis_exists obtain B where
huffman@44133
  2216
    B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "card B = dim S" "pairwise orthogonal B"
huffman@44133
  2217
    by blast
huffman@44133
  2218
  from B have fB: "finite B" "card B = dim S" using independent_bound by auto
huffman@44133
  2219
  from span_mono[OF B(2)] span_mono[OF B(3)]
huffman@44133
  2220
  have sSB: "span S = span B" by (simp add: span_span)
huffman@44133
  2221
  let ?a = "a - setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B"
huffman@44133
  2222
  have "setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B \<in> span S"
huffman@44133
  2223
    unfolding sSB
huffman@44133
  2224
    apply (rule span_setsum[OF fB(1)])
huffman@44133
  2225
    apply clarsimp
huffman@44133
  2226
    apply (rule span_mul)
huffman@44133
  2227
    by (rule span_superset)
huffman@44133
  2228
  with a have a0:"?a  \<noteq> 0" by auto
huffman@44133
  2229
  have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
huffman@44133
  2230
  proof(rule span_induct')
huffman@44170
  2231
    show "subspace {x. ?a \<bullet> x = 0}" by (auto simp add: subspace_def inner_simps)
huffman@44133
  2232
next
huffman@44133
  2233
    {fix x assume x: "x \<in> B"
huffman@44133
  2234
      from x have B': "B = insert x (B - {x})" by blast
huffman@44133
  2235
      have fth: "finite (B - {x})" using fB by simp
huffman@44133
  2236
      have "?a \<bullet> x = 0"
huffman@44133
  2237
        apply (subst B') using fB fth
huffman@44133
  2238
        unfolding setsum_clauses(2)[OF fth]
huffman@44133
  2239
        apply simp unfolding inner_simps
huffman@44133
  2240
        apply (clarsimp simp add: inner_simps dot_lsum)
huffman@44133
  2241
        apply (rule setsum_0', rule ballI)
huffman@44133
  2242
        unfolding inner_commute
huffman@44133
  2243
        by (auto simp add: x field_simps intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])}
huffman@44133
  2244
    then show "\<forall>x \<in> B. ?a \<bullet> x = 0" by blast
huffman@44133
  2245
  qed
huffman@44133
  2246
  with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"])
huffman@44133
  2247
qed
huffman@44133
  2248
huffman@44133
  2249
lemma span_not_univ_subset_hyperplane:
huffman@44133
  2250
  assumes SU: "span S \<noteq> (UNIV ::('a::euclidean_space) set)"
huffman@44133
  2251
  shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
huffman@44133
  2252
  using span_not_univ_orthogonal[OF SU] by auto
huffman@44133
  2253
huffman@44133
  2254
lemma lowdim_subset_hyperplane: fixes S::"('a::euclidean_space) set"
huffman@44133
  2255
  assumes d: "dim S < DIM('a)"
huffman@44133
  2256
  shows "\<exists>(a::'a). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
huffman@44133
  2257
proof-
huffman@44133
  2258
  {assume "span S = UNIV"
huffman@44133
  2259
    hence "dim (span S) = dim (UNIV :: ('a) set)" by simp
huffman@44133
  2260
    hence "dim S = DIM('a)" by (simp add: dim_span dim_UNIV)
huffman@44133
  2261
    with d have False by arith}
huffman@44133
  2262
  hence th: "span S \<noteq> UNIV" by blast
huffman@44133
  2263
  from span_not_univ_subset_hyperplane[OF th] show ?thesis .
huffman@44133
  2264
qed
huffman@44133
  2265
huffman@44133
  2266
text {* We can extend a linear basis-basis injection to the whole set. *}
huffman@44133
  2267
huffman@44133
  2268
lemma linear_indep_image_lemma:
huffman@44133
  2269
  assumes lf: "linear f" and fB: "finite B"
huffman@44133
  2270
  and ifB: "independent (f ` B)"
huffman@44133
  2271
  and fi: "inj_on f B" and xsB: "x \<in> span B"
huffman@44133
  2272
  and fx: "f x = 0"
huffman@44133
  2273
  shows "x = 0"
huffman@44133
  2274
  using fB ifB fi xsB fx
huffman@44133
  2275
proof(induct arbitrary: x rule: finite_induct[OF fB])
huffman@44142
  2276
  case 1 thus ?case by auto
huffman@44133
  2277
next
huffman@44133
  2278
  case (2 a b x)
huffman@44133
  2279
  have fb: "finite b" using "2.prems" by simp
huffman@44133
  2280
  have th0: "f ` b \<subseteq> f ` (insert a b)"
huffman@44133
  2281
    apply (rule image_mono) by blast
huffman@44133
  2282
  from independent_mono[ OF "2.prems"(2) th0]
huffman@44133
  2283
  have ifb: "independent (f ` b)"  .
huffman@44133
  2284
  have fib: "inj_on f b"
huffman@44133
  2285
    apply (rule subset_inj_on [OF "2.prems"(3)])
huffman@44133
  2286
    by blast
huffman@44133
  2287
  from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
huffman@44133
  2288
  obtain k where k: "x - k*\<^sub>R a \<in> span (b -{a})" by blast
huffman@44133
  2289
  have "f (x - k*\<^sub>R a) \<in> span (f ` b)"
huffman@44133
  2290
    unfolding span_linear_image[OF lf]
huffman@44133
  2291
    apply (rule imageI)
huffman@44133
  2292
    using k span_mono[of "b-{a}" b] by blast
huffman@44133
  2293
  hence "f x - k*\<^sub>R f a \<in> span (f ` b)"
huffman@44133
  2294
    by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
huffman@44133
  2295
  hence th: "-k *\<^sub>R f a \<in> span (f ` b)"
huffman@44133
  2296
    using "2.prems"(5) by simp
huffman@44133
  2297
  {assume k0: "k = 0"
huffman@44133
  2298
    from k0 k have "x \<in> span (b -{a})" by simp
huffman@44133
  2299
    then have "x \<in> span b" using span_mono[of "b-{a}" b]
huffman@44133
  2300
      by blast}
huffman@44133
  2301
  moreover
huffman@44133
  2302
  {assume k0: "k \<noteq> 0"
huffman@44133
  2303
    from span_mul[OF th, of "- 1/ k"] k0
huffman@44133
  2304
    have th1: "f a \<in> span (f ` b)"
huffman@44133
  2305
      by auto
huffman@44133
  2306
    from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
huffman@44133
  2307
    have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
huffman@44133
  2308
    from "2.prems"(2) [unfolded dependent_def bex_simps(8), rule_format, of "f a"]
huffman@44133
  2309
    have "f a \<notin> span (f ` b)" using tha
huffman@44133
  2310
      using "2.hyps"(2)
huffman@44133
  2311
      "2.prems"(3) by auto
huffman@44133
  2312
    with th1 have False by blast
huffman@44133
  2313
    then have "x \<in> span b" by blast}
huffman@44133
  2314
  ultimately have xsb: "x \<in> span b" by blast
huffman@44133
  2315
  from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)]
huffman@44133
  2316
  show "x = 0" .
huffman@44133
  2317
qed
huffman@44133
  2318
huffman@44133
  2319
text {* We can extend a linear mapping from basis. *}
huffman@44133
  2320
huffman@44133
  2321
lemma linear_independent_extend_lemma:
huffman@44133
  2322
  fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
huffman@44133
  2323
  assumes fi: "finite B" and ib: "independent B"
huffman@44133
  2324
  shows "\<exists>g. (\<forall>x\<in> span B. \<forall>y\<in> span B. g (x + y) = g x + g y)
huffman@44133
  2325
           \<and> (\<forall>x\<in> span B. \<forall>c. g (c*\<^sub>R x) = c *\<^sub>R g x)
huffman@44133
  2326
           \<and> (\<forall>x\<in> B. g x = f x)"
huffman@44133
  2327
using ib fi
huffman@44133
  2328
proof(induct rule: finite_induct[OF fi])
huffman@44142
  2329
  case 1 thus ?case by auto
huffman@44133
  2330
next
huffman@44133
  2331
  case (2 a b)
huffman@44133
  2332
  from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
huffman@44133
  2333
    by (simp_all add: independent_insert)
huffman@44133
  2334
  from "2.hyps"(3)[OF ibf] obtain g where
huffman@44133
  2335
    g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
huffman@44133
  2336
    "\<forall>x\<in>span b. \<forall>c. g (c *\<^sub>R x) = c *\<^sub>R g x" "\<forall>x\<in>b. g x = f x" by blast
huffman@44133
  2337
  let ?h = "\<lambda>z. SOME k. (z - k *\<^sub>R a) \<in> span b"
huffman@44133
  2338
  {fix z assume z: "z \<in> span (insert a b)"
huffman@44133