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