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