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