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