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