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