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