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