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