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