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