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