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