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