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