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