src/HOL/Library/Euclidean_Space.thy
author huffman
Sat Feb 21 11:18:50 2009 -0800 (2009-02-21)
changeset 30041 9becd197a40e
parent 30040 e2cd1acda1ab
child 30045 b8ddd7667eed
permissions -rw-r--r--
remove duplicated lemmas about norm
     1 (* Title:      Library/Euclidean_Space
     2    ID:         $Id: 
     3    Author:     Amine Chaieb, University of Cambridge
     4 *)
     5 
     6 header {* (Real) Vectors in Euclidean space, and elementary linear algebra.*}
     7 
     8 theory Euclidean_Space
     9   imports "~~/src/HOL/Decision_Procs/Dense_Linear_Order" Complex_Main 
    10   Finite_Cartesian_Product Glbs Infinite_Set Numeral_Type
    11   uses ("normarith.ML")
    12 begin
    13 
    14 text{* Some common special cases.*}
    15 
    16 lemma forall_1: "(\<forall>(i::'a::{order,one}). 1 <= i \<and> i <= 1 --> P i) \<longleftrightarrow> P 1"
    17   by (metis order_eq_iff)
    18 lemma forall_dimindex_1: "(\<forall>i \<in> {1..dimindex(UNIV:: 1 set)}. P i) \<longleftrightarrow> P 1"
    19   by (simp add: dimindex_def)
    20 
    21 lemma forall_2: "(\<forall>(i::nat). 1 <= i \<and> i <= 2 --> P i) \<longleftrightarrow> P 1 \<and> P 2"
    22 proof-
    23   have "\<And>i::nat. 1 <= i \<and> i <= 2 \<longleftrightarrow> i = 1 \<or> i = 2" by arith
    24   thus ?thesis by metis
    25 qed
    26 
    27 lemma forall_3: "(\<forall>(i::nat). 1 <= i \<and> i <= 3 --> P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
    28 proof-
    29   have "\<And>i::nat. 1 <= i \<and> i <= 3 \<longleftrightarrow> i = 1 \<or> i = 2 \<or> i = 3" by arith
    30   thus ?thesis by metis
    31 qed
    32 
    33 lemma setsum_singleton[simp]: "setsum f {x} = f x" by simp
    34 lemma setsum_1: "setsum f {(1::'a::{order,one})..1} = f 1" 
    35   by (simp add: atLeastAtMost_singleton)
    36 
    37 lemma setsum_2: "setsum f {1::nat..2} = f 1 + f 2" 
    38   by (simp add: nat_number  atLeastAtMostSuc_conv add_commute)
    39 
    40 lemma setsum_3: "setsum f {1::nat..3} = f 1 + f 2 + f 3" 
    41   by (simp add: nat_number  atLeastAtMostSuc_conv add_commute)
    42 
    43 subsection{* Basic componentwise operations on vectors. *}
    44 
    45 instantiation "^" :: (plus,type) plus
    46 begin
    47 definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) + (y$i)))" 
    48 instance ..
    49 end
    50 
    51 instantiation "^" :: (times,type) times
    52 begin
    53   definition vector_mult_def : "op * \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))" 
    54   instance ..
    55 end
    56 
    57 instantiation "^" :: (minus,type) minus begin
    58   definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) - (y$i)))"
    59 instance ..
    60 end
    61 
    62 instantiation "^" :: (uminus,type) uminus begin
    63   definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x$i)))"
    64 instance ..
    65 end
    66 instantiation "^" :: (zero,type) zero begin
    67   definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)" 
    68 instance ..
    69 end
    70 
    71 instantiation "^" :: (one,type) one begin
    72   definition vector_one_def : "1 \<equiv> (\<chi> i. 1)" 
    73 instance ..
    74 end
    75 
    76 instantiation "^" :: (ord,type) ord
    77  begin
    78 definition vector_less_eq_def:
    79   "less_eq (x :: 'a ^'b) y = (ALL i : {1 .. dimindex (UNIV :: 'b set)}.
    80   x$i <= y$i)"
    81 definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i : {1 ..
    82   dimindex (UNIV :: 'b set)}. x$i < y$i)"
    83  
    84 instance by (intro_classes)
    85 end
    86 
    87 instantiation "^" :: (scaleR, type) scaleR
    88 begin
    89 definition vector_scaleR_def: "scaleR = (\<lambda> r x.  (\<chi> i. scaleR r (x$i)))" 
    90 instance ..
    91 end
    92 
    93 text{* Also the scalar-vector multiplication. *}
    94 
    95 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'n" (infixr "*s" 75)
    96   where "c *s x = (\<chi> i. c * (x$i))"
    97 
    98 text{* Constant Vectors *}
    99 
   100 definition "vec x = (\<chi> i. x)"
   101 
   102 text{* Dot products. *}
   103 
   104 definition dot :: "'a::{comm_monoid_add, times} ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" (infix "\<bullet>" 70) where
   105   "x \<bullet> y = setsum (\<lambda>i. x$i * y$i) {1 .. dimindex (UNIV:: 'n set)}"
   106 lemma dot_1[simp]: "(x::'a::{comm_monoid_add, times}^1) \<bullet> y = (x$1) * (y$1)"
   107   by (simp add: dot_def dimindex_def)
   108 
   109 lemma dot_2[simp]: "(x::'a::{comm_monoid_add, times}^2) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2)"
   110   by (simp add: dot_def dimindex_def nat_number)
   111 
   112 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)"
   113   by (simp add: dot_def dimindex_def nat_number)
   114 
   115 subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
   116 
   117 lemmas Cart_lambda_beta' = Cart_lambda_beta[rule_format]
   118 method_setup vector = {*
   119 let
   120   val ss1 = HOL_basic_ss addsimps [@{thm dot_def}, @{thm setsum_addf} RS sym, 
   121   @{thm setsum_subtractf} RS sym, @{thm setsum_right_distrib}, 
   122   @{thm setsum_left_distrib}, @{thm setsum_negf} RS sym]
   123   val ss2 = @{simpset} addsimps 
   124              [@{thm vector_add_def}, @{thm vector_mult_def},  
   125               @{thm vector_minus_def}, @{thm vector_uminus_def}, 
   126               @{thm vector_one_def}, @{thm vector_zero_def}, @{thm vec_def}, 
   127               @{thm vector_scaleR_def},
   128               @{thm Cart_lambda_beta'}, @{thm vector_scalar_mult_def}]
   129  fun vector_arith_tac ths = 
   130    simp_tac ss1
   131    THEN' (fn i => rtac @{thm setsum_cong2} i
   132          ORELSE rtac @{thm setsum_0'} i 
   133          ORELSE simp_tac (HOL_basic_ss addsimps [@{thm "Cart_eq"}]) i)
   134    (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
   135    THEN' asm_full_simp_tac (ss2 addsimps ths)
   136  in
   137   Method.thms_args (Method.SIMPLE_METHOD' o vector_arith_tac)
   138 end
   139 *} "Lifts trivial vector statements to real arith statements"
   140 
   141 lemma vec_0[simp]: "vec 0 = 0" by (vector vector_zero_def)
   142 lemma vec_1[simp]: "vec 1 = 1" by (vector vector_one_def)
   143 
   144 
   145 
   146 text{* Obvious "component-pushing". *}
   147 
   148 lemma vec_component: " i \<in> {1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (vec x :: 'a ^ 'n)$i = x" 
   149   by (vector vec_def) 
   150 
   151 lemma vector_add_component: 
   152   fixes x y :: "'a::{plus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   153   shows "(x + y)$i = x$i + y$i"
   154   using i by vector
   155 
   156 lemma vector_minus_component: 
   157   fixes x y :: "'a::{minus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   158   shows "(x - y)$i = x$i - y$i"
   159   using i  by vector
   160 
   161 lemma vector_mult_component: 
   162   fixes x y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   163   shows "(x * y)$i = x$i * y$i"
   164   using i by vector
   165 
   166 lemma vector_smult_component: 
   167   fixes y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   168   shows "(c *s y)$i = c * (y$i)"
   169   using i by vector
   170 
   171 lemma vector_uminus_component: 
   172   fixes x :: "'a::{uminus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   173   shows "(- x)$i = - (x$i)"
   174   using i by vector
   175 
   176 lemma vector_scaleR_component:
   177   fixes x :: "'a::scaleR ^ 'n"
   178   assumes i: "i \<in> {1 .. dimindex(UNIV :: 'n set)}"
   179   shows "(scaleR r x)$i = scaleR r (x$i)"
   180   using i by vector
   181 
   182 lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   183 
   184 lemmas vector_component =
   185   vec_component vector_add_component vector_mult_component
   186   vector_smult_component vector_minus_component vector_uminus_component
   187   vector_scaleR_component cond_component
   188 
   189 subsection {* Some frequently useful arithmetic lemmas over vectors. *}
   190 
   191 instance "^" :: (semigroup_add,type) semigroup_add 
   192   apply (intro_classes) by (vector add_assoc)
   193 
   194 
   195 instance "^" :: (monoid_add,type) monoid_add 
   196   apply (intro_classes) by vector+ 
   197 
   198 instance "^" :: (group_add,type) group_add 
   199   apply (intro_classes) by (vector algebra_simps)+ 
   200 
   201 instance "^" :: (ab_semigroup_add,type) ab_semigroup_add 
   202   apply (intro_classes) by (vector add_commute)
   203 
   204 instance "^" :: (comm_monoid_add,type) comm_monoid_add
   205   apply (intro_classes) by vector
   206 
   207 instance "^" :: (ab_group_add,type) ab_group_add 
   208   apply (intro_classes) by vector+
   209 
   210 instance "^" :: (cancel_semigroup_add,type) cancel_semigroup_add 
   211   apply (intro_classes)
   212   by (vector Cart_eq)+
   213 
   214 instance "^" :: (cancel_ab_semigroup_add,type) cancel_ab_semigroup_add
   215   apply (intro_classes)
   216   by (vector Cart_eq)
   217 
   218 instance "^" :: (real_vector, type) real_vector
   219   by default (vector scaleR_left_distrib scaleR_right_distrib)+
   220 
   221 instance "^" :: (semigroup_mult,type) semigroup_mult 
   222   apply (intro_classes) by (vector mult_assoc)
   223 
   224 instance "^" :: (monoid_mult,type) monoid_mult 
   225   apply (intro_classes) by vector+
   226 
   227 instance "^" :: (ab_semigroup_mult,type) ab_semigroup_mult 
   228   apply (intro_classes) by (vector mult_commute)
   229 
   230 instance "^" :: (ab_semigroup_idem_mult,type) ab_semigroup_idem_mult 
   231   apply (intro_classes) by (vector mult_idem)
   232 
   233 instance "^" :: (comm_monoid_mult,type) comm_monoid_mult 
   234   apply (intro_classes) by vector
   235 
   236 fun vector_power :: "('a::{one,times} ^'n) \<Rightarrow> nat \<Rightarrow> 'a^'n" where
   237   "vector_power x 0 = 1"
   238   | "vector_power x (Suc n) = x * vector_power x n"
   239 
   240 instantiation "^" :: (recpower,type) recpower 
   241 begin
   242   definition vec_power_def: "op ^ \<equiv> vector_power"
   243   instance 
   244   apply (intro_classes) by (simp_all add: vec_power_def) 
   245 end
   246 
   247 instance "^" :: (semiring,type) semiring
   248   apply (intro_classes) by (vector ring_simps)+
   249 
   250 instance "^" :: (semiring_0,type) semiring_0
   251   apply (intro_classes) by (vector ring_simps)+
   252 instance "^" :: (semiring_1,type) semiring_1
   253   apply (intro_classes) apply vector using dimindex_ge_1 by auto 
   254 instance "^" :: (comm_semiring,type) comm_semiring
   255   apply (intro_classes) by (vector ring_simps)+
   256 
   257 instance "^" :: (comm_semiring_0,type) comm_semiring_0 by (intro_classes) 
   258 instance "^" :: (cancel_comm_monoid_add, type) cancel_comm_monoid_add ..
   259 instance "^" :: (semiring_0_cancel,type) semiring_0_cancel by (intro_classes) 
   260 instance "^" :: (comm_semiring_0_cancel,type) comm_semiring_0_cancel by (intro_classes) 
   261 instance "^" :: (ring,type) ring by (intro_classes) 
   262 instance "^" :: (semiring_1_cancel,type) semiring_1_cancel by (intro_classes) 
   263 instance "^" :: (comm_semiring_1,type) comm_semiring_1 by (intro_classes)
   264 
   265 instance "^" :: (ring_1,type) ring_1 ..
   266 
   267 instance "^" :: (real_algebra,type) real_algebra
   268   apply intro_classes
   269   apply (simp_all add: vector_scaleR_def ring_simps)
   270   apply vector
   271   apply vector
   272   done
   273 
   274 instance "^" :: (real_algebra_1,type) real_algebra_1 ..
   275 
   276 lemma of_nat_index: 
   277   "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   278   apply (induct n)
   279   apply vector
   280   apply vector
   281   done
   282 lemma zero_index[simp]: 
   283   "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (0 :: 'a::zero ^'n)$i = 0" by vector
   284 
   285 lemma one_index[simp]: 
   286   "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (1 :: 'a::one ^'n)$i = 1" by vector
   287 
   288 lemma one_plus_of_nat_neq_0: "(1::'a::semiring_char_0) + of_nat n \<noteq> 0"
   289 proof-
   290   have "(1::'a) + of_nat n = 0 \<longleftrightarrow> of_nat 1 + of_nat n = (of_nat 0 :: 'a)" by simp
   291   also have "\<dots> \<longleftrightarrow> 1 + n = 0" by (simp only: of_nat_add[symmetric] of_nat_eq_iff) 
   292   finally show ?thesis by simp 
   293 qed
   294 
   295 instance "^" :: (semiring_char_0,type) semiring_char_0 
   296 proof (intro_classes) 
   297   fix m n ::nat
   298   show "(of_nat m :: 'a^'b) = of_nat n \<longleftrightarrow> m = n"
   299   proof(induct m arbitrary: n)
   300     case 0 thus ?case apply vector 
   301       apply (induct n,auto simp add: ring_simps)
   302       using dimindex_ge_1 apply auto
   303       apply vector
   304       by (auto simp add: of_nat_index one_plus_of_nat_neq_0)
   305   next
   306     case (Suc n m)
   307     thus ?case  apply vector
   308       apply (induct m, auto simp add: ring_simps of_nat_index zero_index)
   309       using dimindex_ge_1 apply simp apply blast
   310       apply (simp add: one_plus_of_nat_neq_0)
   311       using dimindex_ge_1 apply simp apply blast
   312       apply (simp add: vector_component one_index of_nat_index)
   313       apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
   314       using  dimindex_ge_1 apply simp apply blast
   315       apply (simp add: vector_component one_index of_nat_index)
   316       apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
   317       using dimindex_ge_1 apply simp apply blast
   318       apply (simp add: vector_component one_index of_nat_index)
   319       done
   320   qed
   321 qed
   322 
   323 instance "^" :: (comm_ring_1,type) comm_ring_1 by intro_classes
   324 instance "^" :: (ring_char_0,type) ring_char_0 by intro_classes
   325 
   326 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"  
   327   by (vector mult_assoc)
   328 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x" 
   329   by (vector ring_simps)
   330 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y" 
   331   by (vector ring_simps)
   332 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
   333 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
   334 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y" 
   335   by (vector ring_simps)
   336 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   337 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   338 lemma vector_sneg_minus1: "-x = (- (1::'a::ring_1)) *s x" by vector
   339 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   340 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x" 
   341   by (vector ring_simps)
   342 
   343 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)" 
   344   apply (auto simp add: vec_def Cart_eq vec_component Cart_lambda_beta )
   345   using dimindex_ge_1 apply auto done
   346 
   347 subsection {* Square root of sum of squares *}
   348 
   349 definition
   350   "setL2 f A = sqrt (\<Sum>i\<in>A. (f i)\<twosuperior>)"
   351 
   352 lemma setL2_cong:
   353   "\<lbrakk>A = B; \<And>x. x \<in> B \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
   354   unfolding setL2_def by simp
   355 
   356 lemma strong_setL2_cong:
   357   "\<lbrakk>A = B; \<And>x. x \<in> B =simp=> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
   358   unfolding setL2_def simp_implies_def by simp
   359 
   360 lemma setL2_infinite [simp]: "\<not> finite A \<Longrightarrow> setL2 f A = 0"
   361   unfolding setL2_def by simp
   362 
   363 lemma setL2_empty [simp]: "setL2 f {} = 0"
   364   unfolding setL2_def by simp
   365 
   366 lemma setL2_insert [simp]:
   367   "\<lbrakk>finite F; a \<notin> F\<rbrakk> \<Longrightarrow>
   368     setL2 f (insert a F) = sqrt ((f a)\<twosuperior> + (setL2 f F)\<twosuperior>)"
   369   unfolding setL2_def by (simp add: setsum_nonneg)
   370 
   371 lemma setL2_nonneg [simp]: "0 \<le> setL2 f A"
   372   unfolding setL2_def by (simp add: setsum_nonneg)
   373 
   374 lemma setL2_0': "\<forall>a\<in>A. f a = 0 \<Longrightarrow> setL2 f A = 0"
   375   unfolding setL2_def by simp
   376 
   377 lemma setL2_mono:
   378   assumes "\<And>i. i \<in> K \<Longrightarrow> f i \<le> g i"
   379   assumes "\<And>i. i \<in> K \<Longrightarrow> 0 \<le> f i"
   380   shows "setL2 f K \<le> setL2 g K"
   381   unfolding setL2_def
   382   by (simp add: setsum_nonneg setsum_mono power_mono prems)
   383 
   384 lemma setL2_right_distrib:
   385   "0 \<le> r \<Longrightarrow> r * setL2 f A = setL2 (\<lambda>x. r * f x) A"
   386   unfolding setL2_def
   387   apply (simp add: power_mult_distrib)
   388   apply (simp add: setsum_right_distrib [symmetric])
   389   apply (simp add: real_sqrt_mult setsum_nonneg)
   390   done
   391 
   392 lemma setL2_left_distrib:
   393   "0 \<le> r \<Longrightarrow> setL2 f A * r = setL2 (\<lambda>x. f x * r) A"
   394   unfolding setL2_def
   395   apply (simp add: power_mult_distrib)
   396   apply (simp add: setsum_left_distrib [symmetric])
   397   apply (simp add: real_sqrt_mult setsum_nonneg)
   398   done
   399 
   400 lemma setsum_nonneg_eq_0_iff:
   401   fixes f :: "'a \<Rightarrow> 'b::pordered_ab_group_add"
   402   shows "\<lbrakk>finite A; \<forall>x\<in>A. 0 \<le> f x\<rbrakk> \<Longrightarrow> setsum f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
   403   apply (induct set: finite, simp)
   404   apply (simp add: add_nonneg_eq_0_iff setsum_nonneg)
   405   done
   406 
   407 lemma setL2_eq_0_iff: "finite A \<Longrightarrow> setL2 f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
   408   unfolding setL2_def
   409   by (simp add: setsum_nonneg setsum_nonneg_eq_0_iff)
   410 
   411 lemma setL2_triangle_ineq:
   412   shows "setL2 (\<lambda>i. f i + g i) A \<le> setL2 f A + setL2 g A"
   413 proof (cases "finite A")
   414   case False
   415   thus ?thesis by simp
   416 next
   417   case True
   418   thus ?thesis
   419   proof (induct set: finite)
   420     case empty
   421     show ?case by simp
   422   next
   423     case (insert x F)
   424     hence "sqrt ((f x + g x)\<twosuperior> + (setL2 (\<lambda>i. f i + g i) F)\<twosuperior>) \<le>
   425            sqrt ((f x + g x)\<twosuperior> + (setL2 f F + setL2 g F)\<twosuperior>)"
   426       by (intro real_sqrt_le_mono add_left_mono power_mono insert
   427                 setL2_nonneg add_increasing zero_le_power2)
   428     also have
   429       "\<dots> \<le> sqrt ((f x)\<twosuperior> + (setL2 f F)\<twosuperior>) + sqrt ((g x)\<twosuperior> + (setL2 g F)\<twosuperior>)"
   430       by (rule real_sqrt_sum_squares_triangle_ineq)
   431     finally show ?case
   432       using insert by simp
   433   qed
   434 qed
   435 
   436 lemma sqrt_sum_squares_le_sum:
   437   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (x\<twosuperior> + y\<twosuperior>) \<le> x + y"
   438   apply (rule power2_le_imp_le)
   439   apply (simp add: power2_sum)
   440   apply (simp add: mult_nonneg_nonneg)
   441   apply (simp add: add_nonneg_nonneg)
   442   done
   443 
   444 lemma setL2_le_setsum [rule_format]:
   445   "(\<forall>i\<in>A. 0 \<le> f i) \<longrightarrow> setL2 f A \<le> setsum f A"
   446   apply (cases "finite A")
   447   apply (induct set: finite)
   448   apply simp
   449   apply clarsimp
   450   apply (erule order_trans [OF sqrt_sum_squares_le_sum])
   451   apply simp
   452   apply simp
   453   apply simp
   454   done
   455 
   456 lemma sqrt_sum_squares_le_sum_abs: "sqrt (x\<twosuperior> + y\<twosuperior>) \<le> \<bar>x\<bar> + \<bar>y\<bar>"
   457   apply (rule power2_le_imp_le)
   458   apply (simp add: power2_sum)
   459   apply (simp add: mult_nonneg_nonneg)
   460   apply (simp add: add_nonneg_nonneg)
   461   done
   462 
   463 lemma setL2_le_setsum_abs: "setL2 f A \<le> (\<Sum>i\<in>A. \<bar>f i\<bar>)"
   464   apply (cases "finite A")
   465   apply (induct set: finite)
   466   apply simp
   467   apply simp
   468   apply (rule order_trans [OF sqrt_sum_squares_le_sum_abs])
   469   apply simp
   470   apply simp
   471   done
   472 
   473 lemma setL2_mult_ineq_lemma:
   474   fixes a b c d :: real
   475   shows "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
   476 proof -
   477   have "0 \<le> (a * d - b * c)\<twosuperior>" by simp
   478   also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * d) * (b * c)"
   479     by (simp only: power2_diff power_mult_distrib)
   480   also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * c) * (b * d)"
   481     by simp
   482   finally show "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
   483     by simp
   484 qed
   485 
   486 lemma setL2_mult_ineq: "(\<Sum>i\<in>A. \<bar>f i\<bar> * \<bar>g i\<bar>) \<le> setL2 f A * setL2 g A"
   487   apply (cases "finite A")
   488   apply (induct set: finite)
   489   apply simp
   490   apply (rule power2_le_imp_le, simp)
   491   apply (rule order_trans)
   492   apply (rule power_mono)
   493   apply (erule add_left_mono)
   494   apply (simp add: add_nonneg_nonneg mult_nonneg_nonneg setsum_nonneg)
   495   apply (simp add: power2_sum)
   496   apply (simp add: power_mult_distrib)
   497   apply (simp add: right_distrib left_distrib)
   498   apply (rule ord_le_eq_trans)
   499   apply (rule setL2_mult_ineq_lemma)
   500   apply simp
   501   apply (intro mult_nonneg_nonneg setL2_nonneg)
   502   apply simp
   503   done
   504 
   505 lemma member_le_setL2: "\<lbrakk>finite A; i \<in> A\<rbrakk> \<Longrightarrow> f i \<le> setL2 f A"
   506   apply (rule_tac s="insert i (A - {i})" and t="A" in subst)
   507   apply fast
   508   apply (subst setL2_insert)
   509   apply simp
   510   apply simp
   511   apply simp
   512   done
   513 
   514 subsection {* Norms *}
   515 
   516 instantiation "^" :: (real_normed_vector, type) real_normed_vector
   517 begin
   518 
   519 definition vector_norm_def:
   520   "norm (x::'a^'b) = setL2 (\<lambda>i. norm (x$i)) {1 .. dimindex (UNIV:: 'b set)}"
   521 
   522 definition vector_sgn_def:
   523   "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   524 
   525 instance proof
   526   fix a :: real and x y :: "'a ^ 'b"
   527   show "0 \<le> norm x"
   528     unfolding vector_norm_def
   529     by (rule setL2_nonneg)
   530   show "norm x = 0 \<longleftrightarrow> x = 0"
   531     unfolding vector_norm_def
   532     by (simp add: setL2_eq_0_iff Cart_eq)
   533   show "norm (x + y) \<le> norm x + norm y"
   534     unfolding vector_norm_def
   535     apply (rule order_trans [OF _ setL2_triangle_ineq])
   536     apply (rule setL2_mono)
   537     apply (simp add: vector_component norm_triangle_ineq)
   538     apply simp
   539     done
   540   show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   541     unfolding vector_norm_def
   542     by (simp add: vector_component norm_scaleR setL2_right_distrib
   543              cong: strong_setL2_cong)
   544   show "sgn x = scaleR (inverse (norm x)) x"
   545     by (rule vector_sgn_def)
   546 qed
   547 
   548 end
   549 
   550 subsection{* Properties of the dot product.  *}
   551 
   552 lemma dot_sym: "(x::'a:: {comm_monoid_add, ab_semigroup_mult} ^ 'n) \<bullet> y = y \<bullet> x" 
   553   by (vector mult_commute)
   554 lemma dot_ladd: "((x::'a::ring ^ 'n) + y) \<bullet> z = (x \<bullet> z) + (y \<bullet> z)"
   555   by (vector ring_simps)
   556 lemma dot_radd: "x \<bullet> (y + (z::'a::ring ^ 'n)) = (x \<bullet> y) + (x \<bullet> z)" 
   557   by (vector ring_simps)
   558 lemma dot_lsub: "((x::'a::ring ^ 'n) - y) \<bullet> z = (x \<bullet> z) - (y \<bullet> z)" 
   559   by (vector ring_simps)
   560 lemma dot_rsub: "(x::'a::ring ^ 'n) \<bullet> (y - z) = (x \<bullet> y) - (x \<bullet> z)" 
   561   by (vector ring_simps)
   562 lemma dot_lmult: "(c *s x) \<bullet> y = (c::'a::ring) * (x \<bullet> y)" by (vector ring_simps)
   563 lemma dot_rmult: "x \<bullet> (c *s y) = (c::'a::comm_ring) * (x \<bullet> y)" by (vector ring_simps)
   564 lemma dot_lneg: "(-x) \<bullet> (y::'a::ring ^ 'n) = -(x \<bullet> y)" by vector
   565 lemma dot_rneg: "(x::'a::ring ^ 'n) \<bullet> (-y) = -(x \<bullet> y)" by vector
   566 lemma dot_lzero[simp]: "0 \<bullet> x = (0::'a::{comm_monoid_add, mult_zero})" by vector
   567 lemma dot_rzero[simp]: "x \<bullet> 0 = (0::'a::{comm_monoid_add, mult_zero})" by vector
   568 lemma dot_pos_le[simp]: "(0::'a\<Colon>ordered_ring_strict) <= x \<bullet> x"
   569   by (simp add: dot_def setsum_nonneg)
   570 
   571 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)"
   572 using fS fp setsum_nonneg[OF fp]
   573 proof (induct set: finite)
   574   case empty thus ?case by simp
   575 next
   576   case (insert x F)
   577   from insert.prems have Fx: "f x \<ge> 0" and Fp: "\<forall> a \<in> F. f a \<ge> 0" by simp_all
   578   from insert.hyps Fp setsum_nonneg[OF Fp]
   579   have h: "setsum f F = 0 \<longleftrightarrow> (\<forall>a \<in>F. f a = 0)" by metis
   580   from sum_nonneg_eq_zero_iff[OF Fx  setsum_nonneg[OF Fp]] insert.hyps(1,2)
   581   show ?case by (simp add: h)
   582 qed
   583 
   584 lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n) = 0"
   585 proof-
   586   {assume f: "finite (UNIV :: 'n set)"
   587     let ?S = "{Suc 0 .. card (UNIV :: 'n set)}"
   588     have fS: "finite ?S" using f by simp
   589     have fp: "\<forall> i\<in> ?S. x$i * x$i>= 0" by simp
   590     have ?thesis by (vector dimindex_def f setsum_squares_eq_0_iff[OF fS fp])}
   591   moreover
   592   {assume "\<not> finite (UNIV :: 'n set)" then have ?thesis by (vector dimindex_def)}
   593   ultimately show ?thesis by metis
   594 qed
   595 
   596 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] 
   597   by (auto simp add: le_less) 
   598 
   599 subsection{* The collapse of the general concepts to dimension one. *}
   600 
   601 lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
   602   by (vector dimindex_def)
   603 
   604 lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
   605   apply auto
   606   apply (erule_tac x= "x$1" in allE)
   607   apply (simp only: vector_one[symmetric])
   608   done
   609 
   610 lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
   611   by (simp add: vector_norm_def dimindex_def)
   612 
   613 lemma norm_real: "norm(x::real ^ 1) = abs(x$1)" 
   614   by (simp add: norm_vector_1)
   615 
   616 text{* Metric *}
   617 
   618 text {* FIXME: generalize to arbitrary @{text real_normed_vector} types *}
   619 definition dist:: "real ^ 'n \<Rightarrow> real ^ 'n \<Rightarrow> real" where 
   620   "dist x y = norm (x - y)"
   621 
   622 lemma dist_real: "dist(x::real ^ 1) y = abs((x$1) - (y$1))"
   623   using dimindex_ge_1[of "UNIV :: 1 set"]
   624   by (auto simp add: norm_real dist_def vector_component Cart_lambda_beta[where ?'a = "1"] )
   625 
   626 subsection {* A connectedness or intermediate value lemma with several applications. *}
   627 
   628 lemma connected_real_lemma:
   629   fixes f :: "real \<Rightarrow> real ^ 'n"
   630   assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
   631   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"
   632   and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
   633   and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
   634   and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
   635   shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
   636 proof-
   637   let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
   638   have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa) 
   639   have Sub: "\<exists>y. isUb UNIV ?S y" 
   640     apply (rule exI[where x= b])
   641     using ab fb e12 by (auto simp add: isUb_def setle_def)  
   642   from reals_complete[OF Se Sub] obtain l where 
   643     l: "isLub UNIV ?S l"by blast
   644   have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
   645     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)    
   646     by (metis linorder_linear)
   647   have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
   648     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
   649     by (metis linorder_linear not_le)
   650     have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
   651     have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
   652     have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by dlo
   653     {assume le2: "f l \<in> e2"
   654       from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
   655       hence lap: "l - a > 0" using alb by arith
   656       from e2[rule_format, OF le2] obtain e where 
   657 	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
   658       from dst[OF alb e(1)] obtain d where 
   659 	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
   660       have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" using lap d(1) 
   661 	apply ferrack by arith
   662       then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
   663       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
   664       from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
   665       moreover
   666       have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
   667       ultimately have False using e12 alb d' by auto}
   668     moreover
   669     {assume le1: "f l \<in> e1"
   670     from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
   671       hence blp: "b - l > 0" using alb by arith
   672       from e1[rule_format, OF le1] obtain e where 
   673 	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
   674       from dst[OF alb e(1)] obtain d where 
   675 	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
   676       have "\<exists>d'. d' < d \<and> d' >0" using d(1) by dlo 
   677       then obtain d' where d': "d' > 0" "d' < d" by metis
   678       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
   679       hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
   680       with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
   681       with l d' have False 
   682 	by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
   683     ultimately show ?thesis using alb by metis
   684 qed
   685 
   686 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 @{typ "real^1"} *}
   687 
   688 lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
   689 proof-
   690   have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith 
   691   thus ?thesis by (simp add: ring_simps power2_eq_square)
   692 qed
   693 
   694 lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
   695   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)
   696   apply (rule_tac x="s" in exI)
   697   apply auto
   698   apply (erule_tac x=y in allE)
   699   apply auto
   700   done
   701 
   702 lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
   703   using real_sqrt_le_iff[of x "y^2"] by simp
   704 
   705 lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
   706   using real_sqrt_le_mono[of "x^2" y] by simp
   707 
   708 lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
   709   using real_sqrt_less_mono[of "x^2" y] by simp
   710 
   711 lemma sqrt_even_pow2: assumes n: "even n" 
   712   shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
   713 proof-
   714   from n obtain m where m: "n = 2*m" unfolding even_nat_equiv_def2 
   715     by (auto simp add: nat_number) 
   716   from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
   717     by (simp only: power_mult[symmetric] mult_commute)
   718   then show ?thesis  using m by simp 
   719 qed
   720 
   721 lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
   722   apply (cases "x = 0", simp_all)
   723   using sqrt_divide_self_eq[of x]
   724   apply (simp add: inverse_eq_divide real_sqrt_ge_0_iff field_simps)
   725   done
   726 
   727 text{* Hence derive more interesting properties of the norm. *}
   728 
   729 lemma norm_0: "norm (0::real ^ 'n) = 0"
   730   by (rule norm_zero)
   731 
   732 lemma norm_mul: "norm(a *s x) = abs(a) * norm x"
   733   by (simp add: vector_norm_def vector_component setL2_right_distrib
   734            abs_mult cong: strong_setL2_cong)
   735 lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (x \<bullet> x = (0::real))"
   736   by (simp add: vector_norm_def dot_def setL2_def power2_eq_square)
   737 lemma real_vector_norm_def: "norm x = sqrt (x \<bullet> x)"
   738   by (simp add: vector_norm_def setL2_def dot_def power2_eq_square)
   739 lemma norm_pow_2: "norm x ^ 2 = x \<bullet> x"
   740   by (simp add: real_vector_norm_def)
   741 lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n)" by (metis norm_eq_zero)
   742 lemma vector_mul_eq_0: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   743   by vector
   744 lemma vector_mul_lcancel: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
   745   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
   746 lemma vector_mul_rcancel: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
   747   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
   748 lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
   749   by (metis vector_mul_lcancel)
   750 lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
   751   by (metis vector_mul_rcancel)
   752 lemma norm_cauchy_schwarz: "x \<bullet> y <= norm x * norm y"
   753 proof-
   754   {assume "norm x = 0"
   755     hence ?thesis by (simp add: dot_lzero dot_rzero)}
   756   moreover
   757   {assume "norm y = 0" 
   758     hence ?thesis by (simp add: dot_lzero dot_rzero)}
   759   moreover
   760   {assume h: "norm x \<noteq> 0" "norm y \<noteq> 0"
   761     let ?z = "norm y *s x - norm x *s y"
   762     from h have p: "norm x * norm y > 0" by (metis norm_ge_zero le_less zero_compare_simps)
   763     from dot_pos_le[of ?z]
   764     have "(norm x * norm y) * (x \<bullet> y) \<le> norm x ^2 * norm y ^2"
   765       apply (simp add: dot_rsub dot_lsub dot_lmult dot_rmult ring_simps)
   766       by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym)
   767     hence "x\<bullet>y \<le> (norm x ^2 * norm y ^2) / (norm x * norm y)" using p
   768       by (simp add: field_simps)
   769     hence ?thesis using h by (simp add: power2_eq_square)}
   770   ultimately show ?thesis by metis
   771 qed
   772 
   773 lemma norm_cauchy_schwarz_abs: "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
   774   using norm_cauchy_schwarz[of x y] norm_cauchy_schwarz[of x "-y"]
   775   by (simp add: real_abs_def dot_rneg)
   776 
   777 lemma norm_triangle_sub: "norm (x::real ^'n) <= norm(y) + norm(x - y)"
   778   using norm_triangle_ineq[of "y" "x - y"] by (simp add: ring_simps)
   779 lemma norm_triangle_le: "norm(x::real ^'n) + norm y <= e ==> norm(x + y) <= e"
   780   by (metis order_trans norm_triangle_ineq)
   781 lemma norm_triangle_lt: "norm(x::real ^'n) + norm(y) < e ==> norm(x + y) < e"
   782   by (metis basic_trans_rules(21) norm_triangle_ineq)
   783 
   784 lemma setsum_delta: 
   785   assumes fS: "finite S"
   786   shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
   787 proof-
   788   let ?f = "(\<lambda>k. if k=a then b k else 0)"
   789   {assume a: "a \<notin> S"
   790     hence "\<forall> k\<in> S. ?f k = 0" by simp
   791     hence ?thesis  using a by simp}
   792   moreover 
   793   {assume a: "a \<in> S"
   794     let ?A = "S - {a}"
   795     let ?B = "{a}"
   796     have eq: "S = ?A \<union> ?B" using a by blast 
   797     have dj: "?A \<inter> ?B = {}" by simp
   798     from fS have fAB: "finite ?A" "finite ?B" by auto  
   799     have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
   800       using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
   801       by simp
   802     then have ?thesis  using a by simp}
   803   ultimately show ?thesis by blast
   804 qed
   805   
   806 lemma component_le_norm: "i \<in> {1 .. dimindex(UNIV :: 'n set)} ==> \<bar>x$i\<bar> <= norm (x::real ^ 'n)"
   807   apply (simp add: vector_norm_def)
   808   apply (rule member_le_setL2, simp_all)
   809   done
   810 
   811 lemma norm_bound_component_le: "norm(x::real ^ 'n) <= e
   812                 ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x$i\<bar> <= e"
   813   by (metis component_le_norm order_trans)
   814 
   815 lemma norm_bound_component_lt: "norm(x::real ^ 'n) < e
   816                 ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x$i\<bar> < e"
   817   by (metis component_le_norm basic_trans_rules(21))
   818 
   819 lemma norm_le_l1: "norm (x:: real ^'n) <= setsum(\<lambda>i. \<bar>x$i\<bar>) {1..dimindex(UNIV::'n set)}"
   820   by (simp add: vector_norm_def setL2_le_setsum)
   821 
   822 lemma real_abs_norm: "\<bar> norm x\<bar> = norm (x :: real ^'n)" 
   823   by (rule abs_norm_cancel)
   824 lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n) - norm y\<bar> <= norm(x - y)"
   825   by (rule norm_triangle_ineq3)
   826 lemma norm_le: "norm(x::real ^ 'n) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
   827   by (simp add: real_vector_norm_def)
   828 lemma norm_lt: "norm(x::real ^'n) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
   829   by (simp add: real_vector_norm_def)
   830 lemma norm_eq: "norm (x::real ^'n) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
   831   by (simp add: order_eq_iff norm_le)
   832 lemma norm_eq_1: "norm(x::real ^ 'n) = 1 \<longleftrightarrow> x \<bullet> x = 1"
   833   by (simp add: real_vector_norm_def)
   834 
   835 text{* Squaring equations and inequalities involving norms.  *}
   836 
   837 lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
   838   by (simp add: real_vector_norm_def  dot_pos_le )
   839 
   840 lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
   841   by (auto simp add: real_vector_norm_def)
   842 
   843 lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
   844 proof-
   845   have "x^2 \<le> y^2 \<longleftrightarrow> (x -y) * (y + x) \<le> 0" by (simp add: ring_simps power2_eq_square)
   846   also have "\<dots> \<longleftrightarrow> \<bar>x\<bar> \<le> \<bar>y\<bar>" apply (simp add: zero_compare_simps real_abs_def not_less) by arith
   847 finally show ?thesis ..
   848 qed
   849 
   850 lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
   851   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
   852   using norm_ge_zero[of x]
   853   apply arith
   854   done
   855 
   856 lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2" 
   857   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
   858   using norm_ge_zero[of x]
   859   apply arith
   860   done
   861 
   862 lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
   863   by (metis not_le norm_ge_square)
   864 lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
   865   by (metis norm_le_square not_less)
   866 
   867 text{* Dot product in terms of the norm rather than conversely. *}
   868 
   869 lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
   870   by (simp add: norm_pow_2 dot_ladd dot_radd dot_sym)
   871 
   872 lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
   873   by (simp add: norm_pow_2 dot_ladd dot_radd dot_lsub dot_rsub dot_sym)
   874 
   875 
   876 text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
   877 
   878 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")
   879 proof
   880   assume "?lhs" then show ?rhs by simp
   881 next
   882   assume ?rhs
   883   then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y\<bullet> y = 0" by simp
   884   hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0" 
   885     by (simp add: dot_rsub dot_lsub dot_sym)
   886   then have "(x - y) \<bullet> (x - y) = 0" by (simp add: ring_simps dot_lsub dot_rsub)
   887   then show "x = y" by (simp add: dot_eq_0)
   888 qed
   889 
   890 
   891 subsection{* General linear decision procedure for normed spaces. *}
   892 
   893 lemma norm_cmul_rule_thm: "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(c *s x)"
   894   apply (clarsimp simp add: norm_mul)
   895   apply (rule mult_mono1)
   896   apply simp_all
   897   done
   898 
   899 lemma norm_add_rule_thm: "b1 >= norm(x1 :: real ^'n) \<Longrightarrow> b2 >= norm(x2) ==> b1 + b2 >= norm(x1 + x2)"
   900   apply (rule norm_triangle_le) by simp
   901 
   902 lemma ge_iff_diff_ge_0: "(a::'a::ordered_ring) \<ge> b == a - b \<ge> 0"
   903   by (simp add: ring_simps)
   904 
   905 lemma pth_1: "(x::real^'n) == 1 *s x" by (simp only: vector_smult_lid)
   906 lemma pth_2: "x - (y::real^'n) == x + -y" by (atomize (full)) simp
   907 lemma pth_3: "(-x::real^'n) == -1 *s x" by vector
   908 lemma pth_4: "0 *s (x::real^'n) == 0" "c *s 0 = (0::real ^ 'n)" by vector+
   909 lemma pth_5: "c *s (d *s x) == (c * d) *s (x::real ^ 'n)" by (atomize (full)) vector
   910 lemma pth_6: "(c::real) *s (x + y) == c *s x + c *s y" by (atomize (full)) (vector ring_simps)
   911 lemma pth_7: "0 + x == (x::real^'n)" "x + 0 == x" by simp_all 
   912 lemma pth_8: "(c::real) *s x + d *s x == (c + d) *s x" by (atomize (full)) (vector ring_simps) 
   913 lemma pth_9: "((c::real) *s x + z) + d *s x == (c + d) *s x + z"
   914   "c *s x + (d *s x + z) == (c + d) *s x + z"
   915   "(c *s x + w) + (d *s x + z) == (c + d) *s x + (w + z)" by ((atomize (full)), vector ring_simps)+
   916 lemma pth_a: "(0::real) *s x + y == y" by (atomize (full)) vector
   917 lemma pth_b: "(c::real) *s x + d *s y == c *s x + d *s y" 
   918   "(c *s x + z) + d *s y == c *s x + (z + d *s y)"
   919   "c *s x + (d *s y + z) == c *s x + (d *s y + z)"
   920   "(c *s x + w) + (d *s y + z) == c *s x + (w + (d *s y + z))"
   921   by ((atomize (full)), vector)+
   922 lemma pth_c: "(c::real) *s x + d *s y == d *s y + c *s x"
   923   "(c *s x + z) + d *s y == d *s y + (c *s x + z)"
   924   "c *s x + (d *s y + z) == d *s y + (c *s x + z)"
   925   "(c *s x + w) + (d *s y + z) == d *s y + ((c *s x + w) + z)" by ((atomize (full)), vector)+
   926 lemma pth_d: "x + (0::real ^'n) == x" by (atomize (full)) vector
   927 
   928 lemma norm_imp_pos_and_ge: "norm (x::real ^ 'n) == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
   929   by (atomize) (auto simp add: norm_ge_zero)
   930 
   931 lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
   932 
   933 lemma norm_pths: 
   934   "(x::real ^'n) = y \<longleftrightarrow> norm (x - y) \<le> 0"
   935   "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
   936   using norm_ge_zero[of "x - y"] by auto
   937 
   938 use "normarith.ML"
   939 
   940 method_setup norm = {* Method.ctxt_args (Method.SIMPLE_METHOD' o NormArith.norm_arith_tac)
   941 *} "Proves simple linear statements about vector norms"
   942 
   943 
   944 
   945 text{* Hence more metric properties. *}
   946 
   947 lemma dist_refl: "dist x x = 0" by norm
   948 
   949 lemma dist_sym: "dist x y = dist y x"by norm
   950 
   951 lemma dist_pos_le: "0 <= dist x y" by norm
   952 
   953 lemma dist_triangle: "dist x z <= dist x y + dist y z" by norm
   954 
   955 lemma dist_triangle_alt: "dist y z <= dist x y + dist x z" by norm
   956 
   957 lemma dist_eq_0: "dist x y = 0 \<longleftrightarrow> x = y" by norm
   958 
   959 lemma dist_pos_lt: "x \<noteq> y ==> 0 < dist x y" by norm 
   960 lemma dist_nz:  "x \<noteq> y \<longleftrightarrow> 0 < dist x y" by norm 
   961 
   962 lemma dist_triangle_le: "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e" by norm 
   963 
   964 lemma dist_triangle_lt: "dist x z + dist y z < e ==> dist x y < e" by norm 
   965 
   966 lemma dist_triangle_half_l: "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 ==> dist x1 x2 < e" by norm 
   967 
   968 lemma dist_triangle_half_r: "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 ==> dist x1 x2 < e" by norm 
   969 
   970 lemma dist_triangle_add: "dist (x + y) (x' + y') <= dist x x' + dist y y'"
   971   by norm 
   972 
   973 lemma dist_mul: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y" 
   974   unfolding dist_def vector_ssub_ldistrib[symmetric] norm_mul .. 
   975 
   976 lemma dist_triangle_add_half: " dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 ==> dist(x + y) (x' + y') < e" by norm 
   977 
   978 lemma dist_le_0: "dist x y <= 0 \<longleftrightarrow> x = y" by norm 
   979 
   980 instantiation "^" :: (monoid_add,type) monoid_add
   981 begin
   982   instance by (intro_classes)
   983 end
   984 
   985 lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)$i ) S)"
   986   apply vector
   987   apply auto
   988   apply (cases "finite S")
   989   apply (rule finite_induct[of S])
   990   apply (auto simp add: vector_component zero_index)
   991   done
   992 
   993 lemma setsum_clauses: 
   994   shows "setsum f {} = 0"
   995   and "finite S \<Longrightarrow> setsum f (insert x S) =
   996                  (if x \<in> S then setsum f S else f x + setsum f S)"
   997   by (auto simp add: insert_absorb)
   998 
   999 lemma setsum_cmul: 
  1000   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
  1001   shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
  1002   by (simp add: setsum_eq Cart_eq Cart_lambda_beta vector_component setsum_right_distrib)
  1003 
  1004 lemma setsum_component: 
  1005   fixes f:: " 'a \<Rightarrow> ('b::semiring_1) ^'n"
  1006   assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
  1007   shows "(setsum f S)$i = setsum (\<lambda>x. (f x)$i) S"
  1008   using i by (simp add: setsum_eq Cart_lambda_beta)
  1009 
  1010   (* This needs finiteness assumption due to the definition of fold!!! *)
  1011 
  1012 lemma setsum_superset:
  1013   assumes fb: "finite B" and ab: "A \<subseteq> B" 
  1014   and f0: "\<forall>x \<in> B - A. f x = 0"
  1015   shows "setsum f B = setsum f A"
  1016 proof-
  1017   from ab fb have fa: "finite A" by (metis finite_subset)
  1018   from fb have fba: "finite (B - A)" by (metis finite_Diff)
  1019   have d: "A \<inter> (B - A) = {}" by blast
  1020   from ab have b: "B = A \<union> (B - A)" by blast
  1021   from setsum_Un_disjoint[OF fa fba d, of f] b
  1022     setsum_0'[OF f0]
  1023   show "setsum f B = setsum f A" by simp
  1024 qed
  1025 
  1026 lemma setsum_restrict_set:
  1027   assumes fA: "finite A"
  1028   shows "setsum f (A \<inter> B) = setsum (\<lambda>x. if x \<in> B then f x else 0) A"
  1029 proof-
  1030   from fA have fab: "finite (A \<inter> B)" by auto
  1031   have aba: "A \<inter> B \<subseteq> A" by blast
  1032   let ?g = "\<lambda>x. if x \<in> A\<inter>B then f x else 0"
  1033   from setsum_superset[OF fA aba, of ?g]
  1034   show ?thesis by simp
  1035 qed
  1036 
  1037 lemma setsum_cases:
  1038   assumes fA: "finite A"
  1039   shows "setsum (\<lambda>x. if x \<in> B then f x else g x) A =
  1040          setsum f (A \<inter> B) + setsum g (A \<inter> - B)"
  1041 proof-
  1042   have a: "A = A \<inter> B \<union> A \<inter> -B" "(A \<inter> B) \<inter> (A \<inter> -B) = {}" 
  1043     by blast+
  1044   from fA 
  1045   have f: "finite (A \<inter> B)" "finite (A \<inter> -B)" by auto
  1046   let ?g = "\<lambda>x. if x \<in> B then f x else g x"
  1047   from setsum_Un_disjoint[OF f a(2), of ?g] a(1)
  1048   show ?thesis by simp
  1049 qed
  1050 
  1051 lemma setsum_norm: 
  1052   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1053   assumes fS: "finite S"
  1054   shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
  1055 proof(induct rule: finite_induct[OF fS])
  1056   case 1 thus ?case by simp
  1057 next
  1058   case (2 x S)
  1059   from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
  1060   also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
  1061     using "2.hyps" by simp
  1062   finally  show ?case  using "2.hyps" by simp
  1063 qed
  1064 
  1065 lemma real_setsum_norm: 
  1066   fixes f :: "'a \<Rightarrow> real ^'n"
  1067   assumes fS: "finite S"
  1068   shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
  1069 proof(induct rule: finite_induct[OF fS])
  1070   case 1 thus ?case by simp
  1071 next
  1072   case (2 x S)
  1073   from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
  1074   also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
  1075     using "2.hyps" by simp
  1076   finally  show ?case  using "2.hyps" by simp
  1077 qed
  1078 
  1079 lemma setsum_norm_le: 
  1080   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1081   assumes fS: "finite S"
  1082   and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
  1083   shows "norm (setsum f S) \<le> setsum g S"
  1084 proof-
  1085   from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S" 
  1086     by - (rule setsum_mono, simp)
  1087   then show ?thesis using setsum_norm[OF fS, of f] fg
  1088     by arith
  1089 qed
  1090 
  1091 lemma real_setsum_norm_le: 
  1092   fixes f :: "'a \<Rightarrow> real ^ 'n"
  1093   assumes fS: "finite S"
  1094   and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
  1095   shows "norm (setsum f S) \<le> setsum g S"
  1096 proof-
  1097   from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S" 
  1098     by - (rule setsum_mono, simp)
  1099   then show ?thesis using real_setsum_norm[OF fS, of f] fg
  1100     by arith
  1101 qed
  1102 
  1103 lemma setsum_norm_bound:
  1104   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1105   assumes fS: "finite S"
  1106   and K: "\<forall>x \<in> S. norm (f x) \<le> K"
  1107   shows "norm (setsum f S) \<le> of_nat (card S) * K"
  1108   using setsum_norm_le[OF fS K] setsum_constant[symmetric]
  1109   by simp
  1110 
  1111 lemma real_setsum_norm_bound:
  1112   fixes f :: "'a \<Rightarrow> real ^ 'n"
  1113   assumes fS: "finite S"
  1114   and K: "\<forall>x \<in> S. norm (f x) \<le> K"
  1115   shows "norm (setsum f S) \<le> of_nat (card S) * K"
  1116   using real_setsum_norm_le[OF fS K] setsum_constant[symmetric]
  1117   by simp
  1118 
  1119 lemma setsum_vmul:
  1120   fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector,semiring, mult_zero}"
  1121   assumes fS: "finite S"
  1122   shows "setsum f S *s v = setsum (\<lambda>x. f x *s v) S"
  1123 proof(induct rule: finite_induct[OF fS])
  1124   case 1 then show ?case by (simp add: vector_smult_lzero)
  1125 next
  1126   case (2 x F)
  1127   from "2.hyps" have "setsum f (insert x F) *s v = (f x + setsum f F) *s v" 
  1128     by simp
  1129   also have "\<dots> = f x *s v + setsum f F *s v" 
  1130     by (simp add: vector_sadd_rdistrib)
  1131   also have "\<dots> = setsum (\<lambda>x. f x *s v) (insert x F)" using "2.hyps" by simp
  1132   finally show ?case .
  1133 qed
  1134 
  1135 (* FIXME : Problem thm setsum_vmul[of _ "f:: 'a \<Rightarrow> real ^'n"]  ---
  1136  Get rid of *s and use real_vector instead! Also prove that ^ creates a real_vector !! *)
  1137 
  1138 lemma setsum_add_split: assumes mn: "(m::nat) \<le> n + 1"
  1139   shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
  1140 proof-
  1141   let ?A = "{m .. n}"
  1142   let ?B = "{n + 1 .. n + p}"
  1143   have eq: "{m .. n+p} = ?A \<union> ?B" using mn by auto 
  1144   have d: "?A \<inter> ?B = {}" by auto
  1145   from setsum_Un_disjoint[of "?A" "?B" f] eq d show ?thesis by auto
  1146 qed
  1147 
  1148 lemma setsum_reindex_nonzero: 
  1149   assumes fS: "finite S"
  1150   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"
  1151   shows "setsum h (f ` S) = setsum (h o f) S"
  1152 using nz
  1153 proof(induct rule: finite_induct[OF fS])
  1154   case 1 thus ?case by simp
  1155 next
  1156   case (2 x F) 
  1157   {assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto
  1158     then obtain y where y: "y \<in> F" "f x = f y" by auto 
  1159     from "2.hyps" y have xy: "x \<noteq> y" by auto
  1160     
  1161     from "2.prems"[of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp
  1162     have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto
  1163     also have "\<dots> = setsum (h o f) (insert x F)" 
  1164       using "2.hyps" "2.prems" h0  by auto 
  1165     finally have ?case .}
  1166   moreover
  1167   {assume fxF: "f x \<notin> f ` F"
  1168     have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)" 
  1169       using fxF "2.hyps" by simp 
  1170     also have "\<dots> = setsum (h o f) (insert x F)"  
  1171       using "2.hyps" "2.prems" fxF
  1172       apply auto apply metis done
  1173     finally have ?case .}
  1174   ultimately show ?case by blast
  1175 qed
  1176 
  1177 lemma setsum_Un_nonzero:
  1178   assumes fS: "finite S" and fF: "finite F"
  1179   and f: "\<forall> x\<in> S \<inter> F . f x = (0::'a::ab_group_add)"
  1180   shows "setsum f (S \<union> F) = setsum f S + setsum f F"
  1181   using setsum_Un[OF fS fF, of f] setsum_0'[OF f] by simp
  1182 
  1183 lemma setsum_natinterval_left:
  1184   assumes mn: "(m::nat) <= n" 
  1185   shows "setsum f {m..n} = f m + setsum f {m + 1..n}"
  1186 proof-
  1187   from mn have "{m .. n} = insert m {m+1 .. n}" by auto
  1188   then show ?thesis by auto
  1189 qed
  1190 
  1191 lemma setsum_natinterval_difff: 
  1192   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
  1193   shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
  1194           (if m <= n then f m - f(n + 1) else 0)"
  1195 by (induct n, auto simp add: ring_simps not_le le_Suc_eq)
  1196 
  1197 lemmas setsum_restrict_set' = setsum_restrict_set[unfolded Int_def]
  1198 
  1199 lemma setsum_setsum_restrict:
  1200   "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"
  1201   apply (simp add: setsum_restrict_set'[unfolded mem_def] mem_def)
  1202   by (rule setsum_commute)
  1203 
  1204 lemma setsum_image_gen: assumes fS: "finite S"
  1205   shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
  1206 proof-
  1207   {fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto}
  1208   note th0 = this
  1209   have "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S" 
  1210     apply (rule setsum_cong2) 
  1211     by (simp add: th0)
  1212   also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
  1213     apply (rule setsum_setsum_restrict[OF fS])
  1214     by (rule finite_imageI[OF fS])
  1215   finally show ?thesis .
  1216 qed
  1217 
  1218     (* FIXME: Here too need stupid finiteness assumption on T!!! *)
  1219 lemma setsum_group:
  1220   assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
  1221   shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
  1222   
  1223 apply (subst setsum_image_gen[OF fS, of g f])
  1224 apply (rule setsum_superset[OF fT fST])
  1225 by (auto intro: setsum_0')
  1226 
  1227 (* FIXME: Change the name to fold_image\<dots> *)
  1228 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"
  1229   apply (induct set: finite)
  1230   apply simp by (auto simp add: fold_image_insert)
  1231 
  1232 lemma (in comm_monoid_mult) fold_union_nonzero:
  1233   assumes fS: "finite S" and fT: "finite T"
  1234   and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
  1235   shows "fold_image (op *) f 1 (S \<union> T) = fold_image (op *) f 1 S * fold_image (op *) f 1 T"
  1236 proof-
  1237   have "fold_image op * f 1 (S \<inter> T) = 1" 
  1238     apply (rule fold_1')
  1239     using fS fT I0 by auto 
  1240   with fold_image_Un_Int[OF fS fT] show ?thesis by simp
  1241 qed
  1242 
  1243 lemma setsum_union_nonzero:  
  1244   assumes fS: "finite S" and fT: "finite T"
  1245   and I0: "\<forall>x \<in> S\<inter>T. f x = 0"
  1246   shows "setsum f (S \<union> T) = setsum f S  + setsum f T"
  1247   using fS fT
  1248   apply (simp add: setsum_def)
  1249   apply (rule comm_monoid_add.fold_union_nonzero)
  1250   using I0 by auto
  1251 
  1252 lemma setprod_union_nonzero:  
  1253   assumes fS: "finite S" and fT: "finite T"
  1254   and I0: "\<forall>x \<in> S\<inter>T. f x = 1"
  1255   shows "setprod f (S \<union> T) = setprod f S  * setprod f T"
  1256   using fS fT
  1257   apply (simp add: setprod_def)
  1258   apply (rule fold_union_nonzero)
  1259   using I0 by auto
  1260 
  1261 lemma setsum_unions_nonzero: 
  1262   assumes fS: "finite S" and fSS: "\<forall>T \<in> S. finite T"
  1263   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"
  1264   shows "setsum f (\<Union>S) = setsum (\<lambda>T. setsum f T) S"
  1265   using fSS f0
  1266 proof(induct rule: finite_induct[OF fS])
  1267   case 1 thus ?case by simp
  1268 next
  1269   case (2 T F)
  1270   then have fTF: "finite T" "\<forall>T\<in>F. finite T" "finite F" and TF: "T \<notin> F" 
  1271     and H: "setsum f (\<Union> F) = setsum (setsum f) F" by (auto simp add: finite_insert)
  1272   from fTF have fUF: "finite (\<Union>F)" by (auto intro: finite_Union)
  1273   from "2.prems" TF fTF
  1274   show ?case 
  1275     by (auto simp add: H[symmetric] intro: setsum_union_nonzero[OF fTF(1) fUF, of f])
  1276 qed
  1277 
  1278   (* FIXME : Copied from Pocklington --- should be moved to Finite_Set!!!!!!!! *)
  1279 
  1280 
  1281 lemma (in comm_monoid_mult) fold_related: 
  1282   assumes Re: "R e e" 
  1283   and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
  1284   and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
  1285   shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
  1286   using fS by (rule finite_subset_induct) (insert assms, auto)
  1287 
  1288   (* FIXME: I think we can get rid of the finite assumption!! *)	
  1289 lemma (in comm_monoid_mult) 
  1290   fold_eq_general:
  1291   assumes fS: "finite S"
  1292   and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y" 
  1293   and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
  1294   shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
  1295 proof-
  1296   from h f12 have hS: "h ` S = S'" by auto
  1297   {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
  1298     from f12 h H  have "x = y" by auto }
  1299   hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
  1300   from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
  1301   from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
  1302   also have "\<dots> = fold_image (op *) (f2 o h) e S" 
  1303     using fold_image_reindex[OF fS hinj, of f2 e] .
  1304   also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
  1305     by blast
  1306   finally show ?thesis ..
  1307 qed
  1308 
  1309 lemma (in comm_monoid_mult) fold_eq_general_inverses:
  1310   assumes fS: "finite S" 
  1311   and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
  1312   and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
  1313   shows "fold_image (op *) f e S = fold_image (op *) g e T"
  1314   using fold_eq_general[OF fS, of T h g f e] kh hk by metis
  1315 
  1316 lemma setsum_eq_general_reverses:
  1317   assumes fS: "finite S" and fT: "finite T"
  1318   and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
  1319   and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
  1320   shows "setsum f S = setsum g T"
  1321   apply (simp add: setsum_def fS fT)
  1322   apply (rule comm_monoid_add.fold_eq_general_inverses[OF fS])
  1323   apply (erule kh)
  1324   apply (erule hk)
  1325   done
  1326 
  1327 lemma vsum_norm_allsubsets_bound:
  1328   fixes f:: "'a \<Rightarrow> real ^'n"
  1329   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e" 
  1330   shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real (dimindex(UNIV :: 'n set)) *  e"
  1331 proof-
  1332   let ?d = "real (dimindex (UNIV ::'n set))"
  1333   let ?nf = "\<lambda>x. norm (f x)"
  1334   let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
  1335   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"
  1336     by (rule setsum_commute)
  1337   have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
  1338   have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $ i\<bar>) ?U) P"
  1339     apply (rule setsum_mono)
  1340     by (rule norm_le_l1)
  1341   also have "\<dots> \<le> 2 * ?d * e"
  1342     unfolding th0 th1
  1343   proof(rule setsum_bounded)
  1344     fix i assume i: "i \<in> ?U"
  1345     let ?Pp = "{x. x\<in> P \<and> f x $ i \<ge> 0}"
  1346     let ?Pn = "{x. x \<in> P \<and> f x $ i < 0}"
  1347     have thp: "P = ?Pp \<union> ?Pn" by auto
  1348     have thp0: "?Pp \<inter> ?Pn ={}" by auto
  1349     have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
  1350     have Ppe:"setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pp \<le> e"
  1351       using i component_le_norm[OF i, of "setsum (\<lambda>x. f x) ?Pp"]  fPs[OF PpP]
  1352       by (auto simp add: setsum_component intro: abs_le_D1)
  1353     have Pne: "setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pn \<le> e"
  1354       using i component_le_norm[OF i, of "setsum (\<lambda>x. - f x) ?Pn"]  fPs[OF PnP]
  1355       by (auto simp add: setsum_negf setsum_component vector_component intro: abs_le_D1)
  1356     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" 
  1357       apply (subst thp)
  1358       apply (rule setsum_Un_nonzero) 
  1359       using fP thp0 by auto
  1360     also have "\<dots> \<le> 2*e" using Pne Ppe by arith
  1361     finally show "setsum (\<lambda>x. \<bar>f x $ i\<bar>) P \<le> 2*e" .
  1362   qed
  1363   finally show ?thesis .
  1364 qed
  1365 
  1366 lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> (y::'a::{comm_ring}^'n) = setsum (\<lambda>x. f x \<bullet> y) S "
  1367   by (induct rule: finite_induct, auto simp add: dot_lzero dot_ladd)
  1368 
  1369 lemma dot_rsum: "finite S \<Longrightarrow> (y::'a::{comm_ring}^'n) \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
  1370   by (induct rule: finite_induct, auto simp add: dot_rzero dot_radd)
  1371 
  1372 subsection{* Basis vectors in coordinate directions. *}
  1373 
  1374 
  1375 definition "basis k = (\<chi> i. if i = k then 1 else 0)"
  1376 
  1377 lemma delta_mult_idempotent: 
  1378   "(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)
  1379 
  1380 lemma norm_basis:
  1381   assumes k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1382   shows "norm (basis k :: real ^'n) = 1"
  1383   using k 
  1384   apply (simp add: basis_def real_vector_norm_def dot_def)
  1385   apply (vector delta_mult_idempotent)
  1386   using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "k" "\<lambda>k. 1::real"]
  1387   apply auto
  1388   done
  1389 
  1390 lemma norm_basis_1: "norm(basis 1 :: real ^'n) = 1"
  1391   apply (simp add: basis_def real_vector_norm_def dot_def)
  1392   apply (vector delta_mult_idempotent)
  1393   using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "1" "\<lambda>k. 1::real"] dimindex_nonzero[of "UNIV :: 'n set"]
  1394   apply auto
  1395   done
  1396 
  1397 lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n). norm x = c"
  1398   apply (rule exI[where x="c *s basis 1"])
  1399   by (simp only: norm_mul norm_basis_1)
  1400 
  1401 lemma vector_choose_dist: assumes e: "0 <= e" 
  1402   shows "\<exists>(y::real^'n). dist x y = e"
  1403 proof-
  1404   from vector_choose_size[OF e] obtain c:: "real ^'n"  where "norm c = e"
  1405     by blast
  1406   then have "dist x (x - c) = e" by (simp add: dist_def)
  1407   then show ?thesis by blast
  1408 qed
  1409 
  1410 lemma basis_inj: "inj_on (basis :: nat \<Rightarrow> real ^'n) {1 .. dimindex (UNIV :: 'n set)}"
  1411   by (auto simp add: inj_on_def basis_def Cart_eq Cart_lambda_beta)
  1412 
  1413 lemma basis_component: "i \<in> {1 .. dimindex(UNIV:: 'n set)} ==> (basis k ::('a::semiring_1)^'n)$i = (if k=i then 1 else 0)"
  1414   by (simp add: basis_def Cart_lambda_beta)
  1415 
  1416 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
  1417   by auto
  1418 
  1419 lemma basis_expansion:
  1420   "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 = _")
  1421   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)
  1422 
  1423 lemma basis_expansion_unique: 
  1424   "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)"
  1425   by (simp add: Cart_eq setsum_component vector_component basis_component setsum_delta cond_value_iff cong del: if_weak_cong)
  1426 
  1427 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
  1428   by auto
  1429 
  1430 lemma dot_basis:
  1431   assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1432   shows "basis i \<bullet> x = x$i" "x \<bullet> (basis i :: 'a^'n) = (x$i :: 'a::semiring_1)"
  1433   using i
  1434   by (auto simp add: dot_def basis_def Cart_lambda_beta cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
  1435 
  1436 lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> i \<notin> {1..dimindex(UNIV ::'n set)}"
  1437   by (auto simp add: Cart_eq basis_component zero_index)
  1438 
  1439 lemma basis_nonzero: 
  1440   assumes k: "k \<in> {1 .. dimindex(UNIV ::'n set)}"
  1441   shows "basis k \<noteq> (0:: 'a::semiring_1 ^'n)"
  1442   using k by (simp add: basis_eq_0)
  1443 
  1444 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n)"
  1445   apply (auto simp add: Cart_eq dot_basis)
  1446   apply (erule_tac x="basis i" in allE)
  1447   apply (simp add: dot_basis)
  1448   apply (subgoal_tac "y = z")
  1449   apply simp
  1450   apply vector
  1451   done
  1452 
  1453 lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n)"
  1454   apply (auto simp add: Cart_eq dot_basis)
  1455   apply (erule_tac x="basis i" in allE)
  1456   apply (simp add: dot_basis)
  1457   apply (subgoal_tac "x = y")
  1458   apply simp
  1459   apply vector
  1460   done
  1461 
  1462 subsection{* Orthogonality. *}
  1463 
  1464 definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
  1465 
  1466 lemma orthogonal_basis:
  1467   assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}" 
  1468   shows "orthogonal (basis i :: 'a^'n) x \<longleftrightarrow> x$i = (0::'a::ring_1)"
  1469   using i
  1470   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)
  1471 
  1472 lemma orthogonal_basis_basis:
  1473   assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}" 
  1474   and j: "j \<in> {1 .. dimindex(UNIV ::'n set)}" 
  1475   shows "orthogonal (basis i :: 'a::ring_1^'n) (basis j) \<longleftrightarrow> i \<noteq> j" 
  1476   unfolding orthogonal_basis[OF i] basis_component[OF i] by simp
  1477 
  1478   (* FIXME : Maybe some of these require less than comm_ring, but not all*)
  1479 lemma orthogonal_clauses:
  1480   "orthogonal a (0::'a::comm_ring ^'n)"
  1481   "orthogonal a x ==> orthogonal a (c *s x)"
  1482   "orthogonal a x ==> orthogonal a (-x)"
  1483   "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x + y)"
  1484   "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x - y)"
  1485   "orthogonal 0 a"
  1486   "orthogonal x a ==> orthogonal (c *s x) a"
  1487   "orthogonal x a ==> orthogonal (-x) a"
  1488   "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x + y) a"
  1489   "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x - y) a"
  1490   unfolding orthogonal_def dot_rneg dot_rmult dot_radd dot_rsub
  1491   dot_lzero dot_rzero dot_lneg dot_lmult dot_ladd dot_lsub
  1492   by simp_all
  1493 
  1494 lemma orthogonal_commute: "orthogonal (x::'a::{ab_semigroup_mult,comm_monoid_add} ^'n)y \<longleftrightarrow> orthogonal y x"
  1495   by (simp add: orthogonal_def dot_sym)
  1496 
  1497 subsection{* Explicit vector construction from lists. *}
  1498 
  1499 lemma Cart_lambda_beta_1[simp]: "(Cart_lambda g)$1 = g 1"
  1500   apply (rule Cart_lambda_beta[rule_format])
  1501   using dimindex_ge_1 apply auto done
  1502 
  1503 lemma Cart_lambda_beta_1'[simp]: "(Cart_lambda g)$(Suc 0) = g 1"
  1504   by (simp only: One_nat_def[symmetric] Cart_lambda_beta_1)
  1505 
  1506 definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"
  1507 
  1508 lemma vector_1: "(vector[x]) $1 = x"
  1509   using dimindex_ge_1
  1510   by (auto simp add: vector_def Cart_lambda_beta[rule_format])
  1511 lemma dimindex_2[simp]: "2 \<in> {1 .. dimindex (UNIV :: 2 set)}"
  1512   by (auto simp add: dimindex_def)
  1513 lemma dimindex_2'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 2 set)}"
  1514   by (auto simp add: dimindex_def)
  1515 lemma dimindex_3[simp]: "2 \<in> {1 .. dimindex (UNIV :: 3 set)}" "3 \<in> {1 .. dimindex (UNIV :: 3 set)}"
  1516   by (auto simp add: dimindex_def)
  1517 
  1518 lemma dimindex_3'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}" "3 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}"
  1519   by (auto simp add: dimindex_def)
  1520 
  1521 lemma vector_2:
  1522  "(vector[x,y]) $1 = x"
  1523  "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
  1524   apply (simp add: vector_def)
  1525   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)"]
  1526   apply (simp only: vector_def )
  1527   apply auto
  1528   done
  1529 
  1530 lemma vector_3:
  1531  "(vector [x,y,z] ::('a::zero)^3)$1 = x"
  1532  "(vector [x,y,z] ::('a::zero)^3)$2 = y"
  1533  "(vector [x,y,z] ::('a::zero)^3)$3 = z"
  1534 apply (simp_all add: vector_def Cart_lambda_beta dimindex_3)
  1535   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)"]
  1536   by simp_all
  1537 
  1538 lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
  1539   apply auto
  1540   apply (erule_tac x="v$1" in allE)
  1541   apply (subgoal_tac "vector [v$1] = v")
  1542   apply simp
  1543   by (vector vector_def dimindex_def)
  1544 
  1545 lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
  1546   apply auto
  1547   apply (erule_tac x="v$1" in allE)
  1548   apply (erule_tac x="v$2" in allE)
  1549   apply (subgoal_tac "vector [v$1, v$2] = v")
  1550   apply simp
  1551   apply (vector vector_def dimindex_def)
  1552   apply auto
  1553   apply (subgoal_tac "i = 1 \<or> i =2", auto)
  1554   done
  1555 
  1556 lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
  1557   apply auto
  1558   apply (erule_tac x="v$1" in allE)
  1559   apply (erule_tac x="v$2" in allE)
  1560   apply (erule_tac x="v$3" in allE)
  1561   apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
  1562   apply simp
  1563   apply (vector vector_def dimindex_def)
  1564   apply auto
  1565   apply (subgoal_tac "i = 1 \<or> i =2 \<or> i = 3", auto)
  1566   done
  1567 
  1568 subsection{* Linear functions. *}
  1569 
  1570 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)"
  1571 
  1572 lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. (c::'a::comm_semiring) *s f x)"
  1573   by (vector linear_def Cart_eq Cart_lambda_beta[rule_format] ring_simps)
  1574 
  1575 lemma linear_compose_neg: "linear (f :: 'a ^'n \<Rightarrow> 'a::comm_ring ^'m) ==> linear (\<lambda>x. -(f(x)))" by (vector linear_def Cart_eq)
  1576 
  1577 lemma linear_compose_add: "linear (f :: 'a ^'n \<Rightarrow> 'a::semiring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
  1578   by (vector linear_def Cart_eq ring_simps)
  1579 
  1580 lemma linear_compose_sub: "linear (f :: 'a ^'n \<Rightarrow> 'a::ring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
  1581   by (vector linear_def Cart_eq ring_simps)
  1582 
  1583 lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
  1584   by (simp add: linear_def)
  1585 
  1586 lemma linear_id: "linear id" by (simp add: linear_def id_def)
  1587 
  1588 lemma linear_zero: "linear (\<lambda>x. 0::'a::semiring_1 ^ 'n)" by (simp add: linear_def)
  1589 
  1590 lemma linear_compose_setsum:
  1591   assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a :: 'a::semiring_1 ^ 'n \<Rightarrow> 'a ^ 'm)"
  1592   shows "linear(\<lambda>x. setsum (\<lambda>a. f a x :: 'a::semiring_1 ^'m) S)"
  1593   using lS
  1594   apply (induct rule: finite_induct[OF fS])
  1595   by (auto simp add: linear_zero intro: linear_compose_add)
  1596 
  1597 lemma linear_vmul_component:
  1598   fixes f:: "'a::semiring_1^'m \<Rightarrow> 'a^'n"
  1599   assumes lf: "linear f" and k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1600   shows "linear (\<lambda>x. f x $ k *s v)"
  1601   using lf k
  1602   apply (auto simp add: linear_def )
  1603   by (vector ring_simps)+
  1604 
  1605 lemma linear_0: "linear f ==> f 0 = (0::'a::semiring_1 ^'n)"
  1606   unfolding linear_def
  1607   apply clarsimp
  1608   apply (erule allE[where x="0::'a"])
  1609   apply simp
  1610   done
  1611 
  1612 lemma linear_cmul: "linear f ==> f(c*s x) = c *s f x" by (simp add: linear_def)
  1613 
  1614 lemma linear_neg: "linear (f :: 'a::ring_1 ^'n \<Rightarrow> _) ==> f (-x) = - f x"
  1615   unfolding vector_sneg_minus1
  1616   using linear_cmul[of f] by auto 
  1617 
  1618 lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def) 
  1619 
  1620 lemma linear_sub: "linear (f::'a::ring_1 ^'n \<Rightarrow> _) ==> f(x - y) = f x - f y"
  1621   by (simp add: diff_def linear_add linear_neg)
  1622 
  1623 lemma linear_setsum: 
  1624   fixes f:: "'a::semiring_1^'n \<Rightarrow> _"
  1625   assumes lf: "linear f" and fS: "finite S"
  1626   shows "f (setsum g S) = setsum (f o g) S"
  1627 proof (induct rule: finite_induct[OF fS])
  1628   case 1 thus ?case by (simp add: linear_0[OF lf])
  1629 next
  1630   case (2 x F)
  1631   have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
  1632     by simp
  1633   also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
  1634   also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
  1635   finally show ?case .
  1636 qed
  1637 
  1638 lemma linear_setsum_mul:
  1639   fixes f:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m"
  1640   assumes lf: "linear f" and fS: "finite S"
  1641   shows "f (setsum (\<lambda>i. c i *s v i) S) = setsum (\<lambda>i. c i *s f (v i)) S"
  1642   using linear_setsum[OF lf fS, of "\<lambda>i. c i *s v i" , unfolded o_def]
  1643   linear_cmul[OF lf] by simp 
  1644 
  1645 lemma linear_injective_0:
  1646   assumes lf: "linear (f:: 'a::ring_1 ^ 'n \<Rightarrow> _)"
  1647   shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
  1648 proof-
  1649   have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
  1650   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
  1651   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)" 
  1652     by (simp add: linear_sub[OF lf])
  1653   also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
  1654   finally show ?thesis .
  1655 qed
  1656 
  1657 lemma linear_bounded:
  1658   fixes f:: "real ^'m \<Rightarrow> real ^'n"
  1659   assumes lf: "linear f"
  1660   shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
  1661 proof-
  1662   let ?S = "{1..dimindex(UNIV:: 'm set)}"
  1663   let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
  1664   have fS: "finite ?S" by simp
  1665   {fix x:: "real ^ 'm"
  1666     let ?g = "(\<lambda>i::nat. (x$i) *s (basis i) :: real ^ 'm)"
  1667     have "norm (f x) = norm (f (setsum (\<lambda>i. (x$i) *s (basis i)) ?S))"
  1668       by (simp only:  basis_expansion)
  1669     also have "\<dots> = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)"
  1670       using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf]
  1671       by auto
  1672     finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)" .
  1673     {fix i assume i: "i \<in> ?S"
  1674       from component_le_norm[OF i, of x]
  1675       have "norm ((x$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x"
  1676       unfolding norm_mul
  1677       apply (simp only: mult_commute)
  1678       apply (rule mult_mono)
  1679       by (auto simp add: ring_simps norm_ge_zero) }
  1680     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
  1681     from real_setsum_norm_le[OF fS, of "\<lambda>i. (x$i) *s (f (basis i))", OF th]
  1682     have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
  1683   then show ?thesis by blast
  1684 qed
  1685 
  1686 lemma linear_bounded_pos:
  1687   fixes f:: "real ^'n \<Rightarrow> real ^ 'm"
  1688   assumes lf: "linear f"
  1689   shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
  1690 proof-
  1691   from linear_bounded[OF lf] obtain B where 
  1692     B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
  1693   let ?K = "\<bar>B\<bar> + 1"
  1694   have Kp: "?K > 0" by arith
  1695     {assume C: "B < 0"
  1696       have "norm (1::real ^ 'n) > 0" by (simp add: zero_less_norm_iff)
  1697       with C have "B * norm (1:: real ^ 'n) < 0"
  1698 	by (simp add: zero_compare_simps)
  1699       with B[rule_format, of 1] norm_ge_zero[of "f 1"] have False by simp
  1700     }
  1701     then have Bp: "B \<ge> 0" by ferrack
  1702     {fix x::"real ^ 'n"
  1703       have "norm (f x) \<le> ?K *  norm x"
  1704       using B[rule_format, of x] norm_ge_zero[of x] norm_ge_zero[of "f x"] Bp
  1705       apply (auto simp add: ring_simps split add: abs_split)
  1706       apply (erule order_trans, simp)
  1707       done
  1708   }
  1709   then show ?thesis using Kp by blast
  1710 qed
  1711 
  1712 subsection{* Bilinear functions. *}
  1713 
  1714 definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
  1715 
  1716 lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
  1717   by (simp add: bilinear_def linear_def)
  1718 lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
  1719   by (simp add: bilinear_def linear_def)
  1720 
  1721 lemma bilinear_lmul: "bilinear h ==> h (c *s x) y = c *s (h x y)"
  1722   by (simp add: bilinear_def linear_def)
  1723 
  1724 lemma bilinear_rmul: "bilinear h ==> h x (c *s y) = c *s (h x y)"
  1725   by (simp add: bilinear_def linear_def)
  1726 
  1727 lemma bilinear_lneg: "bilinear h ==> h (- (x:: 'a::ring_1 ^ 'n)) y = -(h x y)"
  1728   by (simp only: vector_sneg_minus1 bilinear_lmul)
  1729 
  1730 lemma bilinear_rneg: "bilinear h ==> h x (- (y:: 'a::ring_1 ^ 'n)) = - h x y"
  1731   by (simp only: vector_sneg_minus1 bilinear_rmul)
  1732 
  1733 lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
  1734   using add_imp_eq[of x y 0] by auto
  1735     
  1736 lemma bilinear_lzero: 
  1737   fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h 0 x = 0"
  1738   using bilinear_ladd[OF bh, of 0 0 x] 
  1739     by (simp add: eq_add_iff ring_simps)
  1740 
  1741 lemma bilinear_rzero: 
  1742   fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h x 0 = 0"
  1743   using bilinear_radd[OF bh, of x 0 0 ] 
  1744     by (simp add: eq_add_iff ring_simps)
  1745 
  1746 lemma bilinear_lsub: "bilinear h ==> h (x - (y:: 'a::ring_1 ^ 'n)) z = h x z - h y z"
  1747   by (simp  add: diff_def bilinear_ladd bilinear_lneg)
  1748 
  1749 lemma bilinear_rsub: "bilinear h ==> h z (x - (y:: 'a::ring_1 ^ 'n)) = h z x - h z y"
  1750   by (simp  add: diff_def bilinear_radd bilinear_rneg)
  1751 
  1752 lemma bilinear_setsum:
  1753   fixes h:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m \<Rightarrow> 'a ^ 'k"
  1754   assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
  1755   shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
  1756 proof- 
  1757   have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
  1758     apply (rule linear_setsum[unfolded o_def])
  1759     using bh fS by (auto simp add: bilinear_def)
  1760   also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
  1761     apply (rule setsum_cong, simp)
  1762     apply (rule linear_setsum[unfolded o_def])
  1763     using bh fT by (auto simp add: bilinear_def)
  1764   finally show ?thesis unfolding setsum_cartesian_product .
  1765 qed
  1766 
  1767 lemma bilinear_bounded:
  1768   fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
  1769   assumes bh: "bilinear h"
  1770   shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  1771 proof- 
  1772   let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
  1773   let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
  1774   let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
  1775   have fM: "finite ?M" and fN: "finite ?N" by simp_all
  1776   {fix x:: "real ^ 'm" and  y :: "real^'n"
  1777     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 ..
  1778     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] ..
  1779     finally have th: "norm (h x y) = \<dots>" .
  1780     have "norm (h x y) \<le> ?B * norm x * norm y"
  1781       apply (simp add: setsum_left_distrib th)
  1782       apply (rule real_setsum_norm_le)
  1783       using fN fM
  1784       apply simp
  1785       apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] norm_mul ring_simps)
  1786       apply (rule mult_mono)
  1787       apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
  1788       apply (rule mult_mono)
  1789       apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
  1790       done}
  1791   then show ?thesis by metis
  1792 qed
  1793 
  1794 lemma bilinear_bounded_pos:
  1795   fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
  1796   assumes bh: "bilinear h"
  1797   shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  1798 proof-
  1799   from bilinear_bounded[OF bh] obtain B where 
  1800     B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
  1801   let ?K = "\<bar>B\<bar> + 1"
  1802   have Kp: "?K > 0" by arith
  1803   have KB: "B < ?K" by arith
  1804   {fix x::"real ^'m" and y :: "real ^'n"
  1805     from KB Kp
  1806     have "B * norm x * norm y \<le> ?K * norm x * norm y"
  1807       apply - 
  1808       apply (rule mult_right_mono, rule mult_right_mono)
  1809       by (auto simp add: norm_ge_zero)
  1810     then have "norm (h x y) \<le> ?K * norm x * norm y"
  1811       using B[rule_format, of x y] by simp} 
  1812   with Kp show ?thesis by blast
  1813 qed
  1814 
  1815 subsection{* Adjoints. *}
  1816 
  1817 definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
  1818 
  1819 lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
  1820 
  1821 lemma adjoint_works_lemma:
  1822   fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
  1823   assumes lf: "linear f"
  1824   shows "\<forall>x y. f x \<bullet> y = x \<bullet> adjoint f y"
  1825 proof-
  1826   let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
  1827   let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
  1828   have fN: "finite ?N" by simp
  1829   have fM: "finite ?M" by simp
  1830   {fix y:: "'a ^ 'm"
  1831     let ?w = "(\<chi> i. (f (basis i) \<bullet> y)) :: 'a ^ 'n"
  1832     {fix x
  1833       have "f x \<bullet> y = f (setsum (\<lambda>i. (x$i) *s basis i) ?N) \<bullet> y"
  1834 	by (simp only: basis_expansion)
  1835       also have "\<dots> = (setsum (\<lambda>i. (x$i) *s f (basis i)) ?N) \<bullet> y"
  1836 	unfolding linear_setsum[OF lf fN] 
  1837 	by (simp add: linear_cmul[OF lf])
  1838       finally have "f x \<bullet> y = x \<bullet> ?w"
  1839 	apply (simp only: )
  1840 	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)
  1841 	done}
  1842   }
  1843   then show ?thesis unfolding adjoint_def 
  1844     some_eq_ex[of "\<lambda>f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y"]
  1845     using choice_iff[of "\<lambda>a b. \<forall>x. f x \<bullet> a = x \<bullet> b "]
  1846     by metis
  1847 qed
  1848 
  1849 lemma adjoint_works:
  1850   fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
  1851   assumes lf: "linear f"
  1852   shows "x \<bullet> adjoint f y = f x \<bullet> y"
  1853   using adjoint_works_lemma[OF lf] by metis
  1854 
  1855 
  1856 lemma adjoint_linear:
  1857   fixes f :: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
  1858   assumes lf: "linear f"
  1859   shows "linear (adjoint f)"
  1860   by (simp add: linear_def vector_eq_ldot[symmetric] dot_radd dot_rmult adjoint_works[OF lf])
  1861 
  1862 lemma adjoint_clauses:
  1863   fixes f:: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
  1864   assumes lf: "linear f"
  1865   shows "x \<bullet> adjoint f y = f x \<bullet> y"
  1866   and "adjoint f y \<bullet> x = y \<bullet> f x"
  1867   by (simp_all add: adjoint_works[OF lf] dot_sym )
  1868 
  1869 lemma adjoint_adjoint:
  1870   fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> _"
  1871   assumes lf: "linear f"
  1872   shows "adjoint (adjoint f) = f"
  1873   apply (rule ext)
  1874   by (simp add: vector_eq_ldot[symmetric] adjoint_clauses[OF adjoint_linear[OF lf]] adjoint_clauses[OF lf])
  1875 
  1876 lemma adjoint_unique:
  1877   fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> 'a ^ 'm"
  1878   assumes lf: "linear f" and u: "\<forall>x y. f' x \<bullet> y = x \<bullet> f y"
  1879   shows "f' = adjoint f"
  1880   apply (rule ext)
  1881   using u
  1882   by (simp add: vector_eq_rdot[symmetric] adjoint_clauses[OF lf])
  1883 
  1884 text{* Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"} *}
  1885 
  1886 consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
  1887 
  1888 defs (overloaded) 
  1889 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"
  1890 
  1891 abbreviation 
  1892   matrix_matrix_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"  (infixl "**" 70)
  1893   where "m ** m' == m\<star> m'"
  1894 
  1895 defs (overloaded) 
  1896   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"
  1897 
  1898 abbreviation 
  1899   matrix_vector_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"  (infixl "*v" 70)
  1900   where 
  1901   "m *v v == m \<star> v"
  1902 
  1903 defs (overloaded) 
  1904   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"
  1905 
  1906 abbreviation 
  1907   vactor_matrix_mult' :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "  (infixl "v*" 70)
  1908   where 
  1909   "v v* m == v \<star> m"
  1910 
  1911 definition "(mat::'a::zero => 'a ^'n^'m) k = (\<chi> i j. if i = j then k else 0)"
  1912 definition "(transp::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
  1913 definition "(row::nat => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
  1914 definition "(column::nat =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
  1915 definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> {1 .. dimindex(UNIV :: 'm set)}}"
  1916 definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}}"
  1917 
  1918 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
  1919 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A \<star> B) + (A \<star> C)"
  1920   by (vector matrix_matrix_mult_def setsum_addf[symmetric] ring_simps)
  1921 
  1922 lemma setsum_delta': 
  1923   assumes fS: "finite S" shows 
  1924   "setsum (\<lambda>k. if a = k then b k else 0) S = 
  1925      (if a\<in> S then b a else 0)"
  1926   using setsum_delta[OF fS, of a b, symmetric] 
  1927   by (auto intro: setsum_cong)
  1928 
  1929 lemma matrix_mul_lid: "mat 1 ** A = A"
  1930   apply (simp add: matrix_matrix_mult_def mat_def)
  1931   apply vector
  1932   by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite_atLeastAtMost]  mult_1_left mult_zero_left if_True)
  1933 
  1934 
  1935 lemma matrix_mul_rid: "A ** mat 1 = A"
  1936   apply (simp add: matrix_matrix_mult_def mat_def)
  1937   apply vector
  1938   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)
  1939 
  1940 lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
  1941   apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
  1942   apply (subst setsum_commute)
  1943   apply simp
  1944   done
  1945 
  1946 lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
  1947   apply (vector matrix_matrix_mult_def matrix_vector_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
  1948   apply (subst setsum_commute)
  1949   apply simp
  1950   done
  1951 
  1952 lemma matrix_vector_mul_lid: "mat 1 *v x = x"
  1953   apply (vector matrix_vector_mult_def mat_def)
  1954   by (simp add: cond_value_iff cond_application_beta 
  1955     setsum_delta' cong del: if_weak_cong)
  1956 
  1957 lemma matrix_transp_mul: "transp(A ** B) = transp B ** transp (A::'a::comm_semiring_1^'m^'n)"
  1958   by (simp add: matrix_matrix_mult_def transp_def Cart_eq Cart_lambda_beta mult_commute)
  1959 
  1960 lemma matrix_eq: "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
  1961   apply auto
  1962   apply (subst Cart_eq)
  1963   apply clarify
  1964   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)
  1965   apply (erule_tac x="basis ia" in allE)
  1966   apply (erule_tac x="i" in ballE)
  1967   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)
  1968 
  1969 lemma matrix_vector_mul_component: 
  1970   assumes k: "k \<in> {1.. dimindex (UNIV :: 'm set)}"
  1971   shows "((A::'a::semiring_1^'n'^'m) *v x)$k = (A$k) \<bullet> x"
  1972   using k
  1973   by (simp add: matrix_vector_mult_def Cart_lambda_beta dot_def)
  1974 
  1975 lemma dot_lmul_matrix: "((x::'a::comm_semiring_1 ^'n) v* A) \<bullet> y = x \<bullet> (A *v y)"
  1976   apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib Cart_lambda_beta mult_ac)
  1977   apply (subst setsum_commute)
  1978   by simp
  1979 
  1980 lemma transp_mat: "transp (mat n) = mat n"
  1981   by (vector transp_def mat_def)
  1982 
  1983 lemma transp_transp: "transp(transp A) = A"
  1984   by (vector transp_def)
  1985 
  1986 lemma row_transp: 
  1987   fixes A:: "'a::semiring_1^'n^'m"
  1988   assumes i: "i \<in> {1.. dimindex (UNIV :: 'n set)}"
  1989   shows "row i (transp A) = column i A"
  1990   using i 
  1991   by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
  1992 
  1993 lemma column_transp:
  1994   fixes A:: "'a::semiring_1^'n^'m"
  1995   assumes i: "i \<in> {1.. dimindex (UNIV :: 'm set)}"
  1996   shows "column i (transp A) = row i A"
  1997   using i 
  1998   by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
  1999 
  2000 lemma rows_transp: "rows(transp (A::'a::semiring_1^'n^'m)) = columns A"
  2001 apply (auto simp add: rows_def columns_def row_transp intro: set_ext)
  2002 apply (rule_tac x=i in exI)
  2003 apply (auto simp add: row_transp)
  2004 done
  2005 
  2006 lemma columns_transp: "columns(transp (A::'a::semiring_1^'n^'m)) = rows A" by (metis transp_transp rows_transp)
  2007 
  2008 text{* Two sometimes fruitful ways of looking at matrix-vector multiplication. *}
  2009 
  2010 lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
  2011   by (simp add: matrix_vector_mult_def dot_def)
  2012 
  2013 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)}"
  2014   by (simp add: matrix_vector_mult_def Cart_eq setsum_component Cart_lambda_beta vector_component column_def mult_commute)
  2015 
  2016 lemma vector_componentwise:
  2017   "(x::'a::ring_1^'n) = (\<chi> j. setsum (\<lambda>i. (x$i) * (basis i :: 'a^'n)$j) {1..dimindex(UNIV :: 'n set)})"
  2018   apply (subst basis_expansion[symmetric])
  2019   by (vector Cart_eq Cart_lambda_beta setsum_component)
  2020 
  2021 lemma linear_componentwise:
  2022   fixes f:: "'a::ring_1 ^ 'm \<Rightarrow> 'a ^ 'n"
  2023   assumes lf: "linear f" and j: "j \<in> {1 .. dimindex (UNIV :: 'n set)}"
  2024   shows "(f x)$j = setsum (\<lambda>i. (x$i) * (f (basis i)$j)) {1 .. dimindex (UNIV :: 'm set)}" (is "?lhs = ?rhs")
  2025 proof-
  2026   let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
  2027   let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
  2028   have fM: "finite ?M" by simp
  2029   have "?rhs = (setsum (\<lambda>i.(x$i) *s f (basis i) ) ?M)$j"
  2030     unfolding vector_smult_component[OF j, symmetric]
  2031     unfolding setsum_component[OF j, of "(\<lambda>i.(x$i) *s f (basis i :: 'a^'m))" ?M]
  2032     ..
  2033   then show ?thesis unfolding linear_setsum_mul[OF lf fM, symmetric] basis_expansion ..
  2034 qed
  2035 
  2036 text{* Inverse matrices  (not necessarily square) *}
  2037 
  2038 definition "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
  2039 
  2040 definition "matrix_inv(A:: 'a::semiring_1^'n^'m) =
  2041         (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
  2042 
  2043 text{* Correspondence between matrices and linear operators. *}
  2044 
  2045 definition matrix:: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
  2046 where "matrix f = (\<chi> i j. (f(basis j))$i)"
  2047 
  2048 lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::'a::comm_semiring_1 ^ 'n))"
  2049   by (simp add: linear_def matrix_vector_mult_def Cart_eq Cart_lambda_beta vector_component ring_simps setsum_right_distrib setsum_addf)
  2050 
  2051 lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n)"
  2052 apply (simp add: matrix_def matrix_vector_mult_def Cart_eq Cart_lambda_beta mult_commute del: One_nat_def)
  2053 apply clarify
  2054 apply (rule linear_componentwise[OF lf, symmetric])
  2055 apply simp
  2056 done
  2057 
  2058 lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n))" by (simp add: ext matrix_works)
  2059 
  2060 lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n)) = A"
  2061   by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
  2062 
  2063 lemma matrix_compose: 
  2064   assumes lf: "linear (f::'a::comm_ring_1^'n \<Rightarrow> _)" and lg: "linear g" 
  2065   shows "matrix (g o f) = matrix g ** matrix f"
  2066   using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
  2067   by (simp  add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
  2068 
  2069 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)}"
  2070   by (simp add: matrix_vector_mult_def transp_def Cart_eq Cart_lambda_beta setsum_component vector_component mult_commute)
  2071 
  2072 lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n^'m) *v x) = (\<lambda>x. transp A *v x)"
  2073   apply (rule adjoint_unique[symmetric])
  2074   apply (rule matrix_vector_mul_linear)
  2075   apply (simp add: transp_def dot_def Cart_lambda_beta matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
  2076   apply (subst setsum_commute)
  2077   apply (auto simp add: mult_ac)
  2078   done
  2079 
  2080 lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n \<Rightarrow> 'a ^ 'm)"
  2081   shows "matrix(adjoint f) = transp(matrix f)"
  2082   apply (subst matrix_vector_mul[OF lf])
  2083   unfolding adjoint_matrix matrix_of_matrix_vector_mul ..
  2084 
  2085 subsection{* Interlude: Some properties of real sets *}
  2086 
  2087 lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
  2088   shows "\<forall>n \<ge> m. d n < e m"
  2089   using prems apply auto
  2090   apply (erule_tac x="n" in allE)
  2091   apply (erule_tac x="n" in allE)
  2092   apply auto
  2093   done
  2094 
  2095 
  2096 lemma real_convex_bound_lt: 
  2097   assumes xa: "(x::real) < a" and ya: "y < a" and u: "0 <= u" and v: "0 <= v"
  2098   and uv: "u + v = 1" 
  2099   shows "u * x + v * y < a"
  2100 proof-
  2101   have uv': "u = 0 \<longrightarrow> v \<noteq> 0" using u v uv by arith
  2102   have "a = a * (u + v)" unfolding uv  by simp
  2103   hence th: "u * a + v * a = a" by (simp add: ring_simps)
  2104   from xa u have "u \<noteq> 0 \<Longrightarrow> u*x < u*a" by (simp add: mult_compare_simps)
  2105   from ya v have "v \<noteq> 0 \<Longrightarrow> v * y < v * a" by (simp add: mult_compare_simps)
  2106   from xa ya u v have "u * x + v * y < u * a + v * a"
  2107     apply (cases "u = 0", simp_all add: uv')
  2108     apply(rule mult_strict_left_mono)
  2109     using uv' apply simp_all
  2110     
  2111     apply (rule add_less_le_mono)
  2112     apply(rule mult_strict_left_mono)
  2113     apply simp_all
  2114     apply (rule mult_left_mono)
  2115     apply simp_all
  2116     done
  2117   thus ?thesis unfolding th .
  2118 qed
  2119 
  2120 lemma real_convex_bound_le: 
  2121   assumes xa: "(x::real) \<le> a" and ya: "y \<le> a" and u: "0 <= u" and v: "0 <= v"
  2122   and uv: "u + v = 1" 
  2123   shows "u * x + v * y \<le> a"
  2124 proof-
  2125   from xa ya u v have "u * x + v * y \<le> u * a + v * a" by (simp add: add_mono mult_left_mono)
  2126   also have "\<dots> \<le> (u + v) * a" by (simp add: ring_simps)
  2127   finally show ?thesis unfolding uv by simp
  2128 qed
  2129 
  2130 lemma infinite_enumerate: assumes fS: "infinite S"
  2131   shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
  2132 unfolding subseq_def
  2133 using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
  2134 
  2135 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)"
  2136 apply auto
  2137 apply (rule_tac x="d/2" in exI)
  2138 apply auto
  2139 done
  2140 
  2141 
  2142 lemma triangle_lemma: 
  2143   assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
  2144   shows "x <= y + z"
  2145 proof-
  2146   have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y  by (simp add: zero_compare_simps)
  2147   with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square ring_simps)
  2148   from y z have yz: "y + z \<ge> 0" by arith
  2149   from power2_le_imp_le[OF th yz] show ?thesis .
  2150 qed
  2151 
  2152 
  2153 lemma lambda_skolem: "(\<forall>i \<in> {1 .. dimindex(UNIV :: 'n set)}. \<exists>x. P i x) \<longleftrightarrow>
  2154    (\<exists>x::'a ^ 'n. \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. P i (x$i))" (is "?lhs \<longleftrightarrow> ?rhs")
  2155 proof-
  2156   let ?S = "{1 .. dimindex(UNIV :: 'n set)}"
  2157   {assume H: "?rhs"
  2158     then have ?lhs by auto}
  2159   moreover
  2160   {assume H: "?lhs"
  2161     then obtain f where f:"\<forall>i\<in> ?S. P i (f i)" unfolding Ball_def choice_iff by metis
  2162     let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
  2163     {fix i assume i: "i \<in> ?S"
  2164       with f i have "P i (f i)" by metis
  2165       then have "P i (?x$i)" using Cart_lambda_beta[of f, rule_format, OF i] by auto 
  2166     }
  2167     hence "\<forall>i \<in> ?S. P i (?x$i)" by metis
  2168     hence ?rhs by metis }
  2169   ultimately show ?thesis by metis
  2170 qed 
  2171 
  2172 (* Supremum and infimum of real sets *)
  2173 
  2174 
  2175 definition rsup:: "real set \<Rightarrow> real" where
  2176   "rsup S = (SOME a. isLub UNIV S a)"
  2177 
  2178 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)
  2179 
  2180 lemma rsup: assumes Se: "S \<noteq> {}" and b: "\<exists>b. S *<= b"
  2181   shows "isLub UNIV S (rsup S)"
  2182 using Se b
  2183 unfolding rsup_def
  2184 apply clarify
  2185 apply (rule someI_ex)
  2186 apply (rule reals_complete)
  2187 by (auto simp add: isUb_def setle_def)
  2188 
  2189 lemma rsup_le: assumes Se: "S \<noteq> {}" and Sb: "S *<= b" shows "rsup S \<le> b"
  2190 proof-
  2191   from Sb have bu: "isUb UNIV S b" by (simp add: isUb_def setle_def)
  2192   from rsup[OF Se] Sb have "isLub UNIV S (rsup S)"  by blast 
  2193   then show ?thesis using bu by (auto simp add: isLub_def leastP_def setle_def setge_def)
  2194 qed
  2195 
  2196 lemma rsup_finite_Max: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2197   shows "rsup S = Max S"
  2198 using fS Se
  2199 proof-
  2200   let ?m = "Max S"
  2201   from Max_ge[OF fS] have Sm: "\<forall> x\<in> S. x \<le> ?m" by metis
  2202   with rsup[OF Se] have lub: "isLub UNIV S (rsup S)" by (metis setle_def)
  2203   from Max_in[OF fS Se] lub have mrS: "?m \<le> rsup S" 
  2204     by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def)
  2205   moreover 
  2206   have "rsup S \<le> ?m" using Sm lub
  2207     by (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
  2208   ultimately  show ?thesis by arith 
  2209 qed
  2210 
  2211 lemma rsup_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2212   shows "rsup S \<in> S"
  2213   using rsup_finite_Max[OF fS Se] Max_in[OF fS Se] by metis
  2214 
  2215 lemma rsup_finite_Ub: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2216   shows "isUb S S (rsup S)"
  2217   using rsup_finite_Max[OF fS Se] rsup_finite_in[OF fS Se] Max_ge[OF fS]
  2218   unfolding isUb_def setle_def by metis
  2219 
  2220 lemma rsup_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2221   shows "a \<le> rsup S \<longleftrightarrow> (\<exists> x \<in> S. a \<le> x)"
  2222 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
  2223 
  2224 lemma rsup_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2225   shows "a \<ge> rsup S \<longleftrightarrow> (\<forall> x \<in> S. a \<ge> x)"
  2226 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
  2227 
  2228 lemma rsup_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2229   shows "a < rsup S \<longleftrightarrow> (\<exists> x \<in> S. a < x)"
  2230 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
  2231 
  2232 lemma rsup_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2233   shows "a > rsup S \<longleftrightarrow> (\<forall> x \<in> S. a > x)"
  2234 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
  2235 
  2236 lemma rsup_unique: assumes b: "S *<= b" and S: "\<forall>b' < b. \<exists>x \<in> S. b' < x"
  2237   shows "rsup S = b"
  2238 using b S  
  2239 unfolding setle_def rsup_alt
  2240 apply -
  2241 apply (rule some_equality)
  2242 apply (metis  linorder_not_le order_eq_iff[symmetric])+
  2243 done
  2244 
  2245 lemma rsup_le_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. T *<= b) \<Longrightarrow> rsup S \<le> rsup T"
  2246   apply (rule rsup_le)
  2247   apply simp
  2248   using rsup[of T] by (auto simp add: isLub_def leastP_def setge_def setle_def isUb_def)
  2249 
  2250 lemma isUb_def': "isUb R S = (\<lambda>x. S *<= x \<and> x \<in> R)"
  2251   apply (rule ext)
  2252   by (metis isUb_def)
  2253 
  2254 lemma UNIV_trivial: "UNIV x" using UNIV_I[of x] by (metis mem_def)
  2255 lemma rsup_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
  2256   shows "a \<le> rsup S \<and> rsup S \<le> b"
  2257 proof-
  2258   from rsup[OF Se] u have lub: "isLub UNIV S (rsup S)" by blast
  2259   hence b: "rsup S \<le> b" using u by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
  2260   from Se obtain y where y: "y \<in> S" by blast
  2261   from lub l have "a \<le> rsup S" apply (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
  2262     apply (erule ballE[where x=y])
  2263     apply (erule ballE[where x=y])
  2264     apply arith
  2265     using y apply auto
  2266     done
  2267   with b show ?thesis by blast
  2268 qed
  2269 
  2270 lemma rsup_abs_le: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rsup S\<bar> \<le> a"
  2271   unfolding abs_le_interval_iff  using rsup_bounds[of S "-a" a]
  2272   by (auto simp add: setge_def setle_def)
  2273 
  2274 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"
  2275 proof-
  2276   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
  2277   show ?thesis using S b rsup_bounds[of S "l - e" "l+e"] unfolding th 
  2278     by  (auto simp add: setge_def setle_def)
  2279 qed
  2280 
  2281 definition rinf:: "real set \<Rightarrow> real" where
  2282   "rinf S = (SOME a. isGlb UNIV S a)"
  2283 
  2284 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)
  2285 
  2286 lemma reals_complete_Glb: assumes Se: "\<exists>x. x \<in> S" and lb: "\<exists> y. isLb UNIV S y"
  2287   shows "\<exists>(t::real). isGlb UNIV S t"
  2288 proof-
  2289   let ?M = "uminus ` S"
  2290   from lb have th: "\<exists>y. isUb UNIV ?M y" apply (auto simp add: isUb_def isLb_def setle_def setge_def)
  2291     by (rule_tac x="-y" in exI, auto)
  2292   from Se have Me: "\<exists>x. x \<in> ?M" by blast
  2293   from reals_complete[OF Me th] obtain t where t: "isLub UNIV ?M t" by blast
  2294   have "isGlb UNIV S (- t)" using t
  2295     apply (auto simp add: isLub_def isGlb_def leastP_def greatestP_def setle_def setge_def isUb_def isLb_def)
  2296     apply (erule_tac x="-y" in allE)
  2297     apply auto
  2298     done
  2299   then show ?thesis by metis
  2300 qed
  2301 
  2302 lemma rinf: assumes Se: "S \<noteq> {}" and b: "\<exists>b. b <=* S"
  2303   shows "isGlb UNIV S (rinf S)"
  2304 using Se b
  2305 unfolding rinf_def
  2306 apply clarify
  2307 apply (rule someI_ex)
  2308 apply (rule reals_complete_Glb)
  2309 apply (auto simp add: isLb_def setle_def setge_def)
  2310 done
  2311 
  2312 lemma rinf_ge: assumes Se: "S \<noteq> {}" and Sb: "b <=* S" shows "rinf S \<ge> b"
  2313 proof-
  2314   from Sb have bu: "isLb UNIV S b" by (simp add: isLb_def setge_def)
  2315   from rinf[OF Se] Sb have "isGlb UNIV S (rinf S)"  by blast 
  2316   then show ?thesis using bu by (auto simp add: isGlb_def greatestP_def setle_def setge_def)
  2317 qed
  2318 
  2319 lemma rinf_finite_Min: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2320   shows "rinf S = Min S"
  2321 using fS Se
  2322 proof-
  2323   let ?m = "Min S"
  2324   from Min_le[OF fS] have Sm: "\<forall> x\<in> S. x \<ge> ?m" by metis
  2325   with rinf[OF Se] have glb: "isGlb UNIV S (rinf S)" by (metis setge_def)
  2326   from Min_in[OF fS Se] glb have mrS: "?m \<ge> rinf S" 
  2327     by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def)
  2328   moreover 
  2329   have "rinf S \<ge> ?m" using Sm glb
  2330     by (auto simp add: isGlb_def greatestP_def isLb_def setle_def setge_def)
  2331   ultimately  show ?thesis by arith 
  2332 qed
  2333 
  2334 lemma rinf_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2335   shows "rinf S \<in> S"
  2336   using rinf_finite_Min[OF fS Se] Min_in[OF fS Se] by metis
  2337 
  2338 lemma rinf_finite_Lb: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2339   shows "isLb S S (rinf S)"
  2340   using rinf_finite_Min[OF fS Se] rinf_finite_in[OF fS Se] Min_le[OF fS]
  2341   unfolding isLb_def setge_def by metis
  2342 
  2343 lemma rinf_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2344   shows "a \<le> rinf S \<longleftrightarrow> (\<forall> x \<in> S. a \<le> x)"
  2345 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
  2346 
  2347 lemma rinf_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2348   shows "a \<ge> rinf S \<longleftrightarrow> (\<exists> x \<in> S. a \<ge> x)"
  2349 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
  2350 
  2351 lemma rinf_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2352   shows "a < rinf S \<longleftrightarrow> (\<forall> x \<in> S. a < x)"
  2353 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
  2354 
  2355 lemma rinf_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
  2356   shows "a > rinf S \<longleftrightarrow> (\<exists> x \<in> S. a > x)"
  2357 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
  2358 
  2359 lemma rinf_unique: assumes b: "b <=* S" and S: "\<forall>b' > b. \<exists>x \<in> S. b' > x"
  2360   shows "rinf S = b"
  2361 using b S  
  2362 unfolding setge_def rinf_alt
  2363 apply -
  2364 apply (rule some_equality)
  2365 apply (metis  linorder_not_le order_eq_iff[symmetric])+
  2366 done
  2367 
  2368 lemma rinf_ge_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. b <=* T) \<Longrightarrow> rinf S >= rinf T"
  2369   apply (rule rinf_ge)
  2370   apply simp
  2371   using rinf[of T] by (auto simp add: isGlb_def greatestP_def setge_def setle_def isLb_def)
  2372 
  2373 lemma isLb_def': "isLb R S = (\<lambda>x. x <=* S \<and> x \<in> R)"
  2374   apply (rule ext)
  2375   by (metis isLb_def)
  2376 
  2377 lemma rinf_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
  2378   shows "a \<le> rinf S \<and> rinf S \<le> b"
  2379 proof-
  2380   from rinf[OF Se] l have lub: "isGlb UNIV S (rinf S)" by blast
  2381   hence b: "a \<le> rinf S" using l by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
  2382   from Se obtain y where y: "y \<in> S" by blast
  2383   from lub u have "b \<ge> rinf S" apply (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
  2384     apply (erule ballE[where x=y])
  2385     apply (erule ballE[where x=y])
  2386     apply arith
  2387     using y apply auto
  2388     done
  2389   with b show ?thesis by blast
  2390 qed
  2391 
  2392 lemma rinf_abs_ge: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rinf S\<bar> \<le> a"
  2393   unfolding abs_le_interval_iff  using rinf_bounds[of S "-a" a]
  2394   by (auto simp add: setge_def setle_def)
  2395 
  2396 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"
  2397 proof-
  2398   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
  2399   show ?thesis using S b rinf_bounds[of S "l - e" "l+e"] unfolding th 
  2400     by  (auto simp add: setge_def setle_def)
  2401 qed
  2402 
  2403 
  2404 
  2405 subsection{* Operator norm. *}
  2406 
  2407 definition "onorm f = rsup {norm (f x)| x. norm x = 1}"
  2408 
  2409 lemma norm_bound_generalize:
  2410   fixes f:: "real ^'n \<Rightarrow> real^'m"
  2411   assumes lf: "linear f"
  2412   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")
  2413 proof-
  2414   {assume H: ?rhs
  2415     {fix x :: "real^'n" assume x: "norm x = 1"
  2416       from H[rule_format, of x] x have "norm (f x) \<le> b" by simp}
  2417     then have ?lhs by blast }
  2418 
  2419   moreover
  2420   {assume H: ?lhs
  2421     from H[rule_format, of "basis 1"] 
  2422     have bp: "b \<ge> 0" using norm_ge_zero[of "f (basis 1)"] dimindex_ge_1[of "UNIV:: 'n set"]
  2423       by (auto simp add: norm_basis elim: order_trans [OF norm_ge_zero])
  2424     {fix x :: "real ^'n"
  2425       {assume "x = 0"
  2426 	then have "norm (f x) \<le> b * norm x" by (simp add: linear_0[OF lf] bp)}
  2427       moreover
  2428       {assume x0: "x \<noteq> 0"
  2429 	hence n0: "norm x \<noteq> 0" by (metis norm_eq_zero)
  2430 	let ?c = "1/ norm x"
  2431 	have "norm (?c*s x) = 1" using x0 by (simp add: n0 norm_mul)
  2432 	with H have "norm (f(?c*s x)) \<le> b" by blast
  2433 	hence "?c * norm (f x) \<le> b" 
  2434 	  by (simp add: linear_cmul[OF lf] norm_mul)
  2435 	hence "norm (f x) \<le> b * norm x" 
  2436 	  using n0 norm_ge_zero[of x] by (auto simp add: field_simps)}
  2437       ultimately have "norm (f x) \<le> b * norm x" by blast}
  2438     then have ?rhs by blast}
  2439   ultimately show ?thesis by blast
  2440 qed
  2441 
  2442 lemma onorm:
  2443   fixes f:: "real ^'n \<Rightarrow> real ^'m"
  2444   assumes lf: "linear f"
  2445   shows "norm (f x) <= onorm f * norm x"
  2446   and "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
  2447 proof-
  2448   {
  2449     let ?S = "{norm (f x) |x. norm x = 1}"
  2450     have Se: "?S \<noteq> {}" using  norm_basis_1 by auto
  2451     from linear_bounded[OF lf] have b: "\<exists> b. ?S *<= b" 
  2452       unfolding norm_bound_generalize[OF lf, symmetric] by (auto simp add: setle_def)
  2453     {from rsup[OF Se b, unfolded onorm_def[symmetric]]
  2454       show "norm (f x) <= onorm f * norm x" 
  2455 	apply - 
  2456 	apply (rule spec[where x = x])
  2457 	unfolding norm_bound_generalize[OF lf, symmetric]
  2458 	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
  2459     {
  2460       show "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"  
  2461 	using rsup[OF Se b, unfolded onorm_def[symmetric]]
  2462 	unfolding norm_bound_generalize[OF lf, symmetric]
  2463 	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
  2464   }
  2465 qed
  2466 
  2467 lemma onorm_pos_le: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" shows "0 <= onorm f"
  2468   using order_trans[OF norm_ge_zero onorm(1)[OF lf, of "basis 1"], unfolded norm_basis_1] by simp
  2469 
  2470 lemma onorm_eq_0: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" 
  2471   shows "onorm f = 0 \<longleftrightarrow> (\<forall>x. f x = 0)"
  2472   using onorm[OF lf]
  2473   apply (auto simp add: onorm_pos_le)
  2474   apply atomize
  2475   apply (erule allE[where x="0::real"])
  2476   using onorm_pos_le[OF lf]
  2477   apply arith
  2478   done
  2479 
  2480 lemma onorm_const: "onorm(\<lambda>x::real^'n. (y::real ^ 'm)) = norm y"
  2481 proof-
  2482   let ?f = "\<lambda>x::real^'n. (y::real ^ 'm)"
  2483   have th: "{norm (?f x)| x. norm x = 1} = {norm y}"
  2484     by(auto intro: vector_choose_size set_ext)
  2485   show ?thesis
  2486     unfolding onorm_def th
  2487     apply (rule rsup_unique) by (simp_all  add: setle_def)
  2488 qed
  2489 
  2490 lemma onorm_pos_lt: assumes lf: "linear (f::real ^ 'n \<Rightarrow> real ^'m)" 
  2491   shows "0 < onorm f \<longleftrightarrow> ~(\<forall>x. f x = 0)"
  2492   unfolding onorm_eq_0[OF lf, symmetric]
  2493   using onorm_pos_le[OF lf] by arith
  2494 
  2495 lemma onorm_compose:
  2496   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
  2497   shows "onorm (f o g) <= onorm f * onorm g"
  2498   apply (rule onorm(2)[OF linear_compose[OF lg lf], rule_format])
  2499   unfolding o_def
  2500   apply (subst mult_assoc)
  2501   apply (rule order_trans)
  2502   apply (rule onorm(1)[OF lf])
  2503   apply (rule mult_mono1)
  2504   apply (rule onorm(1)[OF lg])
  2505   apply (rule onorm_pos_le[OF lf])
  2506   done
  2507 
  2508 lemma onorm_neg_lemma: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
  2509   shows "onorm (\<lambda>x. - f x) \<le> onorm f"
  2510   using onorm[OF linear_compose_neg[OF lf]] onorm[OF lf]
  2511   unfolding norm_minus_cancel by metis
  2512 
  2513 lemma onorm_neg: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
  2514   shows "onorm (\<lambda>x. - f x) = onorm f"
  2515   using onorm_neg_lemma[OF lf] onorm_neg_lemma[OF linear_compose_neg[OF lf]]
  2516   by simp
  2517 
  2518 lemma onorm_triangle:
  2519   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
  2520   shows "onorm (\<lambda>x. f x + g x) <= onorm f + onorm g"
  2521   apply(rule onorm(2)[OF linear_compose_add[OF lf lg], rule_format])
  2522   apply (rule order_trans)
  2523   apply (rule norm_triangle_ineq)
  2524   apply (simp add: distrib)
  2525   apply (rule add_mono)
  2526   apply (rule onorm(1)[OF lf])
  2527   apply (rule onorm(1)[OF lg])
  2528   done
  2529 
  2530 lemma onorm_triangle_le: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) <= e
  2531   \<Longrightarrow> onorm(\<lambda>x. f x + g x) <= e"
  2532   apply (rule order_trans)
  2533   apply (rule onorm_triangle)
  2534   apply assumption+
  2535   done
  2536 
  2537 lemma onorm_triangle_lt: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) < e
  2538   ==> onorm(\<lambda>x. f x + g x) < e"
  2539   apply (rule order_le_less_trans)
  2540   apply (rule onorm_triangle)
  2541   by assumption+
  2542 
  2543 (* "lift" from 'a to 'a^1 and "drop" from 'a^1 to 'a -- FIXME: potential use of transfer *)
  2544 
  2545 definition vec1:: "'a \<Rightarrow> 'a ^ 1" where "vec1 x = (\<chi> i. x)"
  2546 
  2547 definition dest_vec1:: "'a ^1 \<Rightarrow> 'a" 
  2548   where "dest_vec1 x = (x$1)"
  2549 
  2550 lemma vec1_component[simp]: "(vec1 x)$1 = x"
  2551   by (simp add: vec1_def)
  2552 
  2553 lemma vec1_dest_vec1[simp]: "vec1(dest_vec1 x) = x" "dest_vec1(vec1 y) = y"
  2554   by (simp_all add: vec1_def dest_vec1_def Cart_eq Cart_lambda_beta dimindex_def del: One_nat_def)
  2555 
  2556 lemma forall_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (vec1 x))" by (metis vec1_dest_vec1)
  2557 
  2558 lemma exists_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(vec1 x))" by (metis vec1_dest_vec1) 
  2559 
  2560 lemma forall_dest_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P(dest_vec1 x))"  by (metis vec1_dest_vec1)
  2561 
  2562 lemma exists_dest_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(dest_vec1 x))"by (metis vec1_dest_vec1)
  2563 
  2564 lemma vec1_eq[simp]:  "vec1 x = vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
  2565 
  2566 lemma dest_vec1_eq[simp]: "dest_vec1 x = dest_vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
  2567 
  2568 lemma vec1_in_image_vec1: "vec1 x \<in> (vec1 ` S) \<longleftrightarrow> x \<in> S" by auto
  2569 
  2570 lemma vec1_vec: "vec1 x = vec x" by (vector vec1_def)
  2571 
  2572 lemma vec1_add: "vec1(x + y) = vec1 x + vec1 y" by (vector vec1_def)
  2573 lemma vec1_sub: "vec1(x - y) = vec1 x - vec1 y" by (vector vec1_def)
  2574 lemma vec1_cmul: "vec1(c* x) = c *s vec1 x " by (vector vec1_def)
  2575 lemma vec1_neg: "vec1(- x) = - vec1 x " by (vector vec1_def)
  2576 
  2577 lemma vec1_setsum: assumes fS: "finite S"
  2578   shows "vec1(setsum f S) = setsum (vec1 o f) S"
  2579   apply (induct rule: finite_induct[OF fS])
  2580   apply (simp add: vec1_vec)
  2581   apply (auto simp add: vec1_add)
  2582   done
  2583 
  2584 lemma dest_vec1_lambda: "dest_vec1(\<chi> i. x i) = x 1"
  2585   by (simp add: dest_vec1_def)
  2586 
  2587 lemma dest_vec1_vec: "dest_vec1(vec x) = x"
  2588   by (simp add: vec1_vec[symmetric])
  2589 
  2590 lemma dest_vec1_add: "dest_vec1(x + y) = dest_vec1 x + dest_vec1 y"
  2591  by (metis vec1_dest_vec1 vec1_add)
  2592 
  2593 lemma dest_vec1_sub: "dest_vec1(x - y) = dest_vec1 x - dest_vec1 y"
  2594  by (metis vec1_dest_vec1 vec1_sub)
  2595 
  2596 lemma dest_vec1_cmul: "dest_vec1(c*sx) = c * dest_vec1 x"
  2597  by (metis vec1_dest_vec1 vec1_cmul)
  2598 
  2599 lemma dest_vec1_neg: "dest_vec1(- x) = - dest_vec1 x"
  2600  by (metis vec1_dest_vec1 vec1_neg)
  2601 
  2602 lemma dest_vec1_0[simp]: "dest_vec1 0 = 0" by (metis vec_0 dest_vec1_vec)
  2603 
  2604 lemma dest_vec1_sum: assumes fS: "finite S"
  2605   shows "dest_vec1(setsum f S) = setsum (dest_vec1 o f) S"
  2606   apply (induct rule: finite_induct[OF fS])
  2607   apply (simp add: dest_vec1_vec)
  2608   apply (auto simp add: dest_vec1_add)
  2609   done
  2610 
  2611 lemma norm_vec1: "norm(vec1 x) = abs(x)"
  2612   by (simp add: vec1_def norm_real)
  2613 
  2614 lemma dist_vec1: "dist(vec1 x) (vec1 y) = abs(x - y)"
  2615   by (simp only: dist_real vec1_component)
  2616 lemma abs_dest_vec1: "norm x = \<bar>dest_vec1 x\<bar>"
  2617   by (metis vec1_dest_vec1 norm_vec1)
  2618 
  2619 lemma linear_vmul_dest_vec1: 
  2620   fixes f:: "'a::semiring_1^'n \<Rightarrow> 'a^1"
  2621   shows "linear f \<Longrightarrow> linear (\<lambda>x. dest_vec1(f x) *s v)"
  2622   unfolding dest_vec1_def
  2623   apply (rule linear_vmul_component)
  2624   by (auto simp add: dimindex_def)
  2625 
  2626 lemma linear_from_scalars:
  2627   assumes lf: "linear (f::'a::comm_ring_1 ^1 \<Rightarrow> 'a^'n)"
  2628   shows "f = (\<lambda>x. dest_vec1 x *s column 1 (matrix f))"
  2629   apply (rule ext)
  2630   apply (subst matrix_works[OF lf, symmetric])
  2631   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 )
  2632   done
  2633 
  2634 lemma linear_to_scalars: assumes lf: "linear (f::'a::comm_ring_1 ^'n \<Rightarrow> 'a^1)"
  2635   shows "f = (\<lambda>x. vec1(row 1 (matrix f) \<bullet> x))"
  2636   apply (rule ext)
  2637   apply (subst matrix_works[OF lf, symmetric])
  2638   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)
  2639   done
  2640 
  2641 lemma dest_vec1_eq_0: "dest_vec1 x = 0 \<longleftrightarrow> x = 0"
  2642   by (simp add: dest_vec1_eq[symmetric])
  2643 
  2644 lemma setsum_scalars: assumes fS: "finite S"
  2645   shows "setsum f S = vec1 (setsum (dest_vec1 o f) S)"
  2646   unfolding vec1_setsum[OF fS] by simp
  2647 
  2648 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"
  2649   apply (cases "dest_vec1 x \<le> dest_vec1 y")
  2650   apply simp
  2651   apply (subgoal_tac "dest_vec1 y \<le> dest_vec1 x")
  2652   apply (auto)
  2653   done
  2654 
  2655 text{* Pasting vectors. *}
  2656 
  2657 lemma linear_fstcart: "linear fstcart"
  2658   by (auto simp add: linear_def fstcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
  2659 
  2660 lemma linear_sndcart: "linear sndcart"
  2661   by (auto simp add: linear_def sndcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
  2662 
  2663 lemma fstcart_vec[simp]: "fstcart(vec x) = vec x"
  2664   by (vector fstcart_def vec_def dimindex_finite_sum)
  2665 
  2666 lemma fstcart_add[simp]:"fstcart(x + y) = fstcart (x::'a::{plus,times}^('b,'c) finite_sum) + fstcart y"
  2667   using linear_fstcart[unfolded linear_def] by blast
  2668 
  2669 lemma fstcart_cmul[simp]:"fstcart(c*s x) = c*s fstcart (x::'a::{plus,times}^('b,'c) finite_sum)"
  2670   using linear_fstcart[unfolded linear_def] by blast
  2671 
  2672 lemma fstcart_neg[simp]:"fstcart(- x) = - fstcart (x::'a::ring_1^('b,'c) finite_sum)"
  2673 unfolding vector_sneg_minus1 fstcart_cmul ..
  2674 
  2675 lemma fstcart_sub[simp]:"fstcart(x - y) = fstcart (x::'a::ring_1^('b,'c) finite_sum) - fstcart y"
  2676   unfolding diff_def fstcart_add fstcart_neg  ..
  2677 
  2678 lemma fstcart_setsum:
  2679   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
  2680   assumes fS: "finite S"
  2681   shows "fstcart (setsum f S) = setsum (\<lambda>i. fstcart (f i)) S"
  2682   by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
  2683 
  2684 lemma sndcart_vec[simp]: "sndcart(vec x) = vec x"
  2685   by (vector sndcart_def vec_def dimindex_finite_sum)
  2686 
  2687 lemma sndcart_add[simp]:"sndcart(x + y) = sndcart (x::'a::{plus,times}^('b,'c) finite_sum) + sndcart y"
  2688   using linear_sndcart[unfolded linear_def] by blast
  2689 
  2690 lemma sndcart_cmul[simp]:"sndcart(c*s x) = c*s sndcart (x::'a::{plus,times}^('b,'c) finite_sum)"
  2691   using linear_sndcart[unfolded linear_def] by blast
  2692 
  2693 lemma sndcart_neg[simp]:"sndcart(- x) = - sndcart (x::'a::ring_1^('b,'c) finite_sum)"
  2694 unfolding vector_sneg_minus1 sndcart_cmul ..
  2695 
  2696 lemma sndcart_sub[simp]:"sndcart(x - y) = sndcart (x::'a::ring_1^('b,'c) finite_sum) - sndcart y"
  2697   unfolding diff_def sndcart_add sndcart_neg  ..
  2698 
  2699 lemma sndcart_setsum:
  2700   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
  2701   assumes fS: "finite S"
  2702   shows "sndcart (setsum f S) = setsum (\<lambda>i. sndcart (f i)) S"
  2703   by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
  2704 
  2705 lemma pastecart_vec[simp]: "pastecart (vec x) (vec x) = vec x"
  2706   by (simp add: pastecart_eq fstcart_vec sndcart_vec fstcart_pastecart sndcart_pastecart)
  2707 
  2708 lemma pastecart_add[simp]:"pastecart (x1::'a::{plus,times}^_) y1 + pastecart x2 y2 = pastecart (x1 + x2) (y1 + y2)"
  2709   by (simp add: pastecart_eq fstcart_add sndcart_add fstcart_pastecart sndcart_pastecart)
  2710 
  2711 lemma pastecart_cmul[simp]: "pastecart (c *s (x1::'a::{plus,times}^_)) (c *s y1) = c *s pastecart x1 y1"
  2712   by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
  2713 
  2714 lemma pastecart_neg[simp]: "pastecart (- (x::'a::ring_1^_)) (- y) = - pastecart x y"
  2715   unfolding vector_sneg_minus1 pastecart_cmul ..
  2716 
  2717 lemma pastecart_sub: "pastecart (x1::'a::ring_1^_) y1 - pastecart x2 y2 = pastecart (x1 - x2) (y1 - y2)"
  2718   by (simp add: diff_def pastecart_neg[symmetric] del: pastecart_neg)
  2719 
  2720 lemma pastecart_setsum:
  2721   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
  2722   assumes fS: "finite S"
  2723   shows "pastecart (setsum f S) (setsum g S) = setsum (\<lambda>i. pastecart (f i) (g i)) S"
  2724   by (simp  add: pastecart_eq fstcart_setsum[OF fS] sndcart_setsum[OF fS] fstcart_pastecart sndcart_pastecart)
  2725 
  2726 lemma norm_fstcart: "norm(fstcart x) <= norm (x::real ^('n,'m) finite_sum)"
  2727 proof-
  2728   let ?n = "dimindex (UNIV :: 'n set)"
  2729   let ?m = "dimindex (UNIV :: 'm set)"
  2730   let ?N = "{1 .. ?n}"
  2731   let ?M = "{1 .. ?m}"
  2732   let ?NM = "{1 .. dimindex (UNIV :: ('n,'m) finite_sum set)}"
  2733   have th_0: "1 \<le> ?n +1" by simp
  2734   have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
  2735     by (simp add: pastecart_fst_snd)
  2736   have th1: "fstcart x \<bullet> fstcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)" 
  2737     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)
  2738   then show ?thesis
  2739     unfolding th0 
  2740     unfolding real_vector_norm_def real_sqrt_le_iff id_def
  2741     by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
  2742 qed
  2743 
  2744 lemma dist_fstcart: "dist(fstcart (x::real^_)) (fstcart y) <= dist x y"
  2745   by (metis dist_def fstcart_sub[symmetric] norm_fstcart)
  2746 
  2747 lemma norm_sndcart: "norm(sndcart x) <= norm (x::real ^('n,'m) finite_sum)"
  2748 proof-
  2749   let ?n = "dimindex (UNIV :: 'n set)"
  2750   let ?m = "dimindex (UNIV :: 'm set)"
  2751   let ?N = "{1 .. ?n}"
  2752   let ?M = "{1 .. ?m}"
  2753   let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
  2754   let ?NM = "{1 .. ?nm}"
  2755   have thnm[simp]: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
  2756   have th_0: "1 \<le> ?n +1" by simp
  2757   have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
  2758     by (simp add: pastecart_fst_snd)
  2759   let ?f = "\<lambda>n. n - ?n"
  2760   let ?S = "{?n+1 .. ?nm}"
  2761   have finj:"inj_on ?f ?S"
  2762     using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"]
  2763     apply (simp add: Ball_def atLeastAtMost_iff inj_on_def dimindex_finite_sum del: One_nat_def)
  2764     by arith
  2765   have fS: "?f ` ?S = ?M" 
  2766     apply (rule set_ext)
  2767     apply (simp add: image_iff Bex_def) using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"] by arith
  2768   have th1: "sndcart x \<bullet> sndcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)" 
  2769     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)    
  2770   then show ?thesis
  2771     unfolding th0 
  2772     unfolding real_vector_norm_def real_sqrt_le_iff id_def
  2773     by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
  2774 qed
  2775 
  2776 lemma dist_sndcart: "dist(sndcart (x::real^_)) (sndcart y) <= dist x y"
  2777   by (metis dist_def sndcart_sub[symmetric] norm_sndcart)
  2778 
  2779 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"
  2780 proof-
  2781   let ?n = "dimindex (UNIV :: 'n set)"
  2782   let ?m = "dimindex (UNIV :: 'm set)"
  2783   let ?N = "{1 .. ?n}"
  2784   let ?M = "{1 .. ?m}"
  2785   let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
  2786   let ?NM = "{1 .. ?nm}"
  2787   have thnm: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
  2788   have th_0: "1 \<le> ?n +1" by simp
  2789   have th_1: "\<And>i. i \<in> {?m + 1 .. ?nm} \<Longrightarrow> i - ?m \<in> ?N" apply (simp add: thnm) by arith
  2790   let ?f = "\<lambda>a b i. (a$i) * (b$i)"
  2791   let ?g = "?f (pastecart x1 x2) (pastecart y1 y2)"
  2792   let ?S = "{?n +1 .. ?nm}"
  2793   {fix i
  2794     assume i: "i \<in> ?N"
  2795     have "?g i = ?f x1 y1 i"
  2796       using i
  2797       apply (simp add: pastecart_def Cart_lambda_beta thnm) done
  2798   }
  2799   hence th2: "setsum ?g ?N = setsum (?f x1 y1) ?N"
  2800     apply -
  2801     apply (rule setsum_cong)
  2802     apply auto
  2803     done
  2804   {fix i
  2805     assume i: "i \<in> ?S"
  2806     have "?g i = ?f x2 y2 (i - ?n)"
  2807       using i
  2808       apply (simp add: pastecart_def Cart_lambda_beta thnm) done
  2809   }
  2810   hence th3: "setsum ?g ?S = setsum (\<lambda>i. ?f x2 y2 (i -?n)) ?S"
  2811     apply -
  2812     apply (rule setsum_cong)
  2813     apply auto
  2814     done
  2815   let ?r = "\<lambda>n. n - ?n"
  2816   have rinj: "inj_on ?r ?S" apply (simp add: inj_on_def Ball_def thnm) by arith
  2817   have rS: "?r ` ?S = ?M" apply (rule set_ext) 
  2818     apply (simp add: thnm image_iff Bex_def) by arith
  2819   have "pastecart x1 x2 \<bullet> (pastecart y1 y2) = setsum ?g ?NM" by (simp add: dot_def)
  2820   also have "\<dots> = setsum ?g ?N + setsum ?g ?S"
  2821     by (simp add: dot_def thnm setsum_add_split[OF th_0, of _ ?m] del: One_nat_def)
  2822   also have "\<dots> = setsum (?f x1 y1) ?N + setsum (?f x2 y2) ?M"
  2823     unfolding setsum_reindex[OF rinj, unfolded rS o_def] th2 th3 ..
  2824   finally 
  2825   show ?thesis by (simp add: dot_def)
  2826 qed
  2827 
  2828 lemma norm_pastecart: "norm(pastecart x y) <= norm(x :: real ^ _) + norm(y)"
  2829   unfolding real_vector_norm_def dot_pastecart real_sqrt_le_iff id_def
  2830   apply (rule power2_le_imp_le)
  2831   apply (simp add: real_sqrt_pow2[OF add_nonneg_nonneg[OF dot_pos_le[of x] dot_pos_le[of y]]])
  2832   apply (auto simp add: power2_eq_square ring_simps)
  2833   apply (simp add: power2_eq_square[symmetric])
  2834   apply (rule mult_nonneg_nonneg)
  2835   apply (simp_all add: real_sqrt_pow2[OF dot_pos_le])
  2836   apply (rule add_nonneg_nonneg)
  2837   apply (simp_all add: real_sqrt_pow2[OF dot_pos_le])
  2838   done
  2839 
  2840 subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
  2841 
  2842 definition hull :: "'a set set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "hull" 75) where
  2843   "S hull s = Inter {t. t \<in> S \<and> s \<subseteq> t}"
  2844 
  2845 lemma hull_same: "s \<in> S \<Longrightarrow> S hull s = s"
  2846   unfolding hull_def by auto
  2847 
  2848 lemma hull_in: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) \<in> S"
  2849 unfolding hull_def subset_iff by auto
  2850 
  2851 lemma hull_eq: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) = s \<longleftrightarrow> s \<in> S"
  2852 using hull_same[of s S] hull_in[of S s] by metis  
  2853 
  2854 
  2855 lemma hull_hull: "S hull (S hull s) = S hull s"
  2856   unfolding hull_def by blast
  2857 
  2858 lemma hull_subset: "s \<subseteq> (S hull s)"
  2859   unfolding hull_def by blast
  2860 
  2861 lemma hull_mono: " s \<subseteq> t ==> (S hull s) \<subseteq> (S hull t)"
  2862   unfolding hull_def by blast
  2863 
  2864 lemma hull_antimono: "S \<subseteq> T ==> (T hull s) \<subseteq> (S hull s)"
  2865   unfolding hull_def by blast
  2866 
  2867 lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> t \<in> S ==> (S hull s) \<subseteq> t"
  2868   unfolding hull_def by blast
  2869 
  2870 lemma subset_hull: "t \<in> S ==> S hull s \<subseteq> t \<longleftrightarrow>  s \<subseteq> t"
  2871   unfolding hull_def by blast
  2872 
  2873 lemma hull_unique: "s \<subseteq> t \<Longrightarrow> t \<in> S \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> t' \<in> S ==> t \<subseteq> t')
  2874            ==> (S hull s = t)"
  2875 unfolding hull_def by auto
  2876 
  2877 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"
  2878   using hull_minimal[of S "{x. P x}" Q]
  2879   by (auto simp add: subset_eq Collect_def mem_def)
  2880 
  2881 lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S" by (metis hull_subset subset_eq)
  2882 
  2883 lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
  2884 unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
  2885 
  2886 lemma hull_union: assumes T: "\<And>T. T \<subseteq> S ==> Inter T \<in> S"
  2887   shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
  2888 apply rule
  2889 apply (rule hull_mono)
  2890 unfolding Un_subset_iff
  2891 apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
  2892 apply (rule hull_minimal)
  2893 apply (metis hull_union_subset)
  2894 apply (metis hull_in T)
  2895 done
  2896 
  2897 lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
  2898   unfolding hull_def by blast
  2899 
  2900 lemma hull_redundant: "a \<in> (S hull s) ==> (S hull (insert a s) = S hull s)"
  2901 by (metis hull_redundant_eq)
  2902 
  2903 text{* Archimedian properties and useful consequences. *}
  2904 
  2905 lemma real_arch_simple: "\<exists>n. x <= real (n::nat)"
  2906   using reals_Archimedean2[of x] apply auto by (rule_tac x="Suc n" in exI, auto)
  2907 lemmas real_arch_lt = reals_Archimedean2
  2908 
  2909 lemmas real_arch = reals_Archimedean3
  2910 
  2911 lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  2912   using reals_Archimedean
  2913   apply (auto simp add: field_simps inverse_positive_iff_positive)
  2914   apply (subgoal_tac "inverse (real n) > 0")
  2915   apply arith
  2916   apply simp
  2917   done
  2918 
  2919 lemma real_pow_lbound: "0 <= x ==> 1 + real n * x <= (1 + x) ^ n"
  2920 proof(induct n)
  2921   case 0 thus ?case by simp
  2922 next 
  2923   case (Suc n)
  2924   hence h: "1 + real n * x \<le> (1 + x) ^ n" by simp
  2925   from h have p: "1 \<le> (1 + x) ^ n" using Suc.prems by simp
  2926   from h have "1 + real n * x + x \<le> (1 + x) ^ n + x" by simp
  2927   also have "\<dots> \<le> (1 + x) ^ Suc n" apply (subst diff_le_0_iff_le[symmetric]) 
  2928     apply (simp add: ring_simps)
  2929     using mult_left_mono[OF p Suc.prems] by simp
  2930   finally show ?case  by (simp add: real_of_nat_Suc ring_simps)
  2931 qed
  2932 
  2933 lemma real_arch_pow: assumes x: "1 < (x::real)" shows "\<exists>n. y < x^n"
  2934 proof-
  2935   from x have x0: "x - 1 > 0" by arith
  2936   from real_arch[OF x0, rule_format, of y] 
  2937   obtain n::nat where n:"y < real n * (x - 1)" by metis
  2938   from x0 have x00: "x- 1 \<ge> 0" by arith
  2939   from real_pow_lbound[OF x00, of n] n 
  2940   have "y < x^n" by auto
  2941   then show ?thesis by metis
  2942 qed 
  2943 
  2944 lemma real_arch_pow2: "\<exists>n. (x::real) < 2^ n"
  2945   using real_arch_pow[of 2 x] by simp
  2946 
  2947 lemma real_arch_pow_inv: assumes y: "(y::real) > 0" and x1: "x < 1"
  2948   shows "\<exists>n. x^n < y"
  2949 proof-
  2950   {assume x0: "x > 0" 
  2951     from x0 x1 have ix: "1 < 1/x" by (simp add: field_simps)
  2952     from real_arch_pow[OF ix, of "1/y"]
  2953     obtain n where n: "1/y < (1/x)^n" by blast
  2954     then 
  2955     have ?thesis using y x0 by (auto simp add: field_simps power_divide) }
  2956   moreover 
  2957   {assume "\<not> x > 0" with y x1 have ?thesis apply auto by (rule exI[where x=1], auto)}
  2958   ultimately show ?thesis by metis
  2959 qed
  2960 
  2961 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)"
  2962   by (metis real_arch_inv)
  2963 
  2964 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"
  2965   apply (rule forall_pos_mono)
  2966   apply auto
  2967   apply (atomize)
  2968   apply (erule_tac x="n - 1" in allE)
  2969   apply auto
  2970   done
  2971 
  2972 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"
  2973   shows "x = 0"
  2974 proof-
  2975   {assume "x \<noteq> 0" with x0 have xp: "x > 0" by arith
  2976     from real_arch[OF xp, rule_format, of c] obtain n::nat where n: "c < real n * x"  by blast
  2977     with xc[rule_format, of n] have "n = 0" by arith
  2978     with n c have False by simp}
  2979   then show ?thesis by blast
  2980 qed
  2981 
  2982 (* ------------------------------------------------------------------------- *)
  2983 (* Relate max and min to sup and inf.                                        *)
  2984 (* ------------------------------------------------------------------------- *)
  2985 
  2986 lemma real_max_rsup: "max x y = rsup {x,y}"
  2987 proof-
  2988   have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
  2989   from rsup_finite_le_iff[OF f, of "max x y"] have "rsup {x,y} \<le> max x y" by simp
  2990   moreover
  2991   have "max x y \<le> rsup {x,y}" using rsup_finite_ge_iff[OF f, of "max x y"]
  2992     by (simp add: linorder_linear)
  2993   ultimately show ?thesis by arith
  2994 qed 
  2995 
  2996 lemma real_min_rinf: "min x y = rinf {x,y}"
  2997 proof-
  2998   have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
  2999   from rinf_finite_le_iff[OF f, of "min x y"] have "rinf {x,y} \<le> min x y" 
  3000     by (simp add: linorder_linear)
  3001   moreover
  3002   have "min x y \<le> rinf {x,y}" using rinf_finite_ge_iff[OF f, of "min x y"]
  3003     by simp
  3004   ultimately show ?thesis by arith
  3005 qed 
  3006 
  3007 (* ------------------------------------------------------------------------- *)
  3008 (* Geometric progression.                                                    *)
  3009 (* ------------------------------------------------------------------------- *)
  3010 
  3011 lemma sum_gp_basic: "((1::'a::{field, recpower}) - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
  3012   (is "?lhs = ?rhs")
  3013 proof-
  3014   {assume x1: "x = 1" hence ?thesis by simp}
  3015   moreover
  3016   {assume x1: "x\<noteq>1"
  3017     hence x1': "x - 1 \<noteq> 0" "1 - x \<noteq> 0" "x - 1 = - (1 - x)" "- (1 - x) \<noteq> 0" by auto
  3018     from geometric_sum[OF x1, of "Suc n", unfolded x1']
  3019     have "(- (1 - x)) * setsum (\<lambda>i. x^i) {0 .. n} = - (1 - x^(Suc n))"
  3020       unfolding atLeastLessThanSuc_atLeastAtMost
  3021       using x1' apply (auto simp only: field_simps)
  3022       apply (simp add: ring_simps)
  3023       done
  3024     then have ?thesis by (simp add: ring_simps) }
  3025   ultimately show ?thesis by metis
  3026 qed
  3027 
  3028 lemma sum_gp_multiplied: assumes mn: "m <= n"
  3029   shows "((1::'a::{field, recpower}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
  3030   (is "?lhs = ?rhs")
  3031 proof-
  3032   let ?S = "{0..(n - m)}"
  3033   from mn have mn': "n - m \<ge> 0" by arith
  3034   let ?f = "op + m"
  3035   have i: "inj_on ?f ?S" unfolding inj_on_def by auto
  3036   have f: "?f ` ?S = {m..n}" 
  3037     using mn apply (auto simp add: image_iff Bex_def) by arith
  3038   have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)" 
  3039     by (rule ext, simp add: power_add power_mult)
  3040   from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
  3041   have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
  3042   then show ?thesis unfolding sum_gp_basic using mn
  3043     by (simp add: ring_simps power_add[symmetric])
  3044 qed
  3045 
  3046 lemma sum_gp: "setsum (op ^ (x::'a::{field, recpower})) {m .. n} = 
  3047    (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m) 
  3048                     else (x^ m - x^ (Suc n)) / (1 - x))"
  3049 proof-
  3050   {assume nm: "n < m" hence ?thesis by simp}
  3051   moreover
  3052   {assume "\<not> n < m" hence nm: "m \<le> n" by arith
  3053     {assume x: "x = 1"  hence ?thesis by simp}
  3054     moreover
  3055     {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
  3056       from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
  3057     ultimately have ?thesis by metis
  3058   }
  3059   ultimately show ?thesis by metis
  3060 qed
  3061 
  3062 lemma sum_gp_offset: "setsum (op ^ (x::'a::{field,recpower})) {m .. m+n} = 
  3063   (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
  3064   unfolding sum_gp[of x m "m + n"] power_Suc
  3065   by (simp add: ring_simps power_add)
  3066 
  3067 
  3068 subsection{* A bit of linear algebra. *}
  3069 
  3070 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 )"
  3071 definition "span S = (subspace hull S)"
  3072 definition "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span(S - {a}))"
  3073 abbreviation "independent s == ~(dependent s)"
  3074 
  3075 (* Closure properties of subspaces.                                          *)
  3076 
  3077 lemma subspace_UNIV[simp]: "subspace(UNIV)" by (simp add: subspace_def)
  3078 
  3079 lemma subspace_0: "subspace S ==> 0 \<in> S" by (metis subspace_def)
  3080 
  3081 lemma subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S ==> x + y \<in> S" 
  3082   by (metis subspace_def)
  3083 
  3084 lemma subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *s x \<in> S"
  3085   by (metis subspace_def)
  3086 
  3087 lemma subspace_neg: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> - x \<in> S"
  3088   by (metis vector_sneg_minus1 subspace_mul)
  3089 
  3090 lemma subspace_sub: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
  3091   by (metis diff_def subspace_add subspace_neg)
  3092 
  3093 lemma subspace_setsum:
  3094   assumes sA: "subspace A" and fB: "finite B"
  3095   and f: "\<forall>x\<in> B. f x \<in> A"
  3096   shows "setsum f B \<in> A"
  3097   using  fB f sA
  3098   apply(induct rule: finite_induct[OF fB])
  3099   by (simp add: subspace_def sA, auto simp add: sA subspace_add) 
  3100 
  3101 lemma subspace_linear_image: 
  3102   assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and sS: "subspace S" 
  3103   shows "subspace(f ` S)"
  3104   using lf sS linear_0[OF lf]
  3105   unfolding linear_def subspace_def
  3106   apply (auto simp add: image_iff)
  3107   apply (rule_tac x="x + y" in bexI, auto)
  3108   apply (rule_tac x="c*s x" in bexI, auto)
  3109   done
  3110 
  3111 lemma subspace_linear_preimage: "linear (f::'a::semiring_1^'n \<Rightarrow> _) ==> subspace S ==> subspace {x. f x \<in> S}"
  3112   by (auto simp add: subspace_def linear_def linear_0[of f])
  3113 
  3114 lemma subspace_trivial: "subspace {0::'a::semiring_1 ^_}"
  3115   by (simp add: subspace_def)
  3116 
  3117 lemma subspace_inter: "subspace A \<Longrightarrow> subspace B ==> subspace (A \<inter> B)"
  3118   by (simp add: subspace_def)
  3119 
  3120 
  3121 lemma span_mono: "A \<subseteq> B ==> span A \<subseteq> span B"
  3122   by (metis span_def hull_mono)
  3123 
  3124 lemma subspace_span: "subspace(span S)"
  3125   unfolding span_def
  3126   apply (rule hull_in[unfolded mem_def])
  3127   apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
  3128   apply auto
  3129   apply (erule_tac x="X" in ballE)
  3130   apply (simp add: mem_def)
  3131   apply blast
  3132   apply (erule_tac x="X" in ballE)
  3133   apply (erule_tac x="X" in ballE)
  3134   apply (erule_tac x="X" in ballE)
  3135   apply (clarsimp simp add: mem_def)
  3136   apply simp
  3137   apply simp
  3138   apply simp
  3139   apply (erule_tac x="X" in ballE)
  3140   apply (erule_tac x="X" in ballE)
  3141   apply (simp add: mem_def)
  3142   apply simp
  3143   apply simp
  3144   done
  3145 
  3146 lemma span_clauses:
  3147   "a \<in> S ==> a \<in> span S"
  3148   "0 \<in> span S"
  3149   "x\<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
  3150   "x \<in> span S \<Longrightarrow> c *s x \<in> span S"
  3151   by (metis span_def hull_subset subset_eq subspace_span subspace_def)+
  3152 
  3153 lemma span_induct: assumes SP: "\<And>x. x \<in> S ==> P x"
  3154   and P: "subspace P" and x: "x \<in> span S" shows "P x"
  3155 proof-
  3156   from SP have SP': "S \<subseteq> P" by (simp add: mem_def subset_eq)
  3157   from P have P': "P \<in> subspace" by (simp add: mem_def)
  3158   from x hull_minimal[OF SP' P', unfolded span_def[symmetric]]
  3159   show "P x" by (metis mem_def subset_eq) 
  3160 qed
  3161 
  3162 lemma span_empty: "span {} = {(0::'a::semiring_0 ^ 'n)}"
  3163   apply (simp add: span_def)
  3164   apply (rule hull_unique)
  3165   apply (auto simp add: mem_def subspace_def)
  3166   unfolding mem_def[of "0::'a^'n", symmetric]
  3167   apply simp
  3168   done
  3169 
  3170 lemma independent_empty: "independent {}"
  3171   by (simp add: dependent_def)
  3172 
  3173 lemma independent_mono: "independent A \<Longrightarrow> B \<subseteq> A ==> independent B"
  3174   apply (clarsimp simp add: dependent_def span_mono)
  3175   apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
  3176   apply force
  3177   apply (rule span_mono)
  3178   apply auto
  3179   done
  3180 
  3181 lemma span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
  3182   by (metis order_antisym span_def hull_minimal mem_def)
  3183 
  3184 lemma span_induct': assumes SP: "\<forall>x \<in> S. P x"
  3185   and P: "subspace P" shows "\<forall>x \<in> span S. P x"
  3186   using span_induct SP P by blast
  3187 
  3188 inductive span_induct_alt_help for S:: "'a::semiring_1^'n \<Rightarrow> bool"
  3189   where 
  3190   span_induct_alt_help_0: "span_induct_alt_help S 0"
  3191   | 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)"
  3192 
  3193 lemma span_induct_alt': 
  3194   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"
  3195 proof-
  3196   {fix x:: "'a^'n" assume x: "span_induct_alt_help S x"
  3197     have "h x"
  3198       apply (rule span_induct_alt_help.induct[OF x])
  3199       apply (rule h0)
  3200       apply (rule hS, assumption, assumption)
  3201       done}
  3202   note th0 = this
  3203   {fix x assume x: "x \<in> span S"
  3204     
  3205     have "span_induct_alt_help S x"
  3206       proof(rule span_induct[where x=x and S=S])
  3207 	show "x \<in> span S" using x .
  3208       next
  3209 	fix x assume xS : "x \<in> S"
  3210 	  from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
  3211 	  show "span_induct_alt_help S x" by simp
  3212 	next
  3213 	have "span_induct_alt_help S 0" by (rule span_induct_alt_help_0)
  3214 	moreover
  3215 	{fix x y assume h: "span_induct_alt_help S x" "span_induct_alt_help S y"
  3216 	  from h 
  3217 	  have "span_induct_alt_help S (x + y)"
  3218 	    apply (induct rule: span_induct_alt_help.induct)
  3219 	    apply simp
  3220 	    unfolding add_assoc
  3221 	    apply (rule span_induct_alt_help_S)
  3222 	    apply assumption
  3223 	    apply simp
  3224 	    done}
  3225 	moreover
  3226 	{fix c x assume xt: "span_induct_alt_help S x"
  3227 	  then have "span_induct_alt_help S (c*s x)" 
  3228 	    apply (induct rule: span_induct_alt_help.induct)
  3229 	    apply (simp add: span_induct_alt_help_0)
  3230 	    apply (simp add: vector_smult_assoc vector_add_ldistrib)
  3231 	    apply (rule span_induct_alt_help_S)
  3232 	    apply assumption
  3233 	    apply simp
  3234 	    done
  3235 	}
  3236 	ultimately show "subspace (span_induct_alt_help S)" 
  3237 	  unfolding subspace_def mem_def Ball_def by blast
  3238       qed}
  3239   with th0 show ?thesis by blast
  3240 qed 
  3241 
  3242 lemma span_induct_alt: 
  3243   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"
  3244   shows "h x"
  3245 using span_induct_alt'[of h S] h0 hS x by blast
  3246 
  3247 (* Individual closure properties. *)
  3248 
  3249 lemma span_superset: "x \<in> S ==> x \<in> span S" by (metis span_clauses)
  3250 
  3251 lemma span_0: "0 \<in> span S" by (metis subspace_span subspace_0)
  3252 
  3253 lemma span_add: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
  3254   by (metis subspace_add subspace_span)
  3255 
  3256 lemma span_mul: "x \<in> span S ==> (c *s x) \<in> span S"
  3257   by (metis subspace_span subspace_mul)
  3258 
  3259 lemma span_neg: "x \<in> span S ==> - (x::'a::ring_1^'n) \<in> span S"
  3260   by (metis subspace_neg subspace_span)
  3261 
  3262 lemma span_sub: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> y \<in> span S ==> x - y \<in> span S"
  3263   by (metis subspace_span subspace_sub)
  3264 
  3265 lemma span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S ==> setsum f A \<in> span S"
  3266   apply (rule subspace_setsum)
  3267   by (metis subspace_span subspace_setsum)+
  3268 
  3269 lemma span_add_eq: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
  3270   apply (auto simp only: span_add span_sub)
  3271   apply (subgoal_tac "(x + y) - x \<in> span S", simp)
  3272   by (simp only: span_add span_sub)
  3273 
  3274 (* Mapping under linear image. *)
  3275 
  3276 lemma span_linear_image: assumes lf: "linear (f::'a::semiring_1 ^ 'n => _)"
  3277   shows "span (f ` S) = f ` (span S)"
  3278 proof-
  3279   {fix x
  3280     assume x: "x \<in> span (f ` S)"
  3281     have "x \<in> f ` span S"
  3282       apply (rule span_induct[where x=x and S = "f ` S"])
  3283       apply (clarsimp simp add: image_iff)
  3284       apply (frule span_superset)
  3285       apply blast
  3286       apply (simp only: mem_def)
  3287       apply (rule subspace_linear_image[OF lf])
  3288       apply (rule subspace_span)
  3289       apply (rule x)
  3290       done}
  3291   moreover 
  3292   {fix x assume x: "x \<in> span S"
  3293     have th0:"(\<lambda>a. f a \<in> span (f ` S)) = {x. f x \<in> span (f ` S)}" apply (rule set_ext) 
  3294       unfolding mem_def Collect_def ..
  3295     have "f x \<in> span (f ` S)"
  3296       apply (rule span_induct[where S=S])
  3297       apply (rule span_superset)
  3298       apply simp
  3299       apply (subst th0)
  3300       apply (rule subspace_linear_preimage[OF lf subspace_span, of "f ` S"])
  3301       apply (rule x)
  3302       done}
  3303   ultimately show ?thesis by blast
  3304 qed
  3305 
  3306 (* The key breakdown property. *)
  3307 
  3308 lemma span_breakdown:
  3309   assumes bS: "(b::'a::ring_1 ^ 'n) \<in> S" and aS: "a \<in> span S"
  3310   shows "\<exists>k. a - k*s b \<in> span (S - {b})" (is "?P a")
  3311 proof-
  3312   {fix x assume xS: "x \<in> S"
  3313     {assume ab: "x = b"
  3314       then have "?P x"
  3315 	apply simp
  3316 	apply (rule exI[where x="1"], simp)
  3317 	by (rule span_0)}
  3318     moreover
  3319     {assume ab: "x \<noteq> b" 
  3320       then have "?P x"  using xS
  3321 	apply -
  3322 	apply (rule exI[where x=0])
  3323 	apply (rule span_superset)
  3324 	by simp}
  3325     ultimately have "?P x" by blast}
  3326   moreover have "subspace ?P" 
  3327     unfolding subspace_def 
  3328     apply auto
  3329     apply (simp add: mem_def)
  3330     apply (rule exI[where x=0])
  3331     using span_0[of "S - {b}"]
  3332     apply (simp add: mem_def)
  3333     apply (clarsimp simp add: mem_def)
  3334     apply (rule_tac x="k + ka" in exI)
  3335     apply (subgoal_tac "x + y - (k + ka) *s b = (x - k*s b) + (y - ka *s b)")
  3336     apply (simp only: )
  3337     apply (rule span_add[unfolded mem_def])
  3338     apply assumption+
  3339     apply (vector ring_simps)
  3340     apply (clarsimp simp add: mem_def)
  3341     apply (rule_tac x= "c*k" in exI)
  3342     apply (subgoal_tac "c *s x - (c * k) *s b = c*s (x - k*s b)")
  3343     apply (simp only: )
  3344     apply (rule span_mul[unfolded mem_def])
  3345     apply assumption
  3346     by (vector ring_simps)
  3347   ultimately show "?P a" using aS span_induct[where S=S and P= "?P"] by metis 
  3348 qed
  3349 
  3350 lemma span_breakdown_eq:
  3351   "(x::'a::ring_1^'n) \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. (x - k *s a) \<in> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
  3352 proof-
  3353   {assume x: "x \<in> span (insert a S)"
  3354     from x span_breakdown[of "a" "insert a S" "x"]
  3355     have ?rhs apply clarsimp
  3356       apply (rule_tac x= "k" in exI)
  3357       apply (rule set_rev_mp[of _ "span (S - {a})" _])
  3358       apply assumption
  3359       apply (rule span_mono)      
  3360       apply blast
  3361       done}
  3362   moreover
  3363   { fix k assume k: "x - k *s a \<in> span S"
  3364     have eq: "x = (x - k *s a) + k *s a" by vector
  3365     have "(x - k *s a) + k *s a \<in> span (insert a S)"
  3366       apply (rule span_add)
  3367       apply (rule set_rev_mp[of _ "span S" _])
  3368       apply (rule k)
  3369       apply (rule span_mono)      
  3370       apply blast
  3371       apply (rule span_mul)
  3372       apply (rule span_superset)
  3373       apply blast
  3374       done
  3375     then have ?lhs using eq by metis}
  3376   ultimately show ?thesis by blast
  3377 qed
  3378 
  3379 (* Hence some "reversal" results.*)
  3380 
  3381 lemma in_span_insert:
  3382   assumes a: "(a::'a::field^'n) \<in> span (insert b S)" and na: "a \<notin> span S"
  3383   shows "b \<in> span (insert a S)"
  3384 proof-
  3385   from span_breakdown[of b "insert b S" a, OF insertI1 a]
  3386   obtain k where k: "a - k*s b \<in> span (S - {b})" by auto
  3387   {assume k0: "k = 0"
  3388     with k have "a \<in> span S"
  3389       apply (simp)
  3390       apply (rule set_rev_mp)
  3391       apply assumption
  3392       apply (rule span_mono)
  3393       apply blast
  3394       done
  3395     with na  have ?thesis by blast}
  3396   moreover
  3397   {assume k0: "k \<noteq> 0" 
  3398     have eq: "b = (1/k) *s a - ((1/k) *s a - b)" by vector
  3399     from k0 have eq': "(1/k) *s (a - k*s b) = (1/k) *s a - b"
  3400       by (vector field_simps)
  3401     from k have "(1/k) *s (a - k*s b) \<in> span (S - {b})"
  3402       by (rule span_mul)
  3403     hence th: "(1/k) *s a - b \<in> span (S - {b})"
  3404       unfolding eq' .
  3405 
  3406     from k
  3407     have ?thesis
  3408       apply (subst eq)
  3409       apply (rule span_sub)
  3410       apply (rule span_mul)
  3411       apply (rule span_superset)
  3412       apply blast
  3413       apply (rule set_rev_mp)
  3414       apply (rule th)
  3415       apply (rule span_mono)
  3416       using na by blast}
  3417   ultimately show ?thesis by blast
  3418 qed
  3419 
  3420 lemma in_span_delete: 
  3421   assumes a: "(a::'a::field^'n) \<in> span S" 
  3422   and na: "a \<notin> span (S-{b})"
  3423   shows "b \<in> span (insert a (S - {b}))"
  3424   apply (rule in_span_insert)
  3425   apply (rule set_rev_mp)
  3426   apply (rule a)
  3427   apply (rule span_mono)
  3428   apply blast
  3429   apply (rule na)
  3430   done
  3431 
  3432 (* Transitivity property. *)
  3433 
  3434 lemma span_trans:
  3435   assumes x: "(x::'a::ring_1^'n) \<in> span S" and y: "y \<in> span (insert x S)"
  3436   shows "y \<in> span S"
  3437 proof-
  3438   from span_breakdown[of x "insert x S" y, OF insertI1 y]
  3439   obtain k where k: "y -k*s x \<in> span (S - {x})" by auto
  3440   have eq: "y = (y - k *s x) + k *s x" by vector
  3441   show ?thesis 
  3442     apply (subst eq)
  3443     apply (rule span_add)
  3444     apply (rule set_rev_mp)
  3445     apply (rule k)
  3446     apply (rule span_mono)
  3447     apply blast
  3448     apply (rule span_mul)
  3449     by (rule x)
  3450 qed
  3451 
  3452 (* ------------------------------------------------------------------------- *)
  3453 (* An explicit expansion is sometimes needed.                                *)
  3454 (* ------------------------------------------------------------------------- *)
  3455 
  3456 lemma span_explicit:
  3457   "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}"
  3458   (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
  3459 proof-
  3460   {fix x assume x: "x \<in> ?E"
  3461     then obtain S u where fS: "finite S" and SP: "S\<subseteq>P" and u: "setsum (\<lambda>v. u v *s v) S = x"
  3462       by blast
  3463     have "x \<in> span P"
  3464       unfolding u[symmetric]
  3465       apply (rule span_setsum[OF fS])
  3466       using span_mono[OF SP]
  3467       by (auto intro: span_superset span_mul)}
  3468   moreover
  3469   have "\<forall>x \<in> span P. x \<in> ?E"
  3470     unfolding mem_def Collect_def
  3471   proof(rule span_induct_alt')
  3472     show "?h 0"
  3473       apply (rule exI[where x="{}"]) by simp
  3474   next
  3475     fix c x y
  3476     assume x: "x \<in> P" and hy: "?h y"
  3477     from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P" 
  3478       and u: "setsum (\<lambda>v. u v *s v) S = y" by blast
  3479     let ?S = "insert x S"
  3480     let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c)
  3481                   else u y"
  3482     from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P" by blast+
  3483     {assume xS: "x \<in> S"
  3484       have S1: "S = (S - {x}) \<union> {x}" 
  3485 	and Sss:"finite (S - {x})" "finite {x}" "(S -{x}) \<inter> {x} = {}" using xS fS by auto
  3486       have "setsum (\<lambda>v. ?u v *s v) ?S =(\<Sum>v\<in>S - {x}. u v *s v) + (u x + c) *s x"
  3487 	using xS 
  3488 	by (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]] 
  3489 	  setsum_clauses(2)[OF fS] cong del: if_weak_cong)
  3490       also have "\<dots> = (\<Sum>v\<in>S. u v *s v) + c *s x"
  3491 	apply (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]])
  3492 	by (vector ring_simps)
  3493       also have "\<dots> = c*s x + y"
  3494 	by (simp add: add_commute u)
  3495       finally have "setsum (\<lambda>v. ?u v *s v) ?S = c*s x + y" .
  3496     then have "?Q ?S ?u (c*s x + y)" using th0 by blast}
  3497   moreover 
  3498   {assume xS: "x \<notin> S"
  3499     have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *s v) = y"
  3500       unfolding u[symmetric]
  3501       apply (rule setsum_cong2)
  3502       using xS by auto
  3503     have "?Q ?S ?u (c*s x + y)" using fS xS th0
  3504       by (simp add: th00 setsum_clauses add_commute cong del: if_weak_cong)}
  3505   ultimately have "?Q ?S ?u (c*s x + y)"
  3506     by (cases "x \<in> S", simp, simp)
  3507     then show "?h (c*s x + y)" 
  3508       apply -
  3509       apply (rule exI[where x="?S"])
  3510       apply (rule exI[where x="?u"]) by metis
  3511   qed
  3512   ultimately show ?thesis by blast
  3513 qed
  3514 
  3515 lemma dependent_explicit:
  3516   "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")
  3517 proof-
  3518   {assume dP: "dependent P"
  3519     then obtain a S u where aP: "a \<in> P" and fS: "finite S" 
  3520       and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *s v) S = a" 
  3521       unfolding dependent_def span_explicit by blast
  3522     let ?S = "insert a S" 
  3523     let ?u = "\<lambda>y. if y = a then - 1 else u y" 
  3524     let ?v = a
  3525     from aP SP have aS: "a \<notin> S" by blast
  3526     from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0" by auto
  3527     have s0: "setsum (\<lambda>v. ?u v *s v) ?S = 0"
  3528       using fS aS
  3529       apply (simp add: vector_smult_lneg vector_smult_lid setsum_clauses ring_simps )
  3530       apply (subst (2) ua[symmetric])
  3531       apply (rule setsum_cong2)
  3532       by auto
  3533     with th0 have ?rhs
  3534       apply -
  3535       apply (rule exI[where x= "?S"])
  3536       apply (rule exI[where x= "?u"])
  3537       by clarsimp}
  3538   moreover
  3539   {fix S u v assume fS: "finite S" 
  3540       and SP: "S \<subseteq> P" and vS: "v \<in> S" and uv: "u v \<noteq> 0" 
  3541     and u: "setsum (\<lambda>v. u v *s v) S = 0"
  3542     let ?a = v 
  3543     let ?S = "S - {v}"
  3544     let ?u = "\<lambda>i. (- u i) / u v"
  3545     have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"       using fS SP vS by auto 
  3546     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"
  3547       using fS vS uv 
  3548       by (simp add: setsum_diff1 vector_smult_lneg divide_inverse 
  3549 	vector_smult_assoc field_simps)
  3550     also have "\<dots> = ?a"
  3551       unfolding setsum_cmul u
  3552       using uv by (simp add: vector_smult_lneg)
  3553     finally  have "setsum (\<lambda>v. ?u v *s v) ?S = ?a" .
  3554     with th0 have ?lhs
  3555       unfolding dependent_def span_explicit
  3556       apply -
  3557       apply (rule bexI[where x= "?a"])
  3558       apply simp_all
  3559       apply (rule exI[where x= "?S"])
  3560       by auto}
  3561   ultimately show ?thesis by blast
  3562 qed
  3563 
  3564 
  3565 lemma span_finite:
  3566   assumes fS: "finite S"
  3567   shows "span S = {(y::'a::semiring_1^'n). \<exists>u. setsum (\<lambda>v. u v *s v) S = y}"
  3568   (is "_ = ?rhs")
  3569 proof-
  3570   {fix y assume y: "y \<in> span S"
  3571     from y obtain S' u where fS': "finite S'" and SS': "S' \<subseteq> S" and 
  3572       u: "setsum (\<lambda>v. u v *s v) S' = y" unfolding span_explicit by blast
  3573     let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
  3574     from setsum_restrict_set[OF fS, of "\<lambda>v. u v *s v" S', symmetric] SS'
  3575     have "setsum (\<lambda>v. ?u v *s v) S = setsum (\<lambda>v. u v *s v) S'"
  3576       unfolding cond_value_iff cond_application_beta
  3577       apply (simp add: cond_value_iff cong del: if_weak_cong)
  3578       apply (rule setsum_cong)
  3579       apply auto
  3580       done
  3581     hence "setsum (\<lambda>v. ?u v *s v) S = y" by (metis u)
  3582     hence "y \<in> ?rhs" by auto}
  3583   moreover 
  3584   {fix y u assume u: "setsum (\<lambda>v. u v *s v) S = y"
  3585     then have "y \<in> span S" using fS unfolding span_explicit by auto}
  3586   ultimately show ?thesis by blast
  3587 qed
  3588 
  3589 
  3590 (* Standard bases are a spanning set, and obviously finite.                  *)
  3591 
  3592 lemma span_stdbasis:"span {basis i :: 'a::ring_1^'n | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}} = UNIV"
  3593 apply (rule set_ext)
  3594 apply auto
  3595 apply (subst basis_expansion[symmetric])
  3596 apply (rule span_setsum)
  3597 apply simp
  3598 apply auto
  3599 apply (rule span_mul)
  3600 apply (rule span_superset)
  3601 apply (auto simp add: Collect_def mem_def)
  3602 done
  3603 
  3604   
  3605 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")
  3606 proof-
  3607   have eq: "?S = basis ` {1 .. ?n}" by blast
  3608   show ?thesis unfolding eq
  3609     apply (rule hassize_image_inj[OF basis_inj])
  3610     by (simp add: hassize_def)
  3611 qed
  3612 
  3613 lemma finite_stdbasis: "finite {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV:: 'n set)}}"
  3614   using has_size_stdbasis[unfolded hassize_def]
  3615   ..
  3616 
  3617 lemma card_stdbasis: "card {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} = dimindex(UNIV :: 'n set)"
  3618   using has_size_stdbasis[unfolded hassize_def]
  3619   ..
  3620 
  3621 lemma independent_stdbasis_lemma:
  3622   assumes x: "(x::'a::semiring_1 ^ 'n) \<in> span (basis ` S)"
  3623   and i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  3624   and iS: "i \<notin> S"
  3625   shows "(x$i) = 0"
  3626 proof-
  3627   let ?n = "dimindex (UNIV :: 'n set)"
  3628   let ?U = "{1 .. ?n}"
  3629   let ?B = "basis ` S"
  3630   let ?P = "\<lambda>(x::'a^'n). \<forall>i\<in> ?U. i \<notin> S \<longrightarrow> x$i =0"
  3631  {fix x::"'a^'n" assume xS: "x\<in> ?B"
  3632    from xS have "?P x" by (auto simp add: basis_component)}
  3633  moreover
  3634  have "subspace ?P" 
  3635    by (auto simp add: subspace_def Collect_def mem_def zero_index vector_component)
  3636  ultimately show ?thesis
  3637    using x span_induct[of ?B ?P x] i iS by blast 
  3638 qed
  3639 
  3640 lemma independent_stdbasis: "independent {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  3641 proof-
  3642   let ?n = "dimindex (UNIV :: 'n set)"
  3643   let ?I = "{1 .. ?n}"
  3644   let ?b = "basis :: nat \<Rightarrow> real ^'n"
  3645   let ?B = "?b ` ?I"
  3646   have eq: "{?b i|i. i \<in> ?I} = ?B"
  3647     by auto
  3648   {assume d: "dependent ?B"
  3649     then obtain k where k: "k \<in> ?I" "?b k \<in> span (?B - {?b k})"
  3650       unfolding dependent_def by auto
  3651     have eq1: "?B - {?b k} = ?B - ?b ` {k}"  by simp
  3652     have eq2: "?B - {?b k} = ?b ` (?I - {k})"
  3653       unfolding eq1
  3654       apply (rule inj_on_image_set_diff[symmetric])
  3655       apply (rule basis_inj) using k(1) by auto
  3656     from k(2) have th0: "?b k \<in> span (?b ` (?I - {k}))" unfolding eq2 .
  3657     from independent_stdbasis_lemma[OF th0 k(1), simplified]
  3658     have False by (simp add: basis_component[OF k(1), of k])}
  3659   then show ?thesis unfolding eq dependent_def ..
  3660 qed
  3661 
  3662 (* This is useful for building a basis step-by-step.                         *)
  3663 
  3664 lemma independent_insert:
  3665   "independent(insert (a::'a::field ^'n) S) \<longleftrightarrow>
  3666       (if a \<in> S then independent S
  3667                 else independent S \<and> a \<notin> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
  3668 proof-
  3669   {assume aS: "a \<in> S"
  3670     hence ?thesis using insert_absorb[OF aS] by simp}
  3671   moreover
  3672   {assume aS: "a \<notin> S"
  3673     {assume i: ?lhs
  3674       then have ?rhs using aS
  3675 	apply simp
  3676 	apply (rule conjI)
  3677 	apply (rule independent_mono)
  3678 	apply assumption
  3679 	apply blast
  3680 	by (simp add: dependent_def)}
  3681     moreover 
  3682     {assume i: ?rhs
  3683       have ?lhs using i aS
  3684 	apply simp
  3685 	apply (auto simp add: dependent_def)
  3686 	apply (case_tac "aa = a", auto)
  3687 	apply (subgoal_tac "insert a S - {aa} = insert a (S - {aa})")
  3688 	apply simp
  3689 	apply (subgoal_tac "a \<in> span (insert aa (S - {aa}))")
  3690 	apply (subgoal_tac "insert aa (S - {aa}) = S")
  3691 	apply simp
  3692 	apply blast
  3693 	apply (rule in_span_insert)
  3694 	apply assumption
  3695 	apply blast
  3696 	apply blast
  3697 	done}
  3698     ultimately have ?thesis by blast}
  3699   ultimately show ?thesis by blast
  3700 qed
  3701 
  3702 (* The degenerate case of the Exchange Lemma.  *)
  3703 
  3704 lemma mem_delete: "x \<in> (A - {a}) \<longleftrightarrow> x \<noteq> a \<and> x \<in> A"
  3705   by blast
  3706 
  3707 lemma span_span: "span (span A) = span A"
  3708   unfolding span_def hull_hull ..
  3709 
  3710 lemma span_inc: "S \<subseteq> span S"
  3711   by (metis subset_eq span_superset)
  3712 
  3713 lemma spanning_subset_independent:
  3714   assumes BA: "B \<subseteq> A" and iA: "independent (A::('a::field ^'n) set)" 
  3715   and AsB: "A \<subseteq> span B"
  3716   shows "A = B"
  3717 proof
  3718   from BA show "B \<subseteq> A" .
  3719 next
  3720   from span_mono[OF BA] span_mono[OF AsB]
  3721   have sAB: "span A = span B" unfolding span_span by blast
  3722 
  3723   {fix x assume x: "x \<in> A"
  3724     from iA have th0: "x \<notin> span (A - {x})"
  3725       unfolding dependent_def using x by blast
  3726     from x have xsA: "x \<in> span A" by (blast intro: span_superset)
  3727     have "A - {x} \<subseteq> A" by blast
  3728     hence th1:"span (A - {x}) \<subseteq> span A" by (metis span_mono)
  3729     {assume xB: "x \<notin> B"
  3730       from xB BA have "B \<subseteq> A -{x}" by blast
  3731       hence "span B \<subseteq> span (A - {x})" by (metis span_mono)
  3732       with th1 th0 sAB have "x \<notin> span A" by blast
  3733       with x have False by (metis span_superset)}
  3734     then have "x \<in> B" by blast}
  3735   then show "A \<subseteq> B" by blast
  3736 qed
  3737 
  3738 (* The general case of the Exchange Lemma, the key to what follows.  *)
  3739 
  3740 lemma exchange_lemma:
  3741   assumes f:"finite (t:: ('a::field^'n) set)" and i: "independent s"
  3742   and sp:"s \<subseteq> span t" 
  3743   shows "\<exists>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
  3744 using f i sp
  3745 proof(induct c\<equiv>"card(t - s)" arbitrary: s t rule: nat_less_induct)
  3746   fix n:: nat and s t :: "('a ^'n) set"
  3747   assume H: " \<forall>m<n. \<forall>(x:: ('a ^'n) set) xa.
  3748                 finite xa \<longrightarrow>
  3749                 independent x \<longrightarrow>
  3750                 x \<subseteq> span xa \<longrightarrow>
  3751                 m = card (xa - x) \<longrightarrow>
  3752                 (\<exists>t'. (t' hassize card xa) \<and>
  3753                       x \<subseteq> t' \<and> t' \<subseteq> x \<union> xa \<and> x \<subseteq> span t')"
  3754     and ft: "finite t" and s: "independent s" and sp: "s \<subseteq> span t"
  3755     and n: "n = card (t - s)"
  3756   let ?P = "\<lambda>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
  3757   let ?ths = "\<exists>t'. ?P t'" 
  3758   {assume st: "s \<subseteq> t" 
  3759     from st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t]) 
  3760       by (auto simp add: hassize_def intro: span_superset)}
  3761   moreover
  3762   {assume st: "t \<subseteq> s"
  3763     
  3764     from spanning_subset_independent[OF st s sp] 
  3765       st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t]) 
  3766       by (auto simp add: hassize_def intro: span_superset)}
  3767   moreover
  3768   {assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
  3769     from st(2) obtain b where b: "b \<in> t" "b \<notin> s" by blast
  3770       from b have "t - {b} - s \<subset> t - s" by blast
  3771       then have cardlt: "card (t - {b} - s) < n" using n ft
  3772  	by (auto intro: psubset_card_mono)
  3773       from b ft have ct0: "card t \<noteq> 0" by auto
  3774     {assume stb: "s \<subseteq> span(t -{b})"
  3775       from ft have ftb: "finite (t -{b})" by auto
  3776       from H[rule_format, OF cardlt ftb s stb] 
  3777       obtain u where u: "u hassize card (t-{b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u" by blast
  3778       let ?w = "insert b u"
  3779       have th0: "s \<subseteq> insert b u" using u by blast
  3780       from u(3) b have "u \<subseteq> s \<union> t" by blast 
  3781       then have th1: "insert b u \<subseteq> s \<union> t" using u b by blast
  3782       have bu: "b \<notin> u" using b u by blast
  3783       from u(1) have fu: "finite u" by (simp add: hassize_def)
  3784       from u(1) ft b have "u hassize (card t - 1)" by auto
  3785       then 
  3786       have th2: "insert b u hassize card t" 
  3787 	using  card_insert_disjoint[OF fu bu] ct0 by (auto simp add: hassize_def)
  3788       from u(4) have "s \<subseteq> span u" .
  3789       also have "\<dots> \<subseteq> span (insert b u)" apply (rule span_mono) by blast
  3790       finally have th3: "s \<subseteq> span (insert b u)" .      from th0 th1 th2 th3 have th: "?P ?w"  by blast
  3791       from th have ?ths by blast}
  3792     moreover
  3793     {assume stb: "\<not> s \<subseteq> span(t -{b})" 
  3794       from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})" by blast
  3795       have ab: "a \<noteq> b" using a b by blast
  3796       have at: "a \<notin> t" using a ab span_superset[of a "t- {b}"] by auto
  3797       have mlt: "card ((insert a (t - {b})) - s) < n" 
  3798 	using cardlt ft n  a b by auto
  3799       have ft': "finite (insert a (t - {b}))" using ft by auto
  3800       {fix x assume xs: "x \<in> s"
  3801 	have t: "t \<subseteq> (insert b (insert a (t -{b})))" using b by auto
  3802 	from b(1) have "b \<in> span t" by (simp add: span_superset)
  3803 	have bs: "b \<in> span (insert a (t - {b}))"
  3804 	  by (metis in_span_delete a sp mem_def subset_eq)
  3805 	from xs sp have "x \<in> span t" by blast
  3806 	with span_mono[OF t]
  3807 	have x: "x \<in> span (insert b (insert a (t - {b})))" ..
  3808 	from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))"  .}
  3809       then have sp': "s \<subseteq> span (insert a (t - {b}))" by blast
  3810       
  3811       from H[rule_format, OF mlt ft' s sp' refl] obtain u where 
  3812 	u: "u hassize card (insert a (t -{b}))" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t -{b})"
  3813 	"s \<subseteq> span u" by blast
  3814       from u a b ft at ct0 have "?P u" by (auto simp add: hassize_def)
  3815       then have ?ths by blast }
  3816     ultimately have ?ths by blast
  3817   }
  3818   ultimately 
  3819   show ?ths  by blast
  3820 qed
  3821 
  3822 (* This implies corresponding size bounds.                                   *)
  3823 
  3824 lemma independent_span_bound:
  3825   assumes f: "finite t" and i: "independent (s::('a::field^'n) set)" and sp:"s \<subseteq> span t"
  3826   shows "finite s \<and> card s \<le> card t"
  3827   by (metis exchange_lemma[OF f i sp] hassize_def finite_subset card_mono)
  3828 
  3829 lemma finite_Atleast_Atmost[simp]: "finite {f x |x. x\<in> {(i::'a::finite_intvl_succ) .. j}}"
  3830 proof-
  3831   have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
  3832   show ?thesis unfolding eq 
  3833     apply (rule finite_imageI)
  3834     apply (rule finite_intvl)
  3835     done
  3836 qed
  3837 
  3838 lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> {(i::nat) .. j}}"
  3839 proof-
  3840   have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
  3841   show ?thesis unfolding eq 
  3842     apply (rule finite_imageI)
  3843     apply (rule finite_atLeastAtMost)
  3844     done
  3845 qed
  3846 
  3847 
  3848 lemma independent_bound:
  3849   fixes S:: "(real^'n) set"
  3850   shows "independent S \<Longrightarrow> finite S \<and> card S <= dimindex(UNIV :: 'n set)"
  3851   apply (subst card_stdbasis[symmetric])
  3852   apply (rule independent_span_bound)
  3853   apply (rule finite_Atleast_Atmost_nat)
  3854   apply assumption
  3855   unfolding span_stdbasis 
  3856   apply (rule subset_UNIV)
  3857   done
  3858 
  3859 lemma dependent_biggerset: "(finite (S::(real ^'n) set) ==> card S > dimindex(UNIV:: 'n set)) ==> dependent S"
  3860   by (metis independent_bound not_less)
  3861 
  3862 (* Hence we can create a maximal independent subset.                         *)
  3863 
  3864 lemma maximal_independent_subset_extend:
  3865   assumes sv: "(S::(real^'n) set) \<subseteq> V" and iS: "independent S"
  3866   shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  3867   using sv iS
  3868 proof(induct d\<equiv> "dimindex (UNIV :: 'n set) - card S" arbitrary: S rule: nat_less_induct)
  3869   fix n and S:: "(real^'n) set"
  3870   assume H: "\<forall>m<n. \<forall>S \<subseteq> V. independent S \<longrightarrow> m = dimindex (UNIV::'n set) - card S \<longrightarrow>
  3871               (\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B)"
  3872     and sv: "S \<subseteq> V" and i: "independent S" and n: "n = dimindex (UNIV :: 'n set) - card S"
  3873   let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  3874   let ?ths = "\<exists>x. ?P x"
  3875   let ?d = "dimindex (UNIV :: 'n set)"
  3876   {assume "V \<subseteq> span S"
  3877     then have ?ths  using sv i by blast }
  3878   moreover
  3879   {assume VS: "\<not> V \<subseteq> span S"
  3880     from VS obtain a where a: "a \<in> V" "a \<notin> span S" by blast
  3881     from a have aS: "a \<notin> S" by (auto simp add: span_superset)
  3882     have th0: "insert a S \<subseteq> V" using a sv by blast
  3883     from independent_insert[of a S]  i a 
  3884     have th1: "independent (insert a S)" by auto
  3885     have mlt: "?d - card (insert a S) < n" 
  3886       using aS a n independent_bound[OF th1] dimindex_ge_1[of "UNIV :: 'n set"] 
  3887       by auto 
  3888       
  3889     from H[rule_format, OF mlt th0 th1 refl] 
  3890     obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B" 
  3891       by blast
  3892     from B have "?P B" by auto
  3893     then have ?ths by blast}
  3894   ultimately show ?ths by blast
  3895 qed
  3896 
  3897 lemma maximal_independent_subset:
  3898   "\<exists>(B:: (real ^'n) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  3899   by (metis maximal_independent_subset_extend[of "{}:: (real ^'n) set"] empty_subsetI independent_empty)
  3900 
  3901 (* Notion of dimension.                                                      *)
  3902 
  3903 definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n))"
  3904 
  3905 lemma basis_exists:  "\<exists>B. (B :: (real ^'n) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize dim V)" 
  3906 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)"]
  3907 unfolding hassize_def
  3908 using maximal_independent_subset[of V] independent_bound
  3909 by auto
  3910 
  3911 (* Consequences of independence or spanning for cardinality.                 *)
  3912 
  3913 lemma independent_card_le_dim: "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B \<le> dim V"
  3914 by (metis basis_exists[of V] independent_span_bound[where ?'a=real] hassize_def subset_trans)
  3915 
  3916 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"
  3917   by (metis basis_exists[of V] independent_span_bound hassize_def subset_trans)
  3918 
  3919 lemma basis_card_eq_dim:
  3920   "B \<subseteq> (V:: (real ^'n) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
  3921   by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_mono)
  3922 
  3923 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"
  3924   by (metis basis_card_eq_dim hassize_def)
  3925 
  3926 (* More lemmas about dimension.                                              *)
  3927 
  3928 lemma dim_univ: "dim (UNIV :: (real^'n) set) = dimindex (UNIV :: 'n set)"
  3929   apply (rule dim_unique[of "{basis i |i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}"])
  3930   by (auto simp only: span_stdbasis has_size_stdbasis independent_stdbasis)
  3931 
  3932 lemma dim_subset:
  3933   "(S:: (real ^'n) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
  3934   using basis_exists[of T] basis_exists[of S]
  3935   by (metis independent_span_bound[where ?'a = real and ?'n = 'n] subset_eq hassize_def)
  3936 
  3937 lemma dim_subset_univ: "dim (S:: (real^'n) set) \<le> dimindex (UNIV :: 'n set)"
  3938   by (metis dim_subset subset_UNIV dim_univ)
  3939 
  3940 (* Converses to those.                                                       *)
  3941 
  3942 lemma card_ge_dim_independent:
  3943   assumes BV:"(B::(real ^'n) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
  3944   shows "V \<subseteq> span B"
  3945 proof-
  3946   {fix a assume aV: "a \<in> V"
  3947     {assume aB: "a \<notin> span B"
  3948       then have iaB: "independent (insert a B)" using iB aV  BV by (simp add: independent_insert)
  3949       from aV BV have th0: "insert a B \<subseteq> V" by blast
  3950       from aB have "a \<notin>B" by (auto simp add: span_superset)
  3951       with independent_card_le_dim[OF th0 iaB] dVB  have False by auto}
  3952     then have "a \<in> span B"  by blast}
  3953   then show ?thesis by blast
  3954 qed
  3955 
  3956 lemma card_le_dim_spanning:
  3957   assumes BV: "(B:: (real ^'n) set) \<subseteq> V" and VB: "V \<subseteq> span B" 
  3958   and fB: "finite B" and dVB: "dim V \<ge> card B"
  3959   shows "independent B"
  3960 proof-
  3961   {fix a assume a: "a \<in> B" "a \<in> span (B -{a})"
  3962     from a fB have c0: "card B \<noteq> 0" by auto
  3963     from a fB have cb: "card (B -{a}) = card B - 1" by auto
  3964     from BV a have th0: "B -{a} \<subseteq> V" by blast
  3965     {fix x assume x: "x \<in> V"
  3966       from a have eq: "insert a (B -{a}) = B" by blast
  3967       from x VB have x': "x \<in> span B" by blast 
  3968       from span_trans[OF a(2), unfolded eq, OF x']
  3969       have "x \<in> span (B -{a})" . }
  3970     then have th1: "V \<subseteq> span (B -{a})" by blast 
  3971     have th2: "finite (B -{a})" using fB by auto
  3972     from span_card_ge_dim[OF th0 th1 th2]
  3973     have c: "dim V \<le> card (B -{a})" .
  3974     from c c0 dVB cb have False by simp}
  3975   then show ?thesis unfolding dependent_def by blast
  3976 qed
  3977 
  3978 lemma card_eq_dim: "(B:: (real ^'n) set) \<subseteq> V \<Longrightarrow> B hassize dim V \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
  3979   by (metis hassize_def order_eq_iff card_le_dim_spanning 
  3980     card_ge_dim_independent)
  3981 
  3982 (* ------------------------------------------------------------------------- *)
  3983 (* More general size bound lemmas.                                           *)
  3984 (* ------------------------------------------------------------------------- *)
  3985 
  3986 lemma independent_bound_general:
  3987   "independent (S:: (real^'n) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
  3988   by (metis independent_card_le_dim independent_bound subset_refl)
  3989 
  3990 lemma dependent_biggerset_general: "(finite (S:: (real^'n) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
  3991   using independent_bound_general[of S] by (metis linorder_not_le) 
  3992 
  3993 lemma dim_span: "dim (span (S:: (real ^'n) set)) = dim S"
  3994 proof-
  3995   have th0: "dim S \<le> dim (span S)" 
  3996     by (auto simp add: subset_eq intro: dim_subset span_superset)
  3997   from basis_exists[of S] 
  3998   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
  3999   from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
  4000   have bSS: "B \<subseteq> span S" using B(1) by (metis subset_eq span_inc) 
  4001   have sssB: "span S \<subseteq> span B" using span_mono[OF B(3)] by (simp add: span_span) 
  4002   from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis 
  4003     using fB(2)  by arith
  4004 qed
  4005 
  4006 lemma subset_le_dim: "(S:: (real ^'n) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
  4007   by (metis dim_span dim_subset)
  4008 
  4009 lemma span_eq_dim: "span (S:: (real ^'n) set) = span T ==> dim S = dim T"
  4010   by (metis dim_span)
  4011 
  4012 lemma spans_image:
  4013   assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and VB: "V \<subseteq> span B"
  4014   shows "f ` V \<subseteq> span (f ` B)"
  4015   unfolding span_linear_image[OF lf]
  4016   by (metis VB image_mono)
  4017 
  4018 lemma dim_image_le: assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S:: (real ^'n) set)"
  4019 proof-
  4020   from basis_exists[of S] obtain B where 
  4021     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
  4022   from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
  4023   have "dim (f ` S) \<le> card (f ` B)"
  4024     apply (rule span_card_ge_dim)
  4025     using lf B fB by (auto simp add: span_linear_image spans_image subset_image_iff)
  4026   also have "\<dots> \<le> dim S" using card_image_le[OF fB(1)] fB by simp
  4027   finally show ?thesis .
  4028 qed
  4029 
  4030 (* Relation between bases and injectivity/surjectivity of map.               *)
  4031 
  4032 lemma spanning_surjective_image:
  4033   assumes us: "UNIV \<subseteq> span (S:: ('a::semiring_1 ^'n) set)" 
  4034   and lf: "linear f" and sf: "surj f"
  4035   shows "UNIV \<subseteq> span (f ` S)"
  4036 proof-
  4037   have "UNIV \<subseteq> f ` UNIV" using sf by (auto simp add: surj_def)
  4038   also have " \<dots> \<subseteq> span (f ` S)" using spans_image[OF lf us] .
  4039 finally show ?thesis .
  4040 qed
  4041 
  4042 lemma independent_injective_image:
  4043   assumes iS: "independent (S::('a::semiring_1^'n) set)" and lf: "linear f" and fi: "inj f"
  4044   shows "independent (f ` S)"
  4045 proof-
  4046   {fix a assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
  4047     have eq: "f ` S - {f a} = f ` (S - {a})" using fi
  4048       by (auto simp add: inj_on_def)
  4049     from a have "f a \<in> f ` span (S -{a})"
  4050       unfolding eq span_linear_image[OF lf, of "S - {a}"]  by blast
  4051     hence "a \<in> span (S -{a})" using fi by (auto simp add: inj_on_def)
  4052     with a(1) iS  have False by (simp add: dependent_def) }
  4053   then show ?thesis unfolding dependent_def by blast
  4054 qed 
  4055 
  4056 (* ------------------------------------------------------------------------- *)
  4057 (* Picking an orthogonal replacement for a spanning set.                     *)
  4058 (* ------------------------------------------------------------------------- *)
  4059     (* FIXME : Move to some general theory ?*)
  4060 definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
  4061 
  4062 lemma vector_sub_project_orthogonal: "(b::'a::ordered_field^'n) \<bullet> (x - ((b \<bullet> x) / (b\<bullet>b)) *s b) = 0"
  4063   apply (cases "b = 0", simp)
  4064   apply (simp add: dot_rsub dot_rmult)
  4065   unfolding times_divide_eq_right[symmetric]
  4066   by (simp add: field_simps dot_eq_0)
  4067 
  4068 lemma basis_orthogonal:
  4069   fixes B :: "(real ^'n) set"
  4070   assumes fB: "finite B"
  4071   shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
  4072   (is " \<exists>C. ?P B C")
  4073 proof(induct rule: finite_induct[OF fB])
  4074   case 1 thus ?case apply (rule exI[where x="{}"]) by (auto simp add: pairwise_def)
  4075 next
  4076   case (2 a B)
  4077   note fB = `finite B` and aB = `a \<notin> B` 
  4078   from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C` 
  4079   obtain C where C: "finite C" "card C \<le> card B" 
  4080     "span C = span B" "pairwise orthogonal C" by blast
  4081   let ?a = "a - setsum (\<lambda>x. (x\<bullet>a / (x\<bullet>x)) *s x) C"
  4082   let ?C = "insert ?a C"
  4083   from C(1) have fC: "finite ?C" by simp
  4084   from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)" by (simp add: card_insert_if)
  4085   {fix x k 
  4086     have th0: "\<And>(a::'b::comm_ring) b c. a - (b - c) = c + (a - b)" by (simp add: ring_simps)
  4087     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"
  4088       apply (simp only: vector_ssub_ldistrib th0)
  4089       apply (rule span_add_eq)
  4090       apply (rule span_mul)
  4091       apply (rule span_setsum[OF C(1)])
  4092       apply clarify
  4093       apply (rule span_mul)
  4094       by (rule span_superset)}
  4095   then have SC: "span ?C = span (insert a B)"
  4096     unfolding expand_set_eq span_breakdown_eq C(3)[symmetric] by auto
  4097   thm pairwise_def 
  4098   {fix x y assume xC: "x \<in> ?C" and yC: "y \<in> ?C" and xy: "x \<noteq> y"
  4099     {assume xa: "x = ?a" and ya: "y = ?a" 
  4100       have "orthogonal x y" using xa ya xy by blast}
  4101     moreover
  4102     {assume xa: "x = ?a" and ya: "y \<noteq> ?a" "y \<in> C" 
  4103       from ya have Cy: "C = insert y (C - {y})" by blast
  4104       have fth: "finite (C - {y})" using C by simp
  4105       have "orthogonal x y"
  4106 	using xa ya
  4107 	unfolding orthogonal_def xa dot_lsub dot_rsub diff_eq_0_iff_eq
  4108 	apply simp 
  4109 	apply (subst Cy)
  4110 	using C(1) fth
  4111 	apply (simp only: setsum_clauses)
  4112 	apply (auto simp add: dot_ladd dot_lmult dot_eq_0 dot_sym[of y a] dot_lsum[OF fth])
  4113 	apply (rule setsum_0')
  4114 	apply clarsimp
  4115 	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
  4116 	by auto}
  4117     moreover
  4118     {assume xa: "x \<noteq> ?a" "x \<in> C" and ya: "y = ?a" 
  4119       from xa have Cx: "C = insert x (C - {x})" by blast
  4120       have fth: "finite (C - {x})" using C by simp
  4121       have "orthogonal x y"
  4122 	using xa ya
  4123 	unfolding orthogonal_def ya dot_rsub dot_lsub diff_eq_0_iff_eq
  4124 	apply simp 
  4125 	apply (subst Cx)
  4126 	using C(1) fth
  4127 	apply (simp only: setsum_clauses)
  4128 	apply (subst dot_sym[of x])
  4129 	apply (auto simp add: dot_radd dot_rmult dot_eq_0 dot_sym[of x a] dot_rsum[OF fth])
  4130 	apply (rule setsum_0')
  4131 	apply clarsimp
  4132 	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
  4133 	by auto}
  4134     moreover
  4135     {assume xa: "x \<in> C" and ya: "y \<in> C" 
  4136       have "orthogonal x y" using xa ya xy C(4) unfolding pairwise_def by blast}
  4137     ultimately have "orthogonal x y" using xC yC by blast}
  4138   then have CPO: "pairwise orthogonal ?C" unfolding pairwise_def by blast
  4139   from fC cC SC CPO have "?P (insert a B) ?C" by blast
  4140   then show ?case by blast 
  4141 qed
  4142 
  4143 lemma orthogonal_basis_exists:
  4144   fixes V :: "(real ^'n) set"
  4145   shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (B hassize dim V) \<and> pairwise orthogonal B"
  4146 proof-
  4147   from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "B hassize dim V" by blast
  4148   from B have fB: "finite B" "card B = dim V" by (simp_all add: hassize_def)
  4149   from basis_orthogonal[OF fB(1)] obtain C where 
  4150     C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C" by blast
  4151   from C B 
  4152   have CSV: "C \<subseteq> span V" by (metis span_inc span_mono subset_trans) 
  4153   from span_mono[OF B(3)]  C have SVC: "span V \<subseteq> span C" by (simp add: span_span)
  4154   from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
  4155   have iC: "independent C" by (simp add: dim_span) 
  4156   from C fB have "card C \<le> dim V" by simp
  4157   moreover have "dim V \<le> card C" using span_card_ge_dim[OF CSV SVC C(1)]
  4158     by (simp add: dim_span)
  4159   ultimately have CdV: "C hassize dim V" unfolding hassize_def using C(1) by simp
  4160   from C B CSV CdV iC show ?thesis by auto 
  4161 qed
  4162 
  4163 lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
  4164   by (metis set_eq_subset span_mono span_span span_inc)
  4165 
  4166 (* ------------------------------------------------------------------------- *)
  4167 (* Low-dimensional subset is in a hyperplane (weak orthogonal complement).   *)
  4168 (* ------------------------------------------------------------------------- *)
  4169 
  4170 lemma span_not_univ_orthogonal:
  4171   assumes sU: "span S \<noteq> UNIV"
  4172   shows "\<exists>(a:: real ^'n). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
  4173 proof-
  4174   from sU obtain a where a: "a \<notin> span S" by blast
  4175   from orthogonal_basis_exists obtain B where 
  4176     B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "B hassize dim S" "pairwise orthogonal B" 
  4177     by blast
  4178   from B have fB: "finite B" "card B = dim S" by (simp_all add: hassize_def)
  4179   from span_mono[OF B(2)] span_mono[OF B(3)]
  4180   have sSB: "span S = span B" by (simp add: span_span)
  4181   let ?a = "a - setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B"
  4182   have "setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B \<in> span S"
  4183     unfolding sSB
  4184     apply (rule span_setsum[OF fB(1)])
  4185     apply clarsimp
  4186     apply (rule span_mul)
  4187     by (rule span_superset)
  4188   with a have a0:"?a  \<noteq> 0" by auto
  4189   have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
  4190   proof(rule span_induct')
  4191     show "subspace (\<lambda>x. ?a \<bullet> x = 0)"
  4192       by (auto simp add: subspace_def mem_def dot_radd dot_rmult) 
  4193   next
  4194     {fix x assume x: "x \<in> B"
  4195       from x have B': "B = insert x (B - {x})" by blast
  4196       have fth: "finite (B - {x})" using fB by simp
  4197       have "?a \<bullet> x = 0" 
  4198 	apply (subst B') using fB fth
  4199 	unfolding setsum_clauses(2)[OF fth]
  4200 	apply simp
  4201 	apply (clarsimp simp add: dot_lsub dot_ladd dot_lmult dot_lsum dot_eq_0)
  4202 	apply (rule setsum_0', rule ballI)
  4203 	unfolding dot_sym
  4204 	by (auto simp add: x field_simps dot_eq_0 intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])}
  4205     then show "\<forall>x \<in> B. ?a \<bullet> x = 0" by blast
  4206   qed
  4207   with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"])
  4208 qed
  4209 
  4210 lemma span_not_univ_subset_hyperplane: 
  4211   assumes SU: "span S \<noteq> (UNIV ::(real^'n) set)"
  4212   shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  4213   using span_not_univ_orthogonal[OF SU] by auto
  4214 
  4215 lemma lowdim_subset_hyperplane:
  4216   assumes d: "dim S < dimindex (UNIV :: 'n set)"
  4217   shows "\<exists>(a::real ^'n). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  4218 proof-
  4219   {assume "span S = UNIV"
  4220     hence "dim (span S) = dim (UNIV :: (real ^'n) set)" by simp
  4221     hence "dim S = dimindex (UNIV :: 'n set)" by (simp add: dim_span dim_univ)
  4222     with d have False by arith}
  4223   hence th: "span S \<noteq> UNIV" by blast
  4224   from span_not_univ_subset_hyperplane[OF th] show ?thesis .
  4225 qed
  4226 
  4227 (* We can extend a linear basis-basis injection to the whole set.            *)
  4228 
  4229 lemma linear_indep_image_lemma:
  4230   assumes lf: "linear f" and fB: "finite B" 
  4231   and ifB: "independent (f ` B)"
  4232   and fi: "inj_on f B" and xsB: "x \<in> span B" 
  4233   and fx: "f (x::'a::field^'n) = 0"
  4234   shows "x = 0"
  4235   using fB ifB fi xsB fx
  4236 proof(induct arbitrary: x rule: finite_induct[OF fB])
  4237   case 1 thus ?case by (auto simp add:  span_empty)
  4238 next
  4239   case (2 a b x)
  4240   have fb: "finite b" using "2.prems" by simp
  4241   have th0: "f ` b \<subseteq> f ` (insert a b)"
  4242     apply (rule image_mono) by blast 
  4243   from independent_mono[ OF "2.prems"(2) th0]
  4244   have ifb: "independent (f ` b)"  .
  4245   have fib: "inj_on f b" 
  4246     apply (rule subset_inj_on [OF "2.prems"(3)]) 
  4247     by blast
  4248   from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
  4249   obtain k where k: "x - k*s a \<in> span (b -{a})" by blast
  4250   have "f (x - k*s a) \<in> span (f ` b)"
  4251     unfolding span_linear_image[OF lf]
  4252     apply (rule imageI)
  4253     using k span_mono[of "b-{a}" b] by blast
  4254   hence "f x - k*s f a \<in> span (f ` b)"
  4255     by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
  4256   hence th: "-k *s f a \<in> span (f ` b)" 
  4257     using "2.prems"(5) by (simp add: vector_smult_lneg)
  4258   {assume k0: "k = 0" 
  4259     from k0 k have "x \<in> span (b -{a})" by simp
  4260     then have "x \<in> span b" using span_mono[of "b-{a}" b]
  4261       by blast}
  4262   moreover
  4263   {assume k0: "k \<noteq> 0"
  4264     from span_mul[OF th, of "- 1/ k"] k0
  4265     have th1: "f a \<in> span (f ` b)" 
  4266       by (auto simp add: vector_smult_assoc)
  4267     from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
  4268     have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
  4269     from "2.prems"(2)[unfolded dependent_def bex_simps(10), rule_format, of "f a"]
  4270     have "f a \<notin> span (f ` b)" using tha
  4271       using "2.hyps"(2)
  4272       "2.prems"(3) by auto
  4273     with th1 have False by blast
  4274     then have "x \<in> span b" by blast}
  4275   ultimately have xsb: "x \<in> span b" by blast
  4276   from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)]
  4277   show "x = 0" .
  4278 qed
  4279 
  4280 (* We can extend a linear mapping from basis.                                *)
  4281 
  4282 lemma linear_independent_extend_lemma:
  4283   assumes fi: "finite B" and ib: "independent B"
  4284   shows "\<exists>g. (\<forall>x\<in> span B. \<forall>y\<in> span B. g ((x::'a::field^'n) + y) = g x + g y) 
  4285            \<and> (\<forall>x\<in> span B. \<forall>c. g (c*s x) = c *s g x)
  4286            \<and> (\<forall>x\<in> B. g x = f x)"
  4287 using ib fi
  4288 proof(induct rule: finite_induct[OF fi])
  4289   case 1 thus ?case by (auto simp add: span_empty) 
  4290 next
  4291   case (2 a b)
  4292   from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
  4293     by (simp_all add: independent_insert)
  4294   from "2.hyps"(3)[OF ibf] obtain g where 
  4295     g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
  4296     "\<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
  4297   let ?h = "\<lambda>z. SOME k. (z - k *s a) \<in> span b"
  4298   {fix z assume z: "z \<in> span (insert a b)"
  4299     have th0: "z - ?h z *s a \<in> span b"
  4300       apply (rule someI_ex)
  4301       unfolding span_breakdown_eq[symmetric]
  4302       using z .
  4303     {fix k assume k: "z - k *s a \<in> span b"
  4304       have eq: "z - ?h z *s a - (z - k*s a) = (k - ?h z) *s a" 
  4305 	by (simp add: ring_simps vector_sadd_rdistrib[symmetric])
  4306       from span_sub[OF th0 k]
  4307       have khz: "(k - ?h z) *s a \<in> span b" by (simp add: eq)
  4308       {assume "k \<noteq> ?h z" hence k0: "k - ?h z \<noteq> 0" by simp
  4309 	from k0 span_mul[OF khz, of "1 /(k - ?h z)"] 
  4310 	have "a \<in> span b" by (simp add: vector_smult_assoc)
  4311 	with "2.prems"(1) "2.hyps"(2) have False
  4312 	  by (auto simp add: dependent_def)}
  4313       then have "k = ?h z" by blast}
  4314     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}
  4315   note h = this
  4316   let ?g = "\<lambda>z. ?h z *s f a + g (z - ?h z *s a)"
  4317   {fix x y assume x: "x \<in> span (insert a b)" and y: "y \<in> span (insert a b)"
  4318     have tha: "\<And>(x::'a^'n) y a k l. (x + y) - (k + l) *s a = (x - k *s a) + (y - l *s a)" 
  4319       by (vector ring_simps)
  4320     have addh: "?h (x + y) = ?h x + ?h y"
  4321       apply (rule conjunct2[OF h, rule_format, symmetric])
  4322       apply (rule span_add[OF x y])
  4323       unfolding tha
  4324       by (metis span_add x y conjunct1[OF h, rule_format])
  4325     have "?g (x + y) = ?g x + ?g y" 
  4326       unfolding addh tha
  4327       g(1)[rule_format,OF conjunct1[OF h, OF x] conjunct1[OF h, OF y]]
  4328       by (simp add: vector_sadd_rdistrib)}
  4329   moreover
  4330   {fix x:: "'a^'n" and c:: 'a  assume x: "x \<in> span (insert a b)"
  4331     have tha: "\<And>(x::'a^'n) c k a. c *s x - (c * k) *s a = c *s (x - k *s a)" 
  4332       by (vector ring_simps)
  4333     have hc: "?h (c *s x) = c * ?h x" 
  4334       apply (rule conjunct2[OF h, rule_format, symmetric])
  4335       apply (metis span_mul x)
  4336       by (metis tha span_mul x conjunct1[OF h])
  4337     have "?g (c *s x) = c*s ?g x" 
  4338       unfolding hc tha g(2)[rule_format, OF conjunct1[OF h, OF x]]
  4339       by (vector ring_simps)}
  4340   moreover
  4341   {fix x assume x: "x \<in> (insert a b)"
  4342     {assume xa: "x = a"
  4343       have ha1: "1 = ?h a"
  4344 	apply (rule conjunct2[OF h, rule_format])
  4345 	apply (metis span_superset insertI1)
  4346 	using conjunct1[OF h, OF span_superset, OF insertI1]
  4347 	by (auto simp add: span_0)
  4348 
  4349       from xa ha1[symmetric] have "?g x = f x" 
  4350 	apply simp
  4351 	using g(2)[rule_format, OF span_0, of 0]
  4352 	by simp}
  4353     moreover
  4354     {assume xb: "x \<in> b"
  4355       have h0: "0 = ?h x"
  4356 	apply (rule conjunct2[OF h, rule_format])
  4357 	apply (metis  span_superset insertI1 xb x)
  4358 	apply simp
  4359 	apply (metis span_superset xb)
  4360 	done
  4361       have "?g x = f x"
  4362 	by (simp add: h0[symmetric] g(3)[rule_format, OF xb])}
  4363     ultimately have "?g x = f x" using x by blast }
  4364   ultimately show ?case apply - apply (rule exI[where x="?g"]) by blast
  4365 qed
  4366 
  4367 lemma linear_independent_extend:
  4368   assumes iB: "independent (B:: (real ^'n) set)"
  4369   shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
  4370 proof-
  4371   from maximal_independent_subset_extend[of B "UNIV"] iB
  4372   obtain C where C: "B \<subseteq> C" "independent C" "\<And>x. x \<in> span C" by auto
  4373   
  4374   from C(2) independent_bound[of C] linear_independent_extend_lemma[of C f]
  4375   obtain g where g: "(\<forall>x\<in> span C. \<forall>y\<in> span C. g (x + y) = g x + g y) 
  4376            \<and> (\<forall>x\<in> span C. \<forall>c. g (c*s x) = c *s g x)
  4377            \<and> (\<forall>x\<in> C. g x = f x)" by blast
  4378   from g show ?thesis unfolding linear_def using C 
  4379     apply clarsimp by blast
  4380 qed
  4381 
  4382 (* Can construct an isomorphism between spaces of same dimension.            *)
  4383 
  4384 lemma card_le_inj: assumes fA: "finite A" and fB: "finite B"
  4385   and c: "card A \<le> card B" shows "(\<exists>f. f ` A \<subseteq> B \<and> inj_on f A)"
  4386 using fB c
  4387 proof(induct arbitrary: B rule: finite_induct[OF fA])
  4388   case 1 thus ?case by simp
  4389 next
  4390   case (2 x s t) 
  4391   thus ?case
  4392   proof(induct rule: finite_induct[OF "2.prems"(1)])
  4393     case 1    then show ?case by simp
  4394   next
  4395     case (2 y t)
  4396     from "2.prems"(1,2,5) "2.hyps"(1,2) have cst:"card s \<le> card t" by simp
  4397     from "2.prems"(3) [OF "2.hyps"(1) cst] obtain f where
  4398       f: "f ` s \<subseteq> t \<and> inj_on f s" by blast
  4399     from f "2.prems"(2) "2.hyps"(2) show ?case
  4400       apply -
  4401       apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
  4402       by (auto simp add: inj_on_def)
  4403   qed
  4404 qed
  4405 
  4406 lemma card_subset_eq: assumes fB: "finite B" and AB: "A \<subseteq> B" and 
  4407   c: "card A = card B"
  4408   shows "A = B"
  4409 proof-
  4410   from fB AB have fA: "finite A" by (auto intro: finite_subset)
  4411   from fA fB have fBA: "finite (B - A)" by auto
  4412   have e: "A \<inter> (B - A) = {}" by blast
  4413   have eq: "A \<union> (B - A) = B" using AB by blast
  4414   from card_Un_disjoint[OF fA fBA e, unfolded eq c]
  4415   have "card (B - A) = 0" by arith
  4416   hence "B - A = {}" unfolding card_eq_0_iff using fA fB by simp
  4417   with AB show "A = B" by blast  
  4418 qed
  4419 
  4420 lemma subspace_isomorphism:
  4421   assumes s: "subspace (S:: (real ^'n) set)" and t: "subspace T" 
  4422   and d: "dim S = dim T"
  4423   shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
  4424 proof-
  4425   from basis_exists[of S] obtain B where 
  4426     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
  4427   from basis_exists[of T] obtain C where 
  4428     C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "C hassize dim T" by blast
  4429   from B(4) C(4) card_le_inj[of B C] d obtain f where
  4430     f: "f ` B \<subseteq> C" "inj_on f B" unfolding hassize_def by auto 
  4431   from linear_independent_extend[OF B(2)] obtain g where
  4432     g: "linear g" "\<forall>x\<in> B. g x = f x" by blast
  4433   from B(4) have fB: "finite B" by (simp add: hassize_def)
  4434   from C(4) have fC: "finite C" by (simp add: hassize_def)
  4435   from inj_on_iff_eq_card[OF fB, of f] f(2) 
  4436   have "card (f ` B) = card B" by simp
  4437   with B(4) C(4) have ceq: "card (f ` B) = card C" using d 
  4438     by (simp add: hassize_def)
  4439   have "g ` B = f ` B" using g(2)
  4440     by (auto simp add: image_iff)
  4441   also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
  4442   finally have gBC: "g ` B = C" .
  4443   have gi: "inj_on g B" using f(2) g(2)
  4444     by (auto simp add: inj_on_def)
  4445   note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
  4446   {fix x y assume x: "x \<in> S" and y: "y \<in> S" and gxy:"g x = g y"
  4447     from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B" by blast+
  4448     from gxy have th0: "g (x - y) = 0" by (simp add: linear_sub[OF g(1)])
  4449     have th1: "x - y \<in> span B" using x' y' by (metis span_sub) 
  4450     have "x=y" using g0[OF th1 th0] by simp }
  4451   then have giS: "inj_on g S" 
  4452     unfolding inj_on_def by blast
  4453   from span_subspace[OF B(1,3) s]
  4454   have "g ` S = span (g ` B)" by (simp add: span_linear_image[OF g(1)])
  4455   also have "\<dots> = span C" unfolding gBC ..
  4456   also have "\<dots> = T" using span_subspace[OF C(1,3) t] .
  4457   finally have gS: "g ` S = T" .
  4458   from g(1) gS giS show ?thesis by blast
  4459 qed
  4460 
  4461 (* linear functions are equal on a subspace if they are on a spanning set.   *)
  4462 
  4463 lemma subspace_kernel:
  4464   assumes lf: "linear (f::'a::semiring_1 ^'n \<Rightarrow> _)"
  4465   shows "subspace {x. f x = 0}"
  4466 apply (simp add: subspace_def)
  4467 by (simp add: linear_add[OF lf] linear_cmul[OF lf] linear_0[OF lf])
  4468 
  4469 lemma linear_eq_0_span:
  4470   assumes lf: "linear f" and f0: "\<forall>x\<in>B. f x = 0"
  4471   shows "\<forall>x \<in> span B. f x = (0::'a::semiring_1 ^'n)"
  4472 proof
  4473   fix x assume x: "x \<in> span B"
  4474   let ?P = "\<lambda>x. f x = 0"
  4475   from subspace_kernel[OF lf] have "subspace ?P" unfolding Collect_def .
  4476   with x f0 span_induct[of B "?P" x] show "f x = 0" by blast
  4477 qed
  4478 
  4479 lemma linear_eq_0:
  4480   assumes lf: "linear f" and SB: "S \<subseteq> span B" and f0: "\<forall>x\<in>B. f x = 0" 
  4481   shows "\<forall>x \<in> S. f x = (0::'a::semiring_1^'n)"
  4482   by (metis linear_eq_0_span[OF lf] subset_eq SB f0)
  4483 
  4484 lemma linear_eq:
  4485   assumes lf: "linear (f::'a::ring_1^'n \<Rightarrow> _)" and lg: "linear g" and S: "S \<subseteq> span B"
  4486   and fg: "\<forall> x\<in> B. f x = g x" 
  4487   shows "\<forall>x\<in> S. f x = g x"
  4488 proof-
  4489   let ?h = "\<lambda>x. f x - g x"
  4490   from fg have fg': "\<forall>x\<in> B. ?h x = 0" by simp
  4491   from linear_eq_0[OF linear_compose_sub[OF lf lg] S fg']
  4492   show ?thesis by simp
  4493 qed    
  4494 
  4495 lemma linear_eq_stdbasis:
  4496   assumes lf: "linear (f::'a::ring_1^'m \<Rightarrow> 'a^'n)" and lg: "linear g"
  4497   and fg: "\<forall>i \<in> {1 .. dimindex(UNIV :: 'm set)}. f (basis i) = g(basis i)"
  4498   shows "f = g"
  4499 proof-
  4500   let ?U = "UNIV :: 'm set"
  4501   let ?I = "{basis i:: 'a^'m|i. i \<in> {1 .. dimindex ?U}}" 
  4502   {fix x assume x: "x \<in> (UNIV :: ('a^'m) set)"
  4503     from equalityD2[OF span_stdbasis]
  4504     have IU: " (UNIV :: ('a^'m) set) \<subseteq> span ?I" by blast
  4505     from linear_eq[OF lf lg IU] fg x
  4506     have "f x = g x" unfolding Collect_def  Ball_def mem_def by metis}
  4507   then show ?thesis by (auto intro: ext)
  4508 qed
  4509 
  4510 (* Similar results for bilinear functions.                                   *)
  4511 
  4512 lemma bilinear_eq:
  4513   assumes bf: "bilinear (f:: 'a::ring^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)" 
  4514   and bg: "bilinear g"
  4515   and SB: "S \<subseteq> span B" and TC: "T \<subseteq> span C"
  4516   and fg: "\<forall>x\<in> B. \<forall>y\<in> C. f x y = g x y"
  4517   shows "\<forall>x\<in>S. \<forall>y\<in>T. f x y = g x y "
  4518 proof-
  4519   let ?P = "\<lambda>x. \<forall>y\<in> span C. f x y = g x y"
  4520   from bf bg have sp: "subspace ?P" 
  4521     unfolding bilinear_def linear_def subspace_def bf bg  
  4522     by(auto simp add: span_0 mem_def bilinear_lzero[OF bf] bilinear_lzero[OF bg] span_add Ball_def intro:  bilinear_ladd[OF bf])
  4523 
  4524   have "\<forall>x \<in> span B. \<forall>y\<in> span C. f x y = g x y" 
  4525     apply -
  4526     apply (rule ballI)
  4527     apply (rule span_induct[of B ?P]) 
  4528     defer
  4529     apply (rule sp)
  4530     apply assumption
  4531     apply (clarsimp simp add: Ball_def)
  4532     apply (rule_tac P="\<lambda>y. f xa y = g xa y" and S=C in span_induct)
  4533     using fg 
  4534     apply (auto simp add: subspace_def)
  4535     using bf bg unfolding bilinear_def linear_def
  4536     by(auto simp add: span_0 mem_def bilinear_rzero[OF bf] bilinear_rzero[OF bg] span_add Ball_def intro:  bilinear_ladd[OF bf])
  4537   then show ?thesis using SB TC by (auto intro: ext)
  4538 qed
  4539 
  4540 lemma bilinear_eq_stdbasis:
  4541   assumes bf: "bilinear (f:: 'a::ring_1^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)" 
  4542   and bg: "bilinear g"
  4543   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)"
  4544   shows "f = g"
  4545 proof-
  4546   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
  4547   from bilinear_eq[OF bf bg equalityD2[OF span_stdbasis] equalityD2[OF span_stdbasis] th] show ?thesis by (blast intro: ext)
  4548 qed
  4549 
  4550 (* Detailed theorems about left and right invertibility in general case.     *)
  4551 
  4552 lemma left_invertible_transp:
  4553   "(\<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)"
  4554   by (metis matrix_transp_mul transp_mat transp_transp)
  4555 
  4556 lemma right_invertible_transp:
  4557   "(\<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)"
  4558   by (metis matrix_transp_mul transp_mat transp_transp)
  4559 
  4560 lemma linear_injective_left_inverse:
  4561   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and fi: "inj f"
  4562   shows "\<exists>g. linear g \<and> g o f = id"
  4563 proof-
  4564   from linear_independent_extend[OF independent_injective_image, OF independent_stdbasis, OF lf fi]
  4565   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
  4566   from h(2) 
  4567   have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (h \<circ> f) (basis i) = id (basis i)"
  4568     using inv_o_cancel[OF fi, unfolded stupid_ext[symmetric] id_def o_def]
  4569     apply auto
  4570     apply (erule_tac x="basis i" in allE)
  4571     by auto
  4572   
  4573   from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
  4574   have "h o f = id" .
  4575   then show ?thesis using h(1) by blast  
  4576 qed
  4577 
  4578 lemma linear_surjective_right_inverse:
  4579   assumes lf: "linear (f:: real ^'m \<Rightarrow> real ^'n)" and sf: "surj f"
  4580   shows "\<exists>g. linear g \<and> f o g = id"
  4581 proof-
  4582   from linear_independent_extend[OF independent_stdbasis]
  4583   obtain h:: "real ^'n \<Rightarrow> real ^'m" where 
  4584     h: "linear h" "\<forall> x\<in> {basis i| i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}. h x = inv f x" by blast
  4585   from h(2) 
  4586   have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (f o h) (basis i) = id (basis i)"
  4587     using sf
  4588     apply (auto simp add: surj_iff o_def stupid_ext[symmetric])
  4589     apply (erule_tac x="basis i" in allE)
  4590     by auto
  4591   
  4592   from linear_eq_stdbasis[OF linear_compose[OF h(1) lf] linear_id th]
  4593   have "f o h = id" .
  4594   then show ?thesis using h(1) by blast  
  4595 qed
  4596 
  4597 lemma matrix_left_invertible_injective:
  4598 "(\<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)"
  4599 proof-
  4600   {fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
  4601     from xy have "B*v (A *v x) = B *v (A*v y)" by simp
  4602     hence "x = y"
  4603       unfolding matrix_vector_mul_assoc B matrix_vector_mul_lid .}
  4604   moreover
  4605   {assume A: "\<forall>x y. A *v x = A *v y \<longrightarrow> x = y"
  4606     hence i: "inj (op *v A)" unfolding inj_on_def by auto 
  4607     from linear_injective_left_inverse[OF matrix_vector_mul_linear i]
  4608     obtain g where g: "linear g" "g o op *v A = id" by blast
  4609     have "matrix g ** A = mat 1"
  4610       unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
  4611       using g(2) by (simp add: o_def id_def stupid_ext)
  4612     then have "\<exists>B. (B::real ^'m^'n) ** A = mat 1" by blast}
  4613   ultimately show ?thesis by blast
  4614 qed
  4615 
  4616 lemma matrix_left_invertible_ker:
  4617   "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
  4618   unfolding matrix_left_invertible_injective
  4619   using linear_injective_0[OF matrix_vector_mul_linear, of A]
  4620   by (simp add: inj_on_def)
  4621 
  4622 lemma matrix_right_invertible_surjective:
  4623 "(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
  4624 proof-
  4625   {fix B :: "real ^'m^'n"  assume AB: "A ** B = mat 1"
  4626     {fix x :: "real ^ 'm" 
  4627       have "A *v (B *v x) = x"
  4628 	by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB)}
  4629     hence "surj (op *v A)" unfolding surj_def by metis }
  4630   moreover
  4631   {assume sf: "surj (op *v A)"
  4632     from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
  4633     obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "op *v A o g = id" 
  4634       by blast
  4635 
  4636     have "A ** (matrix g) = mat 1"
  4637       unfolding matrix_eq  matrix_vector_mul_lid 
  4638 	matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)] 
  4639       using g(2) unfolding o_def stupid_ext[symmetric] id_def
  4640       .
  4641     hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
  4642   }
  4643   ultimately show ?thesis unfolding surj_def by blast
  4644 qed    
  4645 
  4646 lemma matrix_left_invertible_independent_columns:
  4647   fixes A :: "real^'n^'m"
  4648   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))"
  4649    (is "?lhs \<longleftrightarrow> ?rhs")
  4650 proof-
  4651   let ?U = "{1 .. dimindex(UNIV :: 'n set)}"
  4652   {assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
  4653     {fix c i assume c: "setsum (\<lambda>i. c i *s column i A) ?U = 0" 
  4654       and i: "i \<in> ?U"
  4655       let ?x = "\<chi> i. c i"
  4656       have th0:"A *v ?x = 0"
  4657 	using c
  4658 	unfolding matrix_mult_vsum Cart_eq
  4659 	by (auto simp add: vector_component zero_index setsum_component Cart_lambda_beta)
  4660       from k[rule_format, OF th0] i
  4661       have "c i = 0" by (vector Cart_eq)}
  4662     hence ?rhs by blast}
  4663   moreover
  4664   {assume H: ?rhs
  4665     {fix x assume x: "A *v x = 0" 
  4666       let ?c = "\<lambda>i. ((x$i ):: real)"
  4667       from H[rule_format, of ?c, unfolded matrix_mult_vsum[symmetric], OF x]
  4668       have "x = 0" by vector}}
  4669   ultimately show ?thesis unfolding matrix_left_invertible_ker by blast 
  4670 qed
  4671 
  4672 lemma matrix_right_invertible_independent_rows:
  4673   fixes A :: "real^'n^'m"
  4674   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))"
  4675   unfolding left_invertible_transp[symmetric]
  4676     matrix_left_invertible_independent_columns
  4677   by (simp add: column_transp)
  4678 
  4679 lemma matrix_right_invertible_span_columns:
  4680   "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow> span (columns A) = UNIV" (is "?lhs = ?rhs")
  4681 proof-
  4682   let ?U = "{1 .. dimindex (UNIV :: 'm set)}"
  4683   have fU: "finite ?U" by simp
  4684   have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). setsum (\<lambda>i. (x$i) *s column i A) ?U = y)"
  4685     unfolding matrix_right_invertible_surjective matrix_mult_vsum surj_def
  4686     apply (subst eq_commute) ..    
  4687   have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
  4688   {assume h: ?lhs
  4689     {fix x:: "real ^'n" 
  4690 	from h[unfolded lhseq, rule_format, of x] obtain y:: "real ^'m"
  4691 	  where y: "setsum (\<lambda>i. (y$i) *s column i A) ?U = x" by blast
  4692 	have "x \<in> span (columns A)"  
  4693 	  unfolding y[symmetric]
  4694 	  apply (rule span_setsum[OF fU])
  4695 	  apply clarify
  4696 	  apply (rule span_mul)
  4697 	  apply (rule span_superset)
  4698 	  unfolding columns_def
  4699 	  by blast}
  4700     then have ?rhs unfolding rhseq by blast}
  4701   moreover
  4702   {assume h:?rhs
  4703     let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). setsum (\<lambda>i. (x$i) *s column i A) ?U = y"
  4704     {fix y have "?P y" 
  4705       proof(rule span_induct_alt[of ?P "columns A"])
  4706 	show "\<exists>x\<Colon>real ^ 'm. setsum (\<lambda>i. (x$i) *s column i A) ?U = 0"
  4707 	  apply (rule exI[where x=0])
  4708 	  by (simp add: zero_index vector_smult_lzero)
  4709       next
  4710 	fix c y1 y2 assume y1: "y1 \<in> columns A" and y2: "?P y2"
  4711 	from y1 obtain i where i: "i \<in> ?U" "y1 = column i A" 
  4712 	  unfolding columns_def by blast
  4713 	from y2 obtain x:: "real ^'m" where 
  4714 	  x: "setsum (\<lambda>i. (x$i) *s column i A) ?U = y2" by blast
  4715 	let ?x = "(\<chi> j. if j = i then c + (x$i) else (x$j))::real^'m"
  4716 	show "?P (c*s y1 + y2)"
  4717 	  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])
  4718 	    fix j 
  4719 	    have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j)
  4720            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)
  4721 	      by (simp add: ring_simps)
  4722 	    have "setsum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
  4723            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"
  4724 	      apply (rule setsum_cong[OF refl])
  4725 	      using th by blast
  4726 	    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"
  4727 	      by (simp add: setsum_addf)
  4728 	    also have "\<dots> = c * ((column i A)$j) + setsum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
  4729 	      unfolding setsum_delta[OF fU]
  4730 	      using i(1) by simp 
  4731 	    finally show "setsum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
  4732            else (x$xa) * ((column xa A$j))) ?U = c * ((column i A)$j) + setsum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U" .
  4733 	  qed
  4734 	next
  4735 	  show "y \<in> span (columns A)" unfolding h by blast
  4736 	qed}
  4737     then have ?lhs unfolding lhseq ..}
  4738   ultimately show ?thesis by blast
  4739 qed
  4740 
  4741 lemma matrix_left_invertible_span_rows:
  4742   "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
  4743   unfolding right_invertible_transp[symmetric]
  4744   unfolding columns_transp[symmetric]
  4745   unfolding matrix_right_invertible_span_columns
  4746  ..
  4747 
  4748 (* An injective map real^'n->real^'n is also surjective.                       *)
  4749 
  4750 lemma linear_injective_imp_surjective:
  4751   assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and fi: "inj f" 
  4752   shows "surj f"
  4753 proof-
  4754   let ?U = "UNIV :: (real ^'n) set"
  4755   from basis_exists[of ?U] obtain B 
  4756     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U" 
  4757     by blast
  4758   from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
  4759   have th: "?U \<subseteq> span (f ` B)"
  4760     apply (rule card_ge_dim_independent)
  4761     apply blast
  4762     apply (rule independent_injective_image[OF B(2) lf fi])
  4763     apply (rule order_eq_refl)
  4764     apply (rule sym)
  4765     unfolding d
  4766     apply (rule card_image)
  4767     apply (rule subset_inj_on[OF fi])
  4768     by blast
  4769   from th show ?thesis
  4770     unfolding span_linear_image[OF lf] surj_def
  4771     using B(3) by blast
  4772 qed
  4773 
  4774 (* And vice versa.                                                           *)
  4775 
  4776 lemma surjective_iff_injective_gen: 
  4777   assumes fS: "finite S" and fT: "finite T" and c: "card S = card T"
  4778   and ST: "f ` S \<subseteq> T"
  4779   shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" (is "?lhs \<longleftrightarrow> ?rhs")
  4780 proof-
  4781   {assume h: "?lhs"
  4782     {fix x y assume x: "x \<in> S" and y: "y \<in> S" and f: "f x = f y"
  4783       from x fS have S0: "card S \<noteq> 0" by auto
  4784       {assume xy: "x \<noteq> y"
  4785 	have th: "card S \<le> card (f ` (S - {y}))"
  4786 	  unfolding c
  4787 	  apply (rule card_mono)
  4788 	  apply (rule finite_imageI)
  4789 	  using fS apply simp
  4790 	  using h xy x y f unfolding subset_eq image_iff
  4791 	  apply auto
  4792 	  apply (case_tac "xa = f x")
  4793 	  apply (rule bexI[where x=x])
  4794 	  apply auto
  4795 	  done
  4796 	also have " \<dots> \<le> card (S -{y})"
  4797 	  apply (rule card_image_le)
  4798 	  using fS by simp
  4799 	also have "\<dots> \<le> card S - 1" using y fS by simp
  4800 	finally have False  using S0 by arith }
  4801       then have "x = y" by blast}
  4802     then have ?rhs unfolding inj_on_def by blast}
  4803   moreover
  4804   {assume h: ?rhs
  4805     have "f ` S = T"
  4806       apply (rule card_subset_eq[OF fT ST])
  4807       unfolding card_image[OF h] using c .
  4808     then have ?lhs by blast}
  4809   ultimately show ?thesis by blast
  4810 qed
  4811 
  4812 lemma linear_surjective_imp_injective:
  4813   assumes lf: "linear (f::real ^'n => real ^'n)" and sf: "surj f" 
  4814   shows "inj f"
  4815 proof-
  4816   let ?U = "UNIV :: (real ^'n) set"
  4817   from basis_exists[of ?U] obtain B 
  4818     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U" 
  4819     by blast
  4820   {fix x assume x: "x \<in> span B" and fx: "f x = 0"
  4821     from B(4) have fB: "finite B" by (simp add: hassize_def)
  4822     from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
  4823     have fBi: "independent (f ` B)" 
  4824       apply (rule card_le_dim_spanning[of "f ` B" ?U])
  4825       apply blast
  4826       using sf B(3)
  4827       unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
  4828       apply blast
  4829       using fB apply (blast intro: finite_imageI)
  4830       unfolding d
  4831       apply (rule card_image_le)
  4832       apply (rule fB)
  4833       done
  4834     have th0: "dim ?U \<le> card (f ` B)"
  4835       apply (rule span_card_ge_dim)
  4836       apply blast
  4837       unfolding span_linear_image[OF lf]
  4838       apply (rule subset_trans[where B = "f ` UNIV"])
  4839       using sf unfolding surj_def apply blast
  4840       apply (rule image_mono)
  4841       apply (rule B(3))
  4842       apply (metis finite_imageI fB)
  4843       done
  4844 
  4845     moreover have "card (f ` B) \<le> card B"
  4846       by (rule card_image_le, rule fB)
  4847     ultimately have th1: "card B = card (f ` B)" unfolding d by arith
  4848     have fiB: "inj_on f B" 
  4849       unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric] by blast
  4850     from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
  4851     have "x = 0" by blast}
  4852   note th = this
  4853   from th show ?thesis unfolding linear_injective_0[OF lf] 
  4854     using B(3) by blast
  4855 qed
  4856 
  4857 (* Hence either is enough for isomorphism.                                   *)
  4858 
  4859 lemma left_right_inverse_eq:
  4860   assumes fg: "f o g = id" and gh: "g o h = id"
  4861   shows "f = h" 
  4862 proof-
  4863   have "f = f o (g o h)" unfolding gh by simp
  4864   also have "\<dots> = (f o g) o h" by (simp add: o_assoc)
  4865   finally show "f = h" unfolding fg by simp
  4866 qed
  4867 
  4868 lemma isomorphism_expand:
  4869   "f o g = id \<and> g o f = id \<longleftrightarrow> (\<forall>x. f(g x) = x) \<and> (\<forall>x. g(f x) = x)"
  4870   by (simp add: expand_fun_eq o_def id_def)
  4871 
  4872 lemma linear_injective_isomorphism:
  4873   assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'n)" and fi: "inj f"
  4874   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  4875 unfolding isomorphism_expand[symmetric]
  4876 using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]] linear_injective_left_inverse[OF lf fi]
  4877 by (metis left_right_inverse_eq)
  4878 
  4879 lemma linear_surjective_isomorphism:
  4880   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and sf: "surj f"
  4881   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  4882 unfolding isomorphism_expand[symmetric]
  4883 using linear_surjective_right_inverse[OF lf sf] linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
  4884 by (metis left_right_inverse_eq)
  4885 
  4886 (* Left and right inverses are the same for R^N->R^N.                        *)
  4887 
  4888 lemma linear_inverse_left:
  4889   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and lf': "linear f'"
  4890   shows "f o f' = id \<longleftrightarrow> f' o f = id"
  4891 proof-
  4892   {fix f f':: "real ^'n \<Rightarrow> real ^'n"
  4893     assume lf: "linear f" "linear f'" and f: "f o f' = id"
  4894     from f have sf: "surj f"
  4895       
  4896       apply (auto simp add: o_def stupid_ext[symmetric] id_def surj_def)
  4897       by metis
  4898     from linear_surjective_isomorphism[OF lf(1) sf] lf f
  4899     have "f' o f = id" unfolding stupid_ext[symmetric] o_def id_def
  4900       by metis}
  4901   then show ?thesis using lf lf' by metis
  4902 qed
  4903 
  4904 (* Moreover, a one-sided inverse is automatically linear.                    *)
  4905 
  4906 lemma left_inverse_linear:
  4907   assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and gf: "g o f = id" 
  4908   shows "linear g"
  4909 proof-
  4910   from gf have fi: "inj f" apply (auto simp add: inj_on_def o_def id_def stupid_ext[symmetric])
  4911     by metis
  4912   from linear_injective_isomorphism[OF lf fi] 
  4913   obtain h:: "real ^'n \<Rightarrow> real ^'n" where 
  4914     h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
  4915   have "h = g" apply (rule ext) using gf h(2,3)
  4916     apply (simp add: o_def id_def stupid_ext[symmetric])
  4917     by metis
  4918   with h(1) show ?thesis by blast
  4919 qed
  4920 
  4921 lemma right_inverse_linear:
  4922   assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and gf: "f o g = id" 
  4923   shows "linear g"
  4924 proof-
  4925   from gf have fi: "surj f" apply (auto simp add: surj_def o_def id_def stupid_ext[symmetric])
  4926     by metis
  4927   from linear_surjective_isomorphism[OF lf fi] 
  4928   obtain h:: "real ^'n \<Rightarrow> real ^'n" where 
  4929     h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
  4930   have "h = g" apply (rule ext) using gf h(2,3)
  4931     apply (simp add: o_def id_def stupid_ext[symmetric])
  4932     by metis
  4933   with h(1) show ?thesis by blast
  4934 qed
  4935 
  4936 (* The same result in terms of square matrices.                              *)
  4937 
  4938 lemma matrix_left_right_inverse:
  4939   fixes A A' :: "real ^'n^'n" 
  4940   shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
  4941 proof-
  4942   {fix A A' :: "real ^'n^'n" assume AA': "A ** A' = mat 1"
  4943     have sA: "surj (op *v A)"
  4944       unfolding surj_def
  4945       apply clarify
  4946       apply (rule_tac x="(A' *v y)" in exI)
  4947       by (simp add: matrix_vector_mul_assoc AA' matrix_vector_mul_lid)
  4948     from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
  4949     obtain f' :: "real ^'n \<Rightarrow> real ^'n"
  4950       where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
  4951     have th: "matrix f' ** A = mat 1" 
  4952       by (simp add: matrix_eq matrix_works[OF f'(1)] matrix_vector_mul_assoc[symmetric] matrix_vector_mul_lid f'(2)[rule_format])
  4953     hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
  4954     hence "matrix f' = A'" by (simp add: matrix_mul_assoc[symmetric] AA' matrix_mul_rid matrix_mul_lid)
  4955     hence "matrix f' ** A = A' ** A" by simp
  4956     hence "A' ** A = mat 1" by (simp add: th)}
  4957   then show ?thesis by blast
  4958 qed
  4959 
  4960 (* Considering an n-element vector as an n-by-1 or 1-by-n matrix.            *)
  4961 
  4962 definition "rowvector v = (\<chi> i j. (v$j))"
  4963 
  4964 definition "columnvector v = (\<chi> i j. (v$i))"
  4965 
  4966 lemma transp_columnvector:
  4967  "transp(columnvector v) = rowvector v"
  4968   by (simp add: transp_def rowvector_def columnvector_def Cart_eq Cart_lambda_beta)
  4969 
  4970 lemma transp_rowvector: "transp(rowvector v) = columnvector v"
  4971   by (simp add: transp_def columnvector_def rowvector_def Cart_eq Cart_lambda_beta)
  4972 
  4973 lemma dot_rowvector_columnvector:
  4974   "columnvector (A *v v) = A ** columnvector v"
  4975   by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
  4976 
  4977 lemma dot_matrix_product: "(x::'a::semiring_1^'n) \<bullet> y = (((rowvector x ::'a^'n^1) ** (columnvector y :: 'a^1^'n))$1)$1"
  4978   apply (vector matrix_matrix_mult_def rowvector_def columnvector_def dot_def)
  4979   by (simp add: Cart_lambda_beta)
  4980 
  4981 lemma dot_matrix_vector_mul:
  4982   fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
  4983   shows "(A *v x) \<bullet> (B *v y) =
  4984       (((rowvector x :: real^'n^1) ** ((transp A ** B) ** (columnvector y :: real ^1^'n)))$1)$1"
  4985 unfolding dot_matrix_product transp_columnvector[symmetric]
  4986   dot_rowvector_columnvector matrix_transp_mul matrix_mul_assoc ..
  4987 
  4988 (* Infinity norm.                                                            *)
  4989 
  4990 definition "infnorm (x::real^'n) = rsup {abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  4991 
  4992 lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  4993   using dimindex_ge_1 by auto
  4994 
  4995 lemma infnorm_set_image:
  4996   "{abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} =
  4997   (\<lambda>i. abs(x$i)) ` {1 .. dimindex(UNIV :: 'n set)}" by blast
  4998 
  4999 lemma infnorm_set_lemma:
  5000   shows "finite {abs((x::'a::abs ^'n)$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  5001   and "{abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} \<noteq> {}"
  5002   unfolding infnorm_set_image
  5003   using dimindex_ge_1[of "UNIV :: 'n set"]
  5004   by (auto intro: finite_imageI)
  5005 
  5006 lemma infnorm_pos_le: "0 \<le> infnorm x"
  5007   unfolding infnorm_def
  5008   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
  5009   unfolding infnorm_set_image
  5010   using dimindex_ge_1
  5011   by auto
  5012 
  5013 lemma infnorm_triangle: "infnorm ((x::real^'n) + y) \<le> infnorm x + infnorm y"
  5014 proof-
  5015   have th: "\<And>x y (z::real). x - y <= z \<longleftrightarrow> x - z <= y" by arith
  5016   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  5017   have th2: "\<And>x (y::real). abs(x + y) - abs(x) <= abs(y)" by arith
  5018   show ?thesis 
  5019   unfolding infnorm_def
  5020   unfolding rsup_finite_le_iff[ OF infnorm_set_lemma]
  5021   apply (subst diff_le_eq[symmetric])
  5022   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
  5023   unfolding infnorm_set_image bex_simps 
  5024   apply (subst th)
  5025   unfolding th1 
  5026   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
  5027   
  5028   unfolding infnorm_set_image ball_simps bex_simps 
  5029   apply (simp add: vector_add_component)
  5030   apply (metis numseg_dimindex_nonempty th2)
  5031   done
  5032 qed
  5033 
  5034 lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::real ^'n) = 0"
  5035 proof-
  5036   have "infnorm x <= 0 \<longleftrightarrow> x = 0"
  5037     unfolding infnorm_def
  5038     unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
  5039     unfolding infnorm_set_image ball_simps
  5040     by vector
  5041   then show ?thesis using infnorm_pos_le[of x] by simp
  5042 qed
  5043 
  5044 lemma infnorm_0: "infnorm 0 = 0"
  5045   by (simp add: infnorm_eq_0)
  5046 
  5047 lemma infnorm_neg: "infnorm (- x) = infnorm x"
  5048   unfolding infnorm_def
  5049   apply (rule cong[of "rsup" "rsup"])
  5050   apply blast
  5051   apply (rule set_ext)
  5052   apply (auto simp add: vector_component abs_minus_cancel)
  5053   apply (rule_tac x="i" in exI)
  5054   apply (simp add: vector_component)
  5055   done
  5056 
  5057 lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)" 
  5058 proof-
  5059   have "y - x = - (x - y)" by simp
  5060   then show ?thesis  by (metis infnorm_neg)
  5061 qed
  5062 
  5063 lemma real_abs_sub_infnorm: "\<bar> infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
  5064 proof-
  5065   have th: "\<And>(nx::real) n ny. nx <= n + ny \<Longrightarrow> ny <= n + nx ==> \<bar>nx - ny\<bar> <= n"
  5066     by arith
  5067   from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
  5068   have ths: "infnorm x \<le> infnorm (x - y) + infnorm y" 
  5069     "infnorm y \<le> infnorm (x - y) + infnorm x"
  5070     by (simp_all add: ring_simps infnorm_neg diff_def[symmetric])
  5071   from th[OF ths]  show ?thesis .
  5072 qed
  5073 
  5074 lemma real_abs_infnorm: " \<bar>infnorm x\<bar> = infnorm x"
  5075   using infnorm_pos_le[of x] by arith
  5076 
  5077 lemma component_le_infnorm: assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  5078   shows "\<bar>x$i\<bar> \<le> infnorm (x::real^'n)"
  5079 proof-
  5080   let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
  5081   let ?S = "{\<bar>x$i\<bar> |i. i\<in> ?U}"
  5082   have fS: "finite ?S" unfolding image_Collect[symmetric]
  5083     apply (rule finite_imageI) unfolding Collect_def mem_def by simp  
  5084   have S0: "?S \<noteq> {}" using numseg_dimindex_nonempty by blast
  5085   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  5086   from rsup_finite_in[OF fS S0] rsup_finite_Ub[OF fS S0] i
  5087   show ?thesis unfolding infnorm_def isUb_def setle_def 
  5088     unfolding infnorm_set_image ball_simps by auto
  5089 qed
  5090 
  5091 lemma infnorm_mul_lemma: "infnorm(a *s x) <= \<bar>a\<bar> * infnorm x"
  5092   apply (subst infnorm_def)
  5093   unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
  5094   unfolding infnorm_set_image ball_simps
  5095   apply (simp add: abs_mult vector_component del: One_nat_def)
  5096   apply (rule ballI)
  5097   apply (drule component_le_infnorm[of _ x])
  5098   apply (rule mult_mono)
  5099   apply auto
  5100   done
  5101 
  5102 lemma infnorm_mul: "infnorm(a *s x) = abs a * infnorm x"
  5103 proof-
  5104   {assume a0: "a = 0" hence ?thesis by (simp add: infnorm_0) }
  5105   moreover
  5106   {assume a0: "a \<noteq> 0"
  5107     from a0 have th: "(1/a) *s (a *s x) = x"
  5108       by (simp add: vector_smult_assoc)
  5109     from a0 have ap: "\<bar>a\<bar> > 0" by arith
  5110     from infnorm_mul_lemma[of "1/a" "a *s x"]
  5111     have "infnorm x \<le> 1/\<bar>a\<bar> * infnorm (a*s x)"
  5112       unfolding th by simp
  5113     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)
  5114     then have "\<bar>a\<bar> * infnorm x \<le> infnorm (a*s x)" 
  5115       using ap by (simp add: field_simps)
  5116     with infnorm_mul_lemma[of a x] have ?thesis by arith }
  5117   ultimately show ?thesis by blast
  5118 qed
  5119 
  5120 lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
  5121   using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
  5122 
  5123 (* Prove that it differs only up to a bound from Euclidean norm.             *)
  5124 
  5125 lemma infnorm_le_norm: "infnorm x \<le> norm x"
  5126   unfolding infnorm_def rsup_finite_le_iff[OF infnorm_set_lemma] 
  5127   unfolding infnorm_set_image  ball_simps
  5128   by (metis component_le_norm)
  5129 lemma card_enum: "card {1 .. n} = n" by auto
  5130 lemma norm_le_infnorm: "norm(x) <= sqrt(real (dimindex(UNIV ::'n set))) * infnorm(x::real ^'n)"
  5131 proof-
  5132   let ?d = "dimindex(UNIV ::'n set)"
  5133   have d: "?d = card {1 .. ?d}" by auto
  5134   have "real ?d \<ge> 0" by simp
  5135   hence d2: "(sqrt (real ?d))^2 = real ?d"
  5136     by (auto intro: real_sqrt_pow2)
  5137   have th: "sqrt (real ?d) * infnorm x \<ge> 0"
  5138     by (simp add: dimindex_ge_1 zero_le_mult_iff real_sqrt_ge_0_iff infnorm_pos_le)
  5139   have th1: "x\<bullet>x \<le> (sqrt (real ?d) * infnorm x)^2"
  5140     unfolding power_mult_distrib d2 
  5141     apply (subst d)
  5142     apply (subst power2_abs[symmetric])
  5143     unfolding real_of_nat_def dot_def power2_eq_square[symmetric]
  5144     apply (subst power2_abs[symmetric])
  5145     apply (rule setsum_bounded)
  5146     apply (rule power_mono)
  5147     unfolding abs_of_nonneg[OF infnorm_pos_le] 
  5148     unfolding infnorm_def  rsup_finite_ge_iff[OF infnorm_set_lemma]
  5149     unfolding infnorm_set_image bex_simps
  5150     apply blast
  5151     by (rule abs_ge_zero)
  5152   from real_le_lsqrt[OF dot_pos_le th th1]
  5153   show ?thesis unfolding real_vector_norm_def id_def . 
  5154 qed
  5155 
  5156 (* Equality in Cauchy-Schwarz and triangle inequalities.                     *)
  5157 
  5158 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")
  5159 proof-
  5160   {assume h: "x = 0"
  5161     hence ?thesis by simp}
  5162   moreover
  5163   {assume h: "y = 0"
  5164     hence ?thesis by simp}
  5165   moreover
  5166   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  5167     from dot_eq_0[of "norm y *s x - norm x *s y"]
  5168     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)"
  5169       using x y
  5170       unfolding dot_rsub dot_lsub dot_lmult dot_rmult
  5171       unfolding norm_pow_2[symmetric] power2_eq_square diff_eq_0_iff_eq apply (simp add: dot_sym)
  5172       apply (simp add: ring_simps)
  5173       apply metis
  5174       done
  5175     also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" using x y
  5176       by (simp add: ring_simps dot_sym)
  5177     also have "\<dots> \<longleftrightarrow> ?lhs" using x y
  5178       apply simp
  5179       by metis
  5180     finally have ?thesis by blast}
  5181   ultimately show ?thesis by blast
  5182 qed
  5183 
  5184 lemma norm_cauchy_schwarz_abs_eq: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
  5185                 norm x *s y = norm y *s x \<or> norm(x) *s y = - norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
  5186 proof-
  5187   have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a" by arith
  5188   have "?rhs \<longleftrightarrow> norm x *s y = norm y *s x \<or> norm (- x) *s y = norm y *s (- x)"
  5189     apply simp by vector
  5190   also have "\<dots> \<longleftrightarrow>(x \<bullet> y = norm x * norm y \<or>
  5191      (-x) \<bullet> y = norm x * norm y)"
  5192     unfolding norm_cauchy_schwarz_eq[symmetric]
  5193     unfolding norm_minus_cancel
  5194       norm_mul by blast
  5195   also have "\<dots> \<longleftrightarrow> ?lhs"
  5196     unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] dot_lneg
  5197     by arith
  5198   finally show ?thesis ..
  5199 qed
  5200 
  5201 lemma norm_triangle_eq: "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *s y = norm y *s x"
  5202 proof-
  5203   {assume x: "x =0 \<or> y =0"
  5204     hence ?thesis by (cases "x=0", simp_all)}
  5205   moreover
  5206   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  5207     hence "norm x \<noteq> 0" "norm y \<noteq> 0"
  5208       by simp_all
  5209     hence n: "norm x > 0" "norm y > 0" 
  5210       using norm_ge_zero[of x] norm_ge_zero[of y]
  5211       by arith+
  5212     have th: "\<And>(a::real) b c. a + b + c \<noteq> 0 ==> (a = b + c \<longleftrightarrow> a^2 = (b + c)^2)" by algebra
  5213     have "norm(x + y) = norm x + norm y \<longleftrightarrow> norm(x + y)^ 2 = (norm x + norm y) ^2"
  5214       apply (rule th) using n norm_ge_zero[of "x + y"]
  5215       by arith
  5216     also have "\<dots> \<longleftrightarrow> norm x *s y = norm y *s x"
  5217       unfolding norm_cauchy_schwarz_eq[symmetric]
  5218       unfolding norm_pow_2 dot_ladd dot_radd
  5219       by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym ring_simps)
  5220     finally have ?thesis .}
  5221   ultimately show ?thesis by blast
  5222 qed
  5223 
  5224 (* Collinearity.*)
  5225 
  5226 definition "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *s u)"
  5227 
  5228 lemma collinear_empty:  "collinear {}" by (simp add: collinear_def)
  5229 
  5230 lemma collinear_sing: "collinear {(x::'a::ring_1^'n)}" 
  5231   apply (simp add: collinear_def)
  5232   apply (rule exI[where x=0])
  5233   by simp
  5234 
  5235 lemma collinear_2: "collinear {(x::'a::ring_1^'n),y}"
  5236   apply (simp add: collinear_def)
  5237   apply (rule exI[where x="x - y"])
  5238   apply auto
  5239   apply (rule exI[where x=0], simp)
  5240   apply (rule exI[where x=1], simp)
  5241   apply (rule exI[where x="- 1"], simp add: vector_sneg_minus1[symmetric])
  5242   apply (rule exI[where x=0], simp)
  5243   done
  5244 
  5245 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")
  5246 proof-
  5247   {assume "x=0 \<or> y = 0" hence ?thesis 
  5248       by (cases "x = 0", simp_all add: collinear_2 insert_commute)}
  5249   moreover
  5250   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  5251     {assume h: "?lhs"
  5252       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
  5253       from u[rule_format, of x 0] u[rule_format, of y 0]
  5254       obtain cx and cy where 
  5255 	cx: "x = cx*s u" and cy: "y = cy*s u"
  5256 	by auto
  5257       from cx x have cx0: "cx \<noteq> 0" by auto
  5258       from cy y have cy0: "cy \<noteq> 0" by auto
  5259       let ?d = "cy / cx"
  5260       from cx cy cx0 have "y = ?d *s x" 
  5261 	by (simp add: vector_smult_assoc)
  5262       hence ?rhs using x y by blast}
  5263     moreover
  5264     {assume h: "?rhs"
  5265       then obtain c where c: "y = c*s x" using x y by blast
  5266       have ?lhs unfolding collinear_def c
  5267 	apply (rule exI[where x=x])
  5268 	apply auto
  5269 	apply (rule exI[where x=0], simp)
  5270 	apply (rule exI[where x="- 1"], simp only: vector_smult_lneg vector_smult_lid)
  5271 	apply (rule exI[where x= "-c"], simp only: vector_smult_lneg)
  5272 	apply (rule exI[where x=1], simp)
  5273 	apply (rule exI[where x=0], simp)
  5274 	apply (rule exI[where x="1 - c"], simp add: vector_smult_lneg vector_sub_rdistrib)
  5275 	apply (rule exI[where x="c - 1"], simp add: vector_smult_lneg vector_sub_rdistrib)
  5276 	apply (rule exI[where x=0], simp)
  5277 	done}
  5278     ultimately have ?thesis by blast}
  5279   ultimately show ?thesis by blast
  5280 qed
  5281 
  5282 lemma norm_cauchy_schwarz_equal: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {(0::real^'n),x,y}"
  5283 unfolding norm_cauchy_schwarz_abs_eq
  5284 apply (cases "x=0", simp_all add: collinear_2)
  5285 apply (cases "y=0", simp_all add: collinear_2 insert_commute)
  5286 unfolding collinear_lemma
  5287 apply simp
  5288 apply (subgoal_tac "norm x \<noteq> 0")
  5289 apply (subgoal_tac "norm y \<noteq> 0")
  5290 apply (rule iffI)
  5291 apply (cases "norm x *s y = norm y *s x")
  5292 apply (rule exI[where x="(1/norm x) * norm y"])
  5293 apply (drule sym)
  5294 unfolding vector_smult_assoc[symmetric]
  5295 apply (simp add: vector_smult_assoc field_simps)
  5296 apply (rule exI[where x="(1/norm x) * - norm y"])
  5297 apply clarify
  5298 apply (drule sym)
  5299 unfolding vector_smult_assoc[symmetric]
  5300 apply (simp add: vector_smult_assoc field_simps)
  5301 apply (erule exE)
  5302 apply (erule ssubst)
  5303 unfolding vector_smult_assoc
  5304 unfolding norm_mul
  5305 apply (subgoal_tac "norm x * c = \<bar>c\<bar> * norm x \<or> norm x * c = - \<bar>c\<bar> * norm x")
  5306 apply (case_tac "c <= 0", simp add: ring_simps)
  5307 apply (simp add: ring_simps)
  5308 apply (case_tac "c <= 0", simp add: ring_simps)
  5309 apply (simp add: ring_simps)
  5310 apply simp
  5311 apply simp
  5312 done
  5313 
  5314 end