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