src/HOL/Library/Euclidean_Space.thy
 changeset 29842 4ac60c7d9b78 child 29844 4ac95212efcc
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Library/Euclidean_Space.thy	Mon Feb 09 16:54:03 2009 +0000
1.3 @@ -0,0 +1,5171 @@
1.4 +(* Title:      Library/Euclidean_Space
1.5 +   ID:         \$Id:
1.6 +   Author:     Amine Chaieb, University of Cambridge
1.7 +*)
1.8 +
1.9 +header {* (Real) Vectors in Euclidean space, and elementary linear algebra.*}
1.10 +
1.11 +theory Euclidean_Space
1.12 +  imports "~~/src/HOL/Decision_Procs/Dense_Linear_Order" Complex_Main
1.13 +  Finite_Cartesian_Product Glbs Infinite_Set Numeral_Type
1.14 +  uses ("normarith.ML")
1.15 +begin
1.16 +
1.17 +text{* Some common special cases.*}
1.18 +
1.19 +lemma forall_1: "(\<forall>(i::'a::{order,one}). 1 <= i \<and> i <= 1 --> P i) \<longleftrightarrow> P 1"
1.20 +  by (metis order_eq_iff)
1.21 +lemma forall_dimindex_1: "(\<forall>i \<in> {1..dimindex(UNIV:: 1 set)}. P i) \<longleftrightarrow> P 1"
1.22 +  by (simp add: dimindex_def)
1.23 +
1.24 +lemma forall_2: "(\<forall>(i::nat). 1 <= i \<and> i <= 2 --> P i) \<longleftrightarrow> P 1 \<and> P 2"
1.25 +proof-
1.26 +  have "\<And>i::nat. 1 <= i \<and> i <= 2 \<longleftrightarrow> i = 1 \<or> i = 2" by arith
1.27 +  thus ?thesis by metis
1.28 +qed
1.29 +
1.30 +lemma forall_3: "(\<forall>(i::nat). 1 <= i \<and> i <= 3 --> P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
1.31 +proof-
1.32 +  have "\<And>i::nat. 1 <= i \<and> i <= 3 \<longleftrightarrow> i = 1 \<or> i = 2 \<or> i = 3" by arith
1.33 +  thus ?thesis by metis
1.34 +qed
1.35 +
1.36 +lemma setsum_singleton[simp]: "setsum f {x} = f x" by simp
1.37 +lemma setsum_1: "setsum f {(1::'a::{order,one})..1} = f 1"
1.38 +  by (simp add: atLeastAtMost_singleton)
1.39 +
1.40 +lemma setsum_2: "setsum f {1::nat..2} = f 1 + f 2"
1.42 +
1.43 +lemma setsum_3: "setsum f {1::nat..3} = f 1 + f 2 + f 3"
1.45 +
1.46 +section{* Basic componentwise operations on vectors. *}
1.47 +
1.48 +instantiation "^" :: (plus,type) plus
1.49 +begin
1.50 +definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) + (y\$i)))"
1.51 +instance ..
1.52 +end
1.53 +
1.54 +instantiation "^" :: (times,type) times
1.55 +begin
1.56 +  definition vector_mult_def : "op * \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) * (y\$i)))"
1.57 +  instance ..
1.58 +end
1.59 +
1.60 +instantiation "^" :: (minus,type) minus begin
1.61 +  definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) - (y\$i)))"
1.62 +instance ..
1.63 +end
1.64 +
1.65 +instantiation "^" :: (uminus,type) uminus begin
1.66 +  definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x\$i)))"
1.67 +instance ..
1.68 +end
1.69 +instantiation "^" :: (zero,type) zero begin
1.70 +  definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
1.71 +instance ..
1.72 +end
1.73 +
1.74 +instantiation "^" :: (one,type) one begin
1.75 +  definition vector_one_def : "1 \<equiv> (\<chi> i. 1)"
1.76 +instance ..
1.77 +end
1.78 +
1.79 +instantiation "^" :: (ord,type) ord
1.80 + begin
1.81 +definition vector_less_eq_def:
1.82 +  "less_eq (x :: 'a ^'b) y = (ALL i : {1 .. dimindex (UNIV :: 'b set)}.
1.83 +  x\$i <= y\$i)"
1.84 +definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i : {1 ..
1.85 +  dimindex (UNIV :: 'b set)}. x\$i < y\$i)"
1.86 +
1.87 +instance by (intro_classes)
1.88 +end
1.89 +
1.90 +text{* Also the scalar-vector multiplication. FIXME: We should unify this with the scalar multiplication in real_vector *}
1.91 +
1.92 +definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'n" (infixr "*s" 75)
1.93 +  where "c *s x = (\<chi> i. c * (x\$i))"
1.94 +
1.95 +text{* Constant Vectors *}
1.96 +
1.97 +definition "vec x = (\<chi> i. x)"
1.98 +
1.99 +text{* Dot products. *}
1.100 +
1.101 +definition dot :: "'a::{comm_monoid_add, times} ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" (infix "\<bullet>" 70) where
1.102 +  "x \<bullet> y = setsum (\<lambda>i. x\$i * y\$i) {1 .. dimindex (UNIV:: 'n set)}"
1.103 +lemma dot_1[simp]: "(x::'a::{comm_monoid_add, times}^1) \<bullet> y = (x\$1) * (y\$1)"
1.104 +  by (simp add: dot_def dimindex_def)
1.105 +
1.106 +lemma dot_2[simp]: "(x::'a::{comm_monoid_add, times}^2) \<bullet> y = (x\$1) * (y\$1) + (x\$2) * (y\$2)"
1.107 +  by (simp add: dot_def dimindex_def nat_number)
1.108 +
1.109 +lemma dot_3[simp]: "(x::'a::{comm_monoid_add, times}^3) \<bullet> y = (x\$1) * (y\$1) + (x\$2) * (y\$2) + (x\$3) * (y\$3)"
1.110 +  by (simp add: dot_def dimindex_def nat_number)
1.111 +
1.112 +section {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
1.113 +
1.114 +lemmas Cart_lambda_beta' = Cart_lambda_beta[rule_format]
1.115 +method_setup vector = {*
1.116 +let
1.117 +  val ss1 = HOL_basic_ss addsimps [@{thm dot_def}, @{thm setsum_addf} RS sym,
1.118 +  @{thm setsum_subtractf} RS sym, @{thm setsum_right_distrib},
1.119 +  @{thm setsum_left_distrib}, @{thm setsum_negf} RS sym]
1.120 +  val ss2 = @{simpset} addsimps
1.121 +             [@{thm vector_add_def}, @{thm vector_mult_def},
1.122 +              @{thm vector_minus_def}, @{thm vector_uminus_def},
1.123 +              @{thm vector_one_def}, @{thm vector_zero_def}, @{thm vec_def},
1.124 +              @{thm Cart_lambda_beta'}, @{thm vector_scalar_mult_def}]
1.125 + fun vector_arith_tac ths =
1.126 +   simp_tac ss1
1.127 +   THEN' (fn i => rtac @{thm setsum_cong2} i
1.128 +         ORELSE rtac @{thm setsum_0'} i
1.129 +         ORELSE simp_tac (HOL_basic_ss addsimps [@{thm "Cart_eq"}]) i)
1.130 +   (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
1.131 +   THEN' asm_full_simp_tac (ss2 addsimps ths)
1.132 + in
1.133 +  Method.thms_args (Method.SIMPLE_METHOD' o vector_arith_tac)
1.134 +end
1.135 +*} "Lifts trivial vector statements to real arith statements"
1.136 +
1.137 +lemma vec_0[simp]: "vec 0 = 0" by (vector vector_zero_def)
1.138 +lemma vec_1[simp]: "vec 1 = 1" by (vector vector_one_def)
1.139 +
1.140 +
1.141 +
1.142 +text{* Obvious "component-pushing". *}
1.143 +
1.144 +lemma vec_component: " i \<in> {1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (vec x :: 'a ^ 'n)\$i = x"
1.145 +  by (vector vec_def)
1.146 +
1.148 +  fixes x y :: "'a::{plus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.149 +  shows "(x + y)\$i = x\$i + y\$i"
1.150 +  using i by vector
1.151 +
1.152 +lemma vector_minus_component:
1.153 +  fixes x y :: "'a::{minus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.154 +  shows "(x - y)\$i = x\$i - y\$i"
1.155 +  using i  by vector
1.156 +
1.157 +lemma vector_mult_component:
1.158 +  fixes x y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.159 +  shows "(x * y)\$i = x\$i * y\$i"
1.160 +  using i by vector
1.161 +
1.162 +lemma vector_smult_component:
1.163 +  fixes y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.164 +  shows "(c *s y)\$i = c * (y\$i)"
1.165 +  using i by vector
1.166 +
1.167 +lemma vector_uminus_component:
1.168 +  fixes x :: "'a::{uminus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.169 +  shows "(- x)\$i = - (x\$i)"
1.170 +  using i by vector
1.171 +
1.172 +lemma cond_component: "(if b then x else y)\$i = (if b then x\$i else y\$i)" by vector
1.173 +
1.174 +lemmas vector_component = vec_component vector_add_component vector_mult_component vector_smult_component vector_minus_component vector_uminus_component cond_component
1.175 +
1.176 +subsection {* Some frequently useful arithmetic lemmas over vectors. *}
1.177 +
1.179 +  apply (intro_classes) by (vector add_assoc)
1.180 +
1.181 +
1.183 +  apply (intro_classes) by vector+
1.184 +
1.186 +  apply (intro_classes) by (vector algebra_simps)+
1.187 +
1.189 +  apply (intro_classes) by (vector add_commute)
1.190 +
1.192 +  apply (intro_classes) by vector
1.193 +
1.195 +  apply (intro_classes) by vector+
1.196 +
1.198 +  apply (intro_classes)
1.199 +  by (vector Cart_eq)+
1.200 +
1.202 +  apply (intro_classes)
1.203 +  by (vector Cart_eq)
1.204 +
1.205 +instance "^" :: (semigroup_mult,type) semigroup_mult
1.206 +  apply (intro_classes) by (vector mult_assoc)
1.207 +
1.208 +instance "^" :: (monoid_mult,type) monoid_mult
1.209 +  apply (intro_classes) by vector+
1.210 +
1.211 +instance "^" :: (ab_semigroup_mult,type) ab_semigroup_mult
1.212 +  apply (intro_classes) by (vector mult_commute)
1.213 +
1.214 +instance "^" :: (ab_semigroup_idem_mult,type) ab_semigroup_idem_mult
1.215 +  apply (intro_classes) by (vector mult_idem)
1.216 +
1.217 +instance "^" :: (comm_monoid_mult,type) comm_monoid_mult
1.218 +  apply (intro_classes) by vector
1.219 +
1.220 +fun vector_power :: "('a::{one,times} ^'n) \<Rightarrow> nat \<Rightarrow> 'a^'n" where
1.221 +  "vector_power x 0 = 1"
1.222 +  | "vector_power x (Suc n) = x * vector_power x n"
1.223 +
1.224 +instantiation "^" :: (recpower,type) recpower
1.225 +begin
1.226 +  definition vec_power_def: "op ^ \<equiv> vector_power"
1.227 +  instance
1.228 +  apply (intro_classes) by (simp_all add: vec_power_def)
1.229 +end
1.230 +
1.231 +instance "^" :: (semiring,type) semiring
1.232 +  apply (intro_classes) by (vector ring_simps)+
1.233 +
1.234 +instance "^" :: (semiring_0,type) semiring_0
1.235 +  apply (intro_classes) by (vector ring_simps)+
1.236 +instance "^" :: (semiring_1,type) semiring_1
1.237 +  apply (intro_classes) apply vector using dimindex_ge_1 by auto
1.238 +instance "^" :: (comm_semiring,type) comm_semiring
1.239 +  apply (intro_classes) by (vector ring_simps)+
1.240 +
1.241 +instance "^" :: (comm_semiring_0,type) comm_semiring_0 by (intro_classes)
1.242 +instance "^" :: (semiring_0_cancel,type) semiring_0_cancel by (intro_classes)
1.243 +instance "^" :: (comm_semiring_0_cancel,type) comm_semiring_0_cancel by (intro_classes)
1.244 +instance "^" :: (ring,type) ring by (intro_classes)
1.245 +instance "^" :: (semiring_1_cancel,type) semiring_1_cancel by (intro_classes)
1.246 +instance "^" :: (comm_semiring_1,type) comm_semiring_1 by (intro_classes)
1.247 +lemma of_nat_index:
1.248 +  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (of_nat n :: 'a::semiring_1 ^'n)\$i = of_nat n"
1.249 +  apply (induct n)
1.250 +  apply vector
1.251 +  apply vector
1.252 +  done
1.253 +lemma zero_index[simp]:
1.254 +  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (0 :: 'a::zero ^'n)\$i = 0" by vector
1.255 +
1.256 +lemma one_index[simp]:
1.257 +  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (1 :: 'a::one ^'n)\$i = 1" by vector
1.258 +
1.259 +lemma one_plus_of_nat_neq_0: "(1::'a::semiring_char_0) + of_nat n \<noteq> 0"
1.260 +proof-
1.261 +  have "(1::'a) + of_nat n = 0 \<longleftrightarrow> of_nat 1 + of_nat n = (of_nat 0 :: 'a)" by simp
1.262 +  also have "\<dots> \<longleftrightarrow> 1 + n = 0" by (simp only: of_nat_add[symmetric] of_nat_eq_iff)
1.263 +  finally show ?thesis by simp
1.264 +qed
1.265 +
1.266 +instance "^" :: (semiring_char_0,type) semiring_char_0
1.267 +proof (intro_classes)
1.268 +  fix m n ::nat
1.269 +  show "(of_nat m :: 'a^'b) = of_nat n \<longleftrightarrow> m = n"
1.270 +  proof(induct m arbitrary: n)
1.271 +    case 0 thus ?case apply vector
1.272 +      apply (induct n,auto simp add: ring_simps)
1.273 +      using dimindex_ge_1 apply auto
1.274 +      apply vector
1.275 +      by (auto simp add: of_nat_index one_plus_of_nat_neq_0)
1.276 +  next
1.277 +    case (Suc n m)
1.278 +    thus ?case  apply vector
1.279 +      apply (induct m, auto simp add: ring_simps of_nat_index zero_index)
1.280 +      using dimindex_ge_1 apply simp apply blast
1.281 +      apply (simp add: one_plus_of_nat_neq_0)
1.282 +      using dimindex_ge_1 apply simp apply blast
1.283 +      apply (simp add: vector_component one_index of_nat_index)
1.284 +      apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
1.285 +      using  dimindex_ge_1 apply simp apply blast
1.286 +      apply (simp add: vector_component one_index of_nat_index)
1.287 +      apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
1.288 +      using dimindex_ge_1 apply simp apply blast
1.289 +      apply (simp add: vector_component one_index of_nat_index)
1.290 +      done
1.291 +  qed
1.292 +qed
1.293 +
1.294 +instance "^" :: (comm_ring_1,type) comm_ring_1 by intro_classes
1.295 +  (* FIXME!!! Why does the axclass package complain here !!*)
1.296 +(* instance "^" :: (ring_char_0,type) ring_char_0 by intro_classes *)
1.297 +
1.298 +lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
1.299 +  by (vector mult_assoc)
1.300 +lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
1.301 +  by (vector ring_simps)
1.302 +lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
1.303 +  by (vector ring_simps)
1.304 +lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
1.305 +lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
1.306 +lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
1.307 +  by (vector ring_simps)
1.308 +lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
1.309 +lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
1.310 +lemma vector_sneg_minus1: "-x = (- (1::'a::ring_1)) *s x" by vector
1.311 +lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
1.312 +lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
1.313 +  by (vector ring_simps)
1.314 +
1.315 +lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
1.316 +  apply (auto simp add: vec_def Cart_eq vec_component Cart_lambda_beta )
1.317 +  using dimindex_ge_1 apply auto done
1.318 +
1.319 +subsection{* Properties of the dot product.  *}
1.320 +
1.321 +lemma dot_sym: "(x::'a:: {comm_monoid_add, ab_semigroup_mult} ^ 'n) \<bullet> y = y \<bullet> x"
1.322 +  by (vector mult_commute)
1.323 +lemma dot_ladd: "((x::'a::ring ^ 'n) + y) \<bullet> z = (x \<bullet> z) + (y \<bullet> z)"
1.324 +  by (vector ring_simps)
1.325 +lemma dot_radd: "x \<bullet> (y + (z::'a::ring ^ 'n)) = (x \<bullet> y) + (x \<bullet> z)"
1.326 +  by (vector ring_simps)
1.327 +lemma dot_lsub: "((x::'a::ring ^ 'n) - y) \<bullet> z = (x \<bullet> z) - (y \<bullet> z)"
1.328 +  by (vector ring_simps)
1.329 +lemma dot_rsub: "(x::'a::ring ^ 'n) \<bullet> (y - z) = (x \<bullet> y) - (x \<bullet> z)"
1.330 +  by (vector ring_simps)
1.331 +lemma dot_lmult: "(c *s x) \<bullet> y = (c::'a::ring) * (x \<bullet> y)" by (vector ring_simps)
1.332 +lemma dot_rmult: "x \<bullet> (c *s y) = (c::'a::comm_ring) * (x \<bullet> y)" by (vector ring_simps)
1.333 +lemma dot_lneg: "(-x) \<bullet> (y::'a::ring ^ 'n) = -(x \<bullet> y)" by vector
1.334 +lemma dot_rneg: "(x::'a::ring ^ 'n) \<bullet> (-y) = -(x \<bullet> y)" by vector
1.335 +lemma dot_lzero[simp]: "0 \<bullet> x = (0::'a::{comm_monoid_add, mult_zero})" by vector
1.336 +lemma dot_rzero[simp]: "x \<bullet> 0 = (0::'a::{comm_monoid_add, mult_zero})" by vector
1.337 +lemma dot_pos_le[simp]: "(0::'a\<Colon>ordered_ring_strict) <= x \<bullet> x"
1.338 +  by (simp add: dot_def setsum_nonneg)
1.339 +
1.340 +lemma setsum_squares_eq_0_iff: assumes fS: "finite F" and fp: "\<forall>x \<in> F. f x \<ge> (0 ::'a::pordered_ab_group_add)" shows "setsum f F = 0 \<longleftrightarrow> (ALL x:F. f x = 0)"
1.341 +using fS fp setsum_nonneg[OF fp]
1.342 +proof (induct set: finite)
1.343 +  case empty thus ?case by simp
1.344 +next
1.345 +  case (insert x F)
1.346 +  from insert.prems have Fx: "f x \<ge> 0" and Fp: "\<forall> a \<in> F. f a \<ge> 0" by simp_all
1.347 +  from insert.hyps Fp setsum_nonneg[OF Fp]
1.348 +  have h: "setsum f F = 0 \<longleftrightarrow> (\<forall>a \<in>F. f a = 0)" by metis
1.349 +  from sum_nonneg_eq_zero_iff[OF Fx  setsum_nonneg[OF Fp]] insert.hyps(1,2)
1.350 +  show ?case by (simp add: h)
1.351 +qed
1.352 +
1.353 +lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n) = 0"
1.354 +proof-
1.355 +  {assume f: "finite (UNIV :: 'n set)"
1.356 +    let ?S = "{Suc 0 .. card (UNIV :: 'n set)}"
1.357 +    have fS: "finite ?S" using f by simp
1.358 +    have fp: "\<forall> i\<in> ?S. x\$i * x\$i>= 0" by simp
1.359 +    have ?thesis by (vector dimindex_def f setsum_squares_eq_0_iff[OF fS fp])}
1.360 +  moreover
1.361 +  {assume "\<not> finite (UNIV :: 'n set)" then have ?thesis by (vector dimindex_def)}
1.362 +  ultimately show ?thesis by metis
1.363 +qed
1.364 +
1.365 +lemma dot_pos_lt: "(0 < x \<bullet> x) \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n) \<noteq> 0" using dot_eq_0[of x] dot_pos_le[of x]
1.366 +  by (auto simp add: le_less)
1.367 +
1.368 +subsection {* Introduce norms, but defer many properties till we get square roots. *}
1.369 +text{* FIXME : This is ugly *}
1.371 +  real_of_real_def [code inline, simp]: "real == id"
1.372 +
1.373 +instantiation "^" :: ("{times, comm_monoid_add}", type) norm begin
1.374 +definition  real_vector_norm_def: "norm \<equiv> (\<lambda>x. sqrt (real (x \<bullet> x)))"
1.375 +instance ..
1.376 +end
1.377 +
1.378 +
1.379 +subsection{* The collapse of the general concepts to dimention one. *}
1.380 +
1.381 +lemma vector_one: "(x::'a ^1) = (\<chi> i. (x\$1))"
1.382 +  by (vector dimindex_def)
1.383 +
1.384 +lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
1.385 +  apply auto
1.386 +  apply (erule_tac x= "x\$1" in allE)
1.387 +  apply (simp only: vector_one[symmetric])
1.388 +  done
1.389 +
1.390 +lemma norm_real: "norm(x::real ^ 1) = abs(x\$1)"
1.391 +  by (simp add: real_vector_norm_def)
1.392 +
1.393 +text{* Metric *}
1.394 +
1.395 +definition dist:: "real ^ 'n \<Rightarrow> real ^ 'n \<Rightarrow> real" where
1.396 +  "dist x y = norm (x - y)"
1.397 +
1.398 +lemma dist_real: "dist(x::real ^ 1) y = abs((x\$1) - (y\$1))"
1.399 +  using dimindex_ge_1[of "UNIV :: 1 set"]
1.400 +  by (auto simp add: norm_real dist_def vector_component Cart_lambda_beta[where ?'a = "1"] )
1.401 +
1.402 +subsection {* A connectedness or intermediate value lemma with several applications. *}
1.403 +
1.404 +lemma connected_real_lemma:
1.405 +  fixes f :: "real \<Rightarrow> real ^ 'n"
1.406 +  assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
1.407 +  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"
1.408 +  and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
1.409 +  and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
1.410 +  and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
1.411 +  shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
1.412 +proof-
1.413 +  let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
1.414 +  have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa)
1.415 +  have Sub: "\<exists>y. isUb UNIV ?S y"
1.416 +    apply (rule exI[where x= b])
1.417 +    using ab fb e12 by (auto simp add: isUb_def setle_def)
1.418 +  from reals_complete[OF Se Sub] obtain l where
1.419 +    l: "isLub UNIV ?S l"by blast
1.420 +  have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
1.421 +    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
1.422 +    by (metis linorder_linear)
1.423 +  have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
1.424 +    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
1.425 +    by (metis linorder_linear not_le)
1.426 +    have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
1.427 +    have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
1.428 +    have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by dlo
1.429 +    {assume le2: "f l \<in> e2"
1.430 +      from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
1.431 +      hence lap: "l - a > 0" using alb by arith
1.432 +      from e2[rule_format, OF le2] obtain e where
1.433 +	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
1.434 +      from dst[OF alb e(1)] obtain d where
1.435 +	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
1.436 +      have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" using lap d(1)
1.437 +	apply ferrack by arith
1.438 +      then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
1.439 +      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
1.440 +      from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
1.441 +      moreover
1.442 +      have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
1.443 +      ultimately have False using e12 alb d' by auto}
1.444 +    moreover
1.445 +    {assume le1: "f l \<in> e1"
1.446 +    from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
1.447 +      hence blp: "b - l > 0" using alb by arith
1.448 +      from e1[rule_format, OF le1] obtain e where
1.449 +	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
1.450 +      from dst[OF alb e(1)] obtain d where
1.451 +	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
1.452 +      have "\<exists>d'. d' < d \<and> d' >0" using d(1) by dlo
1.453 +      then obtain d' where d': "d' > 0" "d' < d" by metis
1.454 +      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
1.455 +      hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
1.456 +      with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
1.457 +      with l d' have False
1.458 +	by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
1.459 +    ultimately show ?thesis using alb by metis
1.460 +qed
1.461 +
1.462 +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 real ^1 *}
1.463 +
1.464 +lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
1.465 +proof-
1.466 +  have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith
1.467 +  thus ?thesis by (simp add: ring_simps power2_eq_square)
1.468 +qed
1.469 +
1.470 +lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
1.471 +  using isCont_power[OF isCont_ident, of 2, unfolded isCont_def LIM_def, rule_format, of e x] apply (auto simp add: power2_eq_square)
1.472 +  apply (rule_tac x="s" in exI)
1.473 +  apply auto
1.474 +  apply (erule_tac x=y in allE)
1.475 +  apply auto
1.476 +  done
1.477 +
1.478 +lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
1.479 +  using real_sqrt_le_iff[of x "y^2"] by simp
1.480 +
1.481 +lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
1.482 +  using real_sqrt_le_mono[of "x^2" y] by simp
1.483 +
1.484 +lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
1.485 +  using real_sqrt_less_mono[of "x^2" y] by simp
1.486 +
1.487 +lemma sqrt_even_pow2: assumes n: "even n"
1.488 +  shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
1.489 +proof-
1.490 +  from n obtain m where m: "n = 2*m" unfolding even_nat_equiv_def2
1.491 +    by (auto simp add: nat_number)
1.492 +  from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
1.493 +    by (simp only: power_mult[symmetric] mult_commute)
1.494 +  then show ?thesis  using m by simp
1.495 +qed
1.496 +
1.497 +lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
1.498 +  apply (cases "x = 0", simp_all)
1.499 +  using sqrt_divide_self_eq[of x]
1.500 +  apply (simp add: inverse_eq_divide real_sqrt_ge_0_iff field_simps)
1.501 +  done
1.502 +
1.503 +text{* Hence derive more interesting properties of the norm. *}
1.504 +
1.505 +lemma norm_0: "norm (0::real ^ 'n) = 0"
1.506 +  by (simp add: real_vector_norm_def dot_eq_0)
1.507 +
1.508 +lemma norm_pos_le: "0 <= norm (x::real^'n)"
1.509 +  by (simp add: real_vector_norm_def dot_pos_le)
1.510 +lemma norm_neg: " norm(-x) = norm (x:: real ^ 'n)"
1.511 +  by (simp add: real_vector_norm_def dot_lneg dot_rneg)
1.512 +lemma norm_sub: "norm(x - y) = norm(y - (x::real ^ 'n))"
1.513 +  by (metis norm_neg minus_diff_eq)
1.514 +lemma norm_mul: "norm(a *s x) = abs(a) * norm x"
1.515 +  by (simp add: real_vector_norm_def dot_lmult dot_rmult mult_assoc[symmetric] real_sqrt_mult)
1.516 +lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (x \<bullet> x = (0::real))"
1.517 +  by (simp add: real_vector_norm_def)
1.518 +lemma norm_eq_0: "norm x = 0 \<longleftrightarrow> x = (0::real ^ 'n)"
1.519 +  by (simp add: real_vector_norm_def dot_eq_0)
1.520 +lemma norm_pos_lt: "0 < norm x \<longleftrightarrow> x \<noteq> (0::real ^ 'n)"
1.521 +  by (metis less_le real_vector_norm_def norm_pos_le norm_eq_0)
1.522 +lemma norm_pow_2: "norm x ^ 2 = x \<bullet> x"
1.523 +  by (simp add: real_vector_norm_def dot_pos_le)
1.524 +lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n)" by (metis norm_eq_0)
1.525 +lemma norm_le_0: "norm x <= 0 \<longleftrightarrow> x = (0::real ^'n)"
1.526 +  by (metis norm_eq_0 norm_pos_le order_antisym)
1.527 +lemma vector_mul_eq_0: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
1.528 +  by vector
1.529 +lemma vector_mul_lcancel: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
1.530 +  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
1.531 +lemma vector_mul_rcancel: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
1.532 +  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
1.533 +lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
1.534 +  by (metis vector_mul_lcancel)
1.535 +lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
1.536 +  by (metis vector_mul_rcancel)
1.537 +lemma norm_cauchy_schwarz: "x \<bullet> y <= norm x * norm y"
1.538 +proof-
1.539 +  {assume "norm x = 0"
1.540 +    hence ?thesis by (simp add: norm_eq_0 dot_lzero dot_rzero norm_0)}
1.541 +  moreover
1.542 +  {assume "norm y = 0"
1.543 +    hence ?thesis by (simp add: norm_eq_0 dot_lzero dot_rzero norm_0)}
1.544 +  moreover
1.545 +  {assume h: "norm x \<noteq> 0" "norm y \<noteq> 0"
1.546 +    let ?z = "norm y *s x - norm x *s y"
1.547 +    from h have p: "norm x * norm y > 0" by (metis norm_pos_le le_less zero_compare_simps)
1.548 +    from dot_pos_le[of ?z]
1.549 +    have "(norm x * norm y) * (x \<bullet> y) \<le> norm x ^2 * norm y ^2"
1.550 +      apply (simp add: dot_rsub dot_lsub dot_lmult dot_rmult ring_simps)
1.551 +      by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym)
1.552 +    hence "x\<bullet>y \<le> (norm x ^2 * norm y ^2) / (norm x * norm y)" using p
1.553 +      by (simp add: field_simps)
1.554 +    hence ?thesis using h by (simp add: power2_eq_square)}
1.555 +  ultimately show ?thesis by metis
1.556 +qed
1.557 +
1.558 +lemma norm_abs[simp]: "abs (norm x) = norm (x::real ^'n)"
1.559 +  using norm_pos_le[of x] by (simp add: real_abs_def linorder_linear)
1.560 +
1.561 +lemma norm_cauchy_schwarz_abs: "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
1.562 +  using norm_cauchy_schwarz[of x y] norm_cauchy_schwarz[of x "-y"]
1.563 +  by (simp add: real_abs_def dot_rneg norm_neg)
1.564 +lemma norm_triangle: "norm(x + y) <= norm x + norm (y::real ^'n)"
1.565 +  unfolding real_vector_norm_def
1.566 +  apply (rule real_le_lsqrt)
1.567 +  apply (auto simp add: dot_pos_le real_vector_norm_def[symmetric] norm_pos_le norm_pow_2[symmetric] intro: add_nonneg_nonneg)[1]
1.568 +  apply (auto simp add: dot_pos_le real_vector_norm_def[symmetric] norm_pos_le norm_pow_2[symmetric] intro: add_nonneg_nonneg)[1]
1.570 +    by (simp add: norm_pow_2[symmetric] power2_eq_square ring_simps norm_cauchy_schwarz)
1.571 +
1.572 +lemma norm_triangle_sub: "norm (x::real ^'n) <= norm(y) + norm(x - y)"
1.573 +  using norm_triangle[of "y" "x - y"] by (simp add: ring_simps)
1.574 +lemma norm_triangle_le: "norm(x::real ^'n) + norm y <= e ==> norm(x + y) <= e"
1.575 +  by (metis order_trans norm_triangle)
1.576 +lemma norm_triangle_lt: "norm(x::real ^'n) + norm(y) < e ==> norm(x + y) < e"
1.577 +  by (metis basic_trans_rules(21) norm_triangle)
1.578 +
1.579 +lemma setsum_delta:
1.580 +  assumes fS: "finite S"
1.581 +  shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
1.582 +proof-
1.583 +  let ?f = "(\<lambda>k. if k=a then b k else 0)"
1.584 +  {assume a: "a \<notin> S"
1.585 +    hence "\<forall> k\<in> S. ?f k = 0" by simp
1.586 +    hence ?thesis  using a by simp}
1.587 +  moreover
1.588 +  {assume a: "a \<in> S"
1.589 +    let ?A = "S - {a}"
1.590 +    let ?B = "{a}"
1.591 +    have eq: "S = ?A \<union> ?B" using a by blast
1.592 +    have dj: "?A \<inter> ?B = {}" by simp
1.593 +    from fS have fAB: "finite ?A" "finite ?B" by auto
1.594 +    have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
1.595 +      using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
1.596 +      by simp
1.597 +    then have ?thesis  using a by simp}
1.598 +  ultimately show ?thesis by blast
1.599 +qed
1.600 +
1.601 +lemma component_le_norm: "i \<in> {1 .. dimindex(UNIV :: 'n set)} ==> \<bar>x\$i\<bar> <= norm (x::real ^ 'n)"
1.602 +proof(simp add: real_vector_norm_def, rule real_le_rsqrt, clarsimp)
1.603 +  assume i: "Suc 0 \<le> i" "i \<le> dimindex (UNIV :: 'n set)"
1.604 +  let ?S = "{1 .. dimindex(UNIV :: 'n set)}"
1.605 +  let ?f = "(\<lambda>k. if k = i then x\$i ^2 else 0)"
1.606 +  have fS: "finite ?S" by simp
1.607 +  from i setsum_delta[OF fS, of i "\<lambda>k. x\$i ^ 2"]
1.608 +  have th: "x\$i^2 = setsum ?f ?S" by simp
1.609 +  let ?g = "\<lambda>k. x\$k * x\$k"
1.610 +  {fix x assume x: "x \<in> ?S" have "?f x \<le> ?g x" by (simp add: power2_eq_square)}
1.611 +  with setsum_mono[of ?S ?f ?g]
1.612 +  have "setsum ?f ?S \<le> setsum ?g ?S" by blast
1.613 +  then show "x\$i ^2 \<le> x \<bullet> (x:: real ^ 'n)" unfolding dot_def th[symmetric] .
1.614 +qed
1.615 +lemma norm_bound_component_le: "norm(x::real ^ 'n) <= e
1.616 +                ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x\$i\<bar> <= e"
1.617 +  by (metis component_le_norm order_trans)
1.618 +
1.619 +lemma norm_bound_component_lt: "norm(x::real ^ 'n) < e
1.620 +                ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x\$i\<bar> < e"
1.621 +  by (metis component_le_norm basic_trans_rules(21))
1.622 +
1.623 +lemma norm_le_l1: "norm (x:: real ^'n) <= setsum(\<lambda>i. \<bar>x\$i\<bar>) {1..dimindex(UNIV::'n set)}"
1.625 +  case 0 thus ?case by simp
1.626 +next
1.627 +  case (Suc n)
1.628 +  have th: "2 * (\<bar>x\$(Suc n)\<bar> * (\<Sum>i = Suc 0..n. \<bar>x\$i\<bar>)) \<ge> 0"
1.629 +    apply simp
1.630 +    apply (rule mult_nonneg_nonneg)
1.631 +    by (simp_all add: setsum_abs_ge_zero)
1.632 +
1.633 +  from Suc
1.634 +  show ?case using th by (simp add: power2_eq_square ring_simps)
1.635 +qed
1.636 +
1.637 +lemma real_abs_norm: "\<bar> norm x\<bar> = norm (x :: real ^'n)"
1.638 +  by (simp add: norm_pos_le)
1.639 +lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n) - norm y\<bar> <= norm(x - y)"
1.640 +  apply (simp add: abs_le_iff ring_simps)
1.641 +  by (metis norm_triangle_sub norm_sub)
1.642 +lemma norm_le: "norm(x::real ^ 'n) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
1.643 +  by (simp add: real_vector_norm_def)
1.644 +lemma norm_lt: "norm(x::real ^'n) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
1.645 +  by (simp add: real_vector_norm_def)
1.646 +lemma norm_eq: "norm (x::real ^'n) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
1.647 +  by (simp add: order_eq_iff norm_le)
1.648 +lemma norm_eq_1: "norm(x::real ^ 'n) = 1 \<longleftrightarrow> x \<bullet> x = 1"
1.649 +  by (simp add: real_vector_norm_def)
1.650 +
1.651 +text{* Squaring equations and inequalities involving norms.  *}
1.652 +
1.653 +lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
1.654 +  by (simp add: real_vector_norm_def  dot_pos_le )
1.655 +
1.656 +lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
1.657 +proof-
1.658 +  have th: "\<And>x y::real. x^2 = y^2 \<longleftrightarrow> x = y \<or> x = -y" by algebra
1.659 +  show ?thesis using norm_pos_le[of x]
1.660 +  apply (simp add: dot_square_norm th)
1.661 +  apply arith
1.662 +  done
1.663 +qed
1.664 +
1.665 +lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
1.666 +proof-
1.667 +  have "x^2 \<le> y^2 \<longleftrightarrow> (x -y) * (y + x) \<le> 0" by (simp add: ring_simps power2_eq_square)
1.668 +  also have "\<dots> \<longleftrightarrow> \<bar>x\<bar> \<le> \<bar>y\<bar>" apply (simp add: zero_compare_simps real_abs_def not_less) by arith
1.669 +finally show ?thesis ..
1.670 +qed
1.671 +
1.672 +lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
1.673 +  using norm_pos_le[of x]
1.674 +  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
1.675 +  apply arith
1.676 +  done
1.677 +
1.678 +lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2"
1.679 +  using norm_pos_le[of x]
1.680 +  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
1.681 +  apply arith
1.682 +  done
1.683 +
1.684 +lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
1.685 +  by (metis not_le norm_ge_square)
1.686 +lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
1.687 +  by (metis norm_le_square not_less)
1.688 +
1.689 +text{* Dot product in terms of the norm rather than conversely. *}
1.690 +
1.691 +lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
1.693 +
1.694 +lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
1.696 +
1.697 +
1.698 +text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
1.699 +
1.700 +lemma vector_eq: "(x:: real ^ 'n) = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y\<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
1.701 +proof
1.702 +  assume "?lhs" then show ?rhs by simp
1.703 +next
1.704 +  assume ?rhs
1.705 +  then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y\<bullet> y = 0" by simp
1.706 +  hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
1.707 +    by (simp add: dot_rsub dot_lsub dot_sym)
1.708 +  then have "(x - y) \<bullet> (x - y) = 0" by (simp add: ring_simps dot_lsub dot_rsub)
1.709 +  then show "x = y" by (simp add: dot_eq_0)
1.710 +qed
1.711 +
1.712 +
1.713 +subsection{* General linear decision procedure for normed spaces. *}
1.714 +
1.715 +lemma norm_cmul_rule_thm: "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(c *s x)"
1.716 +  apply (clarsimp simp add: norm_mul)
1.717 +  apply (rule mult_mono1)
1.718 +  apply simp_all
1.719 +  done
1.720 +
1.721 +lemma norm_add_rule_thm: "b1 >= norm(x1 :: real ^'n) \<Longrightarrow> b2 >= norm(x2) ==> b1 + b2 >= norm(x1 + x2)"
1.722 +  apply (rule norm_triangle_le) by simp
1.723 +
1.724 +lemma ge_iff_diff_ge_0: "(a::'a::ordered_ring) \<ge> b == a - b \<ge> 0"
1.725 +  by (simp add: ring_simps)
1.726 +
1.727 +lemma pth_1: "(x::real^'n) == 1 *s x" by (simp only: vector_smult_lid)
1.728 +lemma pth_2: "x - (y::real^'n) == x + -y" by (atomize (full)) simp
1.729 +lemma pth_3: "(-x::real^'n) == -1 *s x" by vector
1.730 +lemma pth_4: "0 *s (x::real^'n) == 0" "c *s 0 = (0::real ^ 'n)" by vector+
1.731 +lemma pth_5: "c *s (d *s x) == (c * d) *s (x::real ^ 'n)" by (atomize (full)) vector
1.732 +lemma pth_6: "(c::real) *s (x + y) == c *s x + c *s y" by (atomize (full)) (vector ring_simps)
1.733 +lemma pth_7: "0 + x == (x::real^'n)" "x + 0 == x" by simp_all
1.734 +lemma pth_8: "(c::real) *s x + d *s x == (c + d) *s x" by (atomize (full)) (vector ring_simps)
1.735 +lemma pth_9: "((c::real) *s x + z) + d *s x == (c + d) *s x + z"
1.736 +  "c *s x + (d *s x + z) == (c + d) *s x + z"
1.737 +  "(c *s x + w) + (d *s x + z) == (c + d) *s x + (w + z)" by ((atomize (full)), vector ring_simps)+
1.738 +lemma pth_a: "(0::real) *s x + y == y" by (atomize (full)) vector
1.739 +lemma pth_b: "(c::real) *s x + d *s y == c *s x + d *s y"
1.740 +  "(c *s x + z) + d *s y == c *s x + (z + d *s y)"
1.741 +  "c *s x + (d *s y + z) == c *s x + (d *s y + z)"
1.742 +  "(c *s x + w) + (d *s y + z) == c *s x + (w + (d *s y + z))"
1.743 +  by ((atomize (full)), vector)+
1.744 +lemma pth_c: "(c::real) *s x + d *s y == d *s y + c *s x"
1.745 +  "(c *s x + z) + d *s y == d *s y + (c *s x + z)"
1.746 +  "c *s x + (d *s y + z) == d *s y + (c *s x + z)"
1.747 +  "(c *s x + w) + (d *s y + z) == d *s y + ((c *s x + w) + z)" by ((atomize (full)), vector)+
1.748 +lemma pth_d: "x + (0::real ^'n) == x" by (atomize (full)) vector
1.749 +
1.750 +lemma norm_imp_pos_and_ge: "norm (x::real ^ 'n) == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
1.751 +  by (atomize) (auto simp add: norm_pos_le)
1.752 +
1.753 +lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
1.754 +
1.755 +lemma norm_pths:
1.756 +  "(x::real ^'n) = y \<longleftrightarrow> norm (x - y) \<le> 0"
1.757 +  "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
1.758 +  using norm_pos_le[of "x - y"] by (auto simp add: norm_0 norm_eq_0)
1.759 +
1.760 +use "normarith.ML"
1.761 +
1.762 +method_setup norm = {* Method.ctxt_args (Method.SIMPLE_METHOD' o NormArith.norm_arith_tac)
1.763 +*} "Proves simple linear statements about vector norms"
1.764 +
1.765 +
1.766 +
1.767 +text{* Hence more metric properties. *}
1.768 +
1.769 +lemma dist_refl: "dist x x = 0" by norm
1.770 +
1.771 +lemma dist_sym: "dist x y = dist y x"by norm
1.772 +
1.773 +lemma dist_pos_le: "0 <= dist x y" by norm
1.774 +
1.775 +lemma dist_triangle: "dist x z <= dist x y + dist y z" by norm
1.776 +
1.777 +lemma dist_triangle_alt: "dist y z <= dist x y + dist x z" by norm
1.778 +
1.779 +lemma dist_eq_0: "dist x y = 0 \<longleftrightarrow> x = y" by norm
1.780 +
1.781 +lemma dist_pos_lt: "x \<noteq> y ==> 0 < dist x y" by norm
1.782 +lemma dist_nz:  "x \<noteq> y \<longleftrightarrow> 0 < dist x y" by norm
1.783 +
1.784 +lemma dist_triangle_le: "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e" by norm
1.785 +
1.786 +lemma dist_triangle_lt: "dist x z + dist y z < e ==> dist x y < e" by norm
1.787 +
1.788 +lemma dist_triangle_half_l: "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 ==> dist x1 x2 < e" by norm
1.789 +
1.790 +lemma dist_triangle_half_r: "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 ==> dist x1 x2 < e" by norm
1.791 +
1.792 +lemma dist_triangle_add: "dist (x + y) (x' + y') <= dist x x' + dist y y'"
1.793 +  by norm
1.794 +
1.795 +lemma dist_mul: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
1.796 +  unfolding dist_def vector_ssub_ldistrib[symmetric] norm_mul ..
1.797 +
1.798 +lemma dist_triangle_add_half: " dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 ==> dist(x + y) (x' + y') < e" by norm
1.799 +
1.800 +lemma dist_le_0: "dist x y <= 0 \<longleftrightarrow> x = y" by norm
1.801 +
1.803 +begin
1.804 +  instance by (intro_classes)
1.805 +end
1.806 +
1.807 +lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)\$i ) S)"
1.808 +  apply vector
1.809 +  apply auto
1.810 +  apply (cases "finite S")
1.811 +  apply (rule finite_induct[of S])
1.812 +  apply (auto simp add: vector_component zero_index)
1.813 +  done
1.814 +
1.815 +lemma setsum_clauses:
1.816 +  shows "setsum f {} = 0"
1.817 +  and "finite S \<Longrightarrow> setsum f (insert x S) =
1.818 +                 (if x \<in> S then setsum f S else f x + setsum f S)"
1.819 +  by (auto simp add: insert_absorb)
1.820 +
1.821 +lemma setsum_cmul:
1.822 +  fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
1.823 +  shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
1.824 +  by (simp add: setsum_eq Cart_eq Cart_lambda_beta vector_component setsum_right_distrib)
1.825 +
1.826 +lemma setsum_component:
1.827 +  fixes f:: " 'a \<Rightarrow> ('b::semiring_1) ^'n"
1.828 +  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
1.829 +  shows "(setsum f S)\$i = setsum (\<lambda>x. (f x)\$i) S"
1.830 +  using i by (simp add: setsum_eq Cart_lambda_beta)
1.831 +
1.832 +  (* This needs finiteness assumption due to the definition of fold!!! *)
1.833 +
1.834 +lemma setsum_superset:
1.835 +  assumes fb: "finite B" and ab: "A \<subseteq> B"
1.836 +  and f0: "\<forall>x \<in> B - A. f x = 0"
1.837 +  shows "setsum f B = setsum f A"
1.838 +proof-
1.839 +  from ab fb have fa: "finite A" by (metis finite_subset)
1.840 +  from fb have fba: "finite (B - A)" by (metis finite_Diff)
1.841 +  have d: "A \<inter> (B - A) = {}" by blast
1.842 +  from ab have b: "B = A \<union> (B - A)" by blast
1.843 +  from setsum_Un_disjoint[OF fa fba d, of f] b
1.844 +    setsum_0'[OF f0]
1.845 +  show "setsum f B = setsum f A" by simp
1.846 +qed
1.847 +
1.848 +lemma setsum_restrict_set:
1.849 +  assumes fA: "finite A"
1.850 +  shows "setsum f (A \<inter> B) = setsum (\<lambda>x. if x \<in> B then f x else 0) A"
1.851 +proof-
1.852 +  from fA have fab: "finite (A \<inter> B)" by auto
1.853 +  have aba: "A \<inter> B \<subseteq> A" by blast
1.854 +  let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
1.855 +  from setsum_superset[OF fA aba, of ?g]
1.856 +  show ?thesis by simp
1.857 +qed
1.858 +
1.859 +lemma setsum_cases:
1.860 +  assumes fA: "finite A"
1.861 +  shows "setsum (\<lambda>x. if x \<in> B then f x else g x) A =
1.862 +         setsum f (A \<inter> B) + setsum g (A \<inter> - B)"
1.863 +proof-
1.864 +  have a: "A = A \<inter> B \<union> A \<inter> -B" "(A \<inter> B) \<inter> (A \<inter> -B) = {}"
1.865 +    by blast+
1.866 +  from fA
1.867 +  have f: "finite (A \<inter> B)" "finite (A \<inter> -B)" by auto
1.868 +  let ?g = "\<lambda>x. if x \<in> B then f x else g x"
1.869 +  from setsum_Un_disjoint[OF f a(2), of ?g] a(1)
1.870 +  show ?thesis by simp
1.871 +qed
1.872 +
1.873 +lemma setsum_norm:
1.874 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.875 +  assumes fS: "finite S"
1.876 +  shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
1.877 +proof(induct rule: finite_induct[OF fS])
1.878 +  case 1 thus ?case by (simp add: norm_zero)
1.879 +next
1.880 +  case (2 x S)
1.881 +  from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
1.882 +  also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
1.883 +    using "2.hyps" by simp
1.884 +  finally  show ?case  using "2.hyps" by simp
1.885 +qed
1.886 +
1.887 +lemma real_setsum_norm:
1.888 +  fixes f :: "'a \<Rightarrow> real ^'n"
1.889 +  assumes fS: "finite S"
1.890 +  shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
1.891 +proof(induct rule: finite_induct[OF fS])
1.892 +  case 1 thus ?case by simp norm
1.893 +next
1.894 +  case (2 x S)
1.895 +  from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" apply (simp add: norm_triangle_ineq) by norm
1.896 +  also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
1.897 +    using "2.hyps" by simp
1.898 +  finally  show ?case  using "2.hyps" by simp
1.899 +qed
1.900 +
1.901 +lemma setsum_norm_le:
1.902 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.903 +  assumes fS: "finite S"
1.904 +  and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
1.905 +  shows "norm (setsum f S) \<le> setsum g S"
1.906 +proof-
1.907 +  from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
1.908 +    by - (rule setsum_mono, simp)
1.909 +  then show ?thesis using setsum_norm[OF fS, of f] fg
1.910 +    by arith
1.911 +qed
1.912 +
1.913 +lemma real_setsum_norm_le:
1.914 +  fixes f :: "'a \<Rightarrow> real ^ 'n"
1.915 +  assumes fS: "finite S"
1.916 +  and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
1.917 +  shows "norm (setsum f S) \<le> setsum g S"
1.918 +proof-
1.919 +  from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
1.920 +    by - (rule setsum_mono, simp)
1.921 +  then show ?thesis using real_setsum_norm[OF fS, of f] fg
1.922 +    by arith
1.923 +qed
1.924 +
1.925 +lemma setsum_norm_bound:
1.926 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.927 +  assumes fS: "finite S"
1.928 +  and K: "\<forall>x \<in> S. norm (f x) \<le> K"
1.929 +  shows "norm (setsum f S) \<le> of_nat (card S) * K"
1.930 +  using setsum_norm_le[OF fS K] setsum_constant[symmetric]
1.931 +  by simp
1.932 +
1.933 +lemma real_setsum_norm_bound:
1.934 +  fixes f :: "'a \<Rightarrow> real ^ 'n"
1.935 +  assumes fS: "finite S"
1.936 +  and K: "\<forall>x \<in> S. norm (f x) \<le> K"
1.937 +  shows "norm (setsum f S) \<le> of_nat (card S) * K"
1.938 +  using real_setsum_norm_le[OF fS K] setsum_constant[symmetric]
1.939 +  by simp
1.940 +
1.941 +instantiation "^" :: ("{scaleR, one, times}",type) scaleR
1.942 +begin
1.943 +
1.944 +definition vector_scaleR_def: "(scaleR :: real \<Rightarrow> 'a ^'b \<Rightarrow> 'a ^'b) \<equiv> (\<lambda> c x . (scaleR c 1) *s x)"
1.945 +instance ..
1.946 +end
1.947 +
1.948 +instantiation "^" :: ("ring_1",type) ring_1
1.949 +begin
1.950 +instance by intro_classes
1.951 +end
1.952 +
1.953 +instantiation "^" :: (real_algebra_1,type) real_vector
1.954 +begin
1.955 +
1.956 +instance
1.957 +  apply intro_classes
1.958 +  apply (simp_all  add: vector_scaleR_def)
1.960 +  done
1.961 +end
1.962 +
1.963 +instantiation "^" :: (real_algebra_1,type) real_algebra
1.964 +begin
1.965 +
1.966 +instance
1.967 +  apply intro_classes
1.968 +  apply (simp_all add: vector_scaleR_def ring_simps)
1.969 +  apply vector
1.970 +  apply vector
1.971 +  done
1.972 +end
1.973 +
1.974 +instantiation "^" :: (real_algebra_1,type) real_algebra_1
1.975 +begin
1.976 +
1.977 +instance ..
1.978 +end
1.979 +
1.980 +lemma setsum_vmul:
1.981 +  fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector,semiring, mult_zero}"
1.982 +  assumes fS: "finite S"
1.983 +  shows "setsum f S *s v = setsum (\<lambda>x. f x *s v) S"
1.984 +proof(induct rule: finite_induct[OF fS])
1.985 +  case 1 then show ?case by (simp add: vector_smult_lzero)
1.986 +next
1.987 +  case (2 x F)
1.988 +  from "2.hyps" have "setsum f (insert x F) *s v = (f x + setsum f F) *s v"
1.989 +    by simp
1.990 +  also have "\<dots> = f x *s v + setsum f F *s v"
1.992 +  also have "\<dots> = setsum (\<lambda>x. f x *s v) (insert x F)" using "2.hyps" by simp
1.993 +  finally show ?case .
1.994 +qed
1.995 +
1.996 +(* FIXME : Problem thm setsum_vmul[of _ "f:: 'a \<Rightarrow> real ^'n"]  ---
1.997 + Get rid of *s and use real_vector instead! Also prove that ^ creates a real_vector !! *)
1.998 +
1.999 +lemma setsum_add_split: assumes mn: "(m::nat) \<le> n + 1"
1.1000 +  shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
1.1001 +proof-
1.1002 +  let ?A = "{m .. n}"
1.1003 +  let ?B = "{n + 1 .. n + p}"
1.1004 +  have eq: "{m .. n+p} = ?A \<union> ?B" using mn by auto
1.1005 +  have d: "?A \<inter> ?B = {}" by auto
1.1006 +  from setsum_Un_disjoint[of "?A" "?B" f] eq d show ?thesis by auto
1.1007 +qed
1.1008 +
1.1009 +lemma setsum_reindex_nonzero:
1.1010 +  assumes fS: "finite S"
1.1011 +  and nz: "\<And> x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x = f y \<Longrightarrow> h (f x) = 0"
1.1012 +  shows "setsum h (f ` S) = setsum (h o f) S"
1.1013 +using nz
1.1014 +proof(induct rule: finite_induct[OF fS])
1.1015 +  case 1 thus ?case by simp
1.1016 +next
1.1017 +  case (2 x F)
1.1018 +  {assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
1.1019 +    then obtain y where y: "y \<in> F" "f x = f y" by auto
1.1020 +    from "2.hyps" y have xy: "x \<noteq> y" by auto
1.1021 +
1.1022 +    from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
1.1023 +    have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
1.1024 +    also have "\<dots> = setsum (h o f) (insert x F)"
1.1025 +      using "2.hyps" "2.prems" h0  by auto
1.1026 +    finally have ?case .}
1.1027 +  moreover
1.1028 +  {assume fxF: "f x \<notin> f ` F"
1.1029 +    have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)"
1.1030 +      using fxF "2.hyps" by simp
1.1031 +    also have "\<dots> = setsum (h o f) (insert x F)"
1.1032 +      using "2.hyps" "2.prems" fxF
1.1033 +      apply auto apply metis done
1.1034 +    finally have ?case .}
1.1035 +  ultimately show ?case by blast
1.1036 +qed
1.1037 +
1.1038 +lemma setsum_Un_nonzero:
1.1039 +  assumes fS: "finite S" and fF: "finite F"
1.1040 +  and f: "\<forall> x\<in> S \<inter> F . f x = (0::'a::ab_group_add)"
1.1041 +  shows "setsum f (S \<union> F) = setsum f S + setsum f F"
1.1042 +  using setsum_Un[OF fS fF, of f] setsum_0'[OF f] by simp
1.1043 +
1.1044 +lemma setsum_natinterval_left:
1.1045 +  assumes mn: "(m::nat) <= n"
1.1046 +  shows "setsum f {m..n} = f m + setsum f {m + 1..n}"
1.1047 +proof-
1.1048 +  from mn have "{m .. n} = insert m {m+1 .. n}" by auto
1.1049 +  then show ?thesis by auto
1.1050 +qed
1.1051 +
1.1052 +lemma setsum_natinterval_difff:
1.1053 +  fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
1.1054 +  shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
1.1055 +          (if m <= n then f m - f(n + 1) else 0)"
1.1056 +by (induct n, auto simp add: ring_simps not_le le_Suc_eq)
1.1057 +
1.1058 +lemmas setsum_restrict_set' = setsum_restrict_set[unfolded Int_def]
1.1059 +
1.1060 +lemma setsum_setsum_restrict:
1.1061 +  "finite S \<Longrightarrow> finite T \<Longrightarrow> setsum (\<lambda>x. setsum (\<lambda>y. f x y) {y. y\<in> T \<and> R x y}) S = setsum (\<lambda>y. setsum (\<lambda>x. f x y) {x. x \<in> S \<and> R x y}) T"
1.1062 +  apply (simp add: setsum_restrict_set'[unfolded mem_def] mem_def)
1.1063 +  by (rule setsum_commute)
1.1064 +
1.1065 +lemma setsum_image_gen: assumes fS: "finite S"
1.1066 +  shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
1.1067 +proof-
1.1068 +  {fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto}
1.1069 +  note th0 = this
1.1070 +  have "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
1.1071 +    apply (rule setsum_cong2)
1.1072 +    by (simp add: th0)
1.1073 +  also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
1.1074 +    apply (rule setsum_setsum_restrict[OF fS])
1.1075 +    by (rule finite_imageI[OF fS])
1.1076 +  finally show ?thesis .
1.1077 +qed
1.1078 +
1.1079 +    (* FIXME: Here too need stupid finiteness assumption on T!!! *)
1.1080 +lemma setsum_group:
1.1081 +  assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
1.1082 +  shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
1.1083 +
1.1084 +apply (subst setsum_image_gen[OF fS, of g f])
1.1085 +apply (rule setsum_superset[OF fT fST])
1.1086 +by (auto intro: setsum_0')
1.1087 +
1.1088 +(* FIXME: Change the name to fold_image\<dots> *)
1.1089 +lemma (in comm_monoid_mult) fold_1': "finite S \<Longrightarrow> (\<forall>x\<in>S. f x = 1) \<Longrightarrow> fold_image op * f 1 S = 1"
1.1090 +  apply (induct set: finite)
1.1091 +  apply simp by (auto simp add: fold_image_insert)
1.1092 +
1.1093 +lemma (in comm_monoid_mult) fold_union_nonzero:
1.1094 +  assumes fS: "finite S" and fT: "finite T"
1.1095 +  and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
1.1096 +  shows "fold_image (op *) f 1 (S \<union> T) = fold_image (op *) f 1 S * fold_image (op *) f 1 T"
1.1097 +proof-
1.1098 +  have "fold_image op * f 1 (S \<inter> T) = 1"
1.1099 +    apply (rule fold_1')
1.1100 +    using fS fT I0 by auto
1.1101 +  with fold_image_Un_Int[OF fS fT] show ?thesis by simp
1.1102 +qed
1.1103 +
1.1104 +lemma setsum_union_nonzero:
1.1105 +  assumes fS: "finite S" and fT: "finite T"
1.1106 +  and I0: "\<forall>x \<in> S\<inter>T. f x = 0"
1.1107 +  shows "setsum f (S \<union> T) = setsum f S  + setsum f T"
1.1108 +  using fS fT
1.1109 +  apply (simp add: setsum_def)
1.1111 +  using I0 by auto
1.1112 +
1.1113 +lemma setprod_union_nonzero:
1.1114 +  assumes fS: "finite S" and fT: "finite T"
1.1115 +  and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
1.1116 +  shows "setprod f (S \<union> T) = setprod f S  * setprod f T"
1.1117 +  using fS fT
1.1118 +  apply (simp add: setprod_def)
1.1119 +  apply (rule fold_union_nonzero)
1.1120 +  using I0 by auto
1.1121 +
1.1122 +lemma setsum_unions_nonzero:
1.1123 +  assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
1.1124 +  and f0: "\<And>T1 T2 x. T1\<in>S \<Longrightarrow> T2\<in>S \<Longrightarrow> T1 \<noteq> T2 \<Longrightarrow> x \<in> T1 \<Longrightarrow> x \<in> T2 \<Longrightarrow> f x = 0"
1.1125 +  shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
1.1126 +  using fSS f0
1.1127 +proof(induct rule: finite_induct[OF fS])
1.1128 +  case 1 thus ?case by simp
1.1129 +next
1.1130 +  case (2 T F)
1.1131 +  then have fTF: "finite T" "\<forall>T\<in>F. finite T" "finite F" and TF: "T \<notin> F"
1.1132 +    and H: "setsum f (\<Union> F) = setsum (setsum f) F" by (auto simp add: finite_insert)
1.1133 +  from fTF have fUF: "finite (\<Union>F)" by (auto intro: finite_Union)
1.1134 +  from "2.prems" TF fTF
1.1135 +  show ?case
1.1136 +    by (auto simp add: H[symmetric] intro: setsum_union_nonzero[OF fTF(1) fUF, of f])
1.1137 +qed
1.1138 +
1.1139 +  (* FIXME : Copied from Pocklington --- should be moved to Finite_Set!!!!!!!! *)
1.1140 +
1.1141 +
1.1142 +lemma (in comm_monoid_mult) fold_related:
1.1143 +  assumes Re: "R e e"
1.1144 +  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)"
1.1145 +  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
1.1146 +  shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
1.1147 +  using fS by (rule finite_subset_induct) (insert assms, auto)
1.1148 +
1.1149 +  (* FIXME: I think we can get rid of the finite assumption!! *)
1.1150 +lemma (in comm_monoid_mult)
1.1151 +  fold_eq_general:
1.1152 +  assumes fS: "finite S"
1.1153 +  and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y"
1.1154 +  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
1.1155 +  shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
1.1156 +proof-
1.1157 +  from h f12 have hS: "h ` S = S'" by auto
1.1158 +  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
1.1159 +    from f12 h H  have "x = y" by auto }
1.1160 +  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
1.1161 +  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto
1.1162 +  from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
1.1163 +  also have "\<dots> = fold_image (op *) (f2 o h) e S"
1.1164 +    using fold_image_reindex[OF fS hinj, of f2 e] .
1.1165 +  also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
1.1166 +    by blast
1.1167 +  finally show ?thesis ..
1.1168 +qed
1.1169 +
1.1170 +lemma (in comm_monoid_mult) fold_eq_general_inverses:
1.1171 +  assumes fS: "finite S"
1.1172 +  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
1.1173 +  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
1.1174 +  shows "fold_image (op *) f e S = fold_image (op *) g e T"
1.1175 +  using fold_eq_general[OF fS, of T h g f e] kh hk by metis
1.1176 +
1.1177 +lemma setsum_eq_general_reverses:
1.1178 +  assumes fS: "finite S" and fT: "finite T"
1.1179 +  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
1.1180 +  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
1.1181 +  shows "setsum f S = setsum g T"
1.1182 +  apply (simp add: setsum_def fS fT)
1.1183 +  apply (rule comm_monoid_add.fold_eq_general_inverses[OF fS])
1.1184 +  apply (erule kh)
1.1185 +  apply (erule hk)
1.1186 +  done
1.1187 +
1.1188 +lemma vsum_norm_allsubsets_bound:
1.1189 +  fixes f:: "'a \<Rightarrow> real ^'n"
1.1190 +  assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
1.1191 +  shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real (dimindex(UNIV :: 'n set)) *  e"
1.1192 +proof-
1.1193 +  let ?d = "real (dimindex (UNIV ::'n set))"
1.1194 +  let ?nf = "\<lambda>x. norm (f x)"
1.1195 +  let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
1.1196 +  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"
1.1197 +    by (rule setsum_commute)
1.1198 +  have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
1.1199 +  have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x \$ i\<bar>) ?U) P"
1.1200 +    apply (rule setsum_mono)
1.1201 +    by (rule norm_le_l1)
1.1202 +  also have "\<dots> \<le> 2 * ?d * e"
1.1203 +    unfolding th0 th1
1.1204 +  proof(rule setsum_bounded)
1.1205 +    fix i assume i: "i \<in> ?U"
1.1206 +    let ?Pp = "{x. x\<in> P \<and> f x \$ i \<ge> 0}"
1.1207 +    let ?Pn = "{x. x \<in> P \<and> f x \$ i < 0}"
1.1208 +    have thp: "P = ?Pp \<union> ?Pn" by auto
1.1209 +    have thp0: "?Pp \<inter> ?Pn ={}" by auto
1.1210 +    have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
1.1211 +    have Ppe:"setsum (\<lambda>x. \<bar>f x \$ i\<bar>) ?Pp \<le> e"
1.1212 +      using i component_le_norm[OF i, of "setsum (\<lambda>x. f x) ?Pp"]  fPs[OF PpP]
1.1213 +      by (auto simp add: setsum_component intro: abs_le_D1)
1.1214 +    have Pne: "setsum (\<lambda>x. \<bar>f x \$ i\<bar>) ?Pn \<le> e"
1.1215 +      using i component_le_norm[OF i, of "setsum (\<lambda>x. - f x) ?Pn"]  fPs[OF PnP]
1.1216 +      by (auto simp add: setsum_negf norm_neg setsum_component vector_component intro: abs_le_D1)
1.1217 +    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"
1.1218 +      apply (subst thp)
1.1219 +      apply (rule setsum_Un_nonzero)
1.1220 +      using fP thp0 by auto
1.1221 +    also have "\<dots> \<le> 2*e" using Pne Ppe by arith
1.1222 +    finally show "setsum (\<lambda>x. \<bar>f x \$ i\<bar>) P \<le> 2*e" .
1.1223 +  qed
1.1224 +  finally show ?thesis .
1.1225 +qed
1.1226 +
1.1227 +lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> (y::'a::{comm_ring}^'n) = setsum (\<lambda>x. f x \<bullet> y) S "
1.1229 +
1.1230 +lemma dot_rsum: "finite S \<Longrightarrow> (y::'a::{comm_ring}^'n) \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
1.1232 +
1.1233 +subsection{* Basis vectors in coordinate directions. *}
1.1234 +
1.1235 +
1.1236 +definition "basis k = (\<chi> i. if i = k then 1 else 0)"
1.1237 +
1.1238 +lemma delta_mult_idempotent:
1.1239 +  "(if k=a then 1 else (0::'a::semiring_1)) * (if k=a then 1 else 0) = (if k=a then 1 else 0)" by (cases "k=a", auto)
1.1240 +
1.1241 +lemma norm_basis:
1.1242 +  assumes k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.1243 +  shows "norm (basis k :: real ^'n) = 1"
1.1244 +  using k
1.1245 +  apply (simp add: basis_def real_vector_norm_def dot_def)
1.1246 +  apply (vector delta_mult_idempotent)
1.1247 +  using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "k" "\<lambda>k. 1::real"]
1.1248 +  apply auto
1.1249 +  done
1.1250 +
1.1251 +lemma norm_basis_1: "norm(basis 1 :: real ^'n) = 1"
1.1252 +  apply (simp add: basis_def real_vector_norm_def dot_def)
1.1253 +  apply (vector delta_mult_idempotent)
1.1254 +  using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "1" "\<lambda>k. 1::real"] dimindex_nonzero[of "UNIV :: 'n set"]
1.1255 +  apply auto
1.1256 +  done
1.1257 +
1.1258 +lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n). norm x = c"
1.1259 +  apply (rule exI[where x="c *s basis 1"])
1.1260 +  by (simp only: norm_mul norm_basis_1)
1.1261 +
1.1262 +lemma vector_choose_dist: assumes e: "0 <= e"
1.1263 +  shows "\<exists>(y::real^'n). dist x y = e"
1.1264 +proof-
1.1265 +  from vector_choose_size[OF e] obtain c:: "real ^'n"  where "norm c = e"
1.1266 +    by blast
1.1267 +  then have "dist x (x - c) = e" by (simp add: dist_def)
1.1268 +  then show ?thesis by blast
1.1269 +qed
1.1270 +
1.1271 +lemma basis_inj: "inj_on (basis :: nat \<Rightarrow> real ^'n) {1 .. dimindex (UNIV :: 'n set)}"
1.1272 +  by (auto simp add: inj_on_def basis_def Cart_eq Cart_lambda_beta)
1.1273 +
1.1274 +lemma basis_component: "i \<in> {1 .. dimindex(UNIV:: 'n set)} ==> (basis k ::('a::semiring_1)^'n)\$i = (if k=i then 1 else 0)"
1.1275 +  by (simp add: basis_def Cart_lambda_beta)
1.1276 +
1.1277 +lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
1.1278 +  by auto
1.1279 +
1.1280 +lemma basis_expansion:
1.1281 +  "setsum (\<lambda>i. (x\$i) *s basis i) {1 .. dimindex (UNIV :: 'n set)} = (x::('a::ring_1) ^'n)" (is "?lhs = ?rhs" is "setsum ?f ?S = _")
1.1282 +  by (auto simp add: Cart_eq basis_component[where ?'n = "'n"] setsum_component vector_component cond_value_iff setsum_delta[of "?S", where ?'b = "'a", simplified] cong del: if_weak_cong)
1.1283 +
1.1284 +lemma basis_expansion_unique:
1.1285 +  "setsum (\<lambda>i. f i *s basis i) {1 .. dimindex (UNIV :: 'n set)} = (x::('a::comm_ring_1) ^'n) \<longleftrightarrow> (\<forall>i\<in>{1 .. dimindex(UNIV:: 'n set)}. f i = x\$i)"
1.1286 +  by (simp add: Cart_eq setsum_component vector_component basis_component setsum_delta cond_value_iff cong del: if_weak_cong)
1.1287 +
1.1288 +lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
1.1289 +  by auto
1.1290 +
1.1291 +lemma dot_basis:
1.1292 +  assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.1293 +  shows "basis i \<bullet> x = x\$i" "x \<bullet> (basis i :: 'a^'n) = (x\$i :: 'a::semiring_1)"
1.1294 +  using i
1.1295 +  by (auto simp add: dot_def basis_def Cart_lambda_beta cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
1.1296 +
1.1297 +lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> i \<notin> {1..dimindex(UNIV ::'n set)}"
1.1298 +  by (auto simp add: Cart_eq basis_component zero_index)
1.1299 +
1.1300 +lemma basis_nonzero:
1.1301 +  assumes k: "k \<in> {1 .. dimindex(UNIV ::'n set)}"
1.1302 +  shows "basis k \<noteq> (0:: 'a::semiring_1 ^'n)"
1.1303 +  using k by (simp add: basis_eq_0)
1.1304 +
1.1305 +lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n)"
1.1306 +  apply (auto simp add: Cart_eq dot_basis)
1.1307 +  apply (erule_tac x="basis i" in allE)
1.1308 +  apply (simp add: dot_basis)
1.1309 +  apply (subgoal_tac "y = z")
1.1310 +  apply simp
1.1311 +  apply vector
1.1312 +  done
1.1313 +
1.1314 +lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n)"
1.1315 +  apply (auto simp add: Cart_eq dot_basis)
1.1316 +  apply (erule_tac x="basis i" in allE)
1.1317 +  apply (simp add: dot_basis)
1.1318 +  apply (subgoal_tac "x = y")
1.1319 +  apply simp
1.1320 +  apply vector
1.1321 +  done
1.1322 +
1.1323 +subsection{* Orthogonality. *}
1.1324 +
1.1325 +definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
1.1326 +
1.1327 +lemma orthogonal_basis:
1.1328 +  assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}"
1.1329 +  shows "orthogonal (basis i :: 'a^'n) x \<longleftrightarrow> x\$i = (0::'a::ring_1)"
1.1330 +  using i
1.1331 +  by (auto simp add: orthogonal_def dot_def basis_def Cart_lambda_beta cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
1.1332 +
1.1333 +lemma orthogonal_basis_basis:
1.1334 +  assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}"
1.1335 +  and j: "j \<in> {1 .. dimindex(UNIV ::'n set)}"
1.1336 +  shows "orthogonal (basis i :: 'a::ring_1^'n) (basis j) \<longleftrightarrow> i \<noteq> j"
1.1337 +  unfolding orthogonal_basis[OF i] basis_component[OF i] by simp
1.1338 +
1.1339 +  (* FIXME : Maybe some of these require less than comm_ring, but not all*)
1.1340 +lemma orthogonal_clauses:
1.1341 +  "orthogonal a (0::'a::comm_ring ^'n)"
1.1342 +  "orthogonal a x ==> orthogonal a (c *s x)"
1.1343 +  "orthogonal a x ==> orthogonal a (-x)"
1.1344 +  "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x + y)"
1.1345 +  "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x - y)"
1.1346 +  "orthogonal 0 a"
1.1347 +  "orthogonal x a ==> orthogonal (c *s x) a"
1.1348 +  "orthogonal x a ==> orthogonal (-x) a"
1.1349 +  "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x + y) a"
1.1350 +  "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x - y) a"
1.1351 +  unfolding orthogonal_def dot_rneg dot_rmult dot_radd dot_rsub
1.1352 +  dot_lzero dot_rzero dot_lneg dot_lmult dot_ladd dot_lsub
1.1353 +  by simp_all
1.1354 +
1.1355 +lemma orthogonal_commute: "orthogonal (x::'a::{ab_semigroup_mult,comm_monoid_add} ^'n)y \<longleftrightarrow> orthogonal y x"
1.1356 +  by (simp add: orthogonal_def dot_sym)
1.1357 +
1.1358 +subsection{* Explicit vector construction from lists. *}
1.1359 +
1.1360 +lemma Cart_lambda_beta_1[simp]: "(Cart_lambda g)\$1 = g 1"
1.1361 +  apply (rule Cart_lambda_beta[rule_format])
1.1362 +  using dimindex_ge_1 apply auto done
1.1363 +
1.1364 +lemma Cart_lambda_beta_1'[simp]: "(Cart_lambda g)\$(Suc 0) = g 1"
1.1365 +  by (simp only: One_nat_def[symmetric] Cart_lambda_beta_1)
1.1366 +
1.1367 +definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"
1.1368 +
1.1369 +lemma vector_1: "(vector[x]) \$1 = x"
1.1370 +  using dimindex_ge_1
1.1371 +  by (auto simp add: vector_def Cart_lambda_beta[rule_format])
1.1372 +lemma dimindex_2[simp]: "2 \<in> {1 .. dimindex (UNIV :: 2 set)}"
1.1373 +  by (auto simp add: dimindex_def)
1.1374 +lemma dimindex_2'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 2 set)}"
1.1375 +  by (auto simp add: dimindex_def)
1.1376 +lemma dimindex_3[simp]: "2 \<in> {1 .. dimindex (UNIV :: 3 set)}" "3 \<in> {1 .. dimindex (UNIV :: 3 set)}"
1.1377 +  by (auto simp add: dimindex_def)
1.1378 +
1.1379 +lemma dimindex_3'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}" "3 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}"
1.1380 +  by (auto simp add: dimindex_def)
1.1381 +
1.1382 +lemma vector_2:
1.1383 + "(vector[x,y]) \$1 = x"
1.1384 + "(vector[x,y] :: 'a^2)\$2 = (y::'a::zero)"
1.1385 +  apply (simp add: vector_def)
1.1386 +  using Cart_lambda_beta[rule_format, OF dimindex_2, of "\<lambda>i. if i \<le> length [x,y] then [x,y] ! (i - 1) else (0::'a)"]
1.1387 +  apply (simp only: vector_def )
1.1388 +  apply auto
1.1389 +  done
1.1390 +
1.1391 +lemma vector_3:
1.1392 + "(vector [x,y,z] ::('a::zero)^3)\$1 = x"
1.1393 + "(vector [x,y,z] ::('a::zero)^3)\$2 = y"
1.1394 + "(vector [x,y,z] ::('a::zero)^3)\$3 = z"
1.1395 +apply (simp_all add: vector_def Cart_lambda_beta dimindex_3)
1.1396 +  using Cart_lambda_beta[rule_format, OF dimindex_3(1), of "\<lambda>i. if i \<le> length [x,y,z] then [x,y,z] ! (i - 1) else (0::'a)"]   using Cart_lambda_beta[rule_format, OF dimindex_3(2), of "\<lambda>i. if i \<le> length [x,y,z] then [x,y,z] ! (i - 1) else (0::'a)"]
1.1397 +  by simp_all
1.1398 +
1.1399 +lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
1.1400 +  apply auto
1.1401 +  apply (erule_tac x="v\$1" in allE)
1.1402 +  apply (subgoal_tac "vector [v\$1] = v")
1.1403 +  apply simp
1.1404 +  by (vector vector_def dimindex_def)
1.1405 +
1.1406 +lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
1.1407 +  apply auto
1.1408 +  apply (erule_tac x="v\$1" in allE)
1.1409 +  apply (erule_tac x="v\$2" in allE)
1.1410 +  apply (subgoal_tac "vector [v\$1, v\$2] = v")
1.1411 +  apply simp
1.1412 +  apply (vector vector_def dimindex_def)
1.1413 +  apply auto
1.1414 +  apply (subgoal_tac "i = 1 \<or> i =2", auto)
1.1415 +  done
1.1416 +
1.1417 +lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
1.1418 +  apply auto
1.1419 +  apply (erule_tac x="v\$1" in allE)
1.1420 +  apply (erule_tac x="v\$2" in allE)
1.1421 +  apply (erule_tac x="v\$3" in allE)
1.1422 +  apply (subgoal_tac "vector [v\$1, v\$2, v\$3] = v")
1.1423 +  apply simp
1.1424 +  apply (vector vector_def dimindex_def)
1.1425 +  apply auto
1.1426 +  apply (subgoal_tac "i = 1 \<or> i =2 \<or> i = 3", auto)
1.1427 +  done
1.1428 +
1.1429 +subsection{* Linear functions. *}
1.1430 +
1.1431 +definition "linear f \<longleftrightarrow> (\<forall>x y. f(x + y) = f x + f y) \<and> (\<forall>c x. f(c *s x) = c *s f x)"
1.1432 +
1.1433 +lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. (c::'a::comm_semiring) *s f x)"
1.1434 +  by (vector linear_def Cart_eq Cart_lambda_beta[rule_format] ring_simps)
1.1435 +
1.1436 +lemma linear_compose_neg: "linear (f :: 'a ^'n \<Rightarrow> 'a::comm_ring ^'m) ==> linear (\<lambda>x. -(f(x)))" by (vector linear_def Cart_eq)
1.1437 +
1.1438 +lemma linear_compose_add: "linear (f :: 'a ^'n \<Rightarrow> 'a::semiring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
1.1439 +  by (vector linear_def Cart_eq ring_simps)
1.1440 +
1.1441 +lemma linear_compose_sub: "linear (f :: 'a ^'n \<Rightarrow> 'a::ring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
1.1442 +  by (vector linear_def Cart_eq ring_simps)
1.1443 +
1.1444 +lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
1.1445 +  by (simp add: linear_def)
1.1446 +
1.1447 +lemma linear_id: "linear id" by (simp add: linear_def id_def)
1.1448 +
1.1449 +lemma linear_zero: "linear (\<lambda>x. 0::'a::semiring_1 ^ 'n)" by (simp add: linear_def)
1.1450 +
1.1451 +lemma linear_compose_setsum:
1.1452 +  assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a :: 'a::semiring_1 ^ 'n \<Rightarrow> 'a ^ 'm)"
1.1453 +  shows "linear(\<lambda>x. setsum (\<lambda>a. f a x :: 'a::semiring_1 ^'m) S)"
1.1454 +  using lS
1.1455 +  apply (induct rule: finite_induct[OF fS])
1.1457 +
1.1458 +lemma linear_vmul_component:
1.1459 +  fixes f:: "'a::semiring_1^'m \<Rightarrow> 'a^'n"
1.1460 +  assumes lf: "linear f" and k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.1461 +  shows "linear (\<lambda>x. f x \$ k *s v)"
1.1462 +  using lf k
1.1463 +  apply (auto simp add: linear_def )
1.1464 +  by (vector ring_simps)+
1.1465 +
1.1466 +lemma linear_0: "linear f ==> f 0 = (0::'a::semiring_1 ^'n)"
1.1467 +  unfolding linear_def
1.1468 +  apply clarsimp
1.1469 +  apply (erule allE[where x="0::'a"])
1.1470 +  apply simp
1.1471 +  done
1.1472 +
1.1473 +lemma linear_cmul: "linear f ==> f(c*s x) = c *s f x" by (simp add: linear_def)
1.1474 +
1.1475 +lemma linear_neg: "linear (f :: 'a::ring_1 ^'n \<Rightarrow> _) ==> f (-x) = - f x"
1.1476 +  unfolding vector_sneg_minus1
1.1477 +  using linear_cmul[of f] by auto
1.1478 +
1.1479 +lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def)
1.1480 +
1.1481 +lemma linear_sub: "linear (f::'a::ring_1 ^'n \<Rightarrow> _) ==> f(x - y) = f x - f y"
1.1483 +
1.1484 +lemma linear_setsum:
1.1485 +  fixes f:: "'a::semiring_1^'n \<Rightarrow> _"
1.1486 +  assumes lf: "linear f" and fS: "finite S"
1.1487 +  shows "f (setsum g S) = setsum (f o g) S"
1.1488 +proof (induct rule: finite_induct[OF fS])
1.1489 +  case 1 thus ?case by (simp add: linear_0[OF lf])
1.1490 +next
1.1491 +  case (2 x F)
1.1492 +  have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
1.1493 +    by simp
1.1494 +  also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
1.1495 +  also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
1.1496 +  finally show ?case .
1.1497 +qed
1.1498 +
1.1499 +lemma linear_setsum_mul:
1.1500 +  fixes f:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m"
1.1501 +  assumes lf: "linear f" and fS: "finite S"
1.1502 +  shows "f (setsum (\<lambda>i. c i *s v i) S) = setsum (\<lambda>i. c i *s f (v i)) S"
1.1503 +  using linear_setsum[OF lf fS, of "\<lambda>i. c i *s v i" , unfolded o_def]
1.1504 +  linear_cmul[OF lf] by simp
1.1505 +
1.1506 +lemma linear_injective_0:
1.1507 +  assumes lf: "linear (f:: 'a::ring_1 ^ 'n \<Rightarrow> _)"
1.1508 +  shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
1.1509 +proof-
1.1510 +  have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
1.1511 +  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
1.1512 +  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
1.1513 +    by (simp add: linear_sub[OF lf])
1.1514 +  also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
1.1515 +  finally show ?thesis .
1.1516 +qed
1.1517 +
1.1518 +lemma linear_bounded:
1.1519 +  fixes f:: "real ^'m \<Rightarrow> real ^'n"
1.1520 +  assumes lf: "linear f"
1.1521 +  shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
1.1522 +proof-
1.1523 +  let ?S = "{1..dimindex(UNIV:: 'm set)}"
1.1524 +  let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
1.1525 +  have fS: "finite ?S" by simp
1.1526 +  {fix x:: "real ^ 'm"
1.1527 +    let ?g = "(\<lambda>i::nat. (x\$i) *s (basis i) :: real ^ 'm)"
1.1528 +    have "norm (f x) = norm (f (setsum (\<lambda>i. (x\$i) *s (basis i)) ?S))"
1.1529 +      by (simp only:  basis_expansion)
1.1530 +    also have "\<dots> = norm (setsum (\<lambda>i. (x\$i) *s f (basis i))?S)"
1.1531 +      using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf]
1.1532 +      by auto
1.1533 +    finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x\$i) *s f (basis i))?S)" .
1.1534 +    {fix i assume i: "i \<in> ?S"
1.1535 +      from component_le_norm[OF i, of x]
1.1536 +      have "norm ((x\$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x"
1.1537 +      unfolding norm_mul
1.1538 +      apply (simp only: mult_commute)
1.1539 +      apply (rule mult_mono)
1.1540 +      by (auto simp add: ring_simps norm_pos_le) }
1.1541 +    then have th: "\<forall>i\<in> ?S. norm ((x\$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x" by metis
1.1542 +    from real_setsum_norm_le[OF fS, of "\<lambda>i. (x\$i) *s (f (basis i))", OF th]
1.1543 +    have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
1.1544 +  then show ?thesis by blast
1.1545 +qed
1.1546 +
1.1547 +lemma linear_bounded_pos:
1.1548 +  fixes f:: "real ^'n \<Rightarrow> real ^ 'm"
1.1549 +  assumes lf: "linear f"
1.1550 +  shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
1.1551 +proof-
1.1552 +  from linear_bounded[OF lf] obtain B where
1.1553 +    B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
1.1554 +  let ?K = "\<bar>B\<bar> + 1"
1.1555 +  have Kp: "?K > 0" by arith
1.1556 +    {assume C: "B < 0"
1.1557 +      have "norm (1::real ^ 'n) > 0" by (simp add: norm_pos_lt)
1.1558 +      with C have "B * norm (1:: real ^ 'n) < 0"
1.1559 +	by (simp add: zero_compare_simps)
1.1560 +      with B[rule_format, of 1] norm_pos_le[of "f 1"] have False by simp
1.1561 +    }
1.1562 +    then have Bp: "B \<ge> 0" by ferrack
1.1563 +    {fix x::"real ^ 'n"
1.1564 +      have "norm (f x) \<le> ?K *  norm x"
1.1565 +      using B[rule_format, of x] norm_pos_le[of x] norm_pos_le[of "f x"] Bp
1.1567 +  }
1.1568 +  then show ?thesis using Kp by blast
1.1569 +qed
1.1570 +
1.1571 +subsection{* Bilinear functions. *}
1.1572 +
1.1573 +definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
1.1574 +
1.1575 +lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
1.1576 +  by (simp add: bilinear_def linear_def)
1.1577 +lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
1.1578 +  by (simp add: bilinear_def linear_def)
1.1579 +
1.1580 +lemma bilinear_lmul: "bilinear h ==> h (c *s x) y = c *s (h x y)"
1.1581 +  by (simp add: bilinear_def linear_def)
1.1582 +
1.1583 +lemma bilinear_rmul: "bilinear h ==> h x (c *s y) = c *s (h x y)"
1.1584 +  by (simp add: bilinear_def linear_def)
1.1585 +
1.1586 +lemma bilinear_lneg: "bilinear h ==> h (- (x:: 'a::ring_1 ^ 'n)) y = -(h x y)"
1.1587 +  by (simp only: vector_sneg_minus1 bilinear_lmul)
1.1588 +
1.1589 +lemma bilinear_rneg: "bilinear h ==> h x (- (y:: 'a::ring_1 ^ 'n)) = - h x y"
1.1590 +  by (simp only: vector_sneg_minus1 bilinear_rmul)
1.1591 +
1.1592 +lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
1.1593 +  using add_imp_eq[of x y 0] by auto
1.1594 +
1.1595 +lemma bilinear_lzero:
1.1596 +  fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h 0 x = 0"
1.1597 +  using bilinear_ladd[OF bh, of 0 0 x]
1.1599 +
1.1600 +lemma bilinear_rzero:
1.1601 +  fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h x 0 = 0"
1.1602 +  using bilinear_radd[OF bh, of x 0 0 ]
1.1604 +
1.1605 +lemma bilinear_lsub: "bilinear h ==> h (x - (y:: 'a::ring_1 ^ 'n)) z = h x z - h y z"
1.1607 +
1.1608 +lemma bilinear_rsub: "bilinear h ==> h z (x - (y:: 'a::ring_1 ^ 'n)) = h z x - h z y"
1.1610 +
1.1611 +lemma bilinear_setsum:
1.1612 +  fixes h:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m \<Rightarrow> 'a ^ 'k"
1.1613 +  assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
1.1614 +  shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
1.1615 +proof-
1.1616 +  have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
1.1617 +    apply (rule linear_setsum[unfolded o_def])
1.1618 +    using bh fS by (auto simp add: bilinear_def)
1.1619 +  also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
1.1620 +    apply (rule setsum_cong, simp)
1.1621 +    apply (rule linear_setsum[unfolded o_def])
1.1622 +    using bh fT by (auto simp add: bilinear_def)
1.1623 +  finally show ?thesis unfolding setsum_cartesian_product .
1.1624 +qed
1.1625 +
1.1626 +lemma bilinear_bounded:
1.1627 +  fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
1.1628 +  assumes bh: "bilinear h"
1.1629 +  shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
1.1630 +proof-
1.1631 +  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
1.1632 +  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
1.1633 +  let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
1.1634 +  have fM: "finite ?M" and fN: "finite ?N" by simp_all
1.1635 +  {fix x:: "real ^ 'm" and  y :: "real^'n"
1.1636 +    have "norm (h x y) = norm (h (setsum (\<lambda>i. (x\$i) *s basis i) ?M) (setsum (\<lambda>i. (y\$i) *s basis i) ?N))" unfolding basis_expansion ..
1.1637 +    also have "\<dots> = norm (setsum (\<lambda> (i,j). h ((x\$i) *s basis i) ((y\$j) *s basis j)) (?M \<times> ?N))"  unfolding bilinear_setsum[OF bh fM fN] ..
1.1638 +    finally have th: "norm (h x y) = \<dots>" .
1.1639 +    have "norm (h x y) \<le> ?B * norm x * norm y"
1.1640 +      apply (simp add: setsum_left_distrib th)
1.1641 +      apply (rule real_setsum_norm_le)
1.1642 +      using fN fM
1.1643 +      apply simp
1.1644 +      apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] norm_mul ring_simps)
1.1645 +      apply (rule mult_mono)
1.1646 +      apply (auto simp add: norm_pos_le zero_le_mult_iff component_le_norm)
1.1647 +      apply (rule mult_mono)
1.1648 +      apply (auto simp add: norm_pos_le zero_le_mult_iff component_le_norm)
1.1649 +      done}
1.1650 +  then show ?thesis by metis
1.1651 +qed
1.1652 +
1.1653 +lemma bilinear_bounded_pos:
1.1654 +  fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
1.1655 +  assumes bh: "bilinear h"
1.1656 +  shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
1.1657 +proof-
1.1658 +  from bilinear_bounded[OF bh] obtain B where
1.1659 +    B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
1.1660 +  let ?K = "\<bar>B\<bar> + 1"
1.1661 +  have Kp: "?K > 0" by arith
1.1662 +  have KB: "B < ?K" by arith
1.1663 +  {fix x::"real ^'m" and y :: "real ^'n"
1.1664 +    from KB Kp
1.1665 +    have "B * norm x * norm y \<le> ?K * norm x * norm y"
1.1666 +      apply -
1.1667 +      apply (rule mult_right_mono, rule mult_right_mono)
1.1668 +      by (auto simp add: norm_pos_le)
1.1669 +    then have "norm (h x y) \<le> ?K * norm x * norm y"
1.1670 +      using B[rule_format, of x y] by simp}
1.1671 +  with Kp show ?thesis by blast
1.1672 +qed
1.1673 +
1.1675 +
1.1676 +definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
1.1677 +
1.1678 +lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
1.1679 +
1.1681 +  fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
1.1682 +  assumes lf: "linear f"
1.1683 +  shows "\<forall>x y. f x \<bullet> y = x \<bullet> adjoint f y"
1.1684 +proof-
1.1685 +  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
1.1686 +  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
1.1687 +  have fN: "finite ?N" by simp
1.1688 +  have fM: "finite ?M" by simp
1.1689 +  {fix y:: "'a ^ 'm"
1.1690 +    let ?w = "(\<chi> i. (f (basis i) \<bullet> y)) :: 'a ^ 'n"
1.1691 +    {fix x
1.1692 +      have "f x \<bullet> y = f (setsum (\<lambda>i. (x\$i) *s basis i) ?N) \<bullet> y"
1.1693 +	by (simp only: basis_expansion)
1.1694 +      also have "\<dots> = (setsum (\<lambda>i. (x\$i) *s f (basis i)) ?N) \<bullet> y"
1.1695 +	unfolding linear_setsum[OF lf fN]
1.1696 +	by (simp add: linear_cmul[OF lf])
1.1697 +      finally have "f x \<bullet> y = x \<bullet> ?w"
1.1698 +	apply (simp only: )
1.1699 +	apply (simp add: dot_def setsum_component Cart_lambda_beta setsum_left_distrib setsum_right_distrib vector_component setsum_commute[of _ ?M ?N] ring_simps del: One_nat_def)
1.1700 +	done}
1.1701 +  }
1.1702 +  then show ?thesis unfolding adjoint_def
1.1703 +    some_eq_ex[of "\<lambda>f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y"]
1.1704 +    using choice_iff[of "\<lambda>a b. \<forall>x. f x \<bullet> a = x \<bullet> b "]
1.1705 +    by metis
1.1706 +qed
1.1707 +
1.1709 +  fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
1.1710 +  assumes lf: "linear f"
1.1711 +  shows "x \<bullet> adjoint f y = f x \<bullet> y"
1.1712 +  using adjoint_works_lemma[OF lf] by metis
1.1713 +
1.1714 +
1.1716 +  fixes f :: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
1.1717 +  assumes lf: "linear f"
1.1718 +  shows "linear (adjoint f)"
1.1720 +
1.1722 +  fixes f:: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
1.1723 +  assumes lf: "linear f"
1.1724 +  shows "x \<bullet> adjoint f y = f x \<bullet> y"
1.1725 +  and "adjoint f y \<bullet> x = y \<bullet> f x"
1.1727 +
1.1729 +  fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> _"
1.1730 +  assumes lf: "linear f"
1.1732 +  apply (rule ext)
1.1734 +
1.1736 +  fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> 'a ^ 'm"
1.1737 +  assumes lf: "linear f" and u: "\<forall>x y. f' x \<bullet> y = x \<bullet> f y"
1.1738 +  shows "f' = adjoint f"
1.1739 +  apply (rule ext)
1.1740 +  using u
1.1742 +
1.1743 +text{* Matrix notation. NB: an MxN matrix is of type 'a^'n^'m, not 'a^'m^'n *}
1.1744 +
1.1745 +consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
1.1746 +
1.1748 +matrix_matrix_mult_def: "(m:: ('a::semiring_1) ^'n^'m) \<star> (m' :: 'a ^'p^'n) \<equiv> (\<chi> i j. setsum (\<lambda>k. ((m\$i)\$k) * ((m'\$k)\$j)) {1 .. dimindex (UNIV :: 'n set)}) ::'a ^ 'p ^'m"
1.1749 +
1.1750 +abbreviation
1.1751 +  matrix_matrix_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"  (infixl "**" 70)
1.1752 +  where "m ** m' == m\<star> m'"
1.1753 +
1.1755 +  matrix_vector_mult_def: "(m::('a::semiring_1) ^'n^'m) \<star> (x::'a ^'n) \<equiv> (\<chi> i. setsum (\<lambda>j. ((m\$i)\$j) * (x\$j)) {1..dimindex(UNIV ::'n set)}) :: 'a^'m"
1.1756 +
1.1757 +abbreviation
1.1758 +  matrix_vector_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"  (infixl "*v" 70)
1.1759 +  where
1.1760 +  "m *v v == m \<star> v"
1.1761 +
1.1763 +  vector_matrix_mult_def: "(x::'a^'m) \<star> (m::('a::semiring_1) ^'n^'m) \<equiv> (\<chi> j. setsum (\<lambda>i. ((m\$i)\$j) * (x\$i)) {1..dimindex(UNIV :: 'm set)}) :: 'a^'n"
1.1764 +
1.1765 +abbreviation
1.1766 +  vactor_matrix_mult' :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "  (infixl "v*" 70)
1.1767 +  where
1.1768 +  "v v* m == v \<star> m"
1.1769 +
1.1770 +definition "(mat::'a::zero => 'a ^'n^'m) k = (\<chi> i j. if i = j then k else 0)"
1.1771 +definition "(transp::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A\$j)\$i))"
1.1772 +definition "(row::nat => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A\$i)\$j))"
1.1773 +definition "(column::nat =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A\$i)\$j))"
1.1774 +definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> {1 .. dimindex(UNIV :: 'm set)}}"
1.1775 +definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}}"
1.1776 +
1.1777 +lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
1.1778 +lemma matrix_add_ldistrib: "(A ** (B + C)) = (A \<star> B) + (A \<star> C)"
1.1779 +  by (vector matrix_matrix_mult_def setsum_addf[symmetric] ring_simps)
1.1780 +
1.1781 +lemma setsum_delta':
1.1782 +  assumes fS: "finite S" shows
1.1783 +  "setsum (\<lambda>k. if a = k then b k else 0) S =
1.1784 +     (if a\<in> S then b a else 0)"
1.1785 +  using setsum_delta[OF fS, of a b, symmetric]
1.1786 +  by (auto intro: setsum_cong)
1.1787 +
1.1788 +lemma matrix_mul_lid: "mat 1 ** A = A"
1.1789 +  apply (simp add: matrix_matrix_mult_def mat_def)
1.1790 +  apply vector
1.1791 +  by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite_atLeastAtMost]  mult_1_left mult_zero_left if_True)
1.1792 +
1.1793 +
1.1794 +lemma matrix_mul_rid: "A ** mat 1 = A"
1.1795 +  apply (simp add: matrix_matrix_mult_def mat_def)
1.1796 +  apply vector
1.1797 +  by (auto simp only: cond_value_iff cond_application_beta setsum_delta[OF finite_atLeastAtMost]  mult_1_right mult_zero_right if_True cong: if_cong)
1.1798 +
1.1799 +lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
1.1800 +  apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
1.1801 +  apply (subst setsum_commute)
1.1802 +  apply simp
1.1803 +  done
1.1804 +
1.1805 +lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
1.1806 +  apply (vector matrix_matrix_mult_def matrix_vector_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
1.1807 +  apply (subst setsum_commute)
1.1808 +  apply simp
1.1809 +  done
1.1810 +
1.1811 +lemma matrix_vector_mul_lid: "mat 1 *v x = x"
1.1812 +  apply (vector matrix_vector_mult_def mat_def)
1.1813 +  by (simp add: cond_value_iff cond_application_beta
1.1814 +    setsum_delta' cong del: if_weak_cong)
1.1815 +
1.1816 +lemma matrix_transp_mul: "transp(A ** B) = transp B ** transp (A::'a::comm_semiring_1^'m^'n)"
1.1817 +  by (simp add: matrix_matrix_mult_def transp_def Cart_eq Cart_lambda_beta mult_commute)
1.1818 +
1.1819 +lemma matrix_eq: "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
1.1820 +  apply auto
1.1821 +  apply (subst Cart_eq)
1.1822 +  apply clarify
1.1823 +  apply (clarsimp simp add: matrix_vector_mult_def basis_def cond_value_iff cond_application_beta Cart_eq Cart_lambda_beta cong del: if_weak_cong)
1.1824 +  apply (erule_tac x="basis ia" in allE)
1.1825 +  apply (erule_tac x="i" in ballE)
1.1826 +  by (auto simp add: basis_def cond_value_iff cond_application_beta Cart_lambda_beta setsum_delta[OF finite_atLeastAtMost] cong del: if_weak_cong)
1.1827 +
1.1828 +lemma matrix_vector_mul_component:
1.1829 +  assumes k: "k \<in> {1.. dimindex (UNIV :: 'm set)}"
1.1830 +  shows "((A::'a::semiring_1^'n'^'m) *v x)\$k = (A\$k) \<bullet> x"
1.1831 +  using k
1.1832 +  by (simp add: matrix_vector_mult_def Cart_lambda_beta dot_def)
1.1833 +
1.1834 +lemma dot_lmul_matrix: "((x::'a::comm_semiring_1 ^'n) v* A) \<bullet> y = x \<bullet> (A *v y)"
1.1835 +  apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib Cart_lambda_beta mult_ac)
1.1836 +  apply (subst setsum_commute)
1.1837 +  by simp
1.1838 +
1.1839 +lemma transp_mat: "transp (mat n) = mat n"
1.1840 +  by (vector transp_def mat_def)
1.1841 +
1.1842 +lemma transp_transp: "transp(transp A) = A"
1.1843 +  by (vector transp_def)
1.1844 +
1.1845 +lemma row_transp:
1.1846 +  fixes A:: "'a::semiring_1^'n^'m"
1.1847 +  assumes i: "i \<in> {1.. dimindex (UNIV :: 'n set)}"
1.1848 +  shows "row i (transp A) = column i A"
1.1849 +  using i
1.1850 +  by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
1.1851 +
1.1852 +lemma column_transp:
1.1853 +  fixes A:: "'a::semiring_1^'n^'m"
1.1854 +  assumes i: "i \<in> {1.. dimindex (UNIV :: 'm set)}"
1.1855 +  shows "column i (transp A) = row i A"
1.1856 +  using i
1.1857 +  by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
1.1858 +
1.1859 +lemma rows_transp: "rows(transp (A::'a::semiring_1^'n^'m)) = columns A"
1.1860 +apply (auto simp add: rows_def columns_def row_transp intro: set_ext)
1.1861 +apply (rule_tac x=i in exI)
1.1862 +apply (auto simp add: row_transp)
1.1863 +done
1.1864 +
1.1865 +lemma columns_transp: "columns(transp (A::'a::semiring_1^'n^'m)) = rows A" by (metis transp_transp rows_transp)
1.1866 +
1.1867 +text{* Two sometimes fruitful ways of looking at matrix-vector multiplication. *}
1.1868 +
1.1869 +lemma matrix_mult_dot: "A *v x = (\<chi> i. A\$i \<bullet> x)"
1.1870 +  by (simp add: matrix_vector_mult_def dot_def)
1.1871 +
1.1872 +lemma matrix_mult_vsum: "(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x\$i) *s column i A) {1 .. dimindex(UNIV:: 'n set)}"
1.1873 +  by (simp add: matrix_vector_mult_def Cart_eq setsum_component Cart_lambda_beta vector_component column_def mult_commute)
1.1874 +
1.1875 +lemma vector_componentwise:
1.1876 +  "(x::'a::ring_1^'n) = (\<chi> j. setsum (\<lambda>i. (x\$i) * (basis i :: 'a^'n)\$j) {1..dimindex(UNIV :: 'n set)})"
1.1877 +  apply (subst basis_expansion[symmetric])
1.1878 +  by (vector Cart_eq Cart_lambda_beta setsum_component)
1.1879 +
1.1880 +lemma linear_componentwise:
1.1881 +  fixes f:: "'a::ring_1 ^ 'm \<Rightarrow> 'a ^ 'n"
1.1882 +  assumes lf: "linear f" and j: "j \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.1883 +  shows "(f x)\$j = setsum (\<lambda>i. (x\$i) * (f (basis i)\$j)) {1 .. dimindex (UNIV :: 'm set)}" (is "?lhs = ?rhs")
1.1884 +proof-
1.1885 +  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
1.1886 +  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
1.1887 +  have fM: "finite ?M" by simp
1.1888 +  have "?rhs = (setsum (\<lambda>i.(x\$i) *s f (basis i) ) ?M)\$j"
1.1889 +    unfolding vector_smult_component[OF j, symmetric]
1.1890 +    unfolding setsum_component[OF j, of "(\<lambda>i.(x\$i) *s f (basis i :: 'a^'m))" ?M]
1.1891 +    ..
1.1892 +  then show ?thesis unfolding linear_setsum_mul[OF lf fM, symmetric] basis_expansion ..
1.1893 +qed
1.1894 +
1.1895 +text{* Inverse matrices  (not necessarily square) *}
1.1896 +
1.1897 +definition "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
1.1898 +
1.1899 +definition "matrix_inv(A:: 'a::semiring_1^'n^'m) =
1.1900 +        (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
1.1901 +
1.1902 +text{* Correspondence between matrices and linear operators. *}
1.1903 +
1.1904 +definition matrix:: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
1.1905 +where "matrix f = (\<chi> i j. (f(basis j))\$i)"
1.1906 +
1.1907 +lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::'a::comm_semiring_1 ^ 'n))"
1.1908 +  by (simp add: linear_def matrix_vector_mult_def Cart_eq Cart_lambda_beta vector_component ring_simps setsum_right_distrib setsum_addf)
1.1909 +
1.1910 +lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n)"
1.1911 +apply (simp add: matrix_def matrix_vector_mult_def Cart_eq Cart_lambda_beta mult_commute del: One_nat_def)
1.1912 +apply clarify
1.1913 +apply (rule linear_componentwise[OF lf, symmetric])
1.1914 +apply simp
1.1915 +done
1.1916 +
1.1917 +lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n))" by (simp add: ext matrix_works)
1.1918 +
1.1919 +lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n)) = A"
1.1920 +  by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
1.1921 +
1.1922 +lemma matrix_compose:
1.1923 +  assumes lf: "linear (f::'a::comm_ring_1^'n \<Rightarrow> _)" and lg: "linear g"
1.1924 +  shows "matrix (g o f) = matrix g ** matrix f"
1.1925 +  using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
1.1926 +  by (simp  add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
1.1927 +
1.1928 +lemma matrix_vector_column:"(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x\$i) *s ((transp A)\$i)) {1..dimindex(UNIV:: 'n set)}"
1.1929 +  by (simp add: matrix_vector_mult_def transp_def Cart_eq Cart_lambda_beta setsum_component vector_component mult_commute)
1.1930 +
1.1931 +lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n^'m) *v x) = (\<lambda>x. transp A *v x)"
1.1933 +  apply (rule matrix_vector_mul_linear)
1.1934 +  apply (simp add: transp_def dot_def Cart_lambda_beta matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
1.1935 +  apply (subst setsum_commute)
1.1936 +  apply (auto simp add: mult_ac)
1.1937 +  done
1.1938 +
1.1939 +lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n \<Rightarrow> 'a ^ 'm)"
1.1940 +  shows "matrix(adjoint f) = transp(matrix f)"
1.1941 +  apply (subst matrix_vector_mul[OF lf])
1.1942 +  unfolding adjoint_matrix matrix_of_matrix_vector_mul ..
1.1943 +
1.1944 +subsection{* Interlude: Some properties of real sets *}
1.1945 +
1.1946 +lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
1.1947 +  shows "\<forall>n \<ge> m. d n < e m"
1.1948 +  using prems apply auto
1.1949 +  apply (erule_tac x="n" in allE)
1.1950 +  apply (erule_tac x="n" in allE)
1.1951 +  apply auto
1.1952 +  done
1.1953 +
1.1954 +
1.1955 +lemma real_convex_bound_lt:
1.1956 +  assumes xa: "(x::real) < a" and ya: "y < a" and u: "0 <= u" and v: "0 <= v"
1.1957 +  and uv: "u + v = 1"
1.1958 +  shows "u * x + v * y < a"
1.1959 +proof-
1.1960 +  have uv': "u = 0 \<longrightarrow> v \<noteq> 0" using u v uv by arith
1.1961 +  have "a = a * (u + v)" unfolding uv  by simp
1.1962 +  hence th: "u * a + v * a = a" by (simp add: ring_simps)
1.1963 +  from xa u have "u \<noteq> 0 \<Longrightarrow> u*x < u*a" by (simp add: mult_compare_simps)
1.1964 +  from ya v have "v \<noteq> 0 \<Longrightarrow> v * y < v * a" by (simp add: mult_compare_simps)
1.1965 +  from xa ya u v have "u * x + v * y < u * a + v * a"
1.1966 +    apply (cases "u = 0", simp_all add: uv')
1.1967 +    apply(rule mult_strict_left_mono)
1.1968 +    using uv' apply simp_all
1.1969 +
1.1971 +    apply(rule mult_strict_left_mono)
1.1972 +    apply simp_all
1.1973 +    apply (rule mult_left_mono)
1.1974 +    apply simp_all
1.1975 +    done
1.1976 +  thus ?thesis unfolding th .
1.1977 +qed
1.1978 +
1.1979 +lemma real_convex_bound_le:
1.1980 +  assumes xa: "(x::real) \<le> a" and ya: "y \<le> a" and u: "0 <= u" and v: "0 <= v"
1.1981 +  and uv: "u + v = 1"
1.1982 +  shows "u * x + v * y \<le> a"
1.1983 +proof-
1.1984 +  from xa ya u v have "u * x + v * y \<le> u * a + v * a" by (simp add: add_mono mult_left_mono)
1.1985 +  also have "\<dots> \<le> (u + v) * a" by (simp add: ring_simps)
1.1986 +  finally show ?thesis unfolding uv by simp
1.1987 +qed
1.1988 +
1.1989 +lemma infinite_enumerate: assumes fS: "infinite S"
1.1990 +  shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
1.1991 +unfolding subseq_def
1.1992 +using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
1.1993 +
1.1994 +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)"
1.1995 +apply auto
1.1996 +apply (rule_tac x="d/2" in exI)
1.1997 +apply auto
1.1998 +done
1.1999 +
1.2000 +
1.2001 +lemma triangle_lemma:
1.2002 +  assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
1.2003 +  shows "x <= y + z"
1.2004 +proof-
1.2005 +  have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y  by (simp add: zero_compare_simps)
1.2006 +  with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square ring_simps)
1.2007 +  from y z have yz: "y + z \<ge> 0" by arith
1.2008 +  from power2_le_imp_le[OF th yz] show ?thesis .
1.2009 +qed
1.2010 +
1.2011 +
1.2012 +lemma lambda_skolem: "(\<forall>i \<in> {1 .. dimindex(UNIV :: 'n set)}. \<exists>x. P i x) \<longleftrightarrow>
1.2013 +   (\<exists>x::'a ^ 'n. \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. P i (x\$i))" (is "?lhs \<longleftrightarrow> ?rhs")
1.2014 +proof-
1.2015 +  let ?S = "{1 .. dimindex(UNIV :: 'n set)}"
1.2016 +  {assume H: "?rhs"
1.2017 +    then have ?lhs by auto}
1.2018 +  moreover
1.2019 +  {assume H: "?lhs"
1.2020 +    then obtain f where f:"\<forall>i\<in> ?S. P i (f i)" unfolding Ball_def choice_iff by metis
1.2021 +    let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
1.2022 +    {fix i assume i: "i \<in> ?S"
1.2023 +      with f i have "P i (f i)" by metis
1.2024 +      then have "P i (?x\$i)" using Cart_lambda_beta[of f, rule_format, OF i] by auto
1.2025 +    }
1.2026 +    hence "\<forall>i \<in> ?S. P i (?x\$i)" by metis
1.2027 +    hence ?rhs by metis }
1.2028 +  ultimately show ?thesis by metis
1.2029 +qed
1.2030 +
1.2031 +(* Supremum and infimum of real sets *)
1.2032 +
1.2033 +
1.2034 +definition rsup:: "real set \<Rightarrow> real" where
1.2035 +  "rsup S = (SOME a. isLub UNIV S a)"
1.2036 +
1.2037 +lemma rsup_alt: "rsup S = (SOME a. (\<forall>x \<in> S. x \<le> a) \<and> (\<forall>b. (\<forall>x \<in> S. x \<le> b) \<longrightarrow> a \<le> b))"  by (auto simp  add: isLub_def rsup_def leastP_def isUb_def setle_def setge_def)
1.2038 +
1.2039 +lemma rsup: assumes Se: "S \<noteq> {}" and b: "\<exists>b. S *<= b"
1.2040 +  shows "isLub UNIV S (rsup S)"
1.2041 +using Se b
1.2042 +unfolding rsup_def
1.2043 +apply clarify
1.2044 +apply (rule someI_ex)
1.2045 +apply (rule reals_complete)
1.2046 +by (auto simp add: isUb_def setle_def)
1.2047 +
1.2048 +lemma rsup_le: assumes Se: "S \<noteq> {}" and Sb: "S *<= b" shows "rsup S \<le> b"
1.2049 +proof-
1.2050 +  from Sb have bu: "isUb UNIV S b" by (simp add: isUb_def setle_def)
1.2051 +  from rsup[OF Se] Sb have "isLub UNIV S (rsup S)"  by blast
1.2052 +  then show ?thesis using bu by (auto simp add: isLub_def leastP_def setle_def setge_def)
1.2053 +qed
1.2054 +
1.2055 +lemma rsup_finite_Max: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2056 +  shows "rsup S = Max S"
1.2057 +using fS Se
1.2058 +proof-
1.2059 +  let ?m = "Max S"
1.2060 +  from Max_ge[OF fS] have Sm: "\<forall> x\<in> S. x \<le> ?m" by metis
1.2061 +  with rsup[OF Se] have lub: "isLub UNIV S (rsup S)" by (metis setle_def)
1.2062 +  from Max_in[OF fS Se] lub have mrS: "?m \<le> rsup S"
1.2063 +    by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def)
1.2064 +  moreover
1.2065 +  have "rsup S \<le> ?m" using Sm lub
1.2066 +    by (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
1.2067 +  ultimately  show ?thesis by arith
1.2068 +qed
1.2069 +
1.2070 +lemma rsup_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2071 +  shows "rsup S \<in> S"
1.2072 +  using rsup_finite_Max[OF fS Se] Max_in[OF fS Se] by metis
1.2073 +
1.2074 +lemma rsup_finite_Ub: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2075 +  shows "isUb S S (rsup S)"
1.2076 +  using rsup_finite_Max[OF fS Se] rsup_finite_in[OF fS Se] Max_ge[OF fS]
1.2077 +  unfolding isUb_def setle_def by metis
1.2078 +
1.2079 +lemma rsup_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2080 +  shows "a \<le> rsup S \<longleftrightarrow> (\<exists> x \<in> S. a \<le> x)"
1.2081 +using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
1.2082 +
1.2083 +lemma rsup_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2084 +  shows "a \<ge> rsup S \<longleftrightarrow> (\<forall> x \<in> S. a \<ge> x)"
1.2085 +using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
1.2086 +
1.2087 +lemma rsup_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2088 +  shows "a < rsup S \<longleftrightarrow> (\<exists> x \<in> S. a < x)"
1.2089 +using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
1.2090 +
1.2091 +lemma rsup_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2092 +  shows "a > rsup S \<longleftrightarrow> (\<forall> x \<in> S. a > x)"
1.2093 +using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
1.2094 +
1.2095 +lemma rsup_unique: assumes b: "S *<= b" and S: "\<forall>b' < b. \<exists>x \<in> S. b' < x"
1.2096 +  shows "rsup S = b"
1.2097 +using b S
1.2098 +unfolding setle_def rsup_alt
1.2099 +apply -
1.2100 +apply (rule some_equality)
1.2101 +apply (metis  linorder_not_le order_eq_iff[symmetric])+
1.2102 +done
1.2103 +
1.2104 +lemma rsup_le_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. T *<= b) \<Longrightarrow> rsup S \<le> rsup T"
1.2105 +  apply (rule rsup_le)
1.2106 +  apply simp
1.2107 +  using rsup[of T] by (auto simp add: isLub_def leastP_def setge_def setle_def isUb_def)
1.2108 +
1.2109 +lemma isUb_def': "isUb R S = (\<lambda>x. S *<= x \<and> x \<in> R)"
1.2110 +  apply (rule ext)
1.2111 +  by (metis isUb_def)
1.2112 +
1.2113 +lemma UNIV_trivial: "UNIV x" using UNIV_I[of x] by (metis mem_def)
1.2114 +lemma rsup_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
1.2115 +  shows "a \<le> rsup S \<and> rsup S \<le> b"
1.2116 +proof-
1.2117 +  from rsup[OF Se] u have lub: "isLub UNIV S (rsup S)" by blast
1.2118 +  hence b: "rsup S \<le> b" using u by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
1.2119 +  from Se obtain y where y: "y \<in> S" by blast
1.2120 +  from lub l have "a \<le> rsup S" apply (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
1.2121 +    apply (erule ballE[where x=y])
1.2122 +    apply (erule ballE[where x=y])
1.2123 +    apply arith
1.2124 +    using y apply auto
1.2125 +    done
1.2126 +  with b show ?thesis by blast
1.2127 +qed
1.2128 +
1.2129 +lemma rsup_abs_le: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rsup S\<bar> \<le> a"
1.2130 +  unfolding abs_le_interval_iff  using rsup_bounds[of S "-a" a]
1.2131 +  by (auto simp add: setge_def setle_def)
1.2132 +
1.2133 +lemma rsup_asclose: assumes S:"S \<noteq> {}" and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e" shows "\<bar>rsup S - l\<bar> \<le> e"
1.2134 +proof-
1.2135 +  have th: "\<And>(x::real) l e. \<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e" by arith
1.2136 +  show ?thesis using S b rsup_bounds[of S "l - e" "l+e"] unfolding th
1.2137 +    by  (auto simp add: setge_def setle_def)
1.2138 +qed
1.2139 +
1.2140 +definition rinf:: "real set \<Rightarrow> real" where
1.2141 +  "rinf S = (SOME a. isGlb UNIV S a)"
1.2142 +
1.2143 +lemma rinf_alt: "rinf S = (SOME a. (\<forall>x \<in> S. x \<ge> a) \<and> (\<forall>b. (\<forall>x \<in> S. x \<ge> b) \<longrightarrow> a \<ge> b))"  by (auto simp  add: isGlb_def rinf_def greatestP_def isLb_def setle_def setge_def)
1.2144 +
1.2145 +lemma reals_complete_Glb: assumes Se: "\<exists>x. x \<in> S" and lb: "\<exists> y. isLb UNIV S y"
1.2146 +  shows "\<exists>(t::real). isGlb UNIV S t"
1.2147 +proof-
1.2148 +  let ?M = "uminus ` S"
1.2149 +  from lb have th: "\<exists>y. isUb UNIV ?M y" apply (auto simp add: isUb_def isLb_def setle_def setge_def)
1.2150 +    by (rule_tac x="-y" in exI, auto)
1.2151 +  from Se have Me: "\<exists>x. x \<in> ?M" by blast
1.2152 +  from reals_complete[OF Me th] obtain t where t: "isLub UNIV ?M t" by blast
1.2153 +  have "isGlb UNIV S (- t)" using t
1.2154 +    apply (auto simp add: isLub_def isGlb_def leastP_def greatestP_def setle_def setge_def isUb_def isLb_def)
1.2155 +    apply (erule_tac x="-y" in allE)
1.2156 +    apply auto
1.2157 +    done
1.2158 +  then show ?thesis by metis
1.2159 +qed
1.2160 +
1.2161 +lemma rinf: assumes Se: "S \<noteq> {}" and b: "\<exists>b. b <=* S"
1.2162 +  shows "isGlb UNIV S (rinf S)"
1.2163 +using Se b
1.2164 +unfolding rinf_def
1.2165 +apply clarify
1.2166 +apply (rule someI_ex)
1.2167 +apply (rule reals_complete_Glb)
1.2168 +apply (auto simp add: isLb_def setle_def setge_def)
1.2169 +done
1.2170 +
1.2171 +lemma rinf_ge: assumes Se: "S \<noteq> {}" and Sb: "b <=* S" shows "rinf S \<ge> b"
1.2172 +proof-
1.2173 +  from Sb have bu: "isLb UNIV S b" by (simp add: isLb_def setge_def)
1.2174 +  from rinf[OF Se] Sb have "isGlb UNIV S (rinf S)"  by blast
1.2175 +  then show ?thesis using bu by (auto simp add: isGlb_def greatestP_def setle_def setge_def)
1.2176 +qed
1.2177 +
1.2178 +lemma rinf_finite_Min: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2179 +  shows "rinf S = Min S"
1.2180 +using fS Se
1.2181 +proof-
1.2182 +  let ?m = "Min S"
1.2183 +  from Min_le[OF fS] have Sm: "\<forall> x\<in> S. x \<ge> ?m" by metis
1.2184 +  with rinf[OF Se] have glb: "isGlb UNIV S (rinf S)" by (metis setge_def)
1.2185 +  from Min_in[OF fS Se] glb have mrS: "?m \<ge> rinf S"
1.2186 +    by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def)
1.2187 +  moreover
1.2188 +  have "rinf S \<ge> ?m" using Sm glb
1.2189 +    by (auto simp add: isGlb_def greatestP_def isLb_def setle_def setge_def)
1.2190 +  ultimately  show ?thesis by arith
1.2191 +qed
1.2192 +
1.2193 +lemma rinf_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2194 +  shows "rinf S \<in> S"
1.2195 +  using rinf_finite_Min[OF fS Se] Min_in[OF fS Se] by metis
1.2196 +
1.2197 +lemma rinf_finite_Lb: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2198 +  shows "isLb S S (rinf S)"
1.2199 +  using rinf_finite_Min[OF fS Se] rinf_finite_in[OF fS Se] Min_le[OF fS]
1.2200 +  unfolding isLb_def setge_def by metis
1.2201 +
1.2202 +lemma rinf_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2203 +  shows "a \<le> rinf S \<longleftrightarrow> (\<forall> x \<in> S. a \<le> x)"
1.2204 +using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
1.2205 +
1.2206 +lemma rinf_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2207 +  shows "a \<ge> rinf S \<longleftrightarrow> (\<exists> x \<in> S. a \<ge> x)"
1.2208 +using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
1.2209 +
1.2210 +lemma rinf_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2211 +  shows "a < rinf S \<longleftrightarrow> (\<forall> x \<in> S. a < x)"
1.2212 +using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
1.2213 +
1.2214 +lemma rinf_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
1.2215 +  shows "a > rinf S \<longleftrightarrow> (\<exists> x \<in> S. a > x)"
1.2216 +using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
1.2217 +
1.2218 +lemma rinf_unique: assumes b: "b <=* S" and S: "\<forall>b' > b. \<exists>x \<in> S. b' > x"
1.2219 +  shows "rinf S = b"
1.2220 +using b S
1.2221 +unfolding setge_def rinf_alt
1.2222 +apply -
1.2223 +apply (rule some_equality)
1.2224 +apply (metis  linorder_not_le order_eq_iff[symmetric])+
1.2225 +done
1.2226 +
1.2227 +lemma rinf_ge_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. b <=* T) \<Longrightarrow> rinf S >= rinf T"
1.2228 +  apply (rule rinf_ge)
1.2229 +  apply simp
1.2230 +  using rinf[of T] by (auto simp add: isGlb_def greatestP_def setge_def setle_def isLb_def)
1.2231 +
1.2232 +lemma isLb_def': "isLb R S = (\<lambda>x. x <=* S \<and> x \<in> R)"
1.2233 +  apply (rule ext)
1.2234 +  by (metis isLb_def)
1.2235 +
1.2236 +lemma rinf_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
1.2237 +  shows "a \<le> rinf S \<and> rinf S \<le> b"
1.2238 +proof-
1.2239 +  from rinf[OF Se] l have lub: "isGlb UNIV S (rinf S)" by blast
1.2240 +  hence b: "a \<le> rinf S" using l by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
1.2241 +  from Se obtain y where y: "y \<in> S" by blast
1.2242 +  from lub u have "b \<ge> rinf S" apply (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
1.2243 +    apply (erule ballE[where x=y])
1.2244 +    apply (erule ballE[where x=y])
1.2245 +    apply arith
1.2246 +    using y apply auto
1.2247 +    done
1.2248 +  with b show ?thesis by blast
1.2249 +qed
1.2250 +
1.2251 +lemma rinf_abs_ge: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rinf S\<bar> \<le> a"
1.2252 +  unfolding abs_le_interval_iff  using rinf_bounds[of S "-a" a]
1.2253 +  by (auto simp add: setge_def setle_def)
1.2254 +
1.2255 +lemma rinf_asclose: assumes S:"S \<noteq> {}" and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e" shows "\<bar>rinf S - l\<bar> \<le> e"
1.2256 +proof-
1.2257 +  have th: "\<And>(x::real) l e. \<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e" by arith
1.2258 +  show ?thesis using S b rinf_bounds[of S "l - e" "l+e"] unfolding th
1.2259 +    by  (auto simp add: setge_def setle_def)
1.2260 +qed
1.2261 +
1.2262 +
1.2263 +
1.2264 +subsection{* Operator norm. *}
1.2265 +
1.2266 +definition "onorm f = rsup {norm (f x)| x. norm x = 1}"
1.2267 +
1.2268 +lemma norm_bound_generalize:
1.2269 +  fixes f:: "real ^'n \<Rightarrow> real^'m"
1.2270 +  assumes lf: "linear f"
1.2271 +  shows "(\<forall>x. norm x = 1 \<longrightarrow> norm (f x) \<le> b) \<longleftrightarrow> (\<forall>x. norm (f x) \<le> b * norm x)" (is "?lhs \<longleftrightarrow> ?rhs")
1.2272 +proof-
1.2273 +  {assume H: ?rhs
1.2274 +    {fix x :: "real^'n" assume x: "norm x = 1"
1.2275 +      from H[rule_format, of x] x have "norm (f x) \<le> b" by simp}
1.2276 +    then have ?lhs by blast }
1.2277 +
1.2278 +  moreover
1.2279 +  {assume H: ?lhs
1.2280 +    from H[rule_format, of "basis 1"]
1.2281 +    have bp: "b \<ge> 0" using norm_pos_le[of "f (basis 1)"] dimindex_ge_1[of "UNIV:: 'n set"]
1.2282 +      by (auto simp add: norm_basis)
1.2283 +    {fix x :: "real ^'n"
1.2284 +      {assume "x = 0"
1.2285 +	then have "norm (f x) \<le> b * norm x" by (simp add: linear_0[OF lf] norm_0 bp)}
1.2286 +      moreover
1.2287 +      {assume x0: "x \<noteq> 0"
1.2288 +	hence n0: "norm x \<noteq> 0" by (metis norm_eq_0)
1.2289 +	let ?c = "1/ norm x"
1.2290 +	have "norm (?c*s x) = 1" by (simp add: n0 norm_mul)
1.2291 +	with H have "norm (f(?c*s x)) \<le> b" by blast
1.2292 +	hence "?c * norm (f x) \<le> b"
1.2293 +	  by (simp add: linear_cmul[OF lf] norm_mul)
1.2294 +	hence "norm (f x) \<le> b * norm x"
1.2295 +	  using n0 norm_pos_le[of x] by (auto simp add: field_simps)}
1.2296 +      ultimately have "norm (f x) \<le> b * norm x" by blast}
1.2297 +    then have ?rhs by blast}
1.2298 +  ultimately show ?thesis by blast
1.2299 +qed
1.2300 +
1.2301 +lemma onorm:
1.2302 +  fixes f:: "real ^'n \<Rightarrow> real ^'m"
1.2303 +  assumes lf: "linear f"
1.2304 +  shows "norm (f x) <= onorm f * norm x"
1.2305 +  and "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
1.2306 +proof-
1.2307 +  {
1.2308 +    let ?S = "{norm (f x) |x. norm x = 1}"
1.2309 +    have Se: "?S \<noteq> {}" using  norm_basis_1 by auto
1.2310 +    from linear_bounded[OF lf] have b: "\<exists> b. ?S *<= b"
1.2311 +      unfolding norm_bound_generalize[OF lf, symmetric] by (auto simp add: setle_def)
1.2312 +    {from rsup[OF Se b, unfolded onorm_def[symmetric]]
1.2313 +      show "norm (f x) <= onorm f * norm x"
1.2314 +	apply -
1.2315 +	apply (rule spec[where x = x])
1.2316 +	unfolding norm_bound_generalize[OF lf, symmetric]
1.2317 +	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
1.2318 +    {
1.2319 +      show "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
1.2320 +	using rsup[OF Se b, unfolded onorm_def[symmetric]]
1.2321 +	unfolding norm_bound_generalize[OF lf, symmetric]
1.2322 +	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
1.2323 +  }
1.2324 +qed
1.2325 +
1.2326 +lemma onorm_pos_le: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" shows "0 <= onorm f"
1.2327 +  using order_trans[OF norm_pos_le onorm(1)[OF lf, of "basis 1"], unfolded norm_basis_1] by simp
1.2328 +
1.2329 +lemma onorm_eq_0: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)"
1.2330 +  shows "onorm f = 0 \<longleftrightarrow> (\<forall>x. f x = 0)"
1.2331 +  using onorm[OF lf]
1.2332 +  apply (auto simp add: norm_0 onorm_pos_le norm_le_0)
1.2333 +  apply atomize
1.2334 +  apply (erule allE[where x="0::real"])
1.2335 +  using onorm_pos_le[OF lf]
1.2336 +  apply arith
1.2337 +  done
1.2338 +
1.2339 +lemma onorm_const: "onorm(\<lambda>x::real^'n. (y::real ^ 'm)) = norm y"
1.2340 +proof-
1.2341 +  let ?f = "\<lambda>x::real^'n. (y::real ^ 'm)"
1.2342 +  have th: "{norm (?f x)| x. norm x = 1} = {norm y}"
1.2343 +    by(auto intro: vector_choose_size set_ext)
1.2344 +  show ?thesis
1.2345 +    unfolding onorm_def th
1.2346 +    apply (rule rsup_unique) by (simp_all  add: setle_def)
1.2347 +qed
1.2348 +
1.2349 +lemma onorm_pos_lt: assumes lf: "linear (f::real ^ 'n \<Rightarrow> real ^'m)"
1.2350 +  shows "0 < onorm f \<longleftrightarrow> ~(\<forall>x. f x = 0)"
1.2351 +  unfolding onorm_eq_0[OF lf, symmetric]
1.2352 +  using onorm_pos_le[OF lf] by arith
1.2353 +
1.2354 +lemma onorm_compose:
1.2355 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
1.2356 +  shows "onorm (f o g) <= onorm f * onorm g"
1.2357 +  apply (rule onorm(2)[OF linear_compose[OF lg lf], rule_format])
1.2358 +  unfolding o_def
1.2359 +  apply (subst mult_assoc)
1.2360 +  apply (rule order_trans)
1.2361 +  apply (rule onorm(1)[OF lf])
1.2362 +  apply (rule mult_mono1)
1.2363 +  apply (rule onorm(1)[OF lg])
1.2364 +  apply (rule onorm_pos_le[OF lf])
1.2365 +  done
1.2366 +
1.2367 +lemma onorm_neg_lemma: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
1.2368 +  shows "onorm (\<lambda>x. - f x) \<le> onorm f"
1.2369 +  using onorm[OF linear_compose_neg[OF lf]] onorm[OF lf]
1.2370 +  unfolding norm_neg by metis
1.2371 +
1.2372 +lemma onorm_neg: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
1.2373 +  shows "onorm (\<lambda>x. - f x) = onorm f"
1.2374 +  using onorm_neg_lemma[OF lf] onorm_neg_lemma[OF linear_compose_neg[OF lf]]
1.2375 +  by simp
1.2376 +
1.2377 +lemma onorm_triangle:
1.2378 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
1.2379 +  shows "onorm (\<lambda>x. f x + g x) <= onorm f + onorm g"
1.2380 +  apply(rule onorm(2)[OF linear_compose_add[OF lf lg], rule_format])
1.2381 +  apply (rule order_trans)
1.2382 +  apply (rule norm_triangle)
1.2383 +  apply (simp add: distrib)
1.2385 +  apply (rule onorm(1)[OF lf])
1.2386 +  apply (rule onorm(1)[OF lg])
1.2387 +  done
1.2388 +
1.2389 +lemma onorm_triangle_le: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) <= e
1.2390 +  \<Longrightarrow> onorm(\<lambda>x. f x + g x) <= e"
1.2391 +  apply (rule order_trans)
1.2392 +  apply (rule onorm_triangle)
1.2393 +  apply assumption+
1.2394 +  done
1.2395 +
1.2396 +lemma onorm_triangle_lt: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) < e
1.2397 +  ==> onorm(\<lambda>x. f x + g x) < e"
1.2398 +  apply (rule order_le_less_trans)
1.2399 +  apply (rule onorm_triangle)
1.2400 +  by assumption+
1.2401 +
1.2402 +(* "lift" from 'a to 'a^1 and "drop" from 'a^1 to 'a -- FIXME: potential use of transfer *)
1.2403 +
1.2404 +definition vec1:: "'a \<Rightarrow> 'a ^ 1" where "vec1 x = (\<chi> i. x)"
1.2405 +
1.2406 +definition dest_vec1:: "'a ^1 \<Rightarrow> 'a"
1.2407 +  where "dest_vec1 x = (x\$1)"
1.2408 +
1.2409 +lemma vec1_component[simp]: "(vec1 x)\$1 = x"
1.2410 +  by (simp add: vec1_def)
1.2411 +
1.2412 +lemma vec1_dest_vec1[simp]: "vec1(dest_vec1 x) = x" "dest_vec1(vec1 y) = y"
1.2413 +  by (simp_all add: vec1_def dest_vec1_def Cart_eq Cart_lambda_beta dimindex_def del: One_nat_def)
1.2414 +
1.2415 +lemma forall_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (vec1 x))" by (metis vec1_dest_vec1)
1.2416 +
1.2417 +lemma exists_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(vec1 x))" by (metis vec1_dest_vec1)
1.2418 +
1.2419 +lemma forall_dest_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P(dest_vec1 x))"  by (metis vec1_dest_vec1)
1.2420 +
1.2421 +lemma exists_dest_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(dest_vec1 x))"by (metis vec1_dest_vec1)
1.2422 +
1.2423 +lemma vec1_eq[simp]:  "vec1 x = vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
1.2424 +
1.2425 +lemma dest_vec1_eq[simp]: "dest_vec1 x = dest_vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
1.2426 +
1.2427 +lemma vec1_in_image_vec1: "vec1 x \<in> (vec1 ` S) \<longleftrightarrow> x \<in> S" by auto
1.2428 +
1.2429 +lemma vec1_vec: "vec1 x = vec x" by (vector vec1_def)
1.2430 +
1.2431 +lemma vec1_add: "vec1(x + y) = vec1 x + vec1 y" by (vector vec1_def)
1.2432 +lemma vec1_sub: "vec1(x - y) = vec1 x - vec1 y" by (vector vec1_def)
1.2433 +lemma vec1_cmul: "vec1(c* x) = c *s vec1 x " by (vector vec1_def)
1.2434 +lemma vec1_neg: "vec1(- x) = - vec1 x " by (vector vec1_def)
1.2435 +
1.2436 +lemma vec1_setsum: assumes fS: "finite S"
1.2437 +  shows "vec1(setsum f S) = setsum (vec1 o f) S"
1.2438 +  apply (induct rule: finite_induct[OF fS])
1.2439 +  apply (simp add: vec1_vec)
1.2441 +  done
1.2442 +
1.2443 +lemma dest_vec1_lambda: "dest_vec1(\<chi> i. x i) = x 1"
1.2444 +  by (simp add: dest_vec1_def)
1.2445 +
1.2446 +lemma dest_vec1_vec: "dest_vec1(vec x) = x"
1.2447 +  by (simp add: vec1_vec[symmetric])
1.2448 +
1.2449 +lemma dest_vec1_add: "dest_vec1(x + y) = dest_vec1 x + dest_vec1 y"
1.2450 + by (metis vec1_dest_vec1 vec1_add)
1.2451 +
1.2452 +lemma dest_vec1_sub: "dest_vec1(x - y) = dest_vec1 x - dest_vec1 y"
1.2453 + by (metis vec1_dest_vec1 vec1_sub)
1.2454 +
1.2455 +lemma dest_vec1_cmul: "dest_vec1(c*sx) = c * dest_vec1 x"
1.2456 + by (metis vec1_dest_vec1 vec1_cmul)
1.2457 +
1.2458 +lemma dest_vec1_neg: "dest_vec1(- x) = - dest_vec1 x"
1.2459 + by (metis vec1_dest_vec1 vec1_neg)
1.2460 +
1.2461 +lemma dest_vec1_0[simp]: "dest_vec1 0 = 0" by (metis vec_0 dest_vec1_vec)
1.2462 +
1.2463 +lemma dest_vec1_sum: assumes fS: "finite S"
1.2464 +  shows "dest_vec1(setsum f S) = setsum (dest_vec1 o f) S"
1.2465 +  apply (induct rule: finite_induct[OF fS])
1.2466 +  apply (simp add: dest_vec1_vec)
1.2468 +  done
1.2469 +
1.2470 +lemma norm_vec1: "norm(vec1 x) = abs(x)"
1.2471 +  by (simp add: vec1_def norm_real)
1.2472 +
1.2473 +lemma dist_vec1: "dist(vec1 x) (vec1 y) = abs(x - y)"
1.2474 +  by (simp only: dist_real vec1_component)
1.2475 +lemma abs_dest_vec1: "norm x = \<bar>dest_vec1 x\<bar>"
1.2476 +  by (metis vec1_dest_vec1 norm_vec1)
1.2477 +
1.2478 +lemma linear_vmul_dest_vec1:
1.2479 +  fixes f:: "'a::semiring_1^'n \<Rightarrow> 'a^1"
1.2480 +  shows "linear f \<Longrightarrow> linear (\<lambda>x. dest_vec1(f x) *s v)"
1.2481 +  unfolding dest_vec1_def
1.2482 +  apply (rule linear_vmul_component)
1.2483 +  by (auto simp add: dimindex_def)
1.2484 +
1.2485 +lemma linear_from_scalars:
1.2486 +  assumes lf: "linear (f::'a::comm_ring_1 ^1 \<Rightarrow> 'a^'n)"
1.2487 +  shows "f = (\<lambda>x. dest_vec1 x *s column 1 (matrix f))"
1.2488 +  apply (rule ext)
1.2489 +  apply (subst matrix_works[OF lf, symmetric])
1.2490 +  apply (auto simp add: Cart_eq matrix_vector_mult_def dest_vec1_def column_def Cart_lambda_beta vector_component dimindex_def mult_commute del: One_nat_def )
1.2491 +  done
1.2492 +
1.2493 +lemma linear_to_scalars: assumes lf: "linear (f::'a::comm_ring_1 ^'n \<Rightarrow> 'a^1)"
1.2494 +  shows "f = (\<lambda>x. vec1(row 1 (matrix f) \<bullet> x))"
1.2495 +  apply (rule ext)
1.2496 +  apply (subst matrix_works[OF lf, symmetric])
1.2497 +  apply (auto simp add: Cart_eq matrix_vector_mult_def vec1_def row_def Cart_lambda_beta vector_component dimindex_def dot_def mult_commute)
1.2498 +  done
1.2499 +
1.2500 +lemma dest_vec1_eq_0: "dest_vec1 x = 0 \<longleftrightarrow> x = 0"
1.2501 +  by (simp add: dest_vec1_eq[symmetric])
1.2502 +
1.2503 +lemma setsum_scalars: assumes fS: "finite S"
1.2504 +  shows "setsum f S = vec1 (setsum (dest_vec1 o f) S)"
1.2505 +  unfolding vec1_setsum[OF fS] by simp
1.2506 +
1.2507 +lemma dest_vec1_wlog_le: "(\<And>(x::'a::linorder ^ 1) y. P x y \<longleftrightarrow> P y x)  \<Longrightarrow> (\<And>x y. dest_vec1 x <= dest_vec1 y ==> P x y) \<Longrightarrow> P x y"
1.2508 +  apply (cases "dest_vec1 x \<le> dest_vec1 y")
1.2509 +  apply simp
1.2510 +  apply (subgoal_tac "dest_vec1 y \<le> dest_vec1 x")
1.2511 +  apply (auto)
1.2512 +  done
1.2513 +
1.2514 +text{* Pasting vectors. *}
1.2515 +
1.2516 +lemma linear_fstcart: "linear fstcart"
1.2517 +  by (auto simp add: linear_def fstcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
1.2518 +
1.2519 +lemma linear_sndcart: "linear sndcart"
1.2520 +  by (auto simp add: linear_def sndcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
1.2521 +
1.2522 +lemma fstcart_vec[simp]: "fstcart(vec x) = vec x"
1.2523 +  by (vector fstcart_def vec_def dimindex_finite_sum)
1.2524 +
1.2525 +lemma fstcart_add[simp]:"fstcart(x + y) = fstcart (x::'a::{plus,times}^('b,'c) finite_sum) + fstcart y"
1.2526 +  using linear_fstcart[unfolded linear_def] by blast
1.2527 +
1.2528 +lemma fstcart_cmul[simp]:"fstcart(c*s x) = c*s fstcart (x::'a::{plus,times}^('b,'c) finite_sum)"
1.2529 +  using linear_fstcart[unfolded linear_def] by blast
1.2530 +
1.2531 +lemma fstcart_neg[simp]:"fstcart(- x) = - fstcart (x::'a::ring_1^('b,'c) finite_sum)"
1.2532 +unfolding vector_sneg_minus1 fstcart_cmul ..
1.2533 +
1.2534 +lemma fstcart_sub[simp]:"fstcart(x - y) = fstcart (x::'a::ring_1^('b,'c) finite_sum) - fstcart y"
1.2535 +  unfolding diff_def fstcart_add fstcart_neg  ..
1.2536 +
1.2537 +lemma fstcart_setsum:
1.2538 +  fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
1.2539 +  assumes fS: "finite S"
1.2540 +  shows "fstcart (setsum f S) = setsum (\<lambda>i. fstcart (f i)) S"
1.2541 +  by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
1.2542 +
1.2543 +lemma sndcart_vec[simp]: "sndcart(vec x) = vec x"
1.2544 +  by (vector sndcart_def vec_def dimindex_finite_sum)
1.2545 +
1.2546 +lemma sndcart_add[simp]:"sndcart(x + y) = sndcart (x::'a::{plus,times}^('b,'c) finite_sum) + sndcart y"
1.2547 +  using linear_sndcart[unfolded linear_def] by blast
1.2548 +
1.2549 +lemma sndcart_cmul[simp]:"sndcart(c*s x) = c*s sndcart (x::'a::{plus,times}^('b,'c) finite_sum)"
1.2550 +  using linear_sndcart[unfolded linear_def] by blast
1.2551 +
1.2552 +lemma sndcart_neg[simp]:"sndcart(- x) = - sndcart (x::'a::ring_1^('b,'c) finite_sum)"
1.2553 +unfolding vector_sneg_minus1 sndcart_cmul ..
1.2554 +
1.2555 +lemma sndcart_sub[simp]:"sndcart(x - y) = sndcart (x::'a::ring_1^('b,'c) finite_sum) - sndcart y"
1.2556 +  unfolding diff_def sndcart_add sndcart_neg  ..
1.2557 +
1.2558 +lemma sndcart_setsum:
1.2559 +  fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
1.2560 +  assumes fS: "finite S"
1.2561 +  shows "sndcart (setsum f S) = setsum (\<lambda>i. sndcart (f i)) S"
1.2562 +  by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
1.2563 +
1.2564 +lemma pastecart_vec[simp]: "pastecart (vec x) (vec x) = vec x"
1.2565 +  by (simp add: pastecart_eq fstcart_vec sndcart_vec fstcart_pastecart sndcart_pastecart)
1.2566 +
1.2567 +lemma pastecart_add[simp]:"pastecart (x1::'a::{plus,times}^_) y1 + pastecart x2 y2 = pastecart (x1 + x2) (y1 + y2)"
1.2569 +
1.2570 +lemma pastecart_cmul[simp]: "pastecart (c *s (x1::'a::{plus,times}^_)) (c *s y1) = c *s pastecart x1 y1"
1.2571 +  by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
1.2572 +
1.2573 +lemma pastecart_neg[simp]: "pastecart (- (x::'a::ring_1^_)) (- y) = - pastecart x y"
1.2574 +  unfolding vector_sneg_minus1 pastecart_cmul ..
1.2575 +
1.2576 +lemma pastecart_sub: "pastecart (x1::'a::ring_1^_) y1 - pastecart x2 y2 = pastecart (x1 - x2) (y1 - y2)"
1.2577 +  by (simp add: diff_def pastecart_neg[symmetric] del: pastecart_neg)
1.2578 +
1.2579 +lemma pastecart_setsum:
1.2580 +  fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
1.2581 +  assumes fS: "finite S"
1.2582 +  shows "pastecart (setsum f S) (setsum g S) = setsum (\<lambda>i. pastecart (f i) (g i)) S"
1.2583 +  by (simp  add: pastecart_eq fstcart_setsum[OF fS] sndcart_setsum[OF fS] fstcart_pastecart sndcart_pastecart)
1.2584 +
1.2585 +lemma norm_fstcart: "norm(fstcart x) <= norm (x::real ^('n,'m) finite_sum)"
1.2586 +proof-
1.2587 +  let ?n = "dimindex (UNIV :: 'n set)"
1.2588 +  let ?m = "dimindex (UNIV :: 'm set)"
1.2589 +  let ?N = "{1 .. ?n}"
1.2590 +  let ?M = "{1 .. ?m}"
1.2591 +  let ?NM = "{1 .. dimindex (UNIV :: ('n,'m) finite_sum set)}"
1.2592 +  have th_0: "1 \<le> ?n +1" by simp
1.2593 +  have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
1.2594 +    by (simp add: pastecart_fst_snd)
1.2595 +  have th1: "fstcart x \<bullet> fstcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
1.2596 +    by (simp add: dot_def setsum_add_split[OF th_0, of _ ?m] pastecart_def dimindex_finite_sum Cart_lambda_beta setsum_nonneg zero_le_square del: One_nat_def)
1.2597 +  then show ?thesis
1.2598 +    unfolding th0
1.2599 +    unfolding real_vector_norm_def real_sqrt_le_iff real_of_real_def id_def
1.2600 +    by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
1.2601 +qed
1.2602 +
1.2603 +lemma dist_fstcart: "dist(fstcart (x::real^_)) (fstcart y) <= dist x y"
1.2604 +  by (metis dist_def fstcart_sub[symmetric] norm_fstcart)
1.2605 +
1.2606 +lemma norm_sndcart: "norm(sndcart x) <= norm (x::real ^('n,'m) finite_sum)"
1.2607 +proof-
1.2608 +  let ?n = "dimindex (UNIV :: 'n set)"
1.2609 +  let ?m = "dimindex (UNIV :: 'm set)"
1.2610 +  let ?N = "{1 .. ?n}"
1.2611 +  let ?M = "{1 .. ?m}"
1.2612 +  let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
1.2613 +  let ?NM = "{1 .. ?nm}"
1.2614 +  have thnm[simp]: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
1.2615 +  have th_0: "1 \<le> ?n +1" by simp
1.2616 +  have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
1.2617 +    by (simp add: pastecart_fst_snd)
1.2618 +  let ?f = "\<lambda>n. n - ?n"
1.2619 +  let ?S = "{?n+1 .. ?nm}"
1.2620 +  have finj:"inj_on ?f ?S"
1.2621 +    using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"]
1.2622 +    apply (simp add: Ball_def atLeastAtMost_iff inj_on_def dimindex_finite_sum del: One_nat_def)
1.2623 +    by arith
1.2624 +  have fS: "?f ` ?S = ?M"
1.2625 +    apply (rule set_ext)
1.2626 +    apply (simp add: image_iff Bex_def) using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"] by arith
1.2627 +  have th1: "sndcart x \<bullet> sndcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
1.2628 +    by (simp add: dot_def setsum_add_split[OF th_0, of _ ?m] pastecart_def dimindex_finite_sum Cart_lambda_beta setsum_nonneg zero_le_square setsum_reindex[OF finj, unfolded fS] del: One_nat_def)
1.2629 +  then show ?thesis
1.2630 +    unfolding th0
1.2631 +    unfolding real_vector_norm_def real_sqrt_le_iff real_of_real_def id_def
1.2632 +    by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
1.2633 +qed
1.2634 +
1.2635 +lemma dist_sndcart: "dist(sndcart (x::real^_)) (sndcart y) <= dist x y"
1.2636 +  by (metis dist_def sndcart_sub[symmetric] norm_sndcart)
1.2637 +
1.2638 +lemma dot_pastecart: "(pastecart (x1::'a::{times,comm_monoid_add}^'n) (x2::'a::{times,comm_monoid_add}^'m)) \<bullet> (pastecart y1 y2) =  x1 \<bullet> y1 + x2 \<bullet> y2"
1.2639 +proof-
1.2640 +  let ?n = "dimindex (UNIV :: 'n set)"
1.2641 +  let ?m = "dimindex (UNIV :: 'm set)"
1.2642 +  let ?N = "{1 .. ?n}"
1.2643 +  let ?M = "{1 .. ?m}"
1.2644 +  let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
1.2645 +  let ?NM = "{1 .. ?nm}"
1.2646 +  have thnm: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
1.2647 +  have th_0: "1 \<le> ?n +1" by simp
1.2648 +  have th_1: "\<And>i. i \<in> {?m + 1 .. ?nm} \<Longrightarrow> i - ?m \<in> ?N" apply (simp add: thnm) by arith
1.2649 +  let ?f = "\<lambda>a b i. (a\$i) * (b\$i)"
1.2650 +  let ?g = "?f (pastecart x1 x2) (pastecart y1 y2)"
1.2651 +  let ?S = "{?n +1 .. ?nm}"
1.2652 +  {fix i
1.2653 +    assume i: "i \<in> ?N"
1.2654 +    have "?g i = ?f x1 y1 i"
1.2655 +      using i
1.2656 +      apply (simp add: pastecart_def Cart_lambda_beta thnm) done
1.2657 +  }
1.2658 +  hence th2: "setsum ?g ?N = setsum (?f x1 y1) ?N"
1.2659 +    apply -
1.2660 +    apply (rule setsum_cong)
1.2661 +    apply auto
1.2662 +    done
1.2663 +  {fix i
1.2664 +    assume i: "i \<in> ?S"
1.2665 +    have "?g i = ?f x2 y2 (i - ?n)"
1.2666 +      using i
1.2667 +      apply (simp add: pastecart_def Cart_lambda_beta thnm) done
1.2668 +  }
1.2669 +  hence th3: "setsum ?g ?S = setsum (\<lambda>i. ?f x2 y2 (i -?n)) ?S"
1.2670 +    apply -
1.2671 +    apply (rule setsum_cong)
1.2672 +    apply auto
1.2673 +    done
1.2674 +  let ?r = "\<lambda>n. n - ?n"
1.2675 +  have rinj: "inj_on ?r ?S" apply (simp add: inj_on_def Ball_def thnm) by arith
1.2676 +  have rS: "?r ` ?S = ?M" apply (rule set_ext)
1.2677 +    apply (simp add: thnm image_iff Bex_def) by arith
1.2678 +  have "pastecart x1 x2 \<bullet> (pastecart y1 y2) = setsum ?g ?NM" by (simp add: dot_def)
1.2679 +  also have "\<dots> = setsum ?g ?N + setsum ?g ?S"
1.2680 +    by (simp add: dot_def thnm setsum_add_split[OF th_0, of _ ?m] del: One_nat_def)
1.2681 +  also have "\<dots> = setsum (?f x1 y1) ?N + setsum (?f x2 y2) ?M"
1.2682 +    unfolding setsum_reindex[OF rinj, unfolded rS o_def] th2 th3 ..
1.2683 +  finally
1.2684 +  show ?thesis by (simp add: dot_def)
1.2685 +qed
1.2686 +
1.2687 +lemma norm_pastecart: "norm(pastecart x y) <= norm(x :: real ^ _) + norm(y)"
1.2688 +  unfolding real_vector_norm_def dot_pastecart real_sqrt_le_iff real_of_real_def id_def
1.2689 +  apply (rule power2_le_imp_le)
1.2691 +  apply (auto simp add: power2_eq_square ring_simps)
1.2692 +  apply (simp add: power2_eq_square[symmetric])
1.2693 +  apply (rule mult_nonneg_nonneg)
1.2694 +  apply (simp_all add: real_sqrt_pow2[OF dot_pos_le])
1.2696 +  apply (simp_all add: real_sqrt_pow2[OF dot_pos_le])
1.2697 +  done
1.2698 +
1.2699 +subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
1.2700 +
1.2701 +definition hull :: "'a set set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "hull" 75) where
1.2702 +  "S hull s = Inter {t. t \<in> S \<and> s \<subseteq> t}"
1.2703 +
1.2704 +lemma hull_same: "s \<in> S \<Longrightarrow> S hull s = s"
1.2705 +  unfolding hull_def by auto
1.2706 +
1.2707 +lemma hull_in: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) \<in> S"
1.2708 +unfolding hull_def subset_iff by auto
1.2709 +
1.2710 +lemma hull_eq: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) = s \<longleftrightarrow> s \<in> S"
1.2711 +using hull_same[of s S] hull_in[of S s] by metis
1.2712 +
1.2713 +
1.2714 +lemma hull_hull: "S hull (S hull s) = S hull s"
1.2715 +  unfolding hull_def by blast
1.2716 +
1.2717 +lemma hull_subset: "s \<subseteq> (S hull s)"
1.2718 +  unfolding hull_def by blast
1.2719 +
1.2720 +lemma hull_mono: " s \<subseteq> t ==> (S hull s) \<subseteq> (S hull t)"
1.2721 +  unfolding hull_def by blast
1.2722 +
1.2723 +lemma hull_antimono: "S \<subseteq> T ==> (T hull s) \<subseteq> (S hull s)"
1.2724 +  unfolding hull_def by blast
1.2725 +
1.2726 +lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> t \<in> S ==> (S hull s) \<subseteq> t"
1.2727 +  unfolding hull_def by blast
1.2728 +
1.2729 +lemma subset_hull: "t \<in> S ==> S hull s \<subseteq> t \<longleftrightarrow>  s \<subseteq> t"
1.2730 +  unfolding hull_def by blast
1.2731 +
1.2732 +lemma hull_unique: "s \<subseteq> t \<Longrightarrow> t \<in> S \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> t' \<in> S ==> t \<subseteq> t')
1.2733 +           ==> (S hull s = t)"
1.2734 +unfolding hull_def by auto
1.2735 +
1.2736 +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"
1.2737 +  using hull_minimal[of S "{x. P x}" Q]
1.2738 +  by (auto simp add: subset_eq Collect_def mem_def)
1.2739 +
1.2740 +lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S" by (metis hull_subset subset_eq)
1.2741 +
1.2742 +lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
1.2743 +unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
1.2744 +
1.2745 +lemma hull_union: assumes T: "\<And>T. T \<subseteq> S ==> Inter T \<in> S"
1.2746 +  shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
1.2747 +apply rule
1.2748 +apply (rule hull_mono)
1.2749 +unfolding Un_subset_iff
1.2750 +apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
1.2751 +apply (rule hull_minimal)
1.2752 +apply (metis hull_union_subset)
1.2753 +apply (metis hull_in T)
1.2754 +done
1.2755 +
1.2756 +lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
1.2757 +  unfolding hull_def by blast
1.2758 +
1.2759 +lemma hull_redundant: "a \<in> (S hull s) ==> (S hull (insert a s) = S hull s)"
1.2760 +by (metis hull_redundant_eq)
1.2761 +
1.2762 +text{* Archimedian properties and useful consequences. *}
1.2763 +
1.2764 +lemma real_arch_simple: "\<exists>n. x <= real (n::nat)"
1.2765 +  using reals_Archimedean2[of x] apply auto by (rule_tac x="Suc n" in exI, auto)
1.2766 +lemmas real_arch_lt = reals_Archimedean2
1.2767 +
1.2768 +lemmas real_arch = reals_Archimedean3
1.2769 +
1.2770 +lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
1.2771 +  using reals_Archimedean
1.2772 +  apply (auto simp add: field_simps inverse_positive_iff_positive)
1.2773 +  apply (subgoal_tac "inverse (real n) > 0")
1.2774 +  apply arith
1.2775 +  apply simp
1.2776 +  done
1.2777 +
1.2778 +lemma real_pow_lbound: "0 <= x ==> 1 + real n * x <= (1 + x) ^ n"
1.2779 +proof(induct n)
1.2780 +  case 0 thus ?case by simp
1.2781 +next
1.2782 +  case (Suc n)
1.2783 +  hence h: "1 + real n * x \<le> (1 + x) ^ n" by simp
1.2784 +  from h have p: "1 \<le> (1 + x) ^ n" using Suc.prems by simp
1.2785 +  from h have "1 + real n * x + x \<le> (1 + x) ^ n + x" by simp
1.2786 +  also have "\<dots> \<le> (1 + x) ^ Suc n" apply (subst diff_le_0_iff_le[symmetric])
1.2787 +    apply (simp add: ring_simps)
1.2788 +    using mult_left_mono[OF p Suc.prems] by simp
1.2789 +  finally show ?case  by (simp add: real_of_nat_Suc ring_simps)
1.2790 +qed
1.2791 +
1.2792 +lemma real_arch_pow: assumes x: "1 < (x::real)" shows "\<exists>n. y < x^n"
1.2793 +proof-
1.2794 +  from x have x0: "x - 1 > 0" by arith
1.2795 +  from real_arch[OF x0, rule_format, of y]
1.2796 +  obtain n::nat where n:"y < real n * (x - 1)" by metis
1.2797 +  from x0 have x00: "x- 1 \<ge> 0" by arith
1.2798 +  from real_pow_lbound[OF x00, of n] n
1.2799 +  have "y < x^n" by auto
1.2800 +  then show ?thesis by metis
1.2801 +qed
1.2802 +
1.2803 +lemma real_arch_pow2: "\<exists>n. (x::real) < 2^ n"
1.2804 +  using real_arch_pow[of 2 x] by simp
1.2805 +
1.2806 +lemma real_arch_pow_inv: assumes y: "(y::real) > 0" and x1: "x < 1"
1.2807 +  shows "\<exists>n. x^n < y"
1.2808 +proof-
1.2809 +  {assume x0: "x > 0"
1.2810 +    from x0 x1 have ix: "1 < 1/x" by (simp add: field_simps)
1.2811 +    from real_arch_pow[OF ix, of "1/y"]
1.2812 +    obtain n where n: "1/y < (1/x)^n" by blast
1.2813 +    then
1.2814 +    have ?thesis using y x0 by (auto simp add: field_simps power_divide) }
1.2815 +  moreover
1.2816 +  {assume "\<not> x > 0" with y x1 have ?thesis apply auto by (rule exI[where x=1], auto)}
1.2817 +  ultimately show ?thesis by metis
1.2818 +qed
1.2819 +
1.2820 +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)"
1.2821 +  by (metis real_arch_inv)
1.2822 +
1.2823 +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"
1.2824 +  apply (rule forall_pos_mono)
1.2825 +  apply auto
1.2826 +  apply (atomize)
1.2827 +  apply (erule_tac x="n - 1" in allE)
1.2828 +  apply auto
1.2829 +  done
1.2830 +
1.2831 +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"
1.2832 +  shows "x = 0"
1.2833 +proof-
1.2834 +  {assume "x \<noteq> 0" with x0 have xp: "x > 0" by arith
1.2835 +    from real_arch[OF xp, rule_format, of c] obtain n::nat where n: "c < real n * x"  by blast
1.2836 +    with xc[rule_format, of n] have "n = 0" by arith
1.2837 +    with n c have False by simp}
1.2838 +  then show ?thesis by blast
1.2839 +qed
1.2840 +
1.2841 +(* ------------------------------------------------------------------------- *)
1.2842 +(* Relate max and min to sup and inf.                                        *)
1.2843 +(* ------------------------------------------------------------------------- *)
1.2844 +
1.2845 +lemma real_max_rsup: "max x y = rsup {x,y}"
1.2846 +proof-
1.2847 +  have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
1.2848 +  from rsup_finite_le_iff[OF f, of "max x y"] have "rsup {x,y} \<le> max x y" by simp
1.2849 +  moreover
1.2850 +  have "max x y \<le> rsup {x,y}" using rsup_finite_ge_iff[OF f, of "max x y"]
1.2851 +    by (simp add: linorder_linear)
1.2852 +  ultimately show ?thesis by arith
1.2853 +qed
1.2854 +
1.2855 +lemma real_min_rinf: "min x y = rinf {x,y}"
1.2856 +proof-
1.2857 +  have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
1.2858 +  from rinf_finite_le_iff[OF f, of "min x y"] have "rinf {x,y} \<le> min x y"
1.2859 +    by (simp add: linorder_linear)
1.2860 +  moreover
1.2861 +  have "min x y \<le> rinf {x,y}" using rinf_finite_ge_iff[OF f, of "min x y"]
1.2862 +    by simp
1.2863 +  ultimately show ?thesis by arith
1.2864 +qed
1.2865 +
1.2866 +(* ------------------------------------------------------------------------- *)
1.2867 +(* Geometric progression.                                                    *)
1.2868 +(* ------------------------------------------------------------------------- *)
1.2869 +
1.2870 +lemma sum_gp_basic: "((1::'a::{field, recpower}) - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
1.2871 +  (is "?lhs = ?rhs")
1.2872 +proof-
1.2873 +  {assume x1: "x = 1" hence ?thesis by simp}
1.2874 +  moreover
1.2875 +  {assume x1: "x\<noteq>1"
1.2876 +    hence x1': "x - 1 \<noteq> 0" "1 - x \<noteq> 0" "x - 1 = - (1 - x)" "- (1 - x) \<noteq> 0" by auto
1.2877 +    from geometric_sum[OF x1, of "Suc n", unfolded x1']
1.2878 +    have "(- (1 - x)) * setsum (\<lambda>i. x^i) {0 .. n} = - (1 - x^(Suc n))"
1.2879 +      unfolding atLeastLessThanSuc_atLeastAtMost
1.2880 +      using x1' apply (auto simp only: field_simps)
1.2881 +      apply (simp add: ring_simps)
1.2882 +      done
1.2883 +    then have ?thesis by (simp add: ring_simps) }
1.2884 +  ultimately show ?thesis by metis
1.2885 +qed
1.2886 +
1.2887 +lemma sum_gp_multiplied: assumes mn: "m <= n"
1.2888 +  shows "((1::'a::{field, recpower}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
1.2889 +  (is "?lhs = ?rhs")
1.2890 +proof-
1.2891 +  let ?S = "{0..(n - m)}"
1.2892 +  from mn have mn': "n - m \<ge> 0" by arith
1.2893 +  let ?f = "op + m"
1.2894 +  have i: "inj_on ?f ?S" unfolding inj_on_def by auto
1.2895 +  have f: "?f ` ?S = {m..n}"
1.2896 +    using mn apply (auto simp add: image_iff Bex_def) by arith
1.2897 +  have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)"
1.2899 +  from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
1.2900 +  have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
1.2901 +  then show ?thesis unfolding sum_gp_basic using mn
1.2903 +qed
1.2904 +
1.2905 +lemma sum_gp: "setsum (op ^ (x::'a::{field, recpower})) {m .. n} =
1.2906 +   (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m)
1.2907 +                    else (x^ m - x^ (Suc n)) / (1 - x))"
1.2908 +proof-
1.2909 +  {assume nm: "n < m" hence ?thesis by simp}
1.2910 +  moreover
1.2911 +  {assume "\<not> n < m" hence nm: "m \<le> n" by arith
1.2912 +    {assume x: "x = 1"  hence ?thesis by simp}
1.2913 +    moreover
1.2914 +    {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
1.2915 +      from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
1.2916 +    ultimately have ?thesis by metis
1.2917 +  }
1.2918 +  ultimately show ?thesis by metis
1.2919 +qed
1.2920 +
1.2921 +lemma sum_gp_offset: "setsum (op ^ (x::'a::{field,recpower})) {m .. m+n} =
1.2922 +  (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
1.2923 +  unfolding sum_gp[of x m "m + n"] power_Suc
1.2925 +
1.2926 +
1.2927 +subsection{* A bit of linear algebra. *}
1.2928 +
1.2929 +definition "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 *s x \<in>S )"
1.2930 +definition "span S = (subspace hull S)"
1.2931 +definition "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span(S - {a}))"
1.2932 +abbreviation "independent s == ~(dependent s)"
1.2933 +
1.2934 +(* Closure properties of subspaces.                                          *)
1.2935 +
1.2936 +lemma subspace_UNIV[simp]: "subspace(UNIV)" by (simp add: subspace_def)
1.2937 +
1.2938 +lemma subspace_0: "subspace S ==> 0 \<in> S" by (metis subspace_def)
1.2939 +
1.2940 +lemma subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S ==> x + y \<in> S"
1.2941 +  by (metis subspace_def)
1.2942 +
1.2943 +lemma subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *s x \<in> S"
1.2944 +  by (metis subspace_def)
1.2945 +
1.2946 +lemma subspace_neg: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> - x \<in> S"
1.2947 +  by (metis vector_sneg_minus1 subspace_mul)
1.2948 +
1.2949 +lemma subspace_sub: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
1.2950 +  by (metis diff_def subspace_add subspace_neg)
1.2951 +
1.2952 +lemma subspace_setsum:
1.2953 +  assumes sA: "subspace A" and fB: "finite B"
1.2954 +  and f: "\<forall>x\<in> B. f x \<in> A"
1.2955 +  shows "setsum f B \<in> A"
1.2956 +  using  fB f sA
1.2957 +  apply(induct rule: finite_induct[OF fB])
1.2959 +
1.2960 +lemma subspace_linear_image:
1.2961 +  assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and sS: "subspace S"
1.2962 +  shows "subspace(f ` S)"
1.2963 +  using lf sS linear_0[OF lf]
1.2964 +  unfolding linear_def subspace_def
1.2965 +  apply (auto simp add: image_iff)
1.2966 +  apply (rule_tac x="x + y" in bexI, auto)
1.2967 +  apply (rule_tac x="c*s x" in bexI, auto)
1.2968 +  done
1.2969 +
1.2970 +lemma subspace_linear_preimage: "linear (f::'a::semiring_1^'n \<Rightarrow> _) ==> subspace S ==> subspace {x. f x \<in> S}"
1.2971 +  by (auto simp add: subspace_def linear_def linear_0[of f])
1.2972 +
1.2973 +lemma subspace_trivial: "subspace {0::'a::semiring_1 ^_}"
1.2974 +  by (simp add: subspace_def)
1.2975 +
1.2976 +lemma subspace_inter: "subspace A \<Longrightarrow> subspace B ==> subspace (A \<inter> B)"
1.2977 +  by (simp add: subspace_def)
1.2978 +
1.2979 +
1.2980 +lemma span_mono: "A \<subseteq> B ==> span A \<subseteq> span B"
1.2981 +  by (metis span_def hull_mono)
1.2982 +
1.2983 +lemma subspace_span: "subspace(span S)"
1.2984 +  unfolding span_def
1.2985 +  apply (rule hull_in[unfolded mem_def])
1.2986 +  apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
1.2987 +  apply auto
1.2988 +  apply (erule_tac x="X" in ballE)
1.2989 +  apply (simp add: mem_def)
1.2990 +  apply blast
1.2991 +  apply (erule_tac x="X" in ballE)
1.2992 +  apply (erule_tac x="X" in ballE)
1.2993 +  apply (erule_tac x="X" in ballE)
1.2994 +  apply (clarsimp simp add: mem_def)
1.2995 +  apply simp
1.2996 +  apply simp
1.2997 +  apply simp
1.2998 +  apply (erule_tac x="X" in ballE)
1.2999 +  apply (erule_tac x="X" in ballE)
1.3000 +  apply (simp add: mem_def)
1.3001 +  apply simp
1.3002 +  apply simp
1.3003 +  done
1.3004 +
1.3005 +lemma span_clauses:
1.3006 +  "a \<in> S ==> a \<in> span S"
1.3007 +  "0 \<in> span S"
1.3008 +  "x\<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
1.3009 +  "x \<in> span S \<Longrightarrow> c *s x \<in> span S"
1.3010 +  by (metis span_def hull_subset subset_eq subspace_span subspace_def)+
1.3011 +
1.3012 +lemma span_induct: assumes SP: "\<And>x. x \<in> S ==> P x"
1.3013 +  and P: "subspace P" and x: "x \<in> span S" shows "P x"
1.3014 +proof-
1.3015 +  from SP have SP': "S \<subseteq> P" by (simp add: mem_def subset_eq)
1.3016 +  from P have P': "P \<in> subspace" by (simp add: mem_def)
1.3017 +  from x hull_minimal[OF SP' P', unfolded span_def[symmetric]]
1.3018 +  show "P x" by (metis mem_def subset_eq)
1.3019 +qed
1.3020 +
1.3021 +lemma span_empty: "span {} = {(0::'a::semiring_0 ^ 'n)}"
1.3022 +  apply (simp add: span_def)
1.3023 +  apply (rule hull_unique)
1.3024 +  apply (auto simp add: mem_def subspace_def)
1.3025 +  unfolding mem_def[of "0::'a^'n", symmetric]
1.3026 +  apply simp
1.3027 +  done
1.3028 +
1.3029 +lemma independent_empty: "independent {}"
1.3030 +  by (simp add: dependent_def)
1.3031 +
1.3032 +lemma independent_mono: "independent A \<Longrightarrow> B \<subseteq> A ==> independent B"
1.3033 +  apply (clarsimp simp add: dependent_def span_mono)
1.3034 +  apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
1.3035 +  apply force
1.3036 +  apply (rule span_mono)
1.3037 +  apply auto
1.3038 +  done
1.3039 +
1.3040 +lemma span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
1.3041 +  by (metis order_antisym span_def hull_minimal mem_def)
1.3042 +
1.3043 +lemma span_induct': assumes SP: "\<forall>x \<in> S. P x"
1.3044 +  and P: "subspace P" shows "\<forall>x \<in> span S. P x"
1.3045 +  using span_induct SP P by blast
1.3046 +
1.3047 +inductive span_induct_alt_help for S:: "'a::semiring_1^'n \<Rightarrow> bool"
1.3048 +  where
1.3049 +  span_induct_alt_help_0: "span_induct_alt_help S 0"
1.3050 +  | span_induct_alt_help_S: "x \<in> S \<Longrightarrow> span_induct_alt_help S z \<Longrightarrow> span_induct_alt_help S (c *s x + z)"
1.3051 +
1.3052 +lemma span_induct_alt':
1.3053 +  assumes h0: "h (0::'a::semiring_1^'n)" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c*s x + y)" shows "\<forall>x \<in> span S. h x"
1.3054 +proof-
1.3055 +  {fix x:: "'a^'n" assume x: "span_induct_alt_help S x"
1.3056 +    have "h x"
1.3057 +      apply (rule span_induct_alt_help.induct[OF x])
1.3058 +      apply (rule h0)
1.3059 +      apply (rule hS, assumption, assumption)
1.3060 +      done}
1.3061 +  note th0 = this
1.3062 +  {fix x assume x: "x \<in> span S"
1.3063 +
1.3064 +    have "span_induct_alt_help S x"
1.3065 +      proof(rule span_induct[where x=x and S=S])
1.3066 +	show "x \<in> span S" using x .
1.3067 +      next
1.3068 +	fix x assume xS : "x \<in> S"
1.3069 +	  from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
1.3070 +	  show "span_induct_alt_help S x" by simp
1.3071 +	next
1.3072 +	have "span_induct_alt_help S 0" by (rule span_induct_alt_help_0)
1.3073 +	moreover
1.3074 +	{fix x y assume h: "span_induct_alt_help S x" "span_induct_alt_help S y"
1.3075 +	  from h
1.3076 +	  have "span_induct_alt_help S (x + y)"
1.3077 +	    apply (induct rule: span_induct_alt_help.induct)
1.3078 +	    apply simp
1.3080 +	    apply (rule span_induct_alt_help_S)
1.3081 +	    apply assumption
1.3082 +	    apply simp
1.3083 +	    done}
1.3084 +	moreover
1.3085 +	{fix c x assume xt: "span_induct_alt_help S x"
1.3086 +	  then have "span_induct_alt_help S (c*s x)"
1.3087 +	    apply (induct rule: span_induct_alt_help.induct)
1.3088 +	    apply (simp add: span_induct_alt_help_0)
1.3090 +	    apply (rule span_induct_alt_help_S)
1.3091 +	    apply assumption
1.3092 +	    apply simp
1.3093 +	    done
1.3094 +	}
1.3095 +	ultimately show "subspace (span_induct_alt_help S)"
1.3096 +	  unfolding subspace_def mem_def Ball_def by blast
1.3097 +      qed}
1.3098 +  with th0 show ?thesis by blast
1.3099 +qed
1.3100 +
1.3101 +lemma span_induct_alt:
1.3102 +  assumes h0: "h (0::'a::semiring_1^'n)" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c*s x + y)" and x: "x \<in> span S"
1.3103 +  shows "h x"
1.3104 +using span_induct_alt'[of h S] h0 hS x by blast
1.3105 +
1.3106 +(* Individual closure properties. *)
1.3107 +
1.3108 +lemma span_superset: "x \<in> S ==> x \<in> span S" by (metis span_clauses)
1.3109 +
1.3110 +lemma span_0: "0 \<in> span S" by (metis subspace_span subspace_0)
1.3111 +
1.3112 +lemma span_add: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
1.3113 +  by (metis subspace_add subspace_span)
1.3114 +
1.3115 +lemma span_mul: "x \<in> span S ==> (c *s x) \<in> span S"
1.3116 +  by (metis subspace_span subspace_mul)
1.3117 +
1.3118 +lemma span_neg: "x \<in> span S ==> - (x::'a::ring_1^'n) \<in> span S"
1.3119 +  by (metis subspace_neg subspace_span)
1.3120 +
1.3121 +lemma span_sub: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> y \<in> span S ==> x - y \<in> span S"
1.3122 +  by (metis subspace_span subspace_sub)
1.3123 +
1.3124 +lemma span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S ==> setsum f A \<in> span S"
1.3125 +  apply (rule subspace_setsum)
1.3126 +  by (metis subspace_span subspace_setsum)+
1.3127 +
1.3128 +lemma span_add_eq: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
1.3129 +  apply (auto simp only: span_add span_sub)
1.3130 +  apply (subgoal_tac "(x + y) - x \<in> span S", simp)
1.3131 +  by (simp only: span_add span_sub)
1.3132 +
1.3133 +(* Mapping under linear image. *)
1.3134 +
1.3135 +lemma span_linear_image: assumes lf: "linear (f::'a::semiring_1 ^ 'n => _)"
1.3136 +  shows "span (f ` S) = f ` (span S)"
1.3137 +proof-
1.3138 +  {fix x
1.3139 +    assume x: "x \<in> span (f ` S)"
1.3140 +    have "x \<in> f ` span S"
1.3141 +      apply (rule span_induct[where x=x and S = "f ` S"])
1.3142 +      apply (clarsimp simp add: image_iff)
1.3143 +      apply (frule span_superset)
1.3144 +      apply blast
1.3145 +      apply (simp only: mem_def)
1.3146 +      apply (rule subspace_linear_image[OF lf])
1.3147 +      apply (rule subspace_span)
1.3148 +      apply (rule x)
1.3149 +      done}
1.3150 +  moreover
1.3151 +  {fix x assume x: "x \<in> span S"
1.3152 +    have th0:"(\<lambda>a. f a \<in> span (f ` S)) = {x. f x \<in> span (f ` S)}" apply (rule set_ext)
1.3153 +      unfolding mem_def Collect_def ..
1.3154 +    have "f x \<in> span (f ` S)"
1.3155 +      apply (rule span_induct[where S=S])
1.3156 +      apply (rule span_superset)
1.3157 +      apply simp
1.3158 +      apply (subst th0)
1.3159 +      apply (rule subspace_linear_preimage[OF lf subspace_span, of "f ` S"])
1.3160 +      apply (rule x)
1.3161 +      done}
1.3162 +  ultimately show ?thesis by blast
1.3163 +qed
1.3164 +
1.3165 +(* The key breakdown property. *)
1.3166 +
1.3167 +lemma span_breakdown:
1.3168 +  assumes bS: "(b::'a::ring_1 ^ 'n) \<in> S" and aS: "a \<in> span S"
1.3169 +  shows "\<exists>k. a - k*s b \<in> span (S - {b})" (is "?P a")
1.3170 +proof-
1.3171 +  {fix x assume xS: "x \<in> S"
1.3172 +    {assume ab: "x = b"
1.3173 +      then have "?P x"
1.3174 +	apply simp
1.3175 +	apply (rule exI[where x="1"], simp)
1.3176 +	by (rule span_0)}
1.3177 +    moreover
1.3178 +    {assume ab: "x \<noteq> b"
1.3179 +      then have "?P x"  using xS
1.3180 +	apply -
1.3181 +	apply (rule exI[where x=0])
1.3182 +	apply (rule span_superset)
1.3183 +	by simp}
1.3184 +    ultimately have "?P x" by blast}
1.3185 +  moreover have "subspace ?P"
1.3186 +    unfolding subspace_def
1.3187 +    apply auto
1.3188 +    apply (simp add: mem_def)
1.3189 +    apply (rule exI[where x=0])
1.3190 +    using span_0[of "S - {b}"]
1.3191 +    apply (simp add: mem_def)
1.3192 +    apply (clarsimp simp add: mem_def)
1.3193 +    apply (rule_tac x="k + ka" in exI)
1.3194 +    apply (subgoal_tac "x + y - (k + ka) *s b = (x - k*s b) + (y - ka *s b)")
1.3195 +    apply (simp only: )
1.3196 +    apply (rule span_add[unfolded mem_def])
1.3197 +    apply assumption+
1.3198 +    apply (vector ring_simps)
1.3199 +    apply (clarsimp simp add: mem_def)
1.3200 +    apply (rule_tac x= "c*k" in exI)
1.3201 +    apply (subgoal_tac "c *s x - (c * k) *s b = c*s (x - k*s b)")
1.3202 +    apply (simp only: )
1.3203 +    apply (rule span_mul[unfolded mem_def])
1.3204 +    apply assumption
1.3205 +    by (vector ring_simps)
1.3206 +  ultimately show "?P a" using aS span_induct[where S=S and P= "?P"] by metis
1.3207 +qed
1.3208 +
1.3209 +lemma span_breakdown_eq:
1.3210 +  "(x::'a::ring_1^'n) \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. (x - k *s a) \<in> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
1.3211 +proof-
1.3212 +  {assume x: "x \<in> span (insert a S)"
1.3213 +    from x span_breakdown[of "a" "insert a S" "x"]
1.3214 +    have ?rhs apply clarsimp
1.3215 +      apply (rule_tac x= "k" in exI)
1.3216 +      apply (rule set_rev_mp[of _ "span (S - {a})" _])
1.3217 +      apply assumption
1.3218 +      apply (rule span_mono)
1.3219 +      apply blast
1.3220 +      done}
1.3221 +  moreover
1.3222 +  { fix k assume k: "x - k *s a \<in> span S"
1.3223 +    have eq: "x = (x - k *s a) + k *s a" by vector
1.3224 +    have "(x - k *s a) + k *s a \<in> span (insert a S)"
1.3226 +      apply (rule set_rev_mp[of _ "span S" _])
1.3227 +      apply (rule k)
1.3228 +      apply (rule span_mono)
1.3229 +      apply blast
1.3230 +      apply (rule span_mul)
1.3231 +      apply (rule span_superset)
1.3232 +      apply blast
1.3233 +      done
1.3234 +    then have ?lhs using eq by metis}
1.3235 +  ultimately show ?thesis by blast
1.3236 +qed
1.3237 +
1.3238 +(* Hence some "reversal" results.*)
1.3239 +
1.3240 +lemma in_span_insert:
1.3241 +  assumes a: "(a::'a::field^'n) \<in> span (insert b S)" and na: "a \<notin> span S"
1.3242 +  shows "b \<in> span (insert a S)"
1.3243 +proof-
1.3244 +  from span_breakdown[of b "insert b S" a, OF insertI1 a]
1.3245 +  obtain k where k: "a - k*s b \<in> span (S - {b})" by auto
1.3246 +  {assume k0: "k = 0"
1.3247 +    with k have "a \<in> span S"
1.3248 +      apply (simp)
1.3249 +      apply (rule set_rev_mp)
1.3250 +      apply assumption
1.3251 +      apply (rule span_mono)
1.3252 +      apply blast
1.3253 +      done
1.3254 +    with na  have ?thesis by blast}
1.3255 +  moreover
1.3256 +  {assume k0: "k \<noteq> 0"
1.3257 +    have eq: "b = (1/k) *s a - ((1/k) *s a - b)" by vector
1.3258 +    from k0 have eq': "(1/k) *s (a - k*s b) = (1/k) *s a - b"
1.3259 +      by (vector field_simps)
1.3260 +    from k have "(1/k) *s (a - k*s b) \<in> span (S - {b})"
1.3261 +      by (rule span_mul)
1.3262 +    hence th: "(1/k) *s a - b \<in> span (S - {b})"
1.3263 +      unfolding eq' .
1.3264 +
1.3265 +    from k
1.3266 +    have ?thesis
1.3267 +      apply (subst eq)
1.3268 +      apply (rule span_sub)
1.3269 +      apply (rule span_mul)
1.3270 +      apply (rule span_superset)
1.3271 +      apply blast
1.3272 +      apply (rule set_rev_mp)
1.3273 +      apply (rule th)
1.3274 +      apply (rule span_mono)
1.3275 +      using na by blast}
1.3276 +  ultimately show ?thesis by blast
1.3277 +qed
1.3278 +
1.3279 +lemma in_span_delete:
1.3280 +  assumes a: "(a::'a::field^'n) \<in> span S"
1.3281 +  and na: "a \<notin> span (S-{b})"
1.3282 +  shows "b \<in> span (insert a (S - {b}))"
1.3283 +  apply (rule in_span_insert)
1.3284 +  apply (rule set_rev_mp)
1.3285 +  apply (rule a)
1.3286 +  apply (rule span_mono)
1.3287 +  apply blast
1.3288 +  apply (rule na)
1.3289 +  done
1.3290 +
1.3291 +(* Transitivity property. *)
1.3292 +
1.3293 +lemma span_trans:
1.3294 +  assumes x: "(x::'a::ring_1^'n) \<in> span S" and y: "y \<in> span (insert x S)"
1.3295 +  shows "y \<in> span S"
1.3296 +proof-
1.3297 +  from span_breakdown[of x "insert x S" y, OF insertI1 y]
1.3298 +  obtain k where k: "y -k*s x \<in> span (S - {x})" by auto
1.3299 +  have eq: "y = (y - k *s x) + k *s x" by vector
1.3300 +  show ?thesis
1.3301 +    apply (subst eq)
1.3303 +    apply (rule set_rev_mp)
1.3304 +    apply (rule k)
1.3305 +    apply (rule span_mono)
1.3306 +    apply blast
1.3307 +    apply (rule span_mul)
1.3308 +    by (rule x)
1.3309 +qed
1.3310 +
1.3311 +(* ------------------------------------------------------------------------- *)
1.3312 +(* An explicit expansion is sometimes needed.                                *)
1.3313 +(* ------------------------------------------------------------------------- *)
1.3314 +
1.3315 +lemma span_explicit:
1.3316 +  "span P = {y::'a::semiring_1^'n. \<exists>S u. finite S \<and> S \<subseteq> P \<and> setsum (\<lambda>v. u v *s v) S = y}"
1.3317 +  (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
1.3318 +proof-
1.3319 +  {fix x assume x: "x \<in> ?E"
1.3320 +    then obtain S u where fS: "finite S" and SP: "S\<subseteq>P" and u: "setsum (\<lambda>v. u v *s v) S = x"
1.3321 +      by blast
1.3322 +    have "x \<in> span P"
1.3323 +      unfolding u[symmetric]
1.3324 +      apply (rule span_setsum[OF fS])
1.3325 +      using span_mono[OF SP]
1.3326 +      by (auto intro: span_superset span_mul)}
1.3327 +  moreover
1.3328 +  have "\<forall>x \<in> span P. x \<in> ?E"
1.3329 +    unfolding mem_def Collect_def
1.3330 +  proof(rule span_induct_alt')
1.3331 +    show "?h 0"
1.3332 +      apply (rule exI[where x="{}"]) by simp
1.3333 +  next
1.3334 +    fix c x y
1.3335 +    assume x: "x \<in> P" and hy: "?h y"
1.3336 +    from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P"
1.3337 +      and u: "setsum (\<lambda>v. u v *s v) S = y" by blast
1.3338 +    let ?S = "insert x S"
1.3339 +    let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c)
1.3340 +                  else u y"
1.3341 +    from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P" by blast+
1.3342 +    {assume xS: "x \<in> S"
1.3343 +      have S1: "S = (S - {x}) \<union> {x}"
1.3344 +	and Sss:"finite (S - {x})" "finite {x}" "(S -{x}) \<inter> {x} = {}" using xS fS by auto
1.3345 +      have "setsum (\<lambda>v. ?u v *s v) ?S =(\<Sum>v\<in>S - {x}. u v *s v) + (u x + c) *s x"
1.3346 +	using xS
1.3347 +	by (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]]
1.3348 +	  setsum_clauses(2)[OF fS] cong del: if_weak_cong)
1.3349 +      also have "\<dots> = (\<Sum>v\<in>S. u v *s v) + c *s x"
1.3350 +	apply (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]])
1.3351 +	by (vector ring_simps)
1.3352 +      also have "\<dots> = c*s x + y"
1.3354 +      finally have "setsum (\<lambda>v. ?u v *s v) ?S = c*s x + y" .
1.3355 +    then have "?Q ?S ?u (c*s x + y)" using th0 by blast}
1.3356 +  moreover
1.3357 +  {assume xS: "x \<notin> S"
1.3358 +    have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *s v) = y"
1.3359 +      unfolding u[symmetric]
1.3360 +      apply (rule setsum_cong2)
1.3361 +      using xS by auto
1.3362 +    have "?Q ?S ?u (c*s x + y)" using fS xS th0
1.3364 +  ultimately have "?Q ?S ?u (c*s x + y)"
1.3365 +    by (cases "x \<in> S", simp, simp)
1.3366 +    then show "?h (c*s x + y)"
1.3367 +      apply -
1.3368 +      apply (rule exI[where x="?S"])
1.3369 +      apply (rule exI[where x="?u"]) by metis
1.3370 +  qed
1.3371 +  ultimately show ?thesis by blast
1.3372 +qed
1.3373 +
1.3374 +lemma dependent_explicit:
1.3375 +  "dependent P \<longleftrightarrow> (\<exists>S u. finite S \<and> S \<subseteq> P \<and> (\<exists>(v::'a::{idom,field}^'n) \<in>S. u v \<noteq> 0 \<and> setsum (\<lambda>v. u v *s v) S = 0))" (is "?lhs = ?rhs")
1.3376 +proof-
1.3377 +  {assume dP: "dependent P"
1.3378 +    then obtain a S u where aP: "a \<in> P" and fS: "finite S"
1.3379 +      and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *s v) S = a"
1.3380 +      unfolding dependent_def span_explicit by blast
1.3381 +    let ?S = "insert a S"
1.3382 +    let ?u = "\<lambda>y. if y = a then - 1 else u y"
1.3383 +    let ?v = a
1.3384 +    from aP SP have aS: "a \<notin> S" by blast
1.3385 +    from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0" by auto
1.3386 +    have s0: "setsum (\<lambda>v. ?u v *s v) ?S = 0"
1.3387 +      using fS aS
1.3388 +      apply (simp add: vector_smult_lneg vector_smult_lid setsum_clauses ring_simps )
1.3389 +      apply (subst (2) ua[symmetric])
1.3390 +      apply (rule setsum_cong2)
1.3391 +      by auto
1.3392 +    with th0 have ?rhs
1.3393 +      apply -
1.3394 +      apply (rule exI[where x= "?S"])
1.3395 +      apply (rule exI[where x= "?u"])
1.3396 +      by clarsimp}
1.3397 +  moreover
1.3398 +  {fix S u v assume fS: "finite S"
1.3399 +      and SP: "S \<subseteq> P" and vS: "v \<in> S" and uv: "u v \<noteq> 0"
1.3400 +    and u: "setsum (\<lambda>v. u v *s v) S = 0"
1.3401 +    let ?a = v
1.3402 +    let ?S = "S - {v}"
1.3403 +    let ?u = "\<lambda>i. (- u i) / u v"
1.3404 +    have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"       using fS SP vS by auto
1.3405 +    have "setsum (\<lambda>v. ?u v *s v) ?S = setsum (\<lambda>v. (- (inverse (u ?a))) *s (u v *s v)) S - ?u v *s v"
1.3406 +      using fS vS uv
1.3407 +      by (simp add: setsum_diff1 vector_smult_lneg divide_inverse
1.3408 +	vector_smult_assoc field_simps)
1.3409 +    also have "\<dots> = ?a"
1.3410 +      unfolding setsum_cmul u
1.3411 +      using uv by (simp add: vector_smult_lneg)
1.3412 +    finally  have "setsum (\<lambda>v. ?u v *s v) ?S = ?a" .
1.3413 +    with th0 have ?lhs
1.3414 +      unfolding dependent_def span_explicit
1.3415 +      apply -
1.3416 +      apply (rule bexI[where x= "?a"])
1.3417 +      apply simp_all
1.3418 +      apply (rule exI[where x= "?S"])
1.3419 +      by auto}
1.3420 +  ultimately show ?thesis by blast
1.3421 +qed
1.3422 +
1.3423 +
1.3424 +lemma span_finite:
1.3425 +  assumes fS: "finite S"
1.3426 +  shows "span S = {(y::'a::semiring_1^'n). \<exists>u. setsum (\<lambda>v. u v *s v) S = y}"
1.3427 +  (is "_ = ?rhs")
1.3428 +proof-
1.3429 +  {fix y assume y: "y \<in> span S"
1.3430 +    from y obtain S' u where fS': "finite S'" and SS': "S' \<subseteq> S" and
1.3431 +      u: "setsum (\<lambda>v. u v *s v) S' = y" unfolding span_explicit by blast
1.3432 +    let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
1.3433 +    from setsum_restrict_set[OF fS, of "\<lambda>v. u v *s v" S', symmetric] SS'
1.3434 +    have "setsum (\<lambda>v. ?u v *s v) S = setsum (\<lambda>v. u v *s v) S'"
1.3435 +      unfolding cond_value_iff cond_application_beta
1.3436 +      apply (simp add: cond_value_iff cong del: if_weak_cong)
1.3437 +      apply (rule setsum_cong)
1.3438 +      apply auto
1.3439 +      done
1.3440 +    hence "setsum (\<lambda>v. ?u v *s v) S = y" by (metis u)
1.3441 +    hence "y \<in> ?rhs" by auto}
1.3442 +  moreover
1.3443 +  {fix y u assume u: "setsum (\<lambda>v. u v *s v) S = y"
1.3444 +    then have "y \<in> span S" using fS unfolding span_explicit by auto}
1.3445 +  ultimately show ?thesis by blast
1.3446 +qed
1.3447 +
1.3448 +
1.3449 +(* Standard bases are a spanning set, and obviously finite.                  *)
1.3450 +
1.3451 +lemma span_stdbasis:"span {basis i :: 'a::ring_1^'n | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}} = UNIV"
1.3452 +apply (rule set_ext)
1.3453 +apply auto
1.3454 +apply (subst basis_expansion[symmetric])
1.3455 +apply (rule span_setsum)
1.3456 +apply simp
1.3457 +apply auto
1.3458 +apply (rule span_mul)
1.3459 +apply (rule span_superset)
1.3460 +apply (auto simp add: Collect_def mem_def)
1.3461 +done
1.3462 +
1.3463 +
1.3464 +lemma has_size_stdbasis: "{basis i ::real ^'n | i. i \<in> {1 .. dimindex (UNIV :: 'n set)}} hassize (dimindex(UNIV :: 'n set))" (is "?S hassize ?n")
1.3465 +proof-
1.3466 +  have eq: "?S = basis ` {1 .. ?n}" by blast
1.3467 +  show ?thesis unfolding eq
1.3468 +    apply (rule hassize_image_inj[OF basis_inj])
1.3469 +    by (simp add: hassize_def)
1.3470 +qed
1.3471 +
1.3472 +lemma finite_stdbasis: "finite {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV:: 'n set)}}"
1.3473 +  using has_size_stdbasis[unfolded hassize_def]
1.3474 +  ..
1.3475 +
1.3476 +lemma card_stdbasis: "card {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} = dimindex(UNIV :: 'n set)"
1.3477 +  using has_size_stdbasis[unfolded hassize_def]
1.3478 +  ..
1.3479 +
1.3480 +lemma independent_stdbasis_lemma:
1.3481 +  assumes x: "(x::'a::semiring_1 ^ 'n) \<in> span (basis ` S)"
1.3482 +  and i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.3483 +  and iS: "i \<notin> S"
1.3484 +  shows "(x\$i) = 0"
1.3485 +proof-
1.3486 +  let ?n = "dimindex (UNIV :: 'n set)"
1.3487 +  let ?U = "{1 .. ?n}"
1.3488 +  let ?B = "basis ` S"
1.3489 +  let ?P = "\<lambda>(x::'a^'n). \<forall>i\<in> ?U. i \<notin> S \<longrightarrow> x\$i =0"
1.3490 + {fix x::"'a^'n" assume xS: "x\<in> ?B"
1.3491 +   from xS have "?P x" by (auto simp add: basis_component)}
1.3492 + moreover
1.3493 + have "subspace ?P"
1.3494 +   by (auto simp add: subspace_def Collect_def mem_def zero_index vector_component)
1.3495 + ultimately show ?thesis
1.3496 +   using x span_induct[of ?B ?P x] i iS by blast
1.3497 +qed
1.3498 +
1.3499 +lemma independent_stdbasis: "independent {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
1.3500 +proof-
1.3501 +  let ?n = "dimindex (UNIV :: 'n set)"
1.3502 +  let ?I = "{1 .. ?n}"
1.3503 +  let ?b = "basis :: nat \<Rightarrow> real ^'n"
1.3504 +  let ?B = "?b ` ?I"
1.3505 +  have eq: "{?b i|i. i \<in> ?I} = ?B"
1.3506 +    by auto
1.3507 +  {assume d: "dependent ?B"
1.3508 +    then obtain k where k: "k \<in> ?I" "?b k \<in> span (?B - {?b k})"
1.3509 +      unfolding dependent_def by auto
1.3510 +    have eq1: "?B - {?b k} = ?B - ?b ` {k}"  by simp
1.3511 +    have eq2: "?B - {?b k} = ?b ` (?I - {k})"
1.3512 +      unfolding eq1
1.3513 +      apply (rule inj_on_image_set_diff[symmetric])
1.3514 +      apply (rule basis_inj) using k(1) by auto
1.3515 +    from k(2) have th0: "?b k \<in> span (?b ` (?I - {k}))" unfolding eq2 .
1.3516 +    from independent_stdbasis_lemma[OF th0 k(1), simplified]
1.3517 +    have False by (simp add: basis_component[OF k(1), of k])}
1.3518 +  then show ?thesis unfolding eq dependent_def ..
1.3519 +qed
1.3520 +
1.3521 +(* This is useful for building a basis step-by-step.                         *)
1.3522 +
1.3523 +lemma independent_insert:
1.3524 +  "independent(insert (a::'a::field ^'n) S) \<longleftrightarrow>
1.3525 +      (if a \<in> S then independent S
1.3526 +                else independent S \<and> a \<notin> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
1.3527 +proof-
1.3528 +  {assume aS: "a \<in> S"
1.3529 +    hence ?thesis using insert_absorb[OF aS] by simp}
1.3530 +  moreover
1.3531 +  {assume aS: "a \<notin> S"
1.3532 +    {assume i: ?lhs
1.3533 +      then have ?rhs using aS
1.3534 +	apply simp
1.3535 +	apply (rule conjI)
1.3536 +	apply (rule independent_mono)
1.3537 +	apply assumption
1.3538 +	apply blast
1.3539 +	by (simp add: dependent_def)}
1.3540 +    moreover
1.3541 +    {assume i: ?rhs
1.3542 +      have ?lhs using i aS
1.3543 +	apply simp
1.3544 +	apply (auto simp add: dependent_def)
1.3545 +	apply (case_tac "aa = a", auto)
1.3546 +	apply (subgoal_tac "insert a S - {aa} = insert a (S - {aa})")
1.3547 +	apply simp
1.3548 +	apply (subgoal_tac "a \<in> span (insert aa (S - {aa}))")
1.3549 +	apply (subgoal_tac "insert aa (S - {aa}) = S")
1.3550 +	apply simp
1.3551 +	apply blast
1.3552 +	apply (rule in_span_insert)
1.3553 +	apply assumption
1.3554 +	apply blast
1.3555 +	apply blast
1.3556 +	done}
1.3557 +    ultimately have ?thesis by blast}
1.3558 +  ultimately show ?thesis by blast
1.3559 +qed
1.3560 +
1.3561 +(* The degenerate case of the Exchange Lemma.  *)
1.3562 +
1.3563 +lemma mem_delete: "x \<in> (A - {a}) \<longleftrightarrow> x \<noteq> a \<and> x \<in> A"
1.3564 +  by blast
1.3565 +
1.3566 +lemma span_span: "span (span A) = span A"
1.3567 +  unfolding span_def hull_hull ..
1.3568 +
1.3569 +lemma span_inc: "S \<subseteq> span S"
1.3570 +  by (metis subset_eq span_superset)
1.3571 +
1.3572 +lemma spanning_subset_independent:
1.3573 +  assumes BA: "B \<subseteq> A" and iA: "independent (A::('a::field ^'n) set)"
1.3574 +  and AsB: "A \<subseteq> span B"
1.3575 +  shows "A = B"
1.3576 +proof
1.3577 +  from BA show "B \<subseteq> A" .
1.3578 +next
1.3579 +  from span_mono[OF BA] span_mono[OF AsB]
1.3580 +  have sAB: "span A = span B" unfolding span_span by blast
1.3581 +
1.3582 +  {fix x assume x: "x \<in> A"
1.3583 +    from iA have th0: "x \<notin> span (A - {x})"
1.3584 +      unfolding dependent_def using x by blast
1.3585 +    from x have xsA: "x \<in> span A" by (blast intro: span_superset)
1.3586 +    have "A - {x} \<subseteq> A" by blast
1.3587 +    hence th1:"span (A - {x}) \<subseteq> span A" by (metis span_mono)
1.3588 +    {assume xB: "x \<notin> B"
1.3589 +      from xB BA have "B \<subseteq> A -{x}" by blast
1.3590 +      hence "span B \<subseteq> span (A - {x})" by (metis span_mono)
1.3591 +      with th1 th0 sAB have "x \<notin> span A" by blast
1.3592 +      with x have False by (metis span_superset)}
1.3593 +    then have "x \<in> B" by blast}
1.3594 +  then show "A \<subseteq> B" by blast
1.3595 +qed
1.3596 +
1.3597 +(* The general case of the Exchange Lemma, the key to what follows.  *)
1.3598 +
1.3599 +lemma exchange_lemma:
1.3600 +  assumes f:"finite (t:: ('a::field^'n) set)" and i: "independent s"
1.3601 +  and sp:"s \<subseteq> span t"
1.3602 +  shows "\<exists>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
1.3603 +using f i sp
1.3604 +proof(induct c\<equiv>"card(t - s)" arbitrary: s t rule: nat_less_induct)
1.3605 +  fix n:: nat and s t :: "('a ^'n) set"
1.3606 +  assume H: " \<forall>m<n. \<forall>(x:: ('a ^'n) set) xa.
1.3607 +                finite xa \<longrightarrow>
1.3608 +                independent x \<longrightarrow>
1.3609 +                x \<subseteq> span xa \<longrightarrow>
1.3610 +                m = card (xa - x) \<longrightarrow>
1.3611 +                (\<exists>t'. (t' hassize card xa) \<and>
1.3612 +                      x \<subseteq> t' \<and> t' \<subseteq> x \<union> xa \<and> x \<subseteq> span t')"
1.3613 +    and ft: "finite t" and s: "independent s" and sp: "s \<subseteq> span t"
1.3614 +    and n: "n = card (t - s)"
1.3615 +  let ?P = "\<lambda>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
1.3616 +  let ?ths = "\<exists>t'. ?P t'"
1.3617 +  {assume st: "s \<subseteq> t"
1.3618 +    from st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
1.3619 +      by (auto simp add: hassize_def intro: span_superset)}
1.3620 +  moreover
1.3621 +  {assume st: "t \<subseteq> s"
1.3622 +
1.3623 +    from spanning_subset_independent[OF st s sp]
1.3624 +      st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
1.3625 +      by (auto simp add: hassize_def intro: span_superset)}
1.3626 +  moreover
1.3627 +  {assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
1.3628 +    from st(2) obtain b where b: "b \<in> t" "b \<notin> s" by blast
1.3629 +      from b have "t - {b} - s \<subset> t - s" by blast
1.3630 +      then have cardlt: "card (t - {b} - s) < n" using n ft
1.3631 + 	by (auto intro: psubset_card_mono)
1.3632 +      from b ft have ct0: "card t \<noteq> 0" by auto
1.3633 +    {assume stb: "s \<subseteq> span(t -{b})"
1.3634 +      from ft have ftb: "finite (t -{b})" by auto
1.3635 +      from H[rule_format, OF cardlt ftb s stb]
1.3636 +      obtain u where u: "u hassize card (t-{b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u" by blast
1.3637 +      let ?w = "insert b u"
1.3638 +      have th0: "s \<subseteq> insert b u" using u by blast
1.3639 +      from u(3) b have "u \<subseteq> s \<union> t" by blast
1.3640 +      then have th1: "insert b u \<subseteq> s \<union> t" using u b by blast
1.3641 +      have bu: "b \<notin> u" using b u by blast
1.3642 +      from u(1) have fu: "finite u" by (simp add: hassize_def)
1.3643 +      from u(1) ft b have "u hassize (card t - 1)" by auto
1.3644 +      then
1.3645 +      have th2: "insert b u hassize card t"
1.3646 +	using  card_insert_disjoint[OF fu bu] ct0 by (auto simp add: hassize_def)
1.3647 +      from u(4) have "s \<subseteq> span u" .
1.3648 +      also have "\<dots> \<subseteq> span (insert b u)" apply (rule span_mono) by blast
1.3649 +      finally have th3: "s \<subseteq> span (insert b u)" .      from th0 th1 th2 th3 have th: "?P ?w"  by blast
1.3650 +      from th have ?ths by blast}
1.3651 +    moreover
1.3652 +    {assume stb: "\<not> s \<subseteq> span(t -{b})"
1.3653 +      from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})" by blast
1.3654 +      have ab: "a \<noteq> b" using a b by blast
1.3655 +      have at: "a \<notin> t" using a ab span_superset[of a "t- {b}"] by auto
1.3656 +      have mlt: "card ((insert a (t - {b})) - s) < n"
1.3657 +	using cardlt ft n  a b by auto
1.3658 +      have ft': "finite (insert a (t - {b}))" using ft by auto
1.3659 +      {fix x assume xs: "x \<in> s"
1.3660 +	have t: "t \<subseteq> (insert b (insert a (t -{b})))" using b by auto
1.3661 +	from b(1) have "b \<in> span t" by (simp add: span_superset)
1.3662 +	have bs: "b \<in> span (insert a (t - {b}))"
1.3663 +	  by (metis in_span_delete a sp mem_def subset_eq)
1.3664 +	from xs sp have "x \<in> span t" by blast
1.3665 +	with span_mono[OF t]
1.3666 +	have x: "x \<in> span (insert b (insert a (t - {b})))" ..
1.3667 +	from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))"  .}
1.3668 +      then have sp': "s \<subseteq> span (insert a (t - {b}))" by blast
1.3669 +
1.3670 +      from H[rule_format, OF mlt ft' s sp' refl] obtain u where
1.3671 +	u: "u hassize card (insert a (t -{b}))" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t -{b})"
1.3672 +	"s \<subseteq> span u" by blast
1.3673 +      from u a b ft at ct0 have "?P u" by (auto simp add: hassize_def)
1.3674 +      then have ?ths by blast }
1.3675 +    ultimately have ?ths by blast
1.3676 +  }
1.3677 +  ultimately
1.3678 +  show ?ths  by blast
1.3679 +qed
1.3680 +
1.3681 +(* This implies corresponding size bounds.                                   *)
1.3682 +
1.3683 +lemma independent_span_bound:
1.3684 +  assumes f: "finite t" and i: "independent (s::('a::field^'n) set)" and sp:"s \<subseteq> span t"
1.3685 +  shows "finite s \<and> card s \<le> card t"
1.3686 +  by (metis exchange_lemma[OF f i sp] hassize_def finite_subset card_mono)
1.3687 +
1.3688 +lemma finite_Atleast_Atmost[simp]: "finite {f x |x. x\<in> {(i::'a::finite_intvl_succ) .. j}}"
1.3689 +proof-
1.3690 +  have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
1.3691 +  show ?thesis unfolding eq
1.3692 +    apply (rule finite_imageI)
1.3693 +    apply (rule finite_intvl)
1.3694 +    done
1.3695 +qed
1.3696 +
1.3697 +lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> {(i::nat) .. j}}"
1.3698 +proof-
1.3699 +  have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
1.3700 +  show ?thesis unfolding eq
1.3701 +    apply (rule finite_imageI)
1.3702 +    apply (rule finite_atLeastAtMost)
1.3703 +    done
1.3704 +qed
1.3705 +
1.3706 +
1.3707 +lemma independent_bound:
1.3708 +  fixes S:: "(real^'n) set"
1.3709 +  shows "independent S \<Longrightarrow> finite S \<and> card S <= dimindex(UNIV :: 'n set)"
1.3710 +  apply (subst card_stdbasis[symmetric])
1.3711 +  apply (rule independent_span_bound)
1.3712 +  apply (rule finite_Atleast_Atmost_nat)
1.3713 +  apply assumption
1.3714 +  unfolding span_stdbasis
1.3715 +  apply (rule subset_UNIV)
1.3716 +  done
1.3717 +
1.3718 +lemma dependent_biggerset: "(finite (S::(real ^'n) set) ==> card S > dimindex(UNIV:: 'n set)) ==> dependent S"
1.3719 +  by (metis independent_bound not_less)
1.3720 +
1.3721 +(* Hence we can create a maximal independent subset.                         *)
1.3722 +
1.3723 +lemma maximal_independent_subset_extend:
1.3724 +  assumes sv: "(S::(real^'n) set) \<subseteq> V" and iS: "independent S"
1.3725 +  shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
1.3726 +  using sv iS
1.3727 +proof(induct d\<equiv> "dimindex (UNIV :: 'n set) - card S" arbitrary: S rule: nat_less_induct)
1.3728 +  fix n and S:: "(real^'n) set"
1.3729 +  assume H: "\<forall>m<n. \<forall>S \<subseteq> V. independent S \<longrightarrow> m = dimindex (UNIV::'n set) - card S \<longrightarrow>
1.3730 +              (\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B)"
1.3731 +    and sv: "S \<subseteq> V" and i: "independent S" and n: "n = dimindex (UNIV :: 'n set) - card S"
1.3732 +  let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
1.3733 +  let ?ths = "\<exists>x. ?P x"
1.3734 +  let ?d = "dimindex (UNIV :: 'n set)"
1.3735 +  {assume "V \<subseteq> span S"
1.3736 +    then have ?ths  using sv i by blast }
1.3737 +  moreover
1.3738 +  {assume VS: "\<not> V \<subseteq> span S"
1.3739 +    from VS obtain a where a: "a \<in> V" "a \<notin> span S" by blast
1.3740 +    from a have aS: "a \<notin> S" by (auto simp add: span_superset)
1.3741 +    have th0: "insert a S \<subseteq> V" using a sv by blast
1.3742 +    from independent_insert[of a S]  i a
1.3743 +    have th1: "independent (insert a S)" by auto
1.3744 +    have mlt: "?d - card (insert a S) < n"
1.3745 +      using aS a n independent_bound[OF th1] dimindex_ge_1[of "UNIV :: 'n set"]
1.3746 +      by auto
1.3747 +
1.3748 +    from H[rule_format, OF mlt th0 th1 refl]
1.3749 +    obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B"
1.3750 +      by blast
1.3751 +    from B have "?P B" by auto
1.3752 +    then have ?ths by blast}
1.3753 +  ultimately show ?ths by blast
1.3754 +qed
1.3755 +
1.3756 +lemma maximal_independent_subset:
1.3757 +  "\<exists>(B:: (real ^'n) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
1.3758 +  by (metis maximal_independent_subset_extend[of "{}:: (real ^'n) set"] empty_subsetI independent_empty)
1.3759 +
1.3760 +(* Notion of dimension.                                                      *)
1.3761 +
1.3762 +definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n))"
1.3763 +
1.3764 +lemma basis_exists:  "\<exists>B. (B :: (real ^'n) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize dim V)"
1.3765 +unfolding dim_def some_eq_ex[of "\<lambda>n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n)"]
1.3766 +unfolding hassize_def
1.3767 +using maximal_independent_subset[of V] independent_bound
1.3768 +by auto
1.3769 +
1.3770 +(* Consequences of independence or spanning for cardinality.                 *)
1.3771 +
1.3772 +lemma independent_card_le_dim: "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B \<le> dim V"
1.3773 +by (metis basis_exists[of V] independent_span_bound[where ?'a=real] hassize_def subset_trans)
1.3774 +
1.3775 +lemma span_card_ge_dim:  "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
1.3776 +  by (metis basis_exists[of V] independent_span_bound hassize_def subset_trans)
1.3777 +
1.3778 +lemma basis_card_eq_dim:
1.3779 +  "B \<subseteq> (V:: (real ^'n) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
1.3780 +  by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_mono)
1.3781 +
1.3782 +lemma dim_unique: "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> B hassize n \<Longrightarrow> dim V = n"
1.3783 +  by (metis basis_card_eq_dim hassize_def)
1.3784 +
1.3785 +(* More lemmas about dimension.                                              *)
1.3786 +
1.3787 +lemma dim_univ: "dim (UNIV :: (real^'n) set) = dimindex (UNIV :: 'n set)"
1.3788 +  apply (rule dim_unique[of "{basis i |i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}"])
1.3789 +  by (auto simp only: span_stdbasis has_size_stdbasis independent_stdbasis)
1.3790 +
1.3791 +lemma dim_subset:
1.3792 +  "(S:: (real ^'n) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
1.3793 +  using basis_exists[of T] basis_exists[of S]
1.3794 +  by (metis independent_span_bound[where ?'a = real and ?'n = 'n] subset_eq hassize_def)
1.3795 +
1.3796 +lemma dim_subset_univ: "dim (S:: (real^'n) set) \<le> dimindex (UNIV :: 'n set)"
1.3797 +  by (metis dim_subset subset_UNIV dim_univ)
1.3798 +
1.3799 +(* Converses to those.                                                       *)
1.3800 +
1.3801 +lemma card_ge_dim_independent:
1.3802 +  assumes BV:"(B::(real ^'n) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
1.3803 +  shows "V \<subseteq> span B"
1.3804 +proof-
1.3805 +  {fix a assume aV: "a \<in> V"
1.3806 +    {assume aB: "a \<notin> span B"
1.3807 +      then have iaB: "independent (insert a B)" using iB aV  BV by (simp add: independent_insert)
1.3808 +      from aV BV have th0: "insert a B \<subseteq> V" by blast
1.3809 +      from aB have "a \<notin>B" by (auto simp add: span_superset)
1.3810 +      with independent_card_le_dim[OF th0 iaB] dVB  have False by auto}
1.3811 +    then have "a \<in> span B"  by blast}
1.3812 +  then show ?thesis by blast
1.3813 +qed
1.3814 +
1.3815 +lemma card_le_dim_spanning:
1.3816 +  assumes BV: "(B:: (real ^'n) set) \<subseteq> V" and VB: "V \<subseteq> span B"
1.3817 +  and fB: "finite B" and dVB: "dim V \<ge> card B"
1.3818 +  shows "independent B"
1.3819 +proof-
1.3820 +  {fix a assume a: "a \<in> B" "a \<in> span (B -{a})"
1.3821 +    from a fB have c0: "card B \<noteq> 0" by auto
1.3822 +    from a fB have cb: "card (B -{a}) = card B - 1" by auto
1.3823 +    from BV a have th0: "B -{a} \<subseteq> V" by blast
1.3824 +    {fix x assume x: "x \<in> V"
1.3825 +      from a have eq: "insert a (B -{a}) = B" by blast
1.3826 +      from x VB have x': "x \<in> span B" by blast
1.3827 +      from span_trans[OF a(2), unfolded eq, OF x']
1.3828 +      have "x \<in> span (B -{a})" . }
1.3829 +    then have th1: "V \<subseteq> span (B -{a})" by blast
1.3830 +    have th2: "finite (B -{a})" using fB by auto
1.3831 +    from span_card_ge_dim[OF th0 th1 th2]
1.3832 +    have c: "dim V \<le> card (B -{a})" .
1.3833 +    from c c0 dVB cb have False by simp}
1.3834 +  then show ?thesis unfolding dependent_def by blast
1.3835 +qed
1.3836 +
1.3837 +lemma card_eq_dim: "(B:: (real ^'n) set) \<subseteq> V \<Longrightarrow> B hassize dim V \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
1.3838 +  by (metis hassize_def order_eq_iff card_le_dim_spanning
1.3839 +    card_ge_dim_independent)
1.3840 +
1.3841 +(* ------------------------------------------------------------------------- *)
1.3842 +(* More general size bound lemmas.                                           *)
1.3843 +(* ------------------------------------------------------------------------- *)
1.3844 +
1.3845 +lemma independent_bound_general:
1.3846 +  "independent (S:: (real^'n) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
1.3847 +  by (metis independent_card_le_dim independent_bound subset_refl)
1.3848 +
1.3849 +lemma dependent_biggerset_general: "(finite (S:: (real^'n) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
1.3850 +  using independent_bound_general[of S] by (metis linorder_not_le)
1.3851 +
1.3852 +lemma dim_span: "dim (span (S:: (real ^'n) set)) = dim S"
1.3853 +proof-
1.3854 +  have th0: "dim S \<le> dim (span S)"
1.3855 +    by (auto simp add: subset_eq intro: dim_subset span_superset)
1.3856 +  from basis_exists[of S]
1.3857 +  obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
1.3858 +  from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
1.3859 +  have bSS: "B \<subseteq> span S" using B(1) by (metis subset_eq span_inc)
1.3860 +  have sssB: "span S \<subseteq> span B" using span_mono[OF B(3)] by (simp add: span_span)
1.3861 +  from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
1.3862 +    using fB(2)  by arith
1.3863 +qed
1.3864 +
1.3865 +lemma subset_le_dim: "(S:: (real ^'n) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
1.3866 +  by (metis dim_span dim_subset)
1.3867 +
1.3868 +lemma span_eq_dim: "span (S:: (real ^'n) set) = span T ==> dim S = dim T"
1.3869 +  by (metis dim_span)
1.3870 +
1.3871 +lemma spans_image:
1.3872 +  assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and VB: "V \<subseteq> span B"
1.3873 +  shows "f ` V \<subseteq> span (f ` B)"
1.3874 +  unfolding span_linear_image[OF lf]
1.3875 +  by (metis VB image_mono)
1.3876 +
1.3877 +lemma dim_image_le: assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S:: (real ^'n) set)"
1.3878 +proof-
1.3879 +  from basis_exists[of S] obtain B where
1.3880 +    B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
1.3881 +  from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
1.3882 +  have "dim (f ` S) \<le> card (f ` B)"
1.3883 +    apply (rule span_card_ge_dim)
1.3884 +    using lf B fB by (auto simp add: span_linear_image spans_image subset_image_iff)
1.3885 +  also have "\<dots> \<le> dim S" using card_image_le[OF fB(1)] fB by simp
1.3886 +  finally show ?thesis .
1.3887 +qed
1.3888 +
1.3889 +(* Relation between bases and injectivity/surjectivity of map.               *)
1.3890 +
1.3891 +lemma spanning_surjective_image:
1.3892 +  assumes us: "UNIV \<subseteq> span (S:: ('a::semiring_1 ^'n) set)"
1.3893 +  and lf: "linear f" and sf: "surj f"
1.3894 +  shows "UNIV \<subseteq> span (f ` S)"
1.3895 +proof-
1.3896 +  have "UNIV \<subseteq> f ` UNIV" using sf by (auto simp add: surj_def)
1.3897 +  also have " \<dots> \<subseteq> span (f ` S)" using spans_image[OF lf us] .
1.3898 +finally show ?thesis .
1.3899 +qed
1.3900 +
1.3901 +lemma independent_injective_image:
1.3902 +  assumes iS: "independent (S::('a::semiring_1^'n) set)" and lf: "linear f" and fi: "inj f"
1.3903 +  shows "independent (f ` S)"
1.3904 +proof-
1.3905 +  {fix a assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
1.3906 +    have eq: "f ` S - {f a} = f ` (S - {a})" using fi
1.3907 +      by (auto simp add: inj_on_def)
1.3908 +    from a have "f a \<in> f ` span (S -{a})"
1.3909 +      unfolding eq span_linear_image[OF lf, of "S - {a}"]  by blast
1.3910 +    hence "a \<in> span (S -{a})" using fi by (auto simp add: inj_on_def)
1.3911 +    with a(1) iS  have False by (simp add: dependent_def) }
1.3912 +  then show ?thesis unfolding dependent_def by blast
1.3913 +qed
1.3914 +
1.3915 +(* ------------------------------------------------------------------------- *)
1.3916 +(* Picking an orthogonal replacement for a spanning set.                     *)
1.3917 +(* ------------------------------------------------------------------------- *)
1.3918 +    (* FIXME : Move to some general theory ?*)
1.3919 +definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
1.3920 +
1.3921 +lemma vector_sub_project_orthogonal: "(b::'a::ordered_field^'n) \<bullet> (x - ((b \<bullet> x) / (b\<bullet>b)) *s b) = 0"
1.3922 +  apply (cases "b = 0", simp)
1.3923 +  apply (simp add: dot_rsub dot_rmult)
1.3924 +  unfolding times_divide_eq_right[symmetric]
1.3925 +  by (simp add: field_simps dot_eq_0)
1.3926 +
1.3927 +lemma basis_orthogonal:
1.3928 +  fixes B :: "(real ^'n) set"
1.3929 +  assumes fB: "finite B"
1.3930 +  shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
1.3931 +  (is " \<exists>C. ?P B C")
1.3932 +proof(induct rule: finite_induct[OF fB])
1.3933 +  case 1 thus ?case apply (rule exI[where x="{}"]) by (auto simp add: pairwise_def)
1.3934 +next
1.3935 +  case (2 a B)
1.3936 +  note fB = `finite B` and aB = `a \<notin> B`
1.3937 +  from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C`
1.3938 +  obtain C where C: "finite C" "card C \<le> card B"
1.3939 +    "span C = span B" "pairwise orthogonal C" by blast
1.3940 +  let ?a = "a - setsum (\<lambda>x. (x\<bullet>a / (x\<bullet>x)) *s x) C"
1.3941 +  let ?C = "insert ?a C"
1.3942 +  from C(1) have fC: "finite ?C" by simp
1.3943 +  from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)" by (simp add: card_insert_if)
1.3944 +  {fix x k
1.3945 +    have th0: "\<And>(a::'b::comm_ring) b c. a - (b - c) = c + (a - b)" by (simp add: ring_simps)
1.3946 +    have "x - k *s (a - (\<Sum>x\<in>C. (x \<bullet> a / (x \<bullet> x)) *s x)) \<in> span C \<longleftrightarrow> x - k *s a \<in> span C"
1.3947 +      apply (simp only: vector_ssub_ldistrib th0)
1.3949 +      apply (rule span_mul)
1.3950 +      apply (rule span_setsum[OF C(1)])
1.3951 +      apply clarify
1.3952 +      apply (rule span_mul)
1.3953 +      by (rule span_superset)}
1.3954 +  then have SC: "span ?C = span (insert a B)"
1.3955 +    unfolding expand_set_eq span_breakdown_eq C(3)[symmetric] by auto
1.3956 +  thm pairwise_def
1.3957 +  {fix x y assume xC: "x \<in> ?C" and yC: "y \<in> ?C" and xy: "x \<noteq> y"
1.3958 +    {assume xa: "x = ?a" and ya: "y = ?a"
1.3959 +      have "orthogonal x y" using xa ya xy by blast}
1.3960 +    moreover
1.3961 +    {assume xa: "x = ?a" and ya: "y \<noteq> ?a" "y \<in> C"
1.3962 +      from ya have Cy: "C = insert y (C - {y})" by blast
1.3963 +      have fth: "finite (C - {y})" using C by simp
1.3964 +      have "orthogonal x y"
1.3965 +	using xa ya
1.3966 +	unfolding orthogonal_def xa dot_lsub dot_rsub diff_eq_0_iff_eq
1.3967 +	apply simp
1.3968 +	apply (subst Cy)
1.3969 +	using C(1) fth
1.3970 +	apply (simp only: setsum_clauses)
1.3971 +	apply (auto simp add: dot_ladd dot_lmult dot_eq_0 dot_sym[of y a] dot_lsum[OF fth])
1.3972 +	apply (rule setsum_0')
1.3973 +	apply clarsimp
1.3974 +	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
1.3975 +	by auto}
1.3976 +    moreover
1.3977 +    {assume xa: "x \<noteq> ?a" "x \<in> C" and ya: "y = ?a"
1.3978 +      from xa have Cx: "C = insert x (C - {x})" by blast
1.3979 +      have fth: "finite (C - {x})" using C by simp
1.3980 +      have "orthogonal x y"
1.3981 +	using xa ya
1.3982 +	unfolding orthogonal_def ya dot_rsub dot_lsub diff_eq_0_iff_eq
1.3983 +	apply simp
1.3984 +	apply (subst Cx)
1.3985 +	using C(1) fth
1.3986 +	apply (simp only: setsum_clauses)
1.3987 +	apply (subst dot_sym[of x])
1.3988 +	apply (auto simp add: dot_radd dot_rmult dot_eq_0 dot_sym[of x a] dot_rsum[OF fth])
1.3989 +	apply (rule setsum_0')
1.3990 +	apply clarsimp
1.3991 +	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
1.3992 +	by auto
1.3993 +      have "orthogonal x y" using xa ya sorry}
1.3994 +    moreover
1.3995 +    {assume xa: "x \<in> C" and ya: "y \<in> C"
1.3996 +      have "orthogonal x y" using xa ya xy C(4) unfolding pairwise_def by blast}
1.3997 +    ultimately have "orthogonal x y" using xC yC by blast}
1.3998 +  then have CPO: "pairwise orthogonal ?C" unfolding pairwise_def by blast
1.3999 +  from fC cC SC CPO have "?P (insert a B) ?C" by blast
1.4000 +  then show ?case by blast
1.4001 +qed
1.4002 +
1.4003 +lemma orthogonal_basis_exists:
1.4004 +  fixes V :: "(real ^'n) set"
1.4005 +  shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (B hassize dim V) \<and> pairwise orthogonal B"
1.4006 +proof-
1.4007 +  from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "B hassize dim V" by blast
1.4008 +  from B have fB: "finite B" "card B = dim V" by (simp_all add: hassize_def)
1.4009 +  from basis_orthogonal[OF fB(1)] obtain C where
1.4010 +    C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C" by blast
1.4011 +  from C B
1.4012 +  have CSV: "C \<subseteq> span V" by (metis span_inc span_mono subset_trans)
1.4013 +  from span_mono[OF B(3)]  C have SVC: "span V \<subseteq> span C" by (simp add: span_span)
1.4014 +  from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
1.4015 +  have iC: "independent C" by (simp add: dim_span)
1.4016 +  from C fB have "card C \<le> dim V" by simp
1.4017 +  moreover have "dim V \<le> card C" using span_card_ge_dim[OF CSV SVC C(1)]
1.4018 +    by (simp add: dim_span)
1.4019 +  ultimately have CdV: "C hassize dim V" unfolding hassize_def using C(1) by simp
1.4020 +  from C B CSV CdV iC show ?thesis by auto
1.4021 +qed
1.4022 +
1.4023 +lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
1.4024 +  by (metis set_eq_subset span_mono span_span span_inc)
1.4025 +
1.4026 +(* ------------------------------------------------------------------------- *)
1.4027 +(* Low-dimensional subset is in a hyperplane (weak orthogonal complement).   *)
1.4028 +(* ------------------------------------------------------------------------- *)
1.4029 +
1.4030 +lemma span_not_univ_orthogonal:
1.4031 +  assumes sU: "span S \<noteq> UNIV"
1.4032 +  shows "\<exists>(a:: real ^'n). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
1.4033 +proof-
1.4034 +  from sU obtain a where a: "a \<notin> span S" by blast
1.4035 +  from orthogonal_basis_exists obtain B where
1.4036 +    B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "B hassize dim S" "pairwise orthogonal B"
1.4037 +    by blast
1.4038 +  from B have fB: "finite B" "card B = dim S" by (simp_all add: hassize_def)
1.4039 +  from span_mono[OF B(2)] span_mono[OF B(3)]
1.4040 +  have sSB: "span S = span B" by (simp add: span_span)
1.4041 +  let ?a = "a - setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B"
1.4042 +  have "setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B \<in> span S"
1.4043 +    unfolding sSB
1.4044 +    apply (rule span_setsum[OF fB(1)])
1.4045 +    apply clarsimp
1.4046 +    apply (rule span_mul)
1.4047 +    by (rule span_superset)
1.4048 +  with a have a0:"?a  \<noteq> 0" by auto
1.4049 +  have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
1.4050 +  proof(rule span_induct')
1.4051 +    show "subspace (\<lambda>x. ?a \<bullet> x = 0)"
1.4053 +  next
1.4054 +    {fix x assume x: "x \<in> B"
1.4055 +      from x have B': "B = insert x (B - {x})" by blast
1.4056 +      have fth: "finite (B - {x})" using fB by simp
1.4057 +      have "?a \<bullet> x = 0"
1.4058 +	apply (subst B') using fB fth
1.4059 +	unfolding setsum_clauses(2)[OF fth]
1.4060 +	apply simp
1.4062 +	apply (rule setsum_0', rule ballI)
1.4063 +	unfolding dot_sym
1.4064 +	by (auto simp add: x field_simps dot_eq_0 intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])}
1.4065 +    then show "\<forall>x \<in> B. ?a \<bullet> x = 0" by blast
1.4066 +  qed
1.4067 +  with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"])
1.4068 +qed
1.4069 +
1.4070 +lemma span_not_univ_subset_hyperplane:
1.4071 +  assumes SU: "span S \<noteq> (UNIV ::(real^'n) set)"
1.4072 +  shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
1.4073 +  using span_not_univ_orthogonal[OF SU] by auto
1.4074 +
1.4075 +lemma lowdim_subset_hyperplane:
1.4076 +  assumes d: "dim S < dimindex (UNIV :: 'n set)"
1.4077 +  shows "\<exists>(a::real ^'n). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
1.4078 +proof-
1.4079 +  {assume "span S = UNIV"
1.4080 +    hence "dim (span S) = dim (UNIV :: (real ^'n) set)" by simp
1.4081 +    hence "dim S = dimindex (UNIV :: 'n set)" by (simp add: dim_span dim_univ)
1.4082 +    with d have False by arith}
1.4083 +  hence th: "span S \<noteq> UNIV" by blast
1.4084 +  from span_not_univ_subset_hyperplane[OF th] show ?thesis .
1.4085 +qed
1.4086 +
1.4087 +(* We can extend a linear basis-basis injection to the whole set.            *)
1.4088 +
1.4089 +lemma linear_indep_image_lemma:
1.4090 +  assumes lf: "linear f" and fB: "finite B"
1.4091 +  and ifB: "independent (f ` B)"
1.4092 +  and fi: "inj_on f B" and xsB: "x \<in> span B"
1.4093 +  and fx: "f (x::'a::field^'n) = 0"
1.4094 +  shows "x = 0"
1.4095 +  using fB ifB fi xsB fx
1.4096 +proof(induct arbitrary: x rule: finite_induct[OF fB])
1.4097 +  case 1 thus ?case by (auto simp add:  span_empty)
1.4098 +next
1.4099 +  case (2 a b x)
1.4100 +  have fb: "finite b" using "2.prems" by simp
1.4101 +  have th0: "f ` b \<subseteq> f ` (insert a b)"
1.4102 +    apply (rule image_mono) by blast
1.4103 +  from independent_mono[ OF "2.prems"(2) th0]
1.4104 +  have ifb: "independent (f ` b)"  .
1.4105 +  have fib: "inj_on f b"
1.4106 +    apply (rule subset_inj_on [OF "2.prems"(3)])
1.4107 +    by blast
1.4108 +  from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
1.4109 +  obtain k where k: "x - k*s a \<in> span (b -{a})" by blast
1.4110 +  have "f (x - k*s a) \<in> span (f ` b)"
1.4111 +    unfolding span_linear_image[OF lf]
1.4112 +    apply (rule imageI)
1.4113 +    using k span_mono[of "b-{a}" b] by blast
1.4114 +  hence "f x - k*s f a \<in> span (f ` b)"
1.4115 +    by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
1.4116 +  hence th: "-k *s f a \<in> span (f ` b)"
1.4117 +    using "2.prems"(5) by (simp add: vector_smult_lneg)
1.4118 +  {assume k0: "k = 0"
1.4119 +    from k0 k have "x \<in> span (b -{a})" by simp
1.4120 +    then have "x \<in> span b" using span_mono[of "b-{a}" b]
1.4121 +      by blast}
1.4122 +  moreover
1.4123 +  {assume k0: "k \<noteq> 0"
1.4124 +    from span_mul[OF th, of "- 1/ k"] k0
1.4125 +    have th1: "f a \<in> span (f ` b)"
1.4126 +      by (auto simp add: vector_smult_assoc)
1.4127 +    from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
1.4128 +    have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
1.4129 +    from "2.prems"(2)[unfolded dependent_def bex_simps(10), rule_format, of "f a"]
1.4130 +    have "f a \<notin> span (f ` b)" using tha
1.4131 +      using "2.hyps"(2)
1.4132 +      "2.prems"(3) by auto
1.4133 +    with th1 have False by blast
1.4134 +    then have "x \<in> span b" by blast}
1.4135 +  ultimately have xsb: "x \<in> span b" by blast
1.4136 +  from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)]
1.4137 +  show "x = 0" .
1.4138 +qed
1.4139 +
1.4140 +(* We can extend a linear mapping from basis.                                *)
1.4141 +
1.4142 +lemma linear_independent_extend_lemma:
1.4143 +  assumes fi: "finite B" and ib: "independent B"
1.4144 +  shows "\<exists>g. (\<forall>x\<in> span B. \<forall>y\<in> span B. g ((x::'a::field^'n) + y) = g x + g y)
1.4145 +           \<and> (\<forall>x\<in> span B. \<forall>c. g (c*s x) = c *s g x)
1.4146 +           \<and> (\<forall>x\<in> B. g x = f x)"
1.4147 +using ib fi
1.4148 +proof(induct rule: finite_induct[OF fi])
1.4149 +  case 1 thus ?case by (auto simp add: span_empty)
1.4150 +next
1.4151 +  case (2 a b)
1.4152 +  from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
1.4153 +    by (simp_all add: independent_insert)
1.4154 +  from "2.hyps"(3)[OF ibf] obtain g where
1.4155 +    g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
1.4156 +    "\<forall>x\<in>span b. \<forall>c. g (c *s x) = c *s g x" "\<forall>x\<in>b. g x = f x" by blast
1.4157 +  let ?h = "\<lambda>z. SOME k. (z - k *s a) \<in> span b"
1.4158 +  {fix z assume z: "z \<in> span (insert a b)"
1.4159 +    have th0: "z - ?h z *s a \<in> span b"
1.4160 +      apply (rule someI_ex)
1.4161 +      unfolding span_breakdown_eq[symmetric]
1.4162 +      using z .
1.4163 +    {fix k assume k: "z - k *s a \<in> span b"
1.4164 +      have eq: "z - ?h z *s a - (z - k*s a) = (k - ?h z) *s a"
1.4166 +      from span_sub[OF th0 k]
1.4167 +      have khz: "(k - ?h z) *s a \<in> span b" by (simp add: eq)
1.4168 +      {assume "k \<noteq> ?h z" hence k0: "k - ?h z \<noteq> 0" by simp
1.4169 +	from k0 span_mul[OF khz, of "1 /(k - ?h z)"]
1.4170 +	have "a \<in> span b" by (simp add: vector_smult_assoc)
1.4171 +	with "2.prems"(1) "2.hyps"(2) have False
1.4172 +	  by (auto simp add: dependent_def)}
1.4173 +      then have "k = ?h z" by blast}
1.4174 +    with th0 have "z - ?h z *s a \<in> span b \<and> (\<forall>k. z - k *s a \<in> span b \<longrightarrow> k = ?h z)" by blast}
1.4175 +  note h = this
1.4176 +  let ?g = "\<lambda>z. ?h z *s f a + g (z - ?h z *s a)"
1.4177 +  {fix x y assume x: "x \<in> span (insert a b)" and y: "y \<in> span (insert a b)"
1.4178 +    have tha: "\<And>(x::'a^'n) y a k l. (x + y) - (k + l) *s a = (x - k *s a) + (y - l *s a)"
1.4179 +      by (vector ring_simps)
1.4180 +    have addh: "?h (x + y) = ?h x + ?h y"
1.4181 +      apply (rule conjunct2[OF h, rule_format, symmetric])
1.4182 +      apply (rule span_add[OF x y])
1.4183 +      unfolding tha
1.4184 +      by (metis span_add x y conjunct1[OF h, rule_format])
1.4185 +    have "?g (x + y) = ?g x + ?g y"
1.4187 +      g(1)[rule_format,OF conjunct1[OF h, OF x] conjunct1[OF h, OF y]]
1.4189 +  moreover
1.4190 +  {fix x:: "'a^'n" and c:: 'a  assume x: "x \<in> span (insert a b)"
1.4191 +    have tha: "\<And>(x::'a^'n) c k a. c *s x - (c * k) *s a = c *s (x - k *s a)"
1.4192 +      by (vector ring_simps)
1.4193 +    have hc: "?h (c *s x) = c * ?h x"
1.4194 +      apply (rule conjunct2[OF h, rule_format, symmetric])
1.4195 +      apply (metis span_mul x)
1.4196 +      by (metis tha span_mul x conjunct1[OF h])
1.4197 +    have "?g (c *s x) = c*s ?g x"
1.4198 +      unfolding hc tha g(2)[rule_format, OF conjunct1[OF h, OF x]]
1.4199 +      by (vector ring_simps)}
1.4200 +  moreover
1.4201 +  {fix x assume x: "x \<in> (insert a b)"
1.4202 +    {assume xa: "x = a"
1.4203 +      have ha1: "1 = ?h a"
1.4204 +	apply (rule conjunct2[OF h, rule_format])
1.4205 +	apply (metis span_superset insertI1)
1.4206 +	using conjunct1[OF h, OF span_superset, OF insertI1]
1.4207 +	by (auto simp add: span_0)
1.4208 +
1.4209 +      from xa ha1[symmetric] have "?g x = f x"
1.4210 +	apply simp
1.4211 +	using g(2)[rule_format, OF span_0, of 0]
1.4212 +	by simp}
1.4213 +    moreover
1.4214 +    {assume xb: "x \<in> b"
1.4215 +      have h0: "0 = ?h x"
1.4216 +	apply (rule conjunct2[OF h, rule_format])
1.4217 +	apply (metis  span_superset insertI1 xb x)
1.4218 +	apply simp
1.4219 +	apply (metis span_superset xb)
1.4220 +	done
1.4221 +      have "?g x = f x"
1.4222 +	by (simp add: h0[symmetric] g(3)[rule_format, OF xb])}
1.4223 +    ultimately have "?g x = f x" using x by blast }
1.4224 +  ultimately show ?case apply - apply (rule exI[where x="?g"]) by blast
1.4225 +qed
1.4226 +
1.4227 +lemma linear_independent_extend:
1.4228 +  assumes iB: "independent (B:: (real ^'n) set)"
1.4229 +  shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
1.4230 +proof-
1.4231 +  from maximal_independent_subset_extend[of B "UNIV"] iB
1.4232 +  obtain C where C: "B \<subseteq> C" "independent C" "\<And>x. x \<in> span C" by auto
1.4233 +
1.4234 +  from C(2) independent_bound[of C] linear_independent_extend_lemma[of C f]
1.4235 +  obtain g where g: "(\<forall>x\<in> span C. \<forall>y\<in> span C. g (x + y) = g x + g y)
1.4236 +           \<and> (\<forall>x\<in> span C. \<forall>c. g (c*s x) = c *s g x)
1.4237 +           \<and> (\<forall>x\<in> C. g x = f x)" by blast
1.4238 +  from g show ?thesis unfolding linear_def using C
1.4239 +    apply clarsimp by blast
1.4240 +qed
1.4241 +
1.4242 +(* Can construct an isomorphism between spaces of same dimension.            *)
1.4243 +
1.4244 +lemma card_le_inj: assumes fA: "finite A" and fB: "finite B"
1.4245 +  and c: "card A \<le> card B" shows "(\<exists>f. f ` A \<subseteq> B \<and> inj_on f A)"
1.4246 +using fB c
1.4247 +proof(induct arbitrary: B rule: finite_induct[OF fA])
1.4248 +  case 1 thus ?case by simp
1.4249 +next
1.4250 +  case (2 x s t)
1.4251 +  thus ?case
1.4252 +  proof(induct rule: finite_induct[OF "2.prems"(1)])
1.4253 +    case 1    then show ?case by simp
1.4254 +  next
1.4255 +    case (2 y t)
1.4256 +    from "2.prems"(1,2,5) "2.hyps"(1,2) have cst:"card s \<le> card t" by simp
1.4257 +    from "2.prems"(3) [OF "2.hyps"(1) cst] obtain f where
1.4258 +      f: "f ` s \<subseteq> t \<and> inj_on f s" by blast
1.4259 +    from f "2.prems"(2) "2.hyps"(2) show ?case
1.4260 +      apply -
1.4261 +      apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
1.4262 +      by (auto simp add: inj_on_def)
1.4263 +  qed
1.4264 +qed
1.4265 +
1.4266 +lemma card_subset_eq: assumes fB: "finite B" and AB: "A \<subseteq> B" and
1.4267 +  c: "card A = card B"
1.4268 +  shows "A = B"
1.4269 +proof-
1.4270 +  from fB AB have fA: "finite A" by (auto intro: finite_subset)
1.4271 +  from fA fB have fBA: "finite (B - A)" by auto
1.4272 +  have e: "A \<inter> (B - A) = {}" by blast
1.4273 +  have eq: "A \<union> (B - A) = B" using AB by blast
1.4274 +  from card_Un_disjoint[OF fA fBA e, unfolded eq c]
1.4275 +  have "card (B - A) = 0" by arith
1.4276 +  hence "B - A = {}" unfolding card_eq_0_iff using fA fB by simp
1.4277 +  with AB show "A = B" by blast
1.4278 +qed
1.4279 +
1.4280 +lemma subspace_isomorphism:
1.4281 +  assumes s: "subspace (S:: (real ^'n) set)" and t: "subspace T"
1.4282 +  and d: "dim S = dim T"
1.4283 +  shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
1.4284 +proof-
1.4285 +  from basis_exists[of S] obtain B where
1.4286 +    B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
1.4287 +  from basis_exists[of T] obtain C where
1.4288 +    C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "C hassize dim T" by blast
1.4289 +  from B(4) C(4) card_le_inj[of B C] d obtain f where
1.4290 +    f: "f ` B \<subseteq> C" "inj_on f B" unfolding hassize_def by auto
1.4291 +  from linear_independent_extend[OF B(2)] obtain g where
1.4292 +    g: "linear g" "\<forall>x\<in> B. g x = f x" by blast
1.4293 +  from B(4) have fB: "finite B" by (simp add: hassize_def)
1.4294 +  from C(4) have fC: "finite C" by (simp add: hassize_def)
1.4295 +  from inj_on_iff_eq_card[OF fB, of f] f(2)
1.4296 +  have "card (f ` B) = card B" by simp
1.4297 +  with B(4) C(4) have ceq: "card (f ` B) = card C" using d
1.4298 +    by (simp add: hassize_def)
1.4299 +  have "g ` B = f ` B" using g(2)
1.4300 +    by (auto simp add: image_iff)
1.4301 +  also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
1.4302 +  finally have gBC: "g ` B = C" .
1.4303 +  have gi: "inj_on g B" using f(2) g(2)
1.4304 +    by (auto simp add: inj_on_def)
1.4305 +  note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
1.4306 +  {fix x y assume x: "x \<in> S" and y: "y \<in> S" and gxy:"g x = g y"
1.4307 +    from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B" by blast+
1.4308 +    from gxy have th0: "g (x - y) = 0" by (simp add: linear_sub[OF g(1)])
1.4309 +    have th1: "x - y \<in> span B" using x' y' by (metis span_sub)
1.4310 +    have "x=y" using g0[OF th1 th0] by simp }
1.4311 +  then have giS: "inj_on g S"
1.4312 +    unfolding inj_on_def by blast
1.4313 +  from span_subspace[OF B(1,3) s]
1.4314 +  have "g ` S = span (g ` B)" by (simp add: span_linear_image[OF g(1)])
1.4315 +  also have "\<dots> = span C" unfolding gBC ..
1.4316 +  also have "\<dots> = T" using span_subspace[OF C(1,3) t] .
1.4317 +  finally have gS: "g ` S = T" .
1.4318 +  from g(1) gS giS show ?thesis by blast
1.4319 +qed
1.4320 +
1.4321 +(* linear functions are equal on a subspace if they are on a spanning set.   *)
1.4322 +
1.4323 +lemma subspace_kernel:
1.4324 +  assumes lf: "linear (f::'a::semiring_1 ^'n \<Rightarrow> _)"
1.4325 +  shows "subspace {x. f x = 0}"
1.4328 +
1.4329 +lemma linear_eq_0_span:
1.4330 +  assumes lf: "linear f" and f0: "\<forall>x\<in>B. f x = 0"
1.4331 +  shows "\<forall>x \<in> span B. f x = (0::'a::semiring_1 ^'n)"
1.4332 +proof
1.4333 +  fix x assume x: "x \<in> span B"
1.4334 +  let ?P = "\<lambda>x. f x = 0"
1.4335 +  from subspace_kernel[OF lf] have "subspace ?P" unfolding Collect_def .
1.4336 +  with x f0 span_induct[of B "?P" x] show "f x = 0" by blast
1.4337 +qed
1.4338 +
1.4339 +lemma linear_eq_0:
1.4340 +  assumes lf: "linear f" and SB: "S \<subseteq> span B" and f0: "\<forall>x\<in>B. f x = 0"
1.4341 +  shows "\<forall>x \<in> S. f x = (0::'a::semiring_1^'n)"
1.4342 +  by (metis linear_eq_0_span[OF lf] subset_eq SB f0)
1.4343 +
1.4344 +lemma linear_eq:
1.4345 +  assumes lf: "linear (f::'a::ring_1^'n \<Rightarrow> _)" and lg: "linear g" and S: "S \<subseteq> span B"
1.4346 +  and fg: "\<forall> x\<in> B. f x = g x"
1.4347 +  shows "\<forall>x\<in> S. f x = g x"
1.4348 +proof-
1.4349 +  let ?h = "\<lambda>x. f x - g x"
1.4350 +  from fg have fg': "\<forall>x\<in> B. ?h x = 0" by simp
1.4351 +  from linear_eq_0[OF linear_compose_sub[OF lf lg] S fg']
1.4352 +  show ?thesis by simp
1.4353 +qed
1.4354 +
1.4355 +lemma linear_eq_stdbasis:
1.4356 +  assumes lf: "linear (f::'a::ring_1^'m \<Rightarrow> 'a^'n)" and lg: "linear g"
1.4357 +  and fg: "\<forall>i \<in> {1 .. dimindex(UNIV :: 'm set)}. f (basis i) = g(basis i)"
1.4358 +  shows "f = g"
1.4359 +proof-
1.4360 +  let ?U = "UNIV :: 'm set"
1.4361 +  let ?I = "{basis i:: 'a^'m|i. i \<in> {1 .. dimindex ?U}}"
1.4362 +  {fix x assume x: "x \<in> (UNIV :: ('a^'m) set)"
1.4363 +    from equalityD2[OF span_stdbasis]
1.4364 +    have IU: " (UNIV :: ('a^'m) set) \<subseteq> span ?I" by blast
1.4365 +    from linear_eq[OF lf lg IU] fg x
1.4366 +    have "f x = g x" unfolding Collect_def  Ball_def mem_def by metis}
1.4367 +  then show ?thesis by (auto intro: ext)
1.4368 +qed
1.4369 +
1.4370 +(* Similar results for bilinear functions.                                   *)
1.4371 +
1.4372 +lemma bilinear_eq:
1.4373 +  assumes bf: "bilinear (f:: 'a::ring^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)"
1.4374 +  and bg: "bilinear g"
1.4375 +  and SB: "S \<subseteq> span B" and TC: "T \<subseteq> span C"
1.4376 +  and fg: "\<forall>x\<in> B. \<forall>y\<in> C. f x y = g x y"
1.4377 +  shows "\<forall>x\<in>S. \<forall>y\<in>T. f x y = g x y "
1.4378 +proof-
1.4379 +  let ?P = "\<lambda>x. \<forall>y\<in> span C. f x y = g x y"
1.4380 +  from bf bg have sp: "subspace ?P"
1.4381 +    unfolding bilinear_def linear_def subspace_def bf bg
1.4383 +
1.4384 +  have "\<forall>x \<in> span B. \<forall>y\<in> span C. f x y = g x y"
1.4385 +    apply -
1.4386 +    apply (rule ballI)
1.4387 +    apply (rule span_induct[of B ?P])
1.4388 +    defer
1.4389 +    apply (rule sp)
1.4390 +    apply assumption
1.4391 +    apply (clarsimp simp add: Ball_def)
1.4392 +    apply (rule_tac P="\<lambda>y. f xa y = g xa y" and S=C in span_induct)
1.4393 +    using fg
1.4394 +    apply (auto simp add: subspace_def)
1.4395 +    using bf bg unfolding bilinear_def linear_def
1.4397 +  then show ?thesis using SB TC by (auto intro: ext)
1.4398 +qed
1.4399 +
1.4400 +lemma bilinear_eq_stdbasis:
1.4401 +  assumes bf: "bilinear (f:: 'a::ring_1^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)"
1.4402 +  and bg: "bilinear g"
1.4403 +  and fg: "\<forall>i\<in> {1 .. dimindex (UNIV :: 'm set)}. \<forall>j\<in>  {1 .. dimindex (UNIV :: 'n set)}. f (basis i) (basis j) = g (basis i) (basis j)"
1.4404 +  shows "f = g"
1.4405 +proof-
1.4406 +  from fg have th: "\<forall>x \<in> {basis i| i. i\<in> {1 .. dimindex (UNIV :: 'm set)}}. \<forall>y\<in>  {basis j |j. j \<in> {1 .. dimindex (UNIV :: 'n set)}}. f x y = g x y" by blast
1.4407 +  from bilinear_eq[OF bf bg equalityD2[OF span_stdbasis] equalityD2[OF span_stdbasis] th] show ?thesis by (blast intro: ext)
1.4408 +qed
1.4409 +
1.4410 +(* Detailed theorems about left and right invertibility in general case.     *)
1.4411 +
1.4412 +lemma left_invertible_transp:
1.4413 +  "(\<exists>(B::'a^'n^'m). B ** transp (A::'a^'n^'m) = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B::'a^'m^'n). A ** B = mat 1)"
1.4414 +  by (metis matrix_transp_mul transp_mat transp_transp)
1.4415 +
1.4416 +lemma right_invertible_transp:
1.4417 +  "(\<exists>(B::'a^'n^'m). transp (A::'a^'n^'m) ** B = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B::'a^'m^'n). B ** A = mat 1)"
1.4418 +  by (metis matrix_transp_mul transp_mat transp_transp)
1.4419 +
1.4420 +lemma linear_injective_left_inverse:
1.4421 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and fi: "inj f"
1.4422 +  shows "\<exists>g. linear g \<and> g o f = id"
1.4423 +proof-
1.4424 +  from linear_independent_extend[OF independent_injective_image, OF independent_stdbasis, OF lf fi]
1.4425 +  obtain h:: "real ^'m \<Rightarrow> real ^'n" where h: "linear h" " \<forall>x \<in> f ` {basis i|i. i \<in> {1 .. dimindex (UNIV::'n set)}}. h x = inv f x" by blast
1.4426 +  from h(2)
1.4427 +  have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (h \<circ> f) (basis i) = id (basis i)"
1.4428 +    using inv_o_cancel[OF fi, unfolded stupid_ext[symmetric] id_def o_def]
1.4429 +    apply auto
1.4430 +    apply (erule_tac x="basis i" in allE)
1.4431 +    by auto
1.4432 +
1.4433 +  from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
1.4434 +  have "h o f = id" .
1.4435 +  then show ?thesis using h(1) by blast
1.4436 +qed
1.4437 +
1.4438 +lemma linear_surjective_right_inverse:
1.4439 +  assumes lf: "linear (f:: real ^'m \<Rightarrow> real ^'n)" and sf: "surj f"
1.4440 +  shows "\<exists>g. linear g \<and> f o g = id"
1.4441 +proof-
1.4442 +  from linear_independent_extend[OF independent_stdbasis]
1.4443 +  obtain h:: "real ^'n \<Rightarrow> real ^'m" where
1.4444 +    h: "linear h" "\<forall> x\<in> {basis i| i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}. h x = inv f x" by blast
1.4445 +  from h(2)
1.4446 +  have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (f o h) (basis i) = id (basis i)"
1.4447 +    using sf
1.4448 +    apply (auto simp add: surj_iff o_def stupid_ext[symmetric])
1.4449 +    apply (erule_tac x="basis i" in allE)
1.4450 +    by auto
1.4451 +
1.4452 +  from linear_eq_stdbasis[OF linear_compose[OF h(1) lf] linear_id th]
1.4453 +  have "f o h = id" .
1.4454 +  then show ?thesis using h(1) by blast
1.4455 +qed
1.4456 +
1.4457 +lemma matrix_left_invertible_injective:
1.4458 +"(\<exists>B. (B::real^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
1.4459 +proof-
1.4460 +  {fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
1.4461 +    from xy have "B*v (A *v x) = B *v (A*v y)" by simp
1.4462 +    hence "x = y"
1.4463 +      unfolding matrix_vector_mul_assoc B matrix_vector_mul_lid .}
1.4464 +  moreover
1.4465 +  {assume A: "\<forall>x y. A *v x = A *v y \<longrightarrow> x = y"
1.4466 +    hence i: "inj (op *v A)" unfolding inj_on_def by auto
1.4467 +    from linear_injective_left_inverse[OF matrix_vector_mul_linear i]
1.4468 +    obtain g where g: "linear g" "g o op *v A = id" by blast
1.4469 +    have "matrix g ** A = mat 1"
1.4470 +      unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
1.4471 +      using g(2) by (simp add: o_def id_def stupid_ext)
1.4472 +    then have "\<exists>B. (B::real ^'m^'n) ** A = mat 1" by blast}
1.4473 +  ultimately show ?thesis by blast
1.4474 +qed
1.4475 +
1.4476 +lemma matrix_left_invertible_ker:
1.4477 +  "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
1.4478 +  unfolding matrix_left_invertible_injective
1.4479 +  using linear_injective_0[OF matrix_vector_mul_linear, of A]
1.4480 +  by (simp add: inj_on_def)
1.4481 +
1.4482 +lemma matrix_right_invertible_surjective:
1.4483 +"(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
1.4484 +proof-
1.4485 +  {fix B :: "real ^'m^'n"  assume AB: "A ** B = mat 1"
1.4486 +    {fix x :: "real ^ 'm"
1.4487 +      have "A *v (B *v x) = x"
1.4488 +	by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB)}
1.4489 +    hence "surj (op *v A)" unfolding surj_def by metis }
1.4490 +  moreover
1.4491 +  {assume sf: "surj (op *v A)"
1.4492 +    from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
1.4493 +    obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "op *v A o g = id"
1.4494 +      by blast
1.4495 +
1.4496 +    have "A ** (matrix g) = mat 1"
1.4497 +      unfolding matrix_eq  matrix_vector_mul_lid
1.4498 +	matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
1.4499 +      using g(2) unfolding o_def stupid_ext[symmetric] id_def
1.4500 +      .
1.4501 +    hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
1.4502 +  }
1.4503 +  ultimately show ?thesis unfolding surj_def by blast
1.4504 +qed
1.4505 +
1.4506 +lemma matrix_left_invertible_independent_columns:
1.4507 +  fixes A :: "real^'n^'m"
1.4508 +  shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s column i A) {1 .. dimindex(UNIV :: 'n set)} = 0 \<longrightarrow> (\<forall>i\<in> {1 .. dimindex (UNIV :: 'n set)}. c i = 0))"
1.4509 +   (is "?lhs \<longleftrightarrow> ?rhs")
1.4510 +proof-
1.4511 +  let ?U = "{1 .. dimindex(UNIV :: 'n set)}"
1.4512 +  {assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
1.4513 +    {fix c i assume c: "setsum (\<lambda>i. c i *s column i A) ?U = 0"
1.4514 +      and i: "i \<in> ?U"
1.4515 +      let ?x = "\<chi> i. c i"
1.4516 +      have th0:"A *v ?x = 0"
1.4517 +	using c
1.4518 +	unfolding matrix_mult_vsum Cart_eq
1.4519 +	by (auto simp add: vector_component zero_index setsum_component Cart_lambda_beta)
1.4520 +      from k[rule_format, OF th0] i
1.4521 +      have "c i = 0" by (vector Cart_eq)}
1.4522 +    hence ?rhs by blast}
1.4523 +  moreover
1.4524 +  {assume H: ?rhs
1.4525 +    {fix x assume x: "A *v x = 0"
1.4526 +      let ?c = "\<lambda>i. ((x\$i ):: real)"
1.4527 +      from H[rule_format, of ?c, unfolded matrix_mult_vsum[symmetric], OF x]
1.4528 +      have "x = 0" by vector}}
1.4529 +  ultimately show ?thesis unfolding matrix_left_invertible_ker by blast
1.4530 +qed
1.4531 +
1.4532 +lemma matrix_right_invertible_independent_rows:
1.4533 +  fixes A :: "real^'n^'m"
1.4534 +  shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s row i A) {1 .. dimindex(UNIV :: 'm set)} = 0 \<longrightarrow> (\<forall>i\<in> {1 .. dimindex (UNIV :: 'm set)}. c i = 0))"
1.4535 +  unfolding left_invertible_transp[symmetric]
1.4536 +    matrix_left_invertible_independent_columns
1.4537 +  by (simp add: column_transp)
1.4538 +
1.4539 +lemma matrix_right_invertible_span_columns:
1.4540 +  "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow> span (columns A) = UNIV" (is "?lhs = ?rhs")
1.4541 +proof-
1.4542 +  let ?U = "{1 .. dimindex (UNIV :: 'm set)}"
1.4543 +  have fU: "finite ?U" by simp
1.4544 +  have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). setsum (\<lambda>i. (x\$i) *s column i A) ?U = y)"
1.4545 +    unfolding matrix_right_invertible_surjective matrix_mult_vsum surj_def
1.4546 +    apply (subst eq_commute) ..
1.4547 +  have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
1.4548 +  {assume h: ?lhs
1.4549 +    {fix x:: "real ^'n"
1.4550 +	from h[unfolded lhseq, rule_format, of x] obtain y:: "real ^'m"
1.4551 +	  where y: "setsum (\<lambda>i. (y\$i) *s column i A) ?U = x" by blast
1.4552 +	have "x \<in> span (columns A)"
1.4553 +	  unfolding y[symmetric]
1.4554 +	  apply (rule span_setsum[OF fU])
1.4555 +	  apply clarify
1.4556 +	  apply (rule span_mul)
1.4557 +	  apply (rule span_superset)
1.4558 +	  unfolding columns_def
1.4559 +	  by blast}
1.4560 +    then have ?rhs unfolding rhseq by blast}
1.4561 +  moreover
1.4562 +  {assume h:?rhs
1.4563 +    let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). setsum (\<lambda>i. (x\$i) *s column i A) ?U = y"
1.4564 +    {fix y have "?P y"
1.4565 +      proof(rule span_induct_alt[of ?P "columns A"])
1.4566 +	show "\<exists>x\<Colon>real ^ 'm. setsum (\<lambda>i. (x\$i) *s column i A) ?U = 0"
1.4567 +	  apply (rule exI[where x=0])
1.4568 +	  by (simp add: zero_index vector_smult_lzero)
1.4569 +      next
1.4570 +	fix c y1 y2 assume y1: "y1 \<in> columns A" and y2: "?P y2"
1.4571 +	from y1 obtain i where i: "i \<in> ?U" "y1 = column i A"
1.4572 +	  unfolding columns_def by blast
1.4573 +	from y2 obtain x:: "real ^'m" where
1.4574 +	  x: "setsum (\<lambda>i. (x\$i) *s column i A) ?U = y2" by blast
1.4575 +	let ?x = "(\<chi> j. if j = i then c + (x\$i) else (x\$j))::real^'m"
1.4576 +	show "?P (c*s y1 + y2)"
1.4577 +	  proof(rule exI[where x= "?x"], vector, auto simp add: i x[symmetric]Cart_lambda_beta setsum_component cond_value_iff right_distrib cond_application_beta vector_component cong del: if_weak_cong, simp only: One_nat_def[symmetric])
1.4578 +	    fix j
1.4579 +	    have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x\$i)) * ((column xa A)\$j)
1.4580 +           else (x\$xa) * ((column xa A\$j))) = (if xa = i then c * ((column i A)\$j) else 0) + ((x\$xa) * ((column xa A)\$j))" using i(1)
1.4581 +	      by (simp add: ring_simps)
1.4582 +	    have "setsum (\<lambda>xa. if xa = i then (c + (x\$i)) * ((column xa A)\$j)
1.4583 +           else (x\$xa) * ((column xa A\$j))) ?U = setsum (\<lambda>xa. (if xa = i then c * ((column i A)\$j) else 0) + ((x\$xa) * ((column xa A)\$j))) ?U"
1.4584 +	      apply (rule setsum_cong[OF refl])
1.4585 +	      using th by blast
1.4586 +	    also have "\<dots> = setsum (\<lambda>xa. if xa = i then c * ((column i A)\$j) else 0) ?U + setsum (\<lambda>xa. ((x\$xa) * ((column xa A)\$j))) ?U"
1.4588 +	    also have "\<dots> = c * ((column i A)\$j) + setsum (\<lambda>xa. ((x\$xa) * ((column xa A)\$j))) ?U"
1.4589 +	      unfolding setsum_delta[OF fU]
1.4590 +	      using i(1) by simp
1.4591 +	    finally show "setsum (\<lambda>xa. if xa = i then (c + (x\$i)) * ((column xa A)\$j)
1.4592 +           else (x\$xa) * ((column xa A\$j))) ?U = c * ((column i A)\$j) + setsum (\<lambda>xa. ((x\$xa) * ((column xa A)\$j))) ?U" .
1.4593 +	  qed
1.4594 +	next
1.4595 +	  show "y \<in> span (columns A)" unfolding h by blast
1.4596 +	qed}
1.4597 +    then have ?lhs unfolding lhseq ..}
1.4598 +  ultimately show ?thesis by blast
1.4599 +qed
1.4600 +
1.4601 +lemma matrix_left_invertible_span_rows:
1.4602 +  "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
1.4603 +  unfolding right_invertible_transp[symmetric]
1.4604 +  unfolding columns_transp[symmetric]
1.4605 +  unfolding matrix_right_invertible_span_columns
1.4606 + ..
1.4607 +
1.4608 +(* An injective map real^'n->real^'n is also surjective.                       *)
1.4609 +
1.4610 +lemma linear_injective_imp_surjective:
1.4611 +  assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and fi: "inj f"
1.4612 +  shows "surj f"
1.4613 +proof-
1.4614 +  let ?U = "UNIV :: (real ^'n) set"
1.4615 +  from basis_exists[of ?U] obtain B
1.4616 +    where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U"
1.4617 +    by blast
1.4618 +  from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
1.4619 +  have th: "?U \<subseteq> span (f ` B)"
1.4620 +    apply (rule card_ge_dim_independent)
1.4621 +    apply blast
1.4622 +    apply (rule independent_injective_image[OF B(2) lf fi])
1.4623 +    apply (rule order_eq_refl)
1.4624 +    apply (rule sym)
1.4625 +    unfolding d
1.4626 +    apply (rule card_image)
1.4627 +    apply (rule subset_inj_on[OF fi])
1.4628 +    by blast
1.4629 +  from th show ?thesis
1.4630 +    unfolding span_linear_image[OF lf] surj_def
1.4631 +    using B(3) by blast
1.4632 +qed
1.4633 +
1.4634 +(* And vice versa.                                                           *)
1.4635 +
1.4636 +lemma surjective_iff_injective_gen:
1.4637 +  assumes fS: "finite S" and fT: "finite T" and c: "card S = card T"
1.4638 +  and ST: "f ` S \<subseteq> T"
1.4639 +  shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" (is "?lhs \<longleftrightarrow> ?rhs")
1.4640 +proof-
1.4641 +  {assume h: "?lhs"
1.4642 +    {fix x y assume x: "x \<in> S" and y: "y \<in> S" and f: "f x = f y"
1.4643 +      from x fS have S0: "card S \<noteq> 0" by auto
1.4644 +      {assume xy: "x \<noteq> y"
1.4645 +	have th: "card S \<le> card (f ` (S - {y}))"
1.4646 +	  unfolding c
1.4647 +	  apply (rule card_mono)
1.4648 +	  apply (rule finite_imageI)
1.4649 +	  using fS apply simp
1.4650 +	  using h xy x y f unfolding subset_eq image_iff
1.4651 +	  apply auto
1.4652 +	  apply (case_tac "xa = f x")
1.4653 +	  apply (rule bexI[where x=x])
1.4654 +	  apply auto
1.4655 +	  done
1.4656 +	also have " \<dots> \<le> card (S -{y})"
1.4657 +	  apply (rule card_image_le)
1.4658 +	  using fS by simp
1.4659 +	also have "\<dots> \<le> card S - 1" using y fS by simp
1.4660 +	finally have False  using S0 by arith }
1.4661 +      then have "x = y" by blast}
1.4662 +    then have ?rhs unfolding inj_on_def by blast}
1.4663 +  moreover
1.4664 +  {assume h: ?rhs
1.4665 +    have "f ` S = T"
1.4666 +      apply (rule card_subset_eq[OF fT ST])
1.4667 +      unfolding card_image[OF h] using c .
1.4668 +    then have ?lhs by blast}
1.4669 +  ultimately show ?thesis by blast
1.4670 +qed
1.4671 +
1.4672 +lemma linear_surjective_imp_injective:
1.4673 +  assumes lf: "linear (f::real ^'n => real ^'n)" and sf: "surj f"
1.4674 +  shows "inj f"
1.4675 +proof-
1.4676 +  let ?U = "UNIV :: (real ^'n) set"
1.4677 +  from basis_exists[of ?U] obtain B
1.4678 +    where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U"
1.4679 +    by blast
1.4680 +  {fix x assume x: "x \<in> span B" and fx: "f x = 0"
1.4681 +    from B(4) have fB: "finite B" by (simp add: hassize_def)
1.4682 +    from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
1.4683 +    have fBi: "independent (f ` B)"
1.4684 +      apply (rule card_le_dim_spanning[of "f ` B" ?U])
1.4685 +      apply blast
1.4686 +      using sf B(3)
1.4687 +      unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
1.4688 +      apply blast
1.4689 +      using fB apply (blast intro: finite_imageI)
1.4690 +      unfolding d
1.4691 +      apply (rule card_image_le)
1.4692 +      apply (rule fB)
1.4693 +      done
1.4694 +    have th0: "dim ?U \<le> card (f ` B)"
1.4695 +      apply (rule span_card_ge_dim)
1.4696 +      apply blast
1.4697 +      unfolding span_linear_image[OF lf]
1.4698 +      apply (rule subset_trans[where B = "f ` UNIV"])
1.4699 +      using sf unfolding surj_def apply blast
1.4700 +      apply (rule image_mono)
1.4701 +      apply (rule B(3))
1.4702 +      apply (metis finite_imageI fB)
1.4703 +      done
1.4704 +
1.4705 +    moreover have "card (f ` B) \<le> card B"
1.4706 +      by (rule card_image_le, rule fB)
1.4707 +    ultimately have th1: "card B = card (f ` B)" unfolding d by arith
1.4708 +    have fiB: "inj_on f B"
1.4709 +      unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric] by blast
1.4710 +    from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
1.4711 +    have "x = 0" by blast}
1.4712 +  note th = this
1.4713 +  from th show ?thesis unfolding linear_injective_0[OF lf]
1.4714 +    using B(3) by blast
1.4715 +qed
1.4716 +
1.4717 +(* Hence either is enough for isomorphism.                                   *)
1.4718 +
1.4719 +lemma left_right_inverse_eq:
1.4720 +  assumes fg: "f o g = id" and gh: "g o h = id"
1.4721 +  shows "f = h"
1.4722 +proof-
1.4723 +  have "f = f o (g o h)" unfolding gh by simp
1.4724 +  also have "\<dots> = (f o g) o h" by (simp add: o_assoc)
1.4725 +  finally show "f = h" unfolding fg by simp
1.4726 +qed
1.4727 +
1.4728 +lemma isomorphism_expand:
1.4729 +  "f o g = id \<and> g o f = id \<longleftrightarrow> (\<forall>x. f(g x) = x) \<and> (\<forall>x. g(f x) = x)"
1.4730 +  by (simp add: expand_fun_eq o_def id_def)
1.4731 +
1.4732 +lemma linear_injective_isomorphism:
1.4733 +  assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'n)" and fi: "inj f"
1.4734 +  shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
1.4735 +unfolding isomorphism_expand[symmetric]
1.4736 +using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]] linear_injective_left_inverse[OF lf fi]
1.4737 +by (metis left_right_inverse_eq)
1.4738 +
1.4739 +lemma linear_surjective_isomorphism:
1.4740 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and sf: "surj f"
1.4741 +  shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
1.4742 +unfolding isomorphism_expand[symmetric]
1.4743 +using linear_surjective_right_inverse[OF lf sf] linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
1.4744 +by (metis left_right_inverse_eq)
1.4745 +
1.4746 +(* Left and right inverses are the same for R^N->R^N.                        *)
1.4747 +
1.4748 +lemma linear_inverse_left:
1.4749 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and lf': "linear f'"
1.4750 +  shows "f o f' = id \<longleftrightarrow> f' o f = id"
1.4751 +proof-
1.4752 +  {fix f f':: "real ^'n \<Rightarrow> real ^'n"
1.4753 +    assume lf: "linear f" "linear f'" and f: "f o f' = id"
1.4754 +    from f have sf: "surj f"
1.4755 +
1.4756 +      apply (auto simp add: o_def stupid_ext[symmetric] id_def surj_def)
1.4757 +      by metis
1.4758 +    from linear_surjective_isomorphism[OF lf(1) sf] lf f
1.4759 +    have "f' o f = id" unfolding stupid_ext[symmetric] o_def id_def
1.4760 +      by metis}
1.4761 +  then show ?thesis using lf lf' by metis
1.4762 +qed
1.4763 +
1.4764 +(* Moreover, a one-sided inverse is automatically linear.                    *)
1.4765 +
1.4766 +lemma left_inverse_linear:
1.4767 +  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and gf: "g o f = id"
1.4768 +  shows "linear g"
1.4769 +proof-
1.4770 +  from gf have fi: "inj f" apply (auto simp add: inj_on_def o_def id_def stupid_ext[symmetric])
1.4771 +    by metis
1.4772 +  from linear_injective_isomorphism[OF lf fi]
1.4773 +  obtain h:: "real ^'n \<Rightarrow> real ^'n" where
1.4774 +    h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
1.4775 +  have "h = g" apply (rule ext) using gf h(2,3)
1.4776 +    apply (simp add: o_def id_def stupid_ext[symmetric])
1.4777 +    by metis
1.4778 +  with h(1) show ?thesis by blast
1.4779 +qed
1.4780 +
1.4781 +lemma right_inverse_linear:
1.4782 +  assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and gf: "f o g = id"
1.4783 +  shows "linear g"
1.4784 +proof-
1.4785 +  from gf have fi: "surj f" apply (auto simp add: surj_def o_def id_def stupid_ext[symmetric])
1.4786 +    by metis
1.4787 +  from linear_surjective_isomorphism[OF lf fi]
1.4788 +  obtain h:: "real ^'n \<Rightarrow> real ^'n" where
1.4789 +    h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
1.4790 +  have "h = g" apply (rule ext) using gf h(2,3)
1.4791 +    apply (simp add: o_def id_def stupid_ext[symmetric])
1.4792 +    by metis
1.4793 +  with h(1) show ?thesis by blast
1.4794 +qed
1.4795 +
1.4796 +(* The same result in terms of square matrices.                              *)
1.4797 +
1.4798 +lemma matrix_left_right_inverse:
1.4799 +  fixes A A' :: "real ^'n^'n"
1.4800 +  shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
1.4801 +proof-
1.4802 +  {fix A A' :: "real ^'n^'n" assume AA': "A ** A' = mat 1"
1.4803 +    have sA: "surj (op *v A)"
1.4804 +      unfolding surj_def
1.4805 +      apply clarify
1.4806 +      apply (rule_tac x="(A' *v y)" in exI)
1.4807 +      by (simp add: matrix_vector_mul_assoc AA' matrix_vector_mul_lid)
1.4808 +    from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
1.4809 +    obtain f' :: "real ^'n \<Rightarrow> real ^'n"
1.4810 +      where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
1.4811 +    have th: "matrix f' ** A = mat 1"
1.4812 +      by (simp add: matrix_eq matrix_works[OF f'(1)] matrix_vector_mul_assoc[symmetric] matrix_vector_mul_lid f'(2)[rule_format])
1.4813 +    hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
1.4814 +    hence "matrix f' = A'" by (simp add: matrix_mul_assoc[symmetric] AA' matrix_mul_rid matrix_mul_lid)
1.4815 +    hence "matrix f' ** A = A' ** A" by simp
1.4816 +    hence "A' ** A = mat 1" by (simp add: th)}
1.4817 +  then show ?thesis by blast
1.4818 +qed
1.4819 +
1.4820 +(* Considering an n-element vector as an n-by-1 or 1-by-n matrix.            *)
1.4821 +
1.4822 +definition "rowvector v = (\<chi> i j. (v\$j))"
1.4823 +
1.4824 +definition "columnvector v = (\<chi> i j. (v\$i))"
1.4825 +
1.4826 +lemma transp_columnvector:
1.4827 + "transp(columnvector v) = rowvector v"
1.4828 +  by (simp add: transp_def rowvector_def columnvector_def Cart_eq Cart_lambda_beta)
1.4829 +
1.4830 +lemma transp_rowvector: "transp(rowvector v) = columnvector v"
1.4831 +  by (simp add: transp_def columnvector_def rowvector_def Cart_eq Cart_lambda_beta)
1.4832 +
1.4833 +lemma dot_rowvector_columnvector:
1.4834 +  "columnvector (A *v v) = A ** columnvector v"
1.4835 +  by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
1.4836 +
1.4837 +lemma dot_matrix_product: "(x::'a::semiring_1^'n) \<bullet> y = (((rowvector x ::'a^'n^1) ** (columnvector y :: 'a^1^'n))\$1)\$1"
1.4838 +  apply (vector matrix_matrix_mult_def rowvector_def columnvector_def dot_def)
1.4839 +  by (simp add: Cart_lambda_beta)
1.4840 +
1.4841 +lemma dot_matrix_vector_mul:
1.4842 +  fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
1.4843 +  shows "(A *v x) \<bullet> (B *v y) =
1.4844 +      (((rowvector x :: real^'n^1) ** ((transp A ** B) ** (columnvector y :: real ^1^'n)))\$1)\$1"
1.4845 +unfolding dot_matrix_product transp_columnvector[symmetric]
1.4846 +  dot_rowvector_columnvector matrix_transp_mul matrix_mul_assoc ..
1.4847 +
1.4848 +(* Infinity norm.                                                            *)
1.4849 +
1.4850 +definition "infnorm (x::real^'n) = rsup {abs(x\$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
1.4851 +
1.4852 +lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.4853 +  using dimindex_ge_1 by auto
1.4854 +
1.4855 +lemma infnorm_set_image:
1.4856 +  "{abs(x\$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} =
1.4857 +  (\<lambda>i. abs(x\$i)) ` {1 .. dimindex(UNIV :: 'n set)}" by blast
1.4858 +
1.4859 +lemma infnorm_set_lemma:
1.4860 +  shows "finite {abs((x::'a::abs ^'n)\$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
1.4861 +  and "{abs(x\$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} \<noteq> {}"
1.4862 +  unfolding infnorm_set_image
1.4863 +  using dimindex_ge_1[of "UNIV :: 'n set"]
1.4864 +  by (auto intro: finite_imageI)
1.4865 +
1.4866 +lemma infnorm_pos_le: "0 \<le> infnorm x"
1.4867 +  unfolding infnorm_def
1.4868 +  unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
1.4869 +  unfolding infnorm_set_image
1.4870 +  using dimindex_ge_1
1.4871 +  by auto
1.4872 +
1.4873 +lemma infnorm_triangle: "infnorm ((x::real^'n) + y) \<le> infnorm x + infnorm y"
1.4874 +proof-
1.4875 +  have th: "\<And>x y (z::real). x - y <= z \<longleftrightarrow> x - z <= y" by arith
1.4876 +  have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
1.4877 +  have th2: "\<And>x (y::real). abs(x + y) - abs(x) <= abs(y)" by arith
1.4878 +  show ?thesis
1.4879 +  unfolding infnorm_def
1.4880 +  unfolding rsup_finite_le_iff[ OF infnorm_set_lemma]
1.4881 +  apply (subst diff_le_eq[symmetric])
1.4882 +  unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
1.4883 +  unfolding infnorm_set_image bex_simps
1.4884 +  apply (subst th)
1.4885 +  unfolding th1
1.4886 +  unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
1.4887 +
1.4888 +  unfolding infnorm_set_image ball_simps bex_simps
1.4890 +  apply (metis numseg_dimindex_nonempty th2)
1.4891 +  done
1.4892 +qed
1.4893 +
1.4894 +lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::real ^'n) = 0"
1.4895 +proof-
1.4896 +  have "infnorm x <= 0 \<longleftrightarrow> x = 0"
1.4897 +    unfolding infnorm_def
1.4898 +    unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
1.4899 +    unfolding infnorm_set_image ball_simps
1.4900 +    by vector
1.4901 +  then show ?thesis using infnorm_pos_le[of x] by simp
1.4902 +qed
1.4903 +
1.4904 +lemma infnorm_0: "infnorm 0 = 0"
1.4905 +  by (simp add: infnorm_eq_0)
1.4906 +
1.4907 +lemma infnorm_neg: "infnorm (- x) = infnorm x"
1.4908 +  unfolding infnorm_def
1.4909 +  apply (rule cong[of "rsup" "rsup"])
1.4910 +  apply blast
1.4911 +  apply (rule set_ext)
1.4912 +  apply (auto simp add: vector_component abs_minus_cancel)
1.4913 +  apply (rule_tac x="i" in exI)
1.4914 +  apply (simp add: vector_component)
1.4915 +  done
1.4916 +
1.4917 +lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
1.4918 +proof-
1.4919 +  have "y - x = - (x - y)" by simp
1.4920 +  then show ?thesis  by (metis infnorm_neg)
1.4921 +qed
1.4922 +
1.4923 +lemma real_abs_sub_infnorm: "\<bar> infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
1.4924 +proof-
1.4925 +  have th: "\<And>(nx::real) n ny. nx <= n + ny \<Longrightarrow> ny <= n + nx ==> \<bar>nx - ny\<bar> <= n"
1.4926 +    by arith
1.4927 +  from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
1.4928 +  have ths: "infnorm x \<le> infnorm (x - y) + infnorm y"
1.4929 +    "infnorm y \<le> infnorm (x - y) + infnorm x"
1.4930 +    by (simp_all add: ring_simps infnorm_neg diff_def[symmetric])
1.4931 +  from th[OF ths]  show ?thesis .
1.4932 +qed
1.4933 +
1.4934 +lemma real_abs_infnorm: " \<bar>infnorm x\<bar> = infnorm x"
1.4935 +  using infnorm_pos_le[of x] by arith
1.4936 +
1.4937 +lemma component_le_infnorm: assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
1.4938 +  shows "\<bar>x\$i\<bar> \<le> infnorm (x::real^'n)"
1.4939 +proof-
1.4940 +  let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
1.4941 +  let ?S = "{\<bar>x\$i\<bar> |i. i\<in> ?U}"
1.4942 +  have fS: "finite ?S" unfolding image_Collect[symmetric]
1.4943 +    apply (rule finite_imageI) unfolding Collect_def mem_def by simp
1.4944 +  have S0: "?S \<noteq> {}" using numseg_dimindex_nonempty by blast
1.4945 +  have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
1.4946 +  from rsup_finite_in[OF fS S0] rsup_finite_Ub[OF fS S0] i
1.4947 +  show ?thesis unfolding infnorm_def isUb_def setle_def
1.4948 +    unfolding infnorm_set_image ball_simps by auto
1.4949 +qed
1.4950 +
1.4951 +lemma infnorm_mul_lemma: "infnorm(a *s x) <= \<bar>a\<bar> * infnorm x"
1.4952 +  apply (subst infnorm_def)
1.4953 +  unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
1.4954 +  unfolding infnorm_set_image ball_simps
1.4955 +  apply (simp add: abs_mult vector_component del: One_nat_def)
1.4956 +  apply (rule ballI)
1.4957 +  apply (drule component_le_infnorm[of _ x])
1.4958 +  apply (rule mult_mono)
1.4959 +  apply auto
1.4960 +  done
1.4961 +
1.4962 +lemma infnorm_mul: "infnorm(a *s x) = abs a * infnorm x"
1.4963 +proof-
1.4964 +  {assume a0: "a = 0" hence ?thesis by (simp add: infnorm_0) }
1.4965 +  moreover
1.4966 +  {assume a0: "a \<noteq> 0"
1.4967 +    from a0 have th: "(1/a) *s (a *s x) = x"
1.4968 +      by (simp add: vector_smult_assoc)
1.4969 +    from a0 have ap: "\<bar>a\<bar> > 0" by arith
1.4970 +    from infnorm_mul_lemma[of "1/a" "a *s x"]
1.4971 +    have "infnorm x \<le> 1/\<bar>a\<bar> * infnorm (a*s x)"
1.4972 +      unfolding th by simp
1.4973 +    with ap have "\<bar>a\<bar> * infnorm x \<le> \<bar>a\<bar> * (1/\<bar>a\<bar> * infnorm (a *s x))" by (simp add: field_simps)
1.4974 +    then have "\<bar>a\<bar> * infnorm x \<le> infnorm (a*s x)"
1.4975 +      using ap by (simp add: field_simps)
1.4976 +    with infnorm_mul_lemma[of a x] have ?thesis by arith }
1.4977 +  ultimately show ?thesis by blast
1.4978 +qed
1.4979 +
1.4980 +lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
1.4981 +  using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
1.4982 +
1.4983 +(* Prove that it differs only up to a bound from Euclidean norm.             *)
1.4984 +
1.4985 +lemma infnorm_le_norm: "infnorm x \<le> norm x"
1.4986 +  unfolding infnorm_def rsup_finite_le_iff[OF infnorm_set_lemma]
1.4987 +  unfolding infnorm_set_image  ball_simps
1.4988 +  by (metis component_le_norm)
1.4989 +lemma card_enum: "card {1 .. n} = n" by auto
1.4990 +lemma norm_le_infnorm: "norm(x) <= sqrt(real (dimindex(UNIV ::'n set))) * infnorm(x::real ^'n)"
1.4991 +proof-
1.4992 +  let ?d = "dimindex(UNIV ::'n set)"
1.4993 +  have d: "?d = card {1 .. ?d}" by auto
1.4994 +  have "real ?d \<ge> 0" by simp
1.4995 +  hence d2: "(sqrt (real ?d))^2 = real ?d"
1.4996 +    by (auto intro: real_sqrt_pow2)
1.4997 +  have th: "sqrt (real ?d) * infnorm x \<ge> 0"
1.4998 +    by (simp add: dimindex_ge_1 zero_le_mult_iff real_sqrt_ge_0_iff infnorm_pos_le)
1.4999 +  have th1: "x\<bullet>x \<le> (sqrt (real ?d) * infnorm x)^2"
1.5000 +    unfolding power_mult_distrib d2
1.5001 +    apply (subst d)
1.5002 +    apply (subst power2_abs[symmetric])
1.5003 +    unfolding real_of_nat_def dot_def power2_eq_square[symmetric]
1.5004 +    apply (subst power2_abs[symmetric])
1.5005 +    apply (rule setsum_bounded)
1.5006 +    apply (rule power_mono)
1.5007 +    unfolding abs_of_nonneg[OF infnorm_pos_le]
1.5008 +    unfolding infnorm_def  rsup_finite_ge_iff[OF infnorm_set_lemma]
1.5009 +    unfolding infnorm_set_image bex_simps
1.5010 +    apply blast
1.5011 +    by (rule abs_ge_zero)
1.5012 +  from real_le_lsqrt[OF dot_pos_le th th1]
1.5013 +  show ?thesis unfolding real_vector_norm_def  real_of_real_def id_def .
1.5014 +qed
1.5015 +
1.5016 +(* Equality in Cauchy-Schwarz and triangle inequalities.                     *)
1.5017 +
1.5018 +lemma norm_cauchy_schwarz_eq: "(x::real ^'n) \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *s y = norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
1.5019 +proof-
1.5020 +  {assume h: "x = 0"
1.5021 +    hence ?thesis by (simp add: norm_0)}
1.5022 +  moreover
1.5023 +  {assume h: "y = 0"
1.5024 +    hence ?thesis by (simp add: norm_0)}
1.5025 +  moreover
1.5026 +  {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
1.5027 +    from dot_eq_0[of "norm y *s x - norm x *s y"]
1.5028 +    have "?rhs \<longleftrightarrow> (norm y * (norm y * norm x * norm x - norm x * (x \<bullet> y)) - norm x * (norm y * (y \<bullet> x) - norm x * norm y * norm y) =  0)"
1.5029 +      using x y
1.5030 +      unfolding dot_rsub dot_lsub dot_lmult dot_rmult
1.5031 +      unfolding norm_pow_2[symmetric] power2_eq_square diff_eq_0_iff_eq apply (simp add: dot_sym)
1.5032 +      apply (simp add: ring_simps)
1.5033 +      apply metis
1.5034 +      done
1.5035 +    also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" using x y
1.5036 +      by (simp add: ring_simps dot_sym)
1.5037 +    also have "\<dots> \<longleftrightarrow> ?lhs" using x y
1.5038 +      apply (simp add: norm_eq_0)
1.5039 +      by metis
1.5040 +    finally have ?thesis by blast}
1.5041 +  ultimately show ?thesis by blast
1.5042 +qed
1.5043 +
1.5044 +lemma norm_cauchy_schwarz_abs_eq: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
1.5045 +                norm x *s y = norm y *s x \<or> norm(x) *s y = - norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
1.5046 +proof-
1.5047 +  have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a" by arith
1.5048 +  have "?rhs \<longleftrightarrow> norm x *s y = norm y *s x \<or> norm (- x) *s y = norm y *s (- x)"
1.5049 +    apply (simp add: norm_neg) by vector
1.5050 +  also have "\<dots> \<longleftrightarrow>(x \<bullet> y = norm x * norm y \<or>
1.5051 +     (-x) \<bullet> y = norm x * norm y)"
1.5052 +    unfolding norm_cauchy_schwarz_eq[symmetric]
1.5053 +    unfolding norm_neg
1.5054 +      norm_mul by blast
1.5055 +  also have "\<dots> \<longleftrightarrow> ?lhs"
1.5056 +    unfolding th[OF mult_nonneg_nonneg, OF norm_pos_le[of x] norm_pos_le[of y]] dot_lneg
1.5057 +    by arith
1.5058 +  finally show ?thesis ..
1.5059 +qed
1.5060 +
1.5061 +lemma norm_triangle_eq: "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *s y = norm y *s x"
1.5062 +proof-
1.5063 +  {assume x: "x =0 \<or> y =0"
1.5064 +    hence ?thesis by (cases "x=0", simp_all add: norm_0)}
1.5065 +  moreover
1.5066 +  {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
1.5067 +    hence "norm x \<noteq> 0" "norm y \<noteq> 0"
1.5068 +      by (simp_all add: norm_eq_0)
1.5069 +    hence n: "norm x > 0" "norm y > 0"
1.5070 +      using norm_pos_le[of x] norm_pos_le[of y]
1.5071 +      by arith+
1.5072 +    have th: "\<And>(a::real) b c. a + b + c \<noteq> 0 ==> (a = b + c \<longleftrightarrow> a^2 = (b + c)^2)" by algebra
1.5073 +    have "norm(x + y) = norm x + norm y \<longleftrightarrow> norm(x + y)^ 2 = (norm x + norm y) ^2"
1.5074 +      apply (rule th) using n norm_pos_le[of "x + y"]
1.5075 +      by arith
1.5076 +    also have "\<dots> \<longleftrightarrow> norm x *s y = norm y *s x"
1.5077 +      unfolding norm_cauchy_schwarz_eq[symmetric]
1.5079 +      by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym ring_simps)
1.5080 +    finally have ?thesis .}
1.5081 +  ultimately show ?thesis by blast
1.5082 +qed
1.5083 +
1.5084 +(* Collinearity.*)
1.5085 +
1.5086 +definition "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *s u)"
1.5087 +
1.5088 +lemma collinear_empty:  "collinear {}" by (simp add: collinear_def)
1.5089 +
1.5090 +lemma collinear_sing: "collinear {(x::'a::ring_1^'n)}"
1.5091 +  apply (simp add: collinear_def)
1.5092 +  apply (rule exI[where x=0])
1.5093 +  by simp
1.5094 +
1.5095 +lemma collinear_2: "collinear {(x::'a::ring_1^'n),y}"
1.5096 +  apply (simp add: collinear_def)
1.5097 +  apply (rule exI[where x="x - y"])
1.5098 +  apply auto
1.5099 +  apply (rule exI[where x=0], simp)
1.5100 +  apply (rule exI[where x=1], simp)
1.5101 +  apply (rule exI[where x="- 1"], simp add: vector_sneg_minus1[symmetric])
1.5102 +  apply (rule exI[where x=0], simp)
1.5103 +  done
1.5104 +
1.5105 +lemma collinear_lemma: "collinear {(0::real^'n),x,y} \<longleftrightarrow> x = 0 \<or> y = 0 \<or> (\<exists>c. y = c *s x)" (is "?lhs \<longleftrightarrow> ?rhs")
1.5106 +proof-
1.5107 +  {assume "x=0 \<or> y = 0" hence ?thesis
1.5108 +      by (cases "x = 0", simp_all add: collinear_2 insert_commute)}
1.5109 +  moreover
1.5110 +  {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
1.5111 +    {assume h: "?lhs"
1.5112 +      then obtain u where u: "\<forall> x\<in> {0,x,y}. \<forall>y\<in> {0,x,y}. \<exists>c. x - y = c *s u" unfolding collinear_def by blast
1.5113 +      from u[rule_format, of x 0] u[rule_format, of y 0]
1.5114 +      obtain cx and cy where
1.5115 +	cx: "x = cx*s u" and cy: "y = cy*s u"
1.5116 +	by auto
1.5117 +      from cx x have cx0: "cx \<noteq> 0" by auto
1.5118 +      from cy y have cy0: "cy \<noteq> 0" by auto
1.5119 +      let ?d = "cy / cx"
1.5120 +      from cx cy cx0 have "y = ?d *s x"
1.5121 +	by (simp add: vector_smult_assoc)
1.5122 +      hence ?rhs using x y by blast}
1.5123 +    moreover
1.5124 +    {assume h: "?rhs"
1.5125 +      then obtain c where c: "y = c*s x" using x y by blast
1.5126 +      have ?lhs unfolding collinear_def c
1.5127 +	apply (rule exI[where x=x])
1.5128 +	apply auto
1.5129 +	apply (rule exI[where x=0], simp)
1.5130 +	apply (rule exI[where x="- 1"], simp only: vector_smult_lneg vector_smult_lid)
1.5131 +	apply (rule exI[where x= "-c"], simp only: vector_smult_lneg)
1.5132 +	apply (rule exI[where x=1], simp)
1.5133 +	apply (rule exI[where x=0], simp)
1.5134 +	apply (rule exI[where x="1 - c"], simp add: vector_smult_lneg vector_sub_rdistrib)
1.5135 +	apply (rule exI[where x="c - 1"], simp add: vector_smult_lneg vector_sub_rdistrib)
1.5136 +	apply (rule exI[where x=0], simp)
1.5137 +	done}
1.5138 +    ultimately have ?thesis by blast}
1.5139 +  ultimately show ?thesis by blast
1.5140 +qed
1.5141 +
1.5142 +lemma norm_cauchy_schwarz_equal: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {(0::real^'n),x,y}"
1.5143 +unfolding norm_cauchy_schwarz_abs_eq
1.5144 +apply (cases "x=0", simp_all add: collinear_2 norm_0)
1.5145 +apply (cases "y=0", simp_all add: collinear_2 norm_0 insert_commute)
1.5146 +unfolding collinear_lemma
1.5147 +apply simp
1.5148 +apply (subgoal_tac "norm x \<noteq> 0")
1.5149 +apply (subgoal_tac "norm y \<noteq> 0")
1.5150 +apply (rule iffI)
1.5151 +apply (cases "norm x *s y = norm y *s x")
1.5152 +apply (rule exI[where x="(1/norm x) * norm y"])
1.5153 +apply (drule sym)
1.5154 +unfolding vector_smult_assoc[symmetric]
1.5155 +apply (simp add: vector_smult_assoc field_simps)
1.5156 +apply (rule exI[where x="(1/norm x) * - norm y"])
1.5157 +apply clarify
1.5158 +apply (drule sym)
1.5159 +unfolding vector_smult_assoc[symmetric]
1.5160 +apply (simp add: vector_smult_assoc field_simps)
1.5161 +apply (erule exE)
1.5162 +apply (erule ssubst)
1.5163 +unfolding vector_smult_assoc
1.5164 +unfolding norm_mul
1.5165 +apply (subgoal_tac "norm x * c = \<bar>c\<bar> * norm x \<or> norm x * c = - \<bar>c\<bar> * norm x")
1.5166 +apply (case_tac "c <= 0", simp add: ring_simps)