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