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