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