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