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