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