src/HOL/Library/Euclidean_Space.thy
 author huffman Tue Jun 02 20:10:56 2009 -0700 (2009-06-02) changeset 31399 d9769f093160 parent 31398 b67a3ac4882d child 31406 e23dd3aac0fb permissions -rw-r--r--
generalize lemma norm_pastecart
```     1 (* Title:      Library/Euclidean_Space
```
```     2    Author:     Amine Chaieb, University of Cambridge
```
```     3 *)
```
```     4
```
```     5 header {* (Real) Vectors in Euclidean space, and elementary linear algebra.*}
```
```     6
```
```     7 theory Euclidean_Space
```
```     8 imports
```
```     9   Complex_Main "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
```
```    10   Finite_Cartesian_Product Glbs Infinite_Set Numeral_Type
```
```    11   Inner_Product
```
```    12 uses "positivstellensatz.ML" ("normarith.ML")
```
```    13 begin
```
```    14
```
```    15 text{* Some common special cases.*}
```
```    16
```
```    17 lemma forall_1: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
```
```    18   by (metis num1_eq_iff)
```
```    19
```
```    20 lemma exhaust_2:
```
```    21   fixes x :: 2 shows "x = 1 \<or> x = 2"
```
```    22 proof (induct x)
```
```    23   case (of_int z)
```
```    24   then have "0 <= z" and "z < 2" by simp_all
```
```    25   then have "z = 0 | z = 1" by arith
```
```    26   then show ?case by auto
```
```    27 qed
```
```    28
```
```    29 lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
```
```    30   by (metis exhaust_2)
```
```    31
```
```    32 lemma exhaust_3:
```
```    33   fixes x :: 3 shows "x = 1 \<or> x = 2 \<or> x = 3"
```
```    34 proof (induct x)
```
```    35   case (of_int z)
```
```    36   then have "0 <= z" and "z < 3" by simp_all
```
```    37   then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
```
```    38   then show ?case by auto
```
```    39 qed
```
```    40
```
```    41 lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
```
```    42   by (metis exhaust_3)
```
```    43
```
```    44 lemma UNIV_1: "UNIV = {1::1}"
```
```    45   by (auto simp add: num1_eq_iff)
```
```    46
```
```    47 lemma UNIV_2: "UNIV = {1::2, 2::2}"
```
```    48   using exhaust_2 by auto
```
```    49
```
```    50 lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
```
```    51   using exhaust_3 by auto
```
```    52
```
```    53 lemma setsum_1: "setsum f (UNIV::1 set) = f 1"
```
```    54   unfolding UNIV_1 by simp
```
```    55
```
```    56 lemma setsum_2: "setsum f (UNIV::2 set) = f 1 + f 2"
```
```    57   unfolding UNIV_2 by simp
```
```    58
```
```    59 lemma setsum_3: "setsum f (UNIV::3 set) = f 1 + f 2 + f 3"
```
```    60   unfolding UNIV_3 by (simp add: add_ac)
```
```    61
```
```    62 subsection{* Basic componentwise operations on vectors. *}
```
```    63
```
```    64 instantiation "^" :: (plus,type) plus
```
```    65 begin
```
```    66 definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) + (y\$i)))"
```
```    67 instance ..
```
```    68 end
```
```    69
```
```    70 instantiation "^" :: (times,type) times
```
```    71 begin
```
```    72   definition vector_mult_def : "op * \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) * (y\$i)))"
```
```    73   instance ..
```
```    74 end
```
```    75
```
```    76 instantiation "^" :: (minus,type) minus begin
```
```    77   definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x\$i) - (y\$i)))"
```
```    78 instance ..
```
```    79 end
```
```    80
```
```    81 instantiation "^" :: (uminus,type) uminus begin
```
```    82   definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x\$i)))"
```
```    83 instance ..
```
```    84 end
```
```    85 instantiation "^" :: (zero,type) zero begin
```
```    86   definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
```
```    87 instance ..
```
```    88 end
```
```    89
```
```    90 instantiation "^" :: (one,type) one begin
```
```    91   definition vector_one_def : "1 \<equiv> (\<chi> i. 1)"
```
```    92 instance ..
```
```    93 end
```
```    94
```
```    95 instantiation "^" :: (ord,type) ord
```
```    96  begin
```
```    97 definition vector_less_eq_def:
```
```    98   "less_eq (x :: 'a ^'b) y = (ALL i. x\$i <= y\$i)"
```
```    99 definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i. x\$i < y\$i)"
```
```   100
```
```   101 instance by (intro_classes)
```
```   102 end
```
```   103
```
```   104 instantiation "^" :: (scaleR, type) scaleR
```
```   105 begin
```
```   106 definition vector_scaleR_def: "scaleR = (\<lambda> r x.  (\<chi> i. scaleR r (x\$i)))"
```
```   107 instance ..
```
```   108 end
```
```   109
```
```   110 text{* Also the scalar-vector multiplication. *}
```
```   111
```
```   112 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
```
```   113   where "c *s x = (\<chi> i. c * (x\$i))"
```
```   114
```
```   115 text{* Constant Vectors *}
```
```   116
```
```   117 definition "vec x = (\<chi> i. x)"
```
```   118
```
```   119 text{* Dot products. *}
```
```   120
```
```   121 definition dot :: "'a::{comm_monoid_add, times} ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" (infix "\<bullet>" 70) where
```
```   122   "x \<bullet> y = setsum (\<lambda>i. x\$i * y\$i) UNIV"
```
```   123
```
```   124 lemma dot_1[simp]: "(x::'a::{comm_monoid_add, times}^1) \<bullet> y = (x\$1) * (y\$1)"
```
```   125   by (simp add: dot_def setsum_1)
```
```   126
```
```   127 lemma dot_2[simp]: "(x::'a::{comm_monoid_add, times}^2) \<bullet> y = (x\$1) * (y\$1) + (x\$2) * (y\$2)"
```
```   128   by (simp add: dot_def setsum_2)
```
```   129
```
```   130 lemma dot_3[simp]: "(x::'a::{comm_monoid_add, times}^3) \<bullet> y = (x\$1) * (y\$1) + (x\$2) * (y\$2) + (x\$3) * (y\$3)"
```
```   131   by (simp add: dot_def setsum_3)
```
```   132
```
```   133 subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
```
```   134
```
```   135 method_setup vector = {*
```
```   136 let
```
```   137   val ss1 = HOL_basic_ss addsimps [@{thm dot_def}, @{thm setsum_addf} RS sym,
```
```   138   @{thm setsum_subtractf} RS sym, @{thm setsum_right_distrib},
```
```   139   @{thm setsum_left_distrib}, @{thm setsum_negf} RS sym]
```
```   140   val ss2 = @{simpset} addsimps
```
```   141              [@{thm vector_add_def}, @{thm vector_mult_def},
```
```   142               @{thm vector_minus_def}, @{thm vector_uminus_def},
```
```   143               @{thm vector_one_def}, @{thm vector_zero_def}, @{thm vec_def},
```
```   144               @{thm vector_scaleR_def},
```
```   145               @{thm Cart_lambda_beta}, @{thm vector_scalar_mult_def}]
```
```   146  fun vector_arith_tac ths =
```
```   147    simp_tac ss1
```
```   148    THEN' (fn i => rtac @{thm setsum_cong2} i
```
```   149          ORELSE rtac @{thm setsum_0'} i
```
```   150          ORELSE simp_tac (HOL_basic_ss addsimps [@{thm "Cart_eq"}]) i)
```
```   151    (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
```
```   152    THEN' asm_full_simp_tac (ss2 addsimps ths)
```
```   153  in
```
```   154   Attrib.thms >> (fn ths => K (SIMPLE_METHOD' (vector_arith_tac ths)))
```
```   155  end
```
```   156 *} "Lifts trivial vector statements to real arith statements"
```
```   157
```
```   158 lemma vec_0[simp]: "vec 0 = 0" by (vector vector_zero_def)
```
```   159 lemma vec_1[simp]: "vec 1 = 1" by (vector vector_one_def)
```
```   160
```
```   161
```
```   162
```
```   163 text{* Obvious "component-pushing". *}
```
```   164
```
```   165 lemma vec_component [simp]: "(vec x :: 'a ^ 'n)\$i = x"
```
```   166   by (vector vec_def)
```
```   167
```
```   168 lemma vector_add_component [simp]:
```
```   169   fixes x y :: "'a::{plus} ^ 'n"
```
```   170   shows "(x + y)\$i = x\$i + y\$i"
```
```   171   by vector
```
```   172
```
```   173 lemma vector_minus_component [simp]:
```
```   174   fixes x y :: "'a::{minus} ^ 'n"
```
```   175   shows "(x - y)\$i = x\$i - y\$i"
```
```   176   by vector
```
```   177
```
```   178 lemma vector_mult_component [simp]:
```
```   179   fixes x y :: "'a::{times} ^ 'n"
```
```   180   shows "(x * y)\$i = x\$i * y\$i"
```
```   181   by vector
```
```   182
```
```   183 lemma vector_smult_component [simp]:
```
```   184   fixes y :: "'a::{times} ^ 'n"
```
```   185   shows "(c *s y)\$i = c * (y\$i)"
```
```   186   by vector
```
```   187
```
```   188 lemma vector_uminus_component [simp]:
```
```   189   fixes x :: "'a::{uminus} ^ 'n"
```
```   190   shows "(- x)\$i = - (x\$i)"
```
```   191   by vector
```
```   192
```
```   193 lemma vector_scaleR_component [simp]:
```
```   194   fixes x :: "'a::scaleR ^ 'n"
```
```   195   shows "(scaleR r x)\$i = scaleR r (x\$i)"
```
```   196   by vector
```
```   197
```
```   198 lemma cond_component: "(if b then x else y)\$i = (if b then x\$i else y\$i)" by vector
```
```   199
```
```   200 lemmas vector_component =
```
```   201   vec_component vector_add_component vector_mult_component
```
```   202   vector_smult_component vector_minus_component vector_uminus_component
```
```   203   vector_scaleR_component cond_component
```
```   204
```
```   205 subsection {* Some frequently useful arithmetic lemmas over vectors. *}
```
```   206
```
```   207 instance "^" :: (semigroup_add,type) semigroup_add
```
```   208   apply (intro_classes) by (vector add_assoc)
```
```   209
```
```   210
```
```   211 instance "^" :: (monoid_add,type) monoid_add
```
```   212   apply (intro_classes) by vector+
```
```   213
```
```   214 instance "^" :: (group_add,type) group_add
```
```   215   apply (intro_classes) by (vector algebra_simps)+
```
```   216
```
```   217 instance "^" :: (ab_semigroup_add,type) ab_semigroup_add
```
```   218   apply (intro_classes) by (vector add_commute)
```
```   219
```
```   220 instance "^" :: (comm_monoid_add,type) comm_monoid_add
```
```   221   apply (intro_classes) by vector
```
```   222
```
```   223 instance "^" :: (ab_group_add,type) ab_group_add
```
```   224   apply (intro_classes) by vector+
```
```   225
```
```   226 instance "^" :: (cancel_semigroup_add,type) cancel_semigroup_add
```
```   227   apply (intro_classes)
```
```   228   by (vector Cart_eq)+
```
```   229
```
```   230 instance "^" :: (cancel_ab_semigroup_add,type) cancel_ab_semigroup_add
```
```   231   apply (intro_classes)
```
```   232   by (vector Cart_eq)
```
```   233
```
```   234 instance "^" :: (real_vector, type) real_vector
```
```   235   by default (vector scaleR_left_distrib scaleR_right_distrib)+
```
```   236
```
```   237 instance "^" :: (semigroup_mult,type) semigroup_mult
```
```   238   apply (intro_classes) by (vector mult_assoc)
```
```   239
```
```   240 instance "^" :: (monoid_mult,type) monoid_mult
```
```   241   apply (intro_classes) by vector+
```
```   242
```
```   243 instance "^" :: (ab_semigroup_mult,type) ab_semigroup_mult
```
```   244   apply (intro_classes) by (vector mult_commute)
```
```   245
```
```   246 instance "^" :: (ab_semigroup_idem_mult,type) ab_semigroup_idem_mult
```
```   247   apply (intro_classes) by (vector mult_idem)
```
```   248
```
```   249 instance "^" :: (comm_monoid_mult,type) comm_monoid_mult
```
```   250   apply (intro_classes) by vector
```
```   251
```
```   252 fun vector_power :: "('a::{one,times} ^'n) \<Rightarrow> nat \<Rightarrow> 'a^'n" where
```
```   253   "vector_power x 0 = 1"
```
```   254   | "vector_power x (Suc n) = x * vector_power x n"
```
```   255
```
```   256 instance "^" :: (semiring,type) semiring
```
```   257   apply (intro_classes) by (vector ring_simps)+
```
```   258
```
```   259 instance "^" :: (semiring_0,type) semiring_0
```
```   260   apply (intro_classes) by (vector ring_simps)+
```
```   261 instance "^" :: (semiring_1,type) semiring_1
```
```   262   apply (intro_classes) by vector
```
```   263 instance "^" :: (comm_semiring,type) comm_semiring
```
```   264   apply (intro_classes) by (vector ring_simps)+
```
```   265
```
```   266 instance "^" :: (comm_semiring_0,type) comm_semiring_0 by (intro_classes)
```
```   267 instance "^" :: (cancel_comm_monoid_add, type) cancel_comm_monoid_add ..
```
```   268 instance "^" :: (semiring_0_cancel,type) semiring_0_cancel by (intro_classes)
```
```   269 instance "^" :: (comm_semiring_0_cancel,type) comm_semiring_0_cancel by (intro_classes)
```
```   270 instance "^" :: (ring,type) ring by (intro_classes)
```
```   271 instance "^" :: (semiring_1_cancel,type) semiring_1_cancel by (intro_classes)
```
```   272 instance "^" :: (comm_semiring_1,type) comm_semiring_1 by (intro_classes)
```
```   273
```
```   274 instance "^" :: (ring_1,type) ring_1 ..
```
```   275
```
```   276 instance "^" :: (real_algebra,type) real_algebra
```
```   277   apply intro_classes
```
```   278   apply (simp_all add: vector_scaleR_def ring_simps)
```
```   279   apply vector
```
```   280   apply vector
```
```   281   done
```
```   282
```
```   283 instance "^" :: (real_algebra_1,type) real_algebra_1 ..
```
```   284
```
```   285 lemma of_nat_index:
```
```   286   "(of_nat n :: 'a::semiring_1 ^'n)\$i = of_nat n"
```
```   287   apply (induct n)
```
```   288   apply vector
```
```   289   apply vector
```
```   290   done
```
```   291 lemma zero_index[simp]:
```
```   292   "(0 :: 'a::zero ^'n)\$i = 0" by vector
```
```   293
```
```   294 lemma one_index[simp]:
```
```   295   "(1 :: 'a::one ^'n)\$i = 1" by vector
```
```   296
```
```   297 lemma one_plus_of_nat_neq_0: "(1::'a::semiring_char_0) + of_nat n \<noteq> 0"
```
```   298 proof-
```
```   299   have "(1::'a) + of_nat n = 0 \<longleftrightarrow> of_nat 1 + of_nat n = (of_nat 0 :: 'a)" by simp
```
```   300   also have "\<dots> \<longleftrightarrow> 1 + n = 0" by (simp only: of_nat_add[symmetric] of_nat_eq_iff)
```
```   301   finally show ?thesis by simp
```
```   302 qed
```
```   303
```
```   304 instance "^" :: (semiring_char_0,type) semiring_char_0
```
```   305 proof (intro_classes)
```
```   306   fix m n ::nat
```
```   307   show "(of_nat m :: 'a^'b) = of_nat n \<longleftrightarrow> m = n"
```
```   308     by (simp add: Cart_eq of_nat_index)
```
```   309 qed
```
```   310
```
```   311 instance "^" :: (comm_ring_1,type) comm_ring_1 by intro_classes
```
```   312 instance "^" :: (ring_char_0,type) ring_char_0 by intro_classes
```
```   313
```
```   314 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
```
```   315   by (vector mult_assoc)
```
```   316 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
```
```   317   by (vector ring_simps)
```
```   318 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
```
```   319   by (vector ring_simps)
```
```   320 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
```
```   321 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
```
```   322 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
```
```   323   by (vector ring_simps)
```
```   324 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
```
```   325 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
```
```   326 lemma vector_sneg_minus1: "-x = (- (1::'a::ring_1)) *s x" by vector
```
```   327 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
```
```   328 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
```
```   329   by (vector ring_simps)
```
```   330
```
```   331 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
```
```   332   by (simp add: Cart_eq)
```
```   333
```
```   334 subsection {* Square root of sum of squares *}
```
```   335
```
```   336 definition
```
```   337   "setL2 f A = sqrt (\<Sum>i\<in>A. (f i)\<twosuperior>)"
```
```   338
```
```   339 lemma setL2_cong:
```
```   340   "\<lbrakk>A = B; \<And>x. x \<in> B \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
```
```   341   unfolding setL2_def by simp
```
```   342
```
```   343 lemma strong_setL2_cong:
```
```   344   "\<lbrakk>A = B; \<And>x. x \<in> B =simp=> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
```
```   345   unfolding setL2_def simp_implies_def by simp
```
```   346
```
```   347 lemma setL2_infinite [simp]: "\<not> finite A \<Longrightarrow> setL2 f A = 0"
```
```   348   unfolding setL2_def by simp
```
```   349
```
```   350 lemma setL2_empty [simp]: "setL2 f {} = 0"
```
```   351   unfolding setL2_def by simp
```
```   352
```
```   353 lemma setL2_insert [simp]:
```
```   354   "\<lbrakk>finite F; a \<notin> F\<rbrakk> \<Longrightarrow>
```
```   355     setL2 f (insert a F) = sqrt ((f a)\<twosuperior> + (setL2 f F)\<twosuperior>)"
```
```   356   unfolding setL2_def by (simp add: setsum_nonneg)
```
```   357
```
```   358 lemma setL2_nonneg [simp]: "0 \<le> setL2 f A"
```
```   359   unfolding setL2_def by (simp add: setsum_nonneg)
```
```   360
```
```   361 lemma setL2_0': "\<forall>a\<in>A. f a = 0 \<Longrightarrow> setL2 f A = 0"
```
```   362   unfolding setL2_def by simp
```
```   363
```
```   364 lemma setL2_mono:
```
```   365   assumes "\<And>i. i \<in> K \<Longrightarrow> f i \<le> g i"
```
```   366   assumes "\<And>i. i \<in> K \<Longrightarrow> 0 \<le> f i"
```
```   367   shows "setL2 f K \<le> setL2 g K"
```
```   368   unfolding setL2_def
```
```   369   by (simp add: setsum_nonneg setsum_mono power_mono prems)
```
```   370
```
```   371 lemma setL2_right_distrib:
```
```   372   "0 \<le> r \<Longrightarrow> r * setL2 f A = setL2 (\<lambda>x. r * f x) A"
```
```   373   unfolding setL2_def
```
```   374   apply (simp add: power_mult_distrib)
```
```   375   apply (simp add: setsum_right_distrib [symmetric])
```
```   376   apply (simp add: real_sqrt_mult setsum_nonneg)
```
```   377   done
```
```   378
```
```   379 lemma setL2_left_distrib:
```
```   380   "0 \<le> r \<Longrightarrow> setL2 f A * r = setL2 (\<lambda>x. f x * r) A"
```
```   381   unfolding setL2_def
```
```   382   apply (simp add: power_mult_distrib)
```
```   383   apply (simp add: setsum_left_distrib [symmetric])
```
```   384   apply (simp add: real_sqrt_mult setsum_nonneg)
```
```   385   done
```
```   386
```
```   387 lemma setsum_nonneg_eq_0_iff:
```
```   388   fixes f :: "'a \<Rightarrow> 'b::pordered_ab_group_add"
```
```   389   shows "\<lbrakk>finite A; \<forall>x\<in>A. 0 \<le> f x\<rbrakk> \<Longrightarrow> setsum f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
```
```   390   apply (induct set: finite, simp)
```
```   391   apply (simp add: add_nonneg_eq_0_iff setsum_nonneg)
```
```   392   done
```
```   393
```
```   394 lemma setL2_eq_0_iff: "finite A \<Longrightarrow> setL2 f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
```
```   395   unfolding setL2_def
```
```   396   by (simp add: setsum_nonneg setsum_nonneg_eq_0_iff)
```
```   397
```
```   398 lemma setL2_triangle_ineq:
```
```   399   shows "setL2 (\<lambda>i. f i + g i) A \<le> setL2 f A + setL2 g A"
```
```   400 proof (cases "finite A")
```
```   401   case False
```
```   402   thus ?thesis by simp
```
```   403 next
```
```   404   case True
```
```   405   thus ?thesis
```
```   406   proof (induct set: finite)
```
```   407     case empty
```
```   408     show ?case by simp
```
```   409   next
```
```   410     case (insert x F)
```
```   411     hence "sqrt ((f x + g x)\<twosuperior> + (setL2 (\<lambda>i. f i + g i) F)\<twosuperior>) \<le>
```
```   412            sqrt ((f x + g x)\<twosuperior> + (setL2 f F + setL2 g F)\<twosuperior>)"
```
```   413       by (intro real_sqrt_le_mono add_left_mono power_mono insert
```
```   414                 setL2_nonneg add_increasing zero_le_power2)
```
```   415     also have
```
```   416       "\<dots> \<le> sqrt ((f x)\<twosuperior> + (setL2 f F)\<twosuperior>) + sqrt ((g x)\<twosuperior> + (setL2 g F)\<twosuperior>)"
```
```   417       by (rule real_sqrt_sum_squares_triangle_ineq)
```
```   418     finally show ?case
```
```   419       using insert by simp
```
```   420   qed
```
```   421 qed
```
```   422
```
```   423 lemma sqrt_sum_squares_le_sum:
```
```   424   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (x\<twosuperior> + y\<twosuperior>) \<le> x + y"
```
```   425   apply (rule power2_le_imp_le)
```
```   426   apply (simp add: power2_sum)
```
```   427   apply (simp add: mult_nonneg_nonneg)
```
```   428   apply (simp add: add_nonneg_nonneg)
```
```   429   done
```
```   430
```
```   431 lemma setL2_le_setsum [rule_format]:
```
```   432   "(\<forall>i\<in>A. 0 \<le> f i) \<longrightarrow> setL2 f A \<le> setsum f A"
```
```   433   apply (cases "finite A")
```
```   434   apply (induct set: finite)
```
```   435   apply simp
```
```   436   apply clarsimp
```
```   437   apply (erule order_trans [OF sqrt_sum_squares_le_sum])
```
```   438   apply simp
```
```   439   apply simp
```
```   440   apply simp
```
```   441   done
```
```   442
```
```   443 lemma sqrt_sum_squares_le_sum_abs: "sqrt (x\<twosuperior> + y\<twosuperior>) \<le> \<bar>x\<bar> + \<bar>y\<bar>"
```
```   444   apply (rule power2_le_imp_le)
```
```   445   apply (simp add: power2_sum)
```
```   446   apply (simp add: mult_nonneg_nonneg)
```
```   447   apply (simp add: add_nonneg_nonneg)
```
```   448   done
```
```   449
```
```   450 lemma setL2_le_setsum_abs: "setL2 f A \<le> (\<Sum>i\<in>A. \<bar>f i\<bar>)"
```
```   451   apply (cases "finite A")
```
```   452   apply (induct set: finite)
```
```   453   apply simp
```
```   454   apply simp
```
```   455   apply (rule order_trans [OF sqrt_sum_squares_le_sum_abs])
```
```   456   apply simp
```
```   457   apply simp
```
```   458   done
```
```   459
```
```   460 lemma setL2_mult_ineq_lemma:
```
```   461   fixes a b c d :: real
```
```   462   shows "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
```
```   463 proof -
```
```   464   have "0 \<le> (a * d - b * c)\<twosuperior>" by simp
```
```   465   also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * d) * (b * c)"
```
```   466     by (simp only: power2_diff power_mult_distrib)
```
```   467   also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * c) * (b * d)"
```
```   468     by simp
```
```   469   finally show "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
```
```   470     by simp
```
```   471 qed
```
```   472
```
```   473 lemma setL2_mult_ineq: "(\<Sum>i\<in>A. \<bar>f i\<bar> * \<bar>g i\<bar>) \<le> setL2 f A * setL2 g A"
```
```   474   apply (cases "finite A")
```
```   475   apply (induct set: finite)
```
```   476   apply simp
```
```   477   apply (rule power2_le_imp_le, simp)
```
```   478   apply (rule order_trans)
```
```   479   apply (rule power_mono)
```
```   480   apply (erule add_left_mono)
```
```   481   apply (simp add: add_nonneg_nonneg mult_nonneg_nonneg setsum_nonneg)
```
```   482   apply (simp add: power2_sum)
```
```   483   apply (simp add: power_mult_distrib)
```
```   484   apply (simp add: right_distrib left_distrib)
```
```   485   apply (rule ord_le_eq_trans)
```
```   486   apply (rule setL2_mult_ineq_lemma)
```
```   487   apply simp
```
```   488   apply (intro mult_nonneg_nonneg setL2_nonneg)
```
```   489   apply simp
```
```   490   done
```
```   491
```
```   492 lemma member_le_setL2: "\<lbrakk>finite A; i \<in> A\<rbrakk> \<Longrightarrow> f i \<le> setL2 f A"
```
```   493   apply (rule_tac s="insert i (A - {i})" and t="A" in subst)
```
```   494   apply fast
```
```   495   apply (subst setL2_insert)
```
```   496   apply simp
```
```   497   apply simp
```
```   498   apply simp
```
```   499   done
```
```   500
```
```   501 subsection {* Metric *}
```
```   502
```
```   503 instantiation "^" :: (metric_space, finite) metric_space
```
```   504 begin
```
```   505
```
```   506 definition dist_vector_def:
```
```   507   "dist (x::'a^'b) (y::'a^'b) = setL2 (\<lambda>i. dist (x\$i) (y\$i)) UNIV"
```
```   508
```
```   509 instance proof
```
```   510   fix x y :: "'a ^ 'b"
```
```   511   show "dist x y = 0 \<longleftrightarrow> x = y"
```
```   512     unfolding dist_vector_def
```
```   513     by (simp add: setL2_eq_0_iff Cart_eq)
```
```   514 next
```
```   515   fix x y z :: "'a ^ 'b"
```
```   516   show "dist x y \<le> dist x z + dist y z"
```
```   517     unfolding dist_vector_def
```
```   518     apply (rule order_trans [OF _ setL2_triangle_ineq])
```
```   519     apply (simp add: setL2_mono dist_triangle2)
```
```   520     done
```
```   521 qed
```
```   522
```
```   523 end
```
```   524
```
```   525 lemma dist_nth_le: "dist (x \$ i) (y \$ i) \<le> dist x y"
```
```   526 unfolding dist_vector_def
```
```   527 by (rule member_le_setL2) simp_all
```
```   528
```
```   529 lemma tendsto_Cart_nth:
```
```   530   fixes X :: "'a \<Rightarrow> 'b::metric_space ^ 'n::finite"
```
```   531   assumes "tendsto (\<lambda>n. X n) a net"
```
```   532   shows "tendsto (\<lambda>n. X n \$ i) (a \$ i) net"
```
```   533 proof (rule tendstoI)
```
```   534   fix e :: real assume "0 < e"
```
```   535   with assms have "eventually (\<lambda>n. dist (X n) a < e) net"
```
```   536     by (rule tendstoD)
```
```   537   thus "eventually (\<lambda>n. dist (X n \$ i) (a \$ i) < e) net"
```
```   538   proof (rule eventually_elim1)
```
```   539     fix n :: 'a
```
```   540     have "dist (X n \$ i) (a \$ i) \<le> dist (X n) a"
```
```   541       by (rule dist_nth_le)
```
```   542     also assume "dist (X n) a < e"
```
```   543     finally show "dist (X n \$ i) (a \$ i) < e" .
```
```   544   qed
```
```   545 qed
```
```   546
```
```   547 lemma LIMSEQ_Cart_nth:
```
```   548   "(X ----> a) \<Longrightarrow> (\<lambda>n. X n \$ i) ----> a \$ i"
```
```   549 unfolding LIMSEQ_conv_tendsto by (rule tendsto_Cart_nth)
```
```   550
```
```   551 lemma LIM_Cart_nth:
```
```   552   "(f -- x --> y) \<Longrightarrow> (\<lambda>x. f x \$ i) -- x --> y \$ i"
```
```   553 unfolding LIM_conv_tendsto by (rule tendsto_Cart_nth)
```
```   554
```
```   555 lemma Cauchy_Cart_nth:
```
```   556   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n::finite"
```
```   557   assumes "Cauchy (\<lambda>n. X n)"
```
```   558   shows "Cauchy (\<lambda>n. X n \$ i)"
```
```   559 proof (rule metric_CauchyI)
```
```   560   fix e :: real assume "0 < e"
```
```   561   obtain M where "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
```
```   562     using metric_CauchyD [OF `Cauchy X` `0 < e`] by fast
```
```   563   moreover
```
```   564   {
```
```   565     fix m n
```
```   566     assume "M \<le> m" "M \<le> n"
```
```   567     have "dist (X m \$ i) (X n \$ i) \<le> dist (X m) (X n)"
```
```   568       by (rule dist_nth_le)
```
```   569     also assume "dist (X m) (X n) < e"
```
```   570     finally have "dist (X m \$ i) (X n \$ i) < e" .
```
```   571   }
```
```   572   ultimately
```
```   573   have "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m \$ i) (X n \$ i) < e" by fast
```
```   574   thus "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m \$ i) (X n \$ i) < e" ..
```
```   575 qed
```
```   576
```
```   577 lemma LIMSEQ_vector:
```
```   578   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n::finite"
```
```   579   assumes X: "\<And>i. (\<lambda>n. X n \$ i) ----> (a \$ i)"
```
```   580   shows "X ----> a"
```
```   581 proof (rule metric_LIMSEQ_I)
```
```   582   fix r :: real assume "0 < r"
```
```   583   then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
```
```   584     by (simp add: divide_pos_pos)
```
```   585   def N \<equiv> "\<lambda>i. LEAST N. \<forall>n\<ge>N. dist (X n \$ i) (a \$ i) < ?s"
```
```   586   def M \<equiv> "Max (range N)"
```
```   587   have "\<And>i. \<exists>N. \<forall>n\<ge>N. dist (X n \$ i) (a \$ i) < ?s"
```
```   588     using X `0 < ?s` by (rule metric_LIMSEQ_D)
```
```   589   hence "\<And>i. \<forall>n\<ge>N i. dist (X n \$ i) (a \$ i) < ?s"
```
```   590     unfolding N_def by (rule LeastI_ex)
```
```   591   hence M: "\<And>i. \<forall>n\<ge>M. dist (X n \$ i) (a \$ i) < ?s"
```
```   592     unfolding M_def by simp
```
```   593   {
```
```   594     fix n :: nat assume "M \<le> n"
```
```   595     have "dist (X n) a = setL2 (\<lambda>i. dist (X n \$ i) (a \$ i)) UNIV"
```
```   596       unfolding dist_vector_def ..
```
```   597     also have "\<dots> \<le> setsum (\<lambda>i. dist (X n \$ i) (a \$ i)) UNIV"
```
```   598       by (rule setL2_le_setsum [OF zero_le_dist])
```
```   599     also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
```
```   600       by (rule setsum_strict_mono, simp_all add: M `M \<le> n`)
```
```   601     also have "\<dots> = r"
```
```   602       by simp
```
```   603     finally have "dist (X n) a < r" .
```
```   604   }
```
```   605   hence "\<forall>n\<ge>M. dist (X n) a < r"
```
```   606     by simp
```
```   607   then show "\<exists>M. \<forall>n\<ge>M. dist (X n) a < r" ..
```
```   608 qed
```
```   609
```
```   610 lemma Cauchy_vector:
```
```   611   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n::finite"
```
```   612   assumes X: "\<And>i. Cauchy (\<lambda>n. X n \$ i)"
```
```   613   shows "Cauchy (\<lambda>n. X n)"
```
```   614 proof (rule metric_CauchyI)
```
```   615   fix r :: real assume "0 < r"
```
```   616   then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
```
```   617     by (simp add: divide_pos_pos)
```
```   618   def N \<equiv> "\<lambda>i. LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m \$ i) (X n \$ i) < ?s"
```
```   619   def M \<equiv> "Max (range N)"
```
```   620   have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m \$ i) (X n \$ i) < ?s"
```
```   621     using X `0 < ?s` by (rule metric_CauchyD)
```
```   622   hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m \$ i) (X n \$ i) < ?s"
```
```   623     unfolding N_def by (rule LeastI_ex)
```
```   624   hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m \$ i) (X n \$ i) < ?s"
```
```   625     unfolding M_def by simp
```
```   626   {
```
```   627     fix m n :: nat
```
```   628     assume "M \<le> m" "M \<le> n"
```
```   629     have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m \$ i) (X n \$ i)) UNIV"
```
```   630       unfolding dist_vector_def ..
```
```   631     also have "\<dots> \<le> setsum (\<lambda>i. dist (X m \$ i) (X n \$ i)) UNIV"
```
```   632       by (rule setL2_le_setsum [OF zero_le_dist])
```
```   633     also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
```
```   634       by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
```
```   635     also have "\<dots> = r"
```
```   636       by simp
```
```   637     finally have "dist (X m) (X n) < r" .
```
```   638   }
```
```   639   hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
```
```   640     by simp
```
```   641   then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
```
```   642 qed
```
```   643
```
```   644 subsection {* Norms *}
```
```   645
```
```   646 instantiation "^" :: (real_normed_vector, finite) real_normed_vector
```
```   647 begin
```
```   648
```
```   649 definition vector_norm_def:
```
```   650   "norm (x::'a^'b) = setL2 (\<lambda>i. norm (x\$i)) UNIV"
```
```   651
```
```   652 definition vector_sgn_def:
```
```   653   "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
```
```   654
```
```   655 instance proof
```
```   656   fix a :: real and x y :: "'a ^ 'b"
```
```   657   show "0 \<le> norm x"
```
```   658     unfolding vector_norm_def
```
```   659     by (rule setL2_nonneg)
```
```   660   show "norm x = 0 \<longleftrightarrow> x = 0"
```
```   661     unfolding vector_norm_def
```
```   662     by (simp add: setL2_eq_0_iff Cart_eq)
```
```   663   show "norm (x + y) \<le> norm x + norm y"
```
```   664     unfolding vector_norm_def
```
```   665     apply (rule order_trans [OF _ setL2_triangle_ineq])
```
```   666     apply (simp add: setL2_mono norm_triangle_ineq)
```
```   667     done
```
```   668   show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
```
```   669     unfolding vector_norm_def
```
```   670     by (simp add: norm_scaleR setL2_right_distrib)
```
```   671   show "sgn x = scaleR (inverse (norm x)) x"
```
```   672     by (rule vector_sgn_def)
```
```   673   show "dist x y = norm (x - y)"
```
```   674     unfolding dist_vector_def vector_norm_def
```
```   675     by (simp add: dist_norm)
```
```   676 qed
```
```   677
```
```   678 end
```
```   679
```
```   680 lemma norm_nth_le: "norm (x \$ i) \<le> norm x"
```
```   681 unfolding vector_norm_def
```
```   682 by (rule member_le_setL2) simp_all
```
```   683
```
```   684 interpretation Cart_nth: bounded_linear "\<lambda>x. x \$ i"
```
```   685 apply default
```
```   686 apply (rule vector_add_component)
```
```   687 apply (rule vector_scaleR_component)
```
```   688 apply (rule_tac x="1" in exI, simp add: norm_nth_le)
```
```   689 done
```
```   690
```
```   691 subsection {* Inner products *}
```
```   692
```
```   693 instantiation "^" :: (real_inner, finite) real_inner
```
```   694 begin
```
```   695
```
```   696 definition vector_inner_def:
```
```   697   "inner x y = setsum (\<lambda>i. inner (x\$i) (y\$i)) UNIV"
```
```   698
```
```   699 instance proof
```
```   700   fix r :: real and x y z :: "'a ^ 'b"
```
```   701   show "inner x y = inner y x"
```
```   702     unfolding vector_inner_def
```
```   703     by (simp add: inner_commute)
```
```   704   show "inner (x + y) z = inner x z + inner y z"
```
```   705     unfolding vector_inner_def
```
```   706     by (simp add: inner_left_distrib setsum_addf)
```
```   707   show "inner (scaleR r x) y = r * inner x y"
```
```   708     unfolding vector_inner_def
```
```   709     by (simp add: inner_scaleR_left setsum_right_distrib)
```
```   710   show "0 \<le> inner x x"
```
```   711     unfolding vector_inner_def
```
```   712     by (simp add: setsum_nonneg)
```
```   713   show "inner x x = 0 \<longleftrightarrow> x = 0"
```
```   714     unfolding vector_inner_def
```
```   715     by (simp add: Cart_eq setsum_nonneg_eq_0_iff)
```
```   716   show "norm x = sqrt (inner x x)"
```
```   717     unfolding vector_inner_def vector_norm_def setL2_def
```
```   718     by (simp add: power2_norm_eq_inner)
```
```   719 qed
```
```   720
```
```   721 end
```
```   722
```
```   723 subsection{* Properties of the dot product.  *}
```
```   724
```
```   725 lemma dot_sym: "(x::'a:: {comm_monoid_add, ab_semigroup_mult} ^ 'n) \<bullet> y = y \<bullet> x"
```
```   726   by (vector mult_commute)
```
```   727 lemma dot_ladd: "((x::'a::ring ^ 'n) + y) \<bullet> z = (x \<bullet> z) + (y \<bullet> z)"
```
```   728   by (vector ring_simps)
```
```   729 lemma dot_radd: "x \<bullet> (y + (z::'a::ring ^ 'n)) = (x \<bullet> y) + (x \<bullet> z)"
```
```   730   by (vector ring_simps)
```
```   731 lemma dot_lsub: "((x::'a::ring ^ 'n) - y) \<bullet> z = (x \<bullet> z) - (y \<bullet> z)"
```
```   732   by (vector ring_simps)
```
```   733 lemma dot_rsub: "(x::'a::ring ^ 'n) \<bullet> (y - z) = (x \<bullet> y) - (x \<bullet> z)"
```
```   734   by (vector ring_simps)
```
```   735 lemma dot_lmult: "(c *s x) \<bullet> y = (c::'a::ring) * (x \<bullet> y)" by (vector ring_simps)
```
```   736 lemma dot_rmult: "x \<bullet> (c *s y) = (c::'a::comm_ring) * (x \<bullet> y)" by (vector ring_simps)
```
```   737 lemma dot_lneg: "(-x) \<bullet> (y::'a::ring ^ 'n) = -(x \<bullet> y)" by vector
```
```   738 lemma dot_rneg: "(x::'a::ring ^ 'n) \<bullet> (-y) = -(x \<bullet> y)" by vector
```
```   739 lemma dot_lzero[simp]: "0 \<bullet> x = (0::'a::{comm_monoid_add, mult_zero})" by vector
```
```   740 lemma dot_rzero[simp]: "x \<bullet> 0 = (0::'a::{comm_monoid_add, mult_zero})" by vector
```
```   741 lemma dot_pos_le[simp]: "(0::'a\<Colon>ordered_ring_strict) <= x \<bullet> x"
```
```   742   by (simp add: dot_def setsum_nonneg)
```
```   743
```
```   744 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)"
```
```   745 using fS fp setsum_nonneg[OF fp]
```
```   746 proof (induct set: finite)
```
```   747   case empty thus ?case by simp
```
```   748 next
```
```   749   case (insert x F)
```
```   750   from insert.prems have Fx: "f x \<ge> 0" and Fp: "\<forall> a \<in> F. f a \<ge> 0" by simp_all
```
```   751   from insert.hyps Fp setsum_nonneg[OF Fp]
```
```   752   have h: "setsum f F = 0 \<longleftrightarrow> (\<forall>a \<in>F. f a = 0)" by metis
```
```   753   from add_nonneg_eq_0_iff[OF Fx  setsum_nonneg[OF Fp]] insert.hyps(1,2)
```
```   754   show ?case by (simp add: h)
```
```   755 qed
```
```   756
```
```   757 lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) = 0"
```
```   758   by (simp add: dot_def setsum_squares_eq_0_iff Cart_eq)
```
```   759
```
```   760 lemma dot_pos_lt[simp]: "(0 < x \<bullet> x) \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) \<noteq> 0" using dot_eq_0[of x] dot_pos_le[of x]
```
```   761   by (auto simp add: le_less)
```
```   762
```
```   763 subsection{* The collapse of the general concepts to dimension one. *}
```
```   764
```
```   765 lemma vector_one: "(x::'a ^1) = (\<chi> i. (x\$1))"
```
```   766   by (simp add: Cart_eq forall_1)
```
```   767
```
```   768 lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
```
```   769   apply auto
```
```   770   apply (erule_tac x= "x\$1" in allE)
```
```   771   apply (simp only: vector_one[symmetric])
```
```   772   done
```
```   773
```
```   774 lemma norm_vector_1: "norm (x :: _^1) = norm (x\$1)"
```
```   775   by (simp add: vector_norm_def UNIV_1)
```
```   776
```
```   777 lemma norm_real: "norm(x::real ^ 1) = abs(x\$1)"
```
```   778   by (simp add: norm_vector_1)
```
```   779
```
```   780 lemma dist_real: "dist(x::real ^ 1) y = abs((x\$1) - (y\$1))"
```
```   781   by (auto simp add: norm_real dist_norm)
```
```   782
```
```   783 subsection {* A connectedness or intermediate value lemma with several applications. *}
```
```   784
```
```   785 lemma connected_real_lemma:
```
```   786   fixes f :: "real \<Rightarrow> real ^ 'n::finite"
```
```   787   assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
```
```   788   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"
```
```   789   and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
```
```   790   and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
```
```   791   and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
```
```   792   shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
```
```   793 proof-
```
```   794   let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
```
```   795   have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa)
```
```   796   have Sub: "\<exists>y. isUb UNIV ?S y"
```
```   797     apply (rule exI[where x= b])
```
```   798     using ab fb e12 by (auto simp add: isUb_def setle_def)
```
```   799   from reals_complete[OF Se Sub] obtain l where
```
```   800     l: "isLub UNIV ?S l"by blast
```
```   801   have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
```
```   802     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
```
```   803     by (metis linorder_linear)
```
```   804   have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
```
```   805     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
```
```   806     by (metis linorder_linear not_le)
```
```   807     have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
```
```   808     have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
```
```   809     have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by dlo
```
```   810     {assume le2: "f l \<in> e2"
```
```   811       from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
```
```   812       hence lap: "l - a > 0" using alb by arith
```
```   813       from e2[rule_format, OF le2] obtain e where
```
```   814 	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
```
```   815       from dst[OF alb e(1)] obtain d where
```
```   816 	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
```
```   817       have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" using lap d(1)
```
```   818 	apply ferrack by arith
```
```   819       then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
```
```   820       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
```
```   821       from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
```
```   822       moreover
```
```   823       have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
```
```   824       ultimately have False using e12 alb d' by auto}
```
```   825     moreover
```
```   826     {assume le1: "f l \<in> e1"
```
```   827     from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
```
```   828       hence blp: "b - l > 0" using alb by arith
```
```   829       from e1[rule_format, OF le1] obtain e where
```
```   830 	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
```
```   831       from dst[OF alb e(1)] obtain d where
```
```   832 	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
```
```   833       have "\<exists>d'. d' < d \<and> d' >0" using d(1) by dlo
```
```   834       then obtain d' where d': "d' > 0" "d' < d" by metis
```
```   835       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
```
```   836       hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
```
```   837       with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
```
```   838       with l d' have False
```
```   839 	by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
```
```   840     ultimately show ?thesis using alb by metis
```
```   841 qed
```
```   842
```
```   843 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"} *}
```
```   844
```
```   845 lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
```
```   846 proof-
```
```   847   have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith
```
```   848   thus ?thesis by (simp add: ring_simps power2_eq_square)
```
```   849 qed
```
```   850
```
```   851 lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
```
```   852   using isCont_power[OF isCont_ident, of 2, unfolded isCont_def LIM_eq, rule_format, of e x] apply (auto simp add: power2_eq_square)
```
```   853   apply (rule_tac x="s" in exI)
```
```   854   apply auto
```
```   855   apply (erule_tac x=y in allE)
```
```   856   apply auto
```
```   857   done
```
```   858
```
```   859 lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
```
```   860   using real_sqrt_le_iff[of x "y^2"] by simp
```
```   861
```
```   862 lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
```
```   863   using real_sqrt_le_mono[of "x^2" y] by simp
```
```   864
```
```   865 lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
```
```   866   using real_sqrt_less_mono[of "x^2" y] by simp
```
```   867
```
```   868 lemma sqrt_even_pow2: assumes n: "even n"
```
```   869   shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
```
```   870 proof-
```
```   871   from n obtain m where m: "n = 2*m" unfolding even_nat_equiv_def2
```
```   872     by (auto simp add: nat_number)
```
```   873   from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
```
```   874     by (simp only: power_mult[symmetric] mult_commute)
```
```   875   then show ?thesis  using m by simp
```
```   876 qed
```
```   877
```
```   878 lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
```
```   879   apply (cases "x = 0", simp_all)
```
```   880   using sqrt_divide_self_eq[of x]
```
```   881   apply (simp add: inverse_eq_divide real_sqrt_ge_0_iff field_simps)
```
```   882   done
```
```   883
```
```   884 text{* Hence derive more interesting properties of the norm. *}
```
```   885
```
```   886 text {*
```
```   887   This type-specific version is only here
```
```   888   to make @{text normarith.ML} happy.
```
```   889 *}
```
```   890 lemma norm_0: "norm (0::real ^ _) = 0"
```
```   891   by (rule norm_zero)
```
```   892
```
```   893 lemma norm_mul[simp]: "norm(a *s x) = abs(a) * norm x"
```
```   894   by (simp add: vector_norm_def vector_component setL2_right_distrib
```
```   895            abs_mult cong: strong_setL2_cong)
```
```   896 lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (x \<bullet> x = (0::real))"
```
```   897   by (simp add: vector_norm_def dot_def setL2_def power2_eq_square)
```
```   898 lemma real_vector_norm_def: "norm x = sqrt (x \<bullet> x)"
```
```   899   by (simp add: vector_norm_def setL2_def dot_def power2_eq_square)
```
```   900 lemma norm_pow_2: "norm x ^ 2 = x \<bullet> x"
```
```   901   by (simp add: real_vector_norm_def)
```
```   902 lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n::finite)" by (metis norm_eq_zero)
```
```   903 lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
```
```   904   by vector
```
```   905 lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
```
```   906   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
```
```   907 lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
```
```   908   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
```
```   909 lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
```
```   910   by (metis vector_mul_lcancel)
```
```   911 lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
```
```   912   by (metis vector_mul_rcancel)
```
```   913 lemma norm_cauchy_schwarz:
```
```   914   fixes x y :: "real ^ 'n::finite"
```
```   915   shows "x \<bullet> y <= norm x * norm y"
```
```   916 proof-
```
```   917   {assume "norm x = 0"
```
```   918     hence ?thesis by (simp add: dot_lzero dot_rzero)}
```
```   919   moreover
```
```   920   {assume "norm y = 0"
```
```   921     hence ?thesis by (simp add: dot_lzero dot_rzero)}
```
```   922   moreover
```
```   923   {assume h: "norm x \<noteq> 0" "norm y \<noteq> 0"
```
```   924     let ?z = "norm y *s x - norm x *s y"
```
```   925     from h have p: "norm x * norm y > 0" by (metis norm_ge_zero le_less zero_compare_simps)
```
```   926     from dot_pos_le[of ?z]
```
```   927     have "(norm x * norm y) * (x \<bullet> y) \<le> norm x ^2 * norm y ^2"
```
```   928       apply (simp add: dot_rsub dot_lsub dot_lmult dot_rmult ring_simps)
```
```   929       by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym)
```
```   930     hence "x\<bullet>y \<le> (norm x ^2 * norm y ^2) / (norm x * norm y)" using p
```
```   931       by (simp add: field_simps)
```
```   932     hence ?thesis using h by (simp add: power2_eq_square)}
```
```   933   ultimately show ?thesis by metis
```
```   934 qed
```
```   935
```
```   936 lemma norm_cauchy_schwarz_abs:
```
```   937   fixes x y :: "real ^ 'n::finite"
```
```   938   shows "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
```
```   939   using norm_cauchy_schwarz[of x y] norm_cauchy_schwarz[of x "-y"]
```
```   940   by (simp add: real_abs_def dot_rneg)
```
```   941
```
```   942 lemma norm_triangle_sub:
```
```   943   fixes x y :: "'a::real_normed_vector"
```
```   944   shows "norm x \<le> norm y  + norm (x - y)"
```
```   945   using norm_triangle_ineq[of "y" "x - y"] by (simp add: ring_simps)
```
```   946
```
```   947 lemma norm_triangle_le: "norm(x::real ^'n::finite) + norm y <= e ==> norm(x + y) <= e"
```
```   948   by (metis order_trans norm_triangle_ineq)
```
```   949 lemma norm_triangle_lt: "norm(x::real ^'n::finite) + norm(y) < e ==> norm(x + y) < e"
```
```   950   by (metis basic_trans_rules(21) norm_triangle_ineq)
```
```   951
```
```   952 lemma setsum_delta:
```
```   953   assumes fS: "finite S"
```
```   954   shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
```
```   955 proof-
```
```   956   let ?f = "(\<lambda>k. if k=a then b k else 0)"
```
```   957   {assume a: "a \<notin> S"
```
```   958     hence "\<forall> k\<in> S. ?f k = 0" by simp
```
```   959     hence ?thesis  using a by simp}
```
```   960   moreover
```
```   961   {assume a: "a \<in> S"
```
```   962     let ?A = "S - {a}"
```
```   963     let ?B = "{a}"
```
```   964     have eq: "S = ?A \<union> ?B" using a by blast
```
```   965     have dj: "?A \<inter> ?B = {}" by simp
```
```   966     from fS have fAB: "finite ?A" "finite ?B" by auto
```
```   967     have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
```
```   968       using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
```
```   969       by simp
```
```   970     then have ?thesis  using a by simp}
```
```   971   ultimately show ?thesis by blast
```
```   972 qed
```
```   973
```
```   974 lemma component_le_norm: "\<bar>x\$i\<bar> <= norm (x::real ^ 'n::finite)"
```
```   975   apply (simp add: vector_norm_def)
```
```   976   apply (rule member_le_setL2, simp_all)
```
```   977   done
```
```   978
```
```   979 lemma norm_bound_component_le: "norm(x::real ^ 'n::finite) <= e
```
```   980                 ==> \<bar>x\$i\<bar> <= e"
```
```   981   by (metis component_le_norm order_trans)
```
```   982
```
```   983 lemma norm_bound_component_lt: "norm(x::real ^ 'n::finite) < e
```
```   984                 ==> \<bar>x\$i\<bar> < e"
```
```   985   by (metis component_le_norm basic_trans_rules(21))
```
```   986
```
```   987 lemma norm_le_l1: "norm (x:: real ^'n::finite) <= setsum(\<lambda>i. \<bar>x\$i\<bar>) UNIV"
```
```   988   by (simp add: vector_norm_def setL2_le_setsum)
```
```   989
```
```   990 lemma real_abs_norm: "\<bar>norm x\<bar> = norm (x :: real ^ _)"
```
```   991   by (rule abs_norm_cancel)
```
```   992 lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n::finite) - norm y\<bar> <= norm(x - y)"
```
```   993   by (rule norm_triangle_ineq3)
```
```   994 lemma norm_le: "norm(x::real ^ _) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
```
```   995   by (simp add: real_vector_norm_def)
```
```   996 lemma norm_lt: "norm(x::real ^ _) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
```
```   997   by (simp add: real_vector_norm_def)
```
```   998 lemma norm_eq: "norm (x::real ^ _) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
```
```   999   by (simp add: order_eq_iff norm_le)
```
```  1000 lemma norm_eq_1: "norm(x::real ^ _) = 1 \<longleftrightarrow> x \<bullet> x = 1"
```
```  1001   by (simp add: real_vector_norm_def)
```
```  1002
```
```  1003 text{* Squaring equations and inequalities involving norms.  *}
```
```  1004
```
```  1005 lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
```
```  1006   by (simp add: real_vector_norm_def)
```
```  1007
```
```  1008 lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
```
```  1009   by (auto simp add: real_vector_norm_def)
```
```  1010
```
```  1011 lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
```
```  1012 proof-
```
```  1013   have "x^2 \<le> y^2 \<longleftrightarrow> (x -y) * (y + x) \<le> 0" by (simp add: ring_simps power2_eq_square)
```
```  1014   also have "\<dots> \<longleftrightarrow> \<bar>x\<bar> \<le> \<bar>y\<bar>" apply (simp add: zero_compare_simps real_abs_def not_less) by arith
```
```  1015 finally show ?thesis ..
```
```  1016 qed
```
```  1017
```
```  1018 lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
```
```  1019   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
```
```  1020   using norm_ge_zero[of x]
```
```  1021   apply arith
```
```  1022   done
```
```  1023
```
```  1024 lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2"
```
```  1025   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
```
```  1026   using norm_ge_zero[of x]
```
```  1027   apply arith
```
```  1028   done
```
```  1029
```
```  1030 lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
```
```  1031   by (metis not_le norm_ge_square)
```
```  1032 lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
```
```  1033   by (metis norm_le_square not_less)
```
```  1034
```
```  1035 text{* Dot product in terms of the norm rather than conversely. *}
```
```  1036
```
```  1037 lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
```
```  1038   by (simp add: norm_pow_2 dot_ladd dot_radd dot_sym)
```
```  1039
```
```  1040 lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
```
```  1041   by (simp add: norm_pow_2 dot_ladd dot_radd dot_lsub dot_rsub dot_sym)
```
```  1042
```
```  1043
```
```  1044 text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
```
```  1045
```
```  1046 lemma vector_eq: "(x:: real ^ 'n::finite) = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y\<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  1047 proof
```
```  1048   assume "?lhs" then show ?rhs by simp
```
```  1049 next
```
```  1050   assume ?rhs
```
```  1051   then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y\<bullet> y = 0" by simp
```
```  1052   hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
```
```  1053     by (simp add: dot_rsub dot_lsub dot_sym)
```
```  1054   then have "(x - y) \<bullet> (x - y) = 0" by (simp add: ring_simps dot_lsub dot_rsub)
```
```  1055   then show "x = y" by (simp add: dot_eq_0)
```
```  1056 qed
```
```  1057
```
```  1058
```
```  1059 subsection{* General linear decision procedure for normed spaces. *}
```
```  1060
```
```  1061 lemma norm_cmul_rule_thm: "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(c *s x)"
```
```  1062   apply (clarsimp simp add: norm_mul)
```
```  1063   apply (rule mult_mono1)
```
```  1064   apply simp_all
```
```  1065   done
```
```  1066
```
```  1067   (* FIXME: Move all these theorems into the ML code using lemma antiquotation *)
```
```  1068 lemma norm_add_rule_thm: "b1 >= norm(x1 :: real ^'n::finite) \<Longrightarrow> b2 >= norm(x2) ==> b1 + b2 >= norm(x1 + x2)"
```
```  1069   apply (rule norm_triangle_le) by simp
```
```  1070
```
```  1071 lemma ge_iff_diff_ge_0: "(a::'a::ordered_ring) \<ge> b == a - b \<ge> 0"
```
```  1072   by (simp add: ring_simps)
```
```  1073
```
```  1074 lemma pth_1: "(x::real^'n) == 1 *s x" by (simp only: vector_smult_lid)
```
```  1075 lemma pth_2: "x - (y::real^'n) == x + -y" by (atomize (full)) simp
```
```  1076 lemma pth_3: "(-x::real^'n) == -1 *s x" by vector
```
```  1077 lemma pth_4: "0 *s (x::real^'n) == 0" "c *s 0 = (0::real ^ 'n)" by vector+
```
```  1078 lemma pth_5: "c *s (d *s x) == (c * d) *s (x::real ^ 'n)" by (atomize (full)) vector
```
```  1079 lemma pth_6: "(c::real) *s (x + y) == c *s x + c *s y" by (atomize (full)) (vector ring_simps)
```
```  1080 lemma pth_7: "0 + x == (x::real^'n)" "x + 0 == x" by simp_all
```
```  1081 lemma pth_8: "(c::real) *s x + d *s x == (c + d) *s x" by (atomize (full)) (vector ring_simps)
```
```  1082 lemma pth_9: "((c::real) *s x + z) + d *s x == (c + d) *s x + z"
```
```  1083   "c *s x + (d *s x + z) == (c + d) *s x + z"
```
```  1084   "(c *s x + w) + (d *s x + z) == (c + d) *s x + (w + z)" by ((atomize (full)), vector ring_simps)+
```
```  1085 lemma pth_a: "(0::real) *s x + y == y" by (atomize (full)) vector
```
```  1086 lemma pth_b: "(c::real) *s x + d *s y == c *s x + d *s y"
```
```  1087   "(c *s x + z) + d *s y == c *s x + (z + d *s y)"
```
```  1088   "c *s x + (d *s y + z) == c *s x + (d *s y + z)"
```
```  1089   "(c *s x + w) + (d *s y + z) == c *s x + (w + (d *s y + z))"
```
```  1090   by ((atomize (full)), vector)+
```
```  1091 lemma pth_c: "(c::real) *s x + d *s y == d *s y + c *s x"
```
```  1092   "(c *s x + z) + d *s y == d *s y + (c *s x + z)"
```
```  1093   "c *s x + (d *s y + z) == d *s y + (c *s x + z)"
```
```  1094   "(c *s x + w) + (d *s y + z) == d *s y + ((c *s x + w) + z)" by ((atomize (full)), vector)+
```
```  1095 lemma pth_d: "x + (0::real ^'n) == x" by (atomize (full)) vector
```
```  1096
```
```  1097 lemma norm_imp_pos_and_ge: "norm (x::real ^ _) == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
```
```  1098   by (atomize) (auto simp add: norm_ge_zero)
```
```  1099
```
```  1100 lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
```
```  1101
```
```  1102 lemma norm_pths:
```
```  1103   "(x::real ^'n::finite) = y \<longleftrightarrow> norm (x - y) \<le> 0"
```
```  1104   "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
```
```  1105   using norm_ge_zero[of "x - y"] by auto
```
```  1106
```
```  1107 lemma vector_dist_norm:
```
```  1108   fixes x y :: "real ^ _"
```
```  1109   shows "dist x y = norm (x - y)"
```
```  1110   by (rule dist_norm)
```
```  1111
```
```  1112 use "normarith.ML"
```
```  1113
```
```  1114 method_setup norm = {* Scan.succeed (SIMPLE_METHOD' o NormArith.norm_arith_tac)
```
```  1115 *} "Proves simple linear statements about vector norms"
```
```  1116
```
```  1117
```
```  1118
```
```  1119 text{* Hence more metric properties. *}
```
```  1120
```
```  1121 lemma dist_triangle_alt:
```
```  1122   fixes x y z :: "'a::metric_space"
```
```  1123   shows "dist y z <= dist x y + dist x z"
```
```  1124 using dist_triangle [of y z x] by (simp add: dist_commute)
```
```  1125
```
```  1126 lemma dist_pos_lt:
```
```  1127   fixes x y :: "'a::metric_space"
```
```  1128   shows "x \<noteq> y ==> 0 < dist x y"
```
```  1129 by (simp add: zero_less_dist_iff)
```
```  1130
```
```  1131 lemma dist_nz:
```
```  1132   fixes x y :: "'a::metric_space"
```
```  1133   shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
```
```  1134 by (simp add: zero_less_dist_iff)
```
```  1135
```
```  1136 lemma dist_triangle_le:
```
```  1137   fixes x y z :: "'a::metric_space"
```
```  1138   shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
```
```  1139 by (rule order_trans [OF dist_triangle2])
```
```  1140
```
```  1141 lemma dist_triangle_lt:
```
```  1142   fixes x y z :: "'a::metric_space"
```
```  1143   shows "dist x z + dist y z < e ==> dist x y < e"
```
```  1144 by (rule le_less_trans [OF dist_triangle2])
```
```  1145
```
```  1146 lemma dist_triangle_half_l:
```
```  1147   fixes x1 x2 y :: "'a::metric_space"
```
```  1148   shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
```
```  1149 by (rule dist_triangle_lt [where z=y], simp)
```
```  1150
```
```  1151 lemma dist_triangle_half_r:
```
```  1152   fixes x1 x2 y :: "'a::metric_space"
```
```  1153   shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
```
```  1154 by (rule dist_triangle_half_l, simp_all add: dist_commute)
```
```  1155
```
```  1156 lemma dist_triangle_add:
```
```  1157   fixes x y x' y' :: "'a::real_normed_vector"
```
```  1158   shows "dist (x + y) (x' + y') <= dist x x' + dist y y'"
```
```  1159 unfolding dist_norm by (rule norm_diff_triangle_ineq)
```
```  1160
```
```  1161 lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
```
```  1162   unfolding dist_norm vector_ssub_ldistrib[symmetric] norm_mul ..
```
```  1163
```
```  1164 lemma dist_triangle_add_half:
```
```  1165   fixes x x' y y' :: "'a::real_normed_vector"
```
```  1166   shows "dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 \<Longrightarrow> dist(x + y) (x' + y') < e"
```
```  1167 by (rule le_less_trans [OF dist_triangle_add], simp)
```
```  1168
```
```  1169 lemma setsum_component [simp]:
```
```  1170   fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
```
```  1171   shows "(setsum f S)\$i = setsum (\<lambda>x. (f x)\$i) S"
```
```  1172   by (cases "finite S", induct S set: finite, simp_all)
```
```  1173
```
```  1174 lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)\$i ) S)"
```
```  1175   by (simp add: Cart_eq)
```
```  1176
```
```  1177 lemma setsum_clauses:
```
```  1178   shows "setsum f {} = 0"
```
```  1179   and "finite S \<Longrightarrow> setsum f (insert x S) =
```
```  1180                  (if x \<in> S then setsum f S else f x + setsum f S)"
```
```  1181   by (auto simp add: insert_absorb)
```
```  1182
```
```  1183 lemma setsum_cmul:
```
```  1184   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
```
```  1185   shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
```
```  1186   by (simp add: Cart_eq setsum_right_distrib)
```
```  1187
```
```  1188 lemma setsum_norm:
```
```  1189   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
```
```  1190   assumes fS: "finite S"
```
```  1191   shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
```
```  1192 proof(induct rule: finite_induct[OF fS])
```
```  1193   case 1 thus ?case by simp
```
```  1194 next
```
```  1195   case (2 x S)
```
```  1196   from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
```
```  1197   also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
```
```  1198     using "2.hyps" by simp
```
```  1199   finally  show ?case  using "2.hyps" by simp
```
```  1200 qed
```
```  1201
```
```  1202 lemma real_setsum_norm:
```
```  1203   fixes f :: "'a \<Rightarrow> real ^'n::finite"
```
```  1204   assumes fS: "finite S"
```
```  1205   shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
```
```  1206 proof(induct rule: finite_induct[OF fS])
```
```  1207   case 1 thus ?case by simp
```
```  1208 next
```
```  1209   case (2 x S)
```
```  1210   from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
```
```  1211   also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
```
```  1212     using "2.hyps" by simp
```
```  1213   finally  show ?case  using "2.hyps" by simp
```
```  1214 qed
```
```  1215
```
```  1216 lemma setsum_norm_le:
```
```  1217   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
```
```  1218   assumes fS: "finite S"
```
```  1219   and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
```
```  1220   shows "norm (setsum f S) \<le> setsum g S"
```
```  1221 proof-
```
```  1222   from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
```
```  1223     by - (rule setsum_mono, simp)
```
```  1224   then show ?thesis using setsum_norm[OF fS, of f] fg
```
```  1225     by arith
```
```  1226 qed
```
```  1227
```
```  1228 lemma real_setsum_norm_le:
```
```  1229   fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
```
```  1230   assumes fS: "finite S"
```
```  1231   and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
```
```  1232   shows "norm (setsum f S) \<le> setsum g S"
```
```  1233 proof-
```
```  1234   from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
```
```  1235     by - (rule setsum_mono, simp)
```
```  1236   then show ?thesis using real_setsum_norm[OF fS, of f] fg
```
```  1237     by arith
```
```  1238 qed
```
```  1239
```
```  1240 lemma setsum_norm_bound:
```
```  1241   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
```
```  1242   assumes fS: "finite S"
```
```  1243   and K: "\<forall>x \<in> S. norm (f x) \<le> K"
```
```  1244   shows "norm (setsum f S) \<le> of_nat (card S) * K"
```
```  1245   using setsum_norm_le[OF fS K] setsum_constant[symmetric]
```
```  1246   by simp
```
```  1247
```
```  1248 lemma real_setsum_norm_bound:
```
```  1249   fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
```
```  1250   assumes fS: "finite S"
```
```  1251   and K: "\<forall>x \<in> S. norm (f x) \<le> K"
```
```  1252   shows "norm (setsum f S) \<le> of_nat (card S) * K"
```
```  1253   using real_setsum_norm_le[OF fS K] setsum_constant[symmetric]
```
```  1254   by simp
```
```  1255
```
```  1256 lemma setsum_vmul:
```
```  1257   fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector,semiring, mult_zero}"
```
```  1258   assumes fS: "finite S"
```
```  1259   shows "setsum f S *s v = setsum (\<lambda>x. f x *s v) S"
```
```  1260 proof(induct rule: finite_induct[OF fS])
```
```  1261   case 1 then show ?case by (simp add: vector_smult_lzero)
```
```  1262 next
```
```  1263   case (2 x F)
```
```  1264   from "2.hyps" have "setsum f (insert x F) *s v = (f x + setsum f F) *s v"
```
```  1265     by simp
```
```  1266   also have "\<dots> = f x *s v + setsum f F *s v"
```
```  1267     by (simp add: vector_sadd_rdistrib)
```
```  1268   also have "\<dots> = setsum (\<lambda>x. f x *s v) (insert x F)" using "2.hyps" by simp
```
```  1269   finally show ?case .
```
```  1270 qed
```
```  1271
```
```  1272 (* FIXME : Problem thm setsum_vmul[of _ "f:: 'a \<Rightarrow> real ^'n"]  ---
```
```  1273  Get rid of *s and use real_vector instead! Also prove that ^ creates a real_vector !! *)
```
```  1274
```
```  1275 lemma setsum_add_split: assumes mn: "(m::nat) \<le> n + 1"
```
```  1276   shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
```
```  1277 proof-
```
```  1278   let ?A = "{m .. n}"
```
```  1279   let ?B = "{n + 1 .. n + p}"
```
```  1280   have eq: "{m .. n+p} = ?A \<union> ?B" using mn by auto
```
```  1281   have d: "?A \<inter> ?B = {}" by auto
```
```  1282   from setsum_Un_disjoint[of "?A" "?B" f] eq d show ?thesis by auto
```
```  1283 qed
```
```  1284
```
```  1285 lemma setsum_natinterval_left:
```
```  1286   assumes mn: "(m::nat) <= n"
```
```  1287   shows "setsum f {m..n} = f m + setsum f {m + 1..n}"
```
```  1288 proof-
```
```  1289   from mn have "{m .. n} = insert m {m+1 .. n}" by auto
```
```  1290   then show ?thesis by auto
```
```  1291 qed
```
```  1292
```
```  1293 lemma setsum_natinterval_difff:
```
```  1294   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
```
```  1295   shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
```
```  1296           (if m <= n then f m - f(n + 1) else 0)"
```
```  1297 by (induct n, auto simp add: ring_simps not_le le_Suc_eq)
```
```  1298
```
```  1299 lemmas setsum_restrict_set' = setsum_restrict_set[unfolded Int_def]
```
```  1300
```
```  1301 lemma setsum_setsum_restrict:
```
```  1302   "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"
```
```  1303   apply (simp add: setsum_restrict_set'[unfolded mem_def] mem_def)
```
```  1304   by (rule setsum_commute)
```
```  1305
```
```  1306 lemma setsum_image_gen: assumes fS: "finite S"
```
```  1307   shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
```
```  1308 proof-
```
```  1309   {fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto}
```
```  1310   note th0 = this
```
```  1311   have "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
```
```  1312     apply (rule setsum_cong2)
```
```  1313     by (simp add: th0)
```
```  1314   also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
```
```  1315     apply (rule setsum_setsum_restrict[OF fS])
```
```  1316     by (rule finite_imageI[OF fS])
```
```  1317   finally show ?thesis .
```
```  1318 qed
```
```  1319
```
```  1320     (* FIXME: Here too need stupid finiteness assumption on T!!! *)
```
```  1321 lemma setsum_group:
```
```  1322   assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
```
```  1323   shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
```
```  1324
```
```  1325 apply (subst setsum_image_gen[OF fS, of g f])
```
```  1326 apply (rule setsum_mono_zero_right[OF fT fST])
```
```  1327 by (auto intro: setsum_0')
```
```  1328
```
```  1329 lemma vsum_norm_allsubsets_bound:
```
```  1330   fixes f:: "'a \<Rightarrow> real ^'n::finite"
```
```  1331   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
```
```  1332   shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
```
```  1333 proof-
```
```  1334   let ?d = "real CARD('n)"
```
```  1335   let ?nf = "\<lambda>x. norm (f x)"
```
```  1336   let ?U = "UNIV :: 'n set"
```
```  1337   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"
```
```  1338     by (rule setsum_commute)
```
```  1339   have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
```
```  1340   have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x \$ i\<bar>) ?U) P"
```
```  1341     apply (rule setsum_mono)
```
```  1342     by (rule norm_le_l1)
```
```  1343   also have "\<dots> \<le> 2 * ?d * e"
```
```  1344     unfolding th0 th1
```
```  1345   proof(rule setsum_bounded)
```
```  1346     fix i assume i: "i \<in> ?U"
```
```  1347     let ?Pp = "{x. x\<in> P \<and> f x \$ i \<ge> 0}"
```
```  1348     let ?Pn = "{x. x \<in> P \<and> f x \$ i < 0}"
```
```  1349     have thp: "P = ?Pp \<union> ?Pn" by auto
```
```  1350     have thp0: "?Pp \<inter> ?Pn ={}" by auto
```
```  1351     have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
```
```  1352     have Ppe:"setsum (\<lambda>x. \<bar>f x \$ i\<bar>) ?Pp \<le> e"
```
```  1353       using component_le_norm[of "setsum (\<lambda>x. f x) ?Pp" i]  fPs[OF PpP]
```
```  1354       by (auto intro: abs_le_D1)
```
```  1355     have Pne: "setsum (\<lambda>x. \<bar>f x \$ i\<bar>) ?Pn \<le> e"
```
```  1356       using component_le_norm[of "setsum (\<lambda>x. - f x) ?Pn" i]  fPs[OF PnP]
```
```  1357       by (auto simp add: setsum_negf intro: abs_le_D1)
```
```  1358     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"
```
```  1359       apply (subst thp)
```
```  1360       apply (rule setsum_Un_zero)
```
```  1361       using fP thp0 by auto
```
```  1362     also have "\<dots> \<le> 2*e" using Pne Ppe by arith
```
```  1363     finally show "setsum (\<lambda>x. \<bar>f x \$ i\<bar>) P \<le> 2*e" .
```
```  1364   qed
```
```  1365   finally show ?thesis .
```
```  1366 qed
```
```  1367
```
```  1368 lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> (y::'a::{comm_ring}^'n) = setsum (\<lambda>x. f x \<bullet> y) S "
```
```  1369   by (induct rule: finite_induct, auto simp add: dot_lzero dot_ladd dot_radd)
```
```  1370
```
```  1371 lemma dot_rsum: "finite S \<Longrightarrow> (y::'a::{comm_ring}^'n) \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
```
```  1372   by (induct rule: finite_induct, auto simp add: dot_rzero dot_radd)
```
```  1373
```
```  1374 subsection{* Basis vectors in coordinate directions. *}
```
```  1375
```
```  1376
```
```  1377 definition "basis k = (\<chi> i. if i = k then 1 else 0)"
```
```  1378
```
```  1379 lemma basis_component [simp]: "basis k \$ i = (if k=i then 1 else 0)"
```
```  1380   unfolding basis_def by simp
```
```  1381
```
```  1382 lemma delta_mult_idempotent:
```
```  1383   "(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)
```
```  1384
```
```  1385 lemma norm_basis:
```
```  1386   shows "norm (basis k :: real ^'n::finite) = 1"
```
```  1387   apply (simp add: basis_def real_vector_norm_def dot_def)
```
```  1388   apply (vector delta_mult_idempotent)
```
```  1389   using setsum_delta[of "UNIV :: 'n set" "k" "\<lambda>k. 1::real"]
```
```  1390   apply auto
```
```  1391   done
```
```  1392
```
```  1393 lemma norm_basis_1: "norm(basis 1 :: real ^'n::{finite,one}) = 1"
```
```  1394   by (rule norm_basis)
```
```  1395
```
```  1396 lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n::finite). norm x = c"
```
```  1397   apply (rule exI[where x="c *s basis arbitrary"])
```
```  1398   by (simp only: norm_mul norm_basis)
```
```  1399
```
```  1400 lemma vector_choose_dist: assumes e: "0 <= e"
```
```  1401   shows "\<exists>(y::real^'n::finite). dist x y = e"
```
```  1402 proof-
```
```  1403   from vector_choose_size[OF e] obtain c:: "real ^'n"  where "norm c = e"
```
```  1404     by blast
```
```  1405   then have "dist x (x - c) = e" by (simp add: dist_norm)
```
```  1406   then show ?thesis by blast
```
```  1407 qed
```
```  1408
```
```  1409 lemma basis_inj: "inj (basis :: 'n \<Rightarrow> real ^'n::finite)"
```
```  1410   by (simp add: inj_on_def Cart_eq)
```
```  1411
```
```  1412 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
```
```  1413   by auto
```
```  1414
```
```  1415 lemma basis_expansion:
```
```  1416   "setsum (\<lambda>i. (x\$i) *s basis i) UNIV = (x::('a::ring_1) ^'n::finite)" (is "?lhs = ?rhs" is "setsum ?f ?S = _")
```
```  1417   by (auto simp add: Cart_eq cond_value_iff setsum_delta[of "?S", where ?'b = "'a", simplified] cong del: if_weak_cong)
```
```  1418
```
```  1419 lemma basis_expansion_unique:
```
```  1420   "setsum (\<lambda>i. f i *s basis i) UNIV = (x::('a::comm_ring_1) ^'n::finite) \<longleftrightarrow> (\<forall>i. f i = x\$i)"
```
```  1421   by (simp add: Cart_eq setsum_delta cond_value_iff cong del: if_weak_cong)
```
```  1422
```
```  1423 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
```
```  1424   by auto
```
```  1425
```
```  1426 lemma dot_basis:
```
```  1427   shows "basis i \<bullet> x = x\$i" "x \<bullet> (basis i :: 'a^'n::finite) = (x\$i :: 'a::semiring_1)"
```
```  1428   by (auto simp add: dot_def basis_def cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
```
```  1429
```
```  1430 lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> False"
```
```  1431   by (auto simp add: Cart_eq)
```
```  1432
```
```  1433 lemma basis_nonzero:
```
```  1434   shows "basis k \<noteq> (0:: 'a::semiring_1 ^'n)"
```
```  1435   by (simp add: basis_eq_0)
```
```  1436
```
```  1437 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n::finite)"
```
```  1438   apply (auto simp add: Cart_eq dot_basis)
```
```  1439   apply (erule_tac x="basis i" in allE)
```
```  1440   apply (simp add: dot_basis)
```
```  1441   apply (subgoal_tac "y = z")
```
```  1442   apply simp
```
```  1443   apply (simp add: Cart_eq)
```
```  1444   done
```
```  1445
```
```  1446 lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n::finite)"
```
```  1447   apply (auto simp add: Cart_eq dot_basis)
```
```  1448   apply (erule_tac x="basis i" in allE)
```
```  1449   apply (simp add: dot_basis)
```
```  1450   apply (subgoal_tac "x = y")
```
```  1451   apply simp
```
```  1452   apply (simp add: Cart_eq)
```
```  1453   done
```
```  1454
```
```  1455 subsection{* Orthogonality. *}
```
```  1456
```
```  1457 definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
```
```  1458
```
```  1459 lemma orthogonal_basis:
```
```  1460   shows "orthogonal (basis i :: 'a^'n::finite) x \<longleftrightarrow> x\$i = (0::'a::ring_1)"
```
```  1461   by (auto simp add: orthogonal_def dot_def basis_def cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
```
```  1462
```
```  1463 lemma orthogonal_basis_basis:
```
```  1464   shows "orthogonal (basis i :: 'a::ring_1^'n::finite) (basis j) \<longleftrightarrow> i \<noteq> j"
```
```  1465   unfolding orthogonal_basis[of i] basis_component[of j] by simp
```
```  1466
```
```  1467   (* FIXME : Maybe some of these require less than comm_ring, but not all*)
```
```  1468 lemma orthogonal_clauses:
```
```  1469   "orthogonal a (0::'a::comm_ring ^'n)"
```
```  1470   "orthogonal a x ==> orthogonal a (c *s x)"
```
```  1471   "orthogonal a x ==> orthogonal a (-x)"
```
```  1472   "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x + y)"
```
```  1473   "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x - y)"
```
```  1474   "orthogonal 0 a"
```
```  1475   "orthogonal x a ==> orthogonal (c *s x) a"
```
```  1476   "orthogonal x a ==> orthogonal (-x) a"
```
```  1477   "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x + y) a"
```
```  1478   "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x - y) a"
```
```  1479   unfolding orthogonal_def dot_rneg dot_rmult dot_radd dot_rsub
```
```  1480   dot_lzero dot_rzero dot_lneg dot_lmult dot_ladd dot_lsub
```
```  1481   by simp_all
```
```  1482
```
```  1483 lemma orthogonal_commute: "orthogonal (x::'a::{ab_semigroup_mult,comm_monoid_add} ^'n)y \<longleftrightarrow> orthogonal y x"
```
```  1484   by (simp add: orthogonal_def dot_sym)
```
```  1485
```
```  1486 subsection{* Explicit vector construction from lists. *}
```
```  1487
```
```  1488 primrec from_nat :: "nat \<Rightarrow> 'a::{monoid_add,one}"
```
```  1489 where "from_nat 0 = 0" | "from_nat (Suc n) = 1 + from_nat n"
```
```  1490
```
```  1491 lemma from_nat [simp]: "from_nat = of_nat"
```
```  1492 by (rule ext, induct_tac x, simp_all)
```
```  1493
```
```  1494 primrec
```
```  1495   list_fun :: "nat \<Rightarrow> _ list \<Rightarrow> _ \<Rightarrow> _"
```
```  1496 where
```
```  1497   "list_fun n [] = (\<lambda>x. 0)"
```
```  1498 | "list_fun n (x # xs) = fun_upd (list_fun (Suc n) xs) (from_nat n) x"
```
```  1499
```
```  1500 definition "vector l = (\<chi> i. list_fun 1 l i)"
```
```  1501 (*definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"*)
```
```  1502
```
```  1503 lemma vector_1: "(vector[x]) \$1 = x"
```
```  1504   unfolding vector_def by simp
```
```  1505
```
```  1506 lemma vector_2:
```
```  1507  "(vector[x,y]) \$1 = x"
```
```  1508  "(vector[x,y] :: 'a^2)\$2 = (y::'a::zero)"
```
```  1509   unfolding vector_def by simp_all
```
```  1510
```
```  1511 lemma vector_3:
```
```  1512  "(vector [x,y,z] ::('a::zero)^3)\$1 = x"
```
```  1513  "(vector [x,y,z] ::('a::zero)^3)\$2 = y"
```
```  1514  "(vector [x,y,z] ::('a::zero)^3)\$3 = z"
```
```  1515   unfolding vector_def by simp_all
```
```  1516
```
```  1517 lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
```
```  1518   apply auto
```
```  1519   apply (erule_tac x="v\$1" in allE)
```
```  1520   apply (subgoal_tac "vector [v\$1] = v")
```
```  1521   apply simp
```
```  1522   apply (vector vector_def)
```
```  1523   apply (simp add: forall_1)
```
```  1524   done
```
```  1525
```
```  1526 lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
```
```  1527   apply auto
```
```  1528   apply (erule_tac x="v\$1" in allE)
```
```  1529   apply (erule_tac x="v\$2" in allE)
```
```  1530   apply (subgoal_tac "vector [v\$1, v\$2] = v")
```
```  1531   apply simp
```
```  1532   apply (vector vector_def)
```
```  1533   apply (simp add: forall_2)
```
```  1534   done
```
```  1535
```
```  1536 lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
```
```  1537   apply auto
```
```  1538   apply (erule_tac x="v\$1" in allE)
```
```  1539   apply (erule_tac x="v\$2" in allE)
```
```  1540   apply (erule_tac x="v\$3" in allE)
```
```  1541   apply (subgoal_tac "vector [v\$1, v\$2, v\$3] = v")
```
```  1542   apply simp
```
```  1543   apply (vector vector_def)
```
```  1544   apply (simp add: forall_3)
```
```  1545   done
```
```  1546
```
```  1547 subsection{* Linear functions. *}
```
```  1548
```
```  1549 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)"
```
```  1550
```
```  1551 lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. (c::'a::comm_semiring) *s f x)"
```
```  1552   by (vector linear_def Cart_eq ring_simps)
```
```  1553
```
```  1554 lemma linear_compose_neg: "linear (f :: 'a ^'n \<Rightarrow> 'a::comm_ring ^'m) ==> linear (\<lambda>x. -(f(x)))" by (vector linear_def Cart_eq)
```
```  1555
```
```  1556 lemma linear_compose_add: "linear (f :: 'a ^'n \<Rightarrow> 'a::semiring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
```
```  1557   by (vector linear_def Cart_eq ring_simps)
```
```  1558
```
```  1559 lemma linear_compose_sub: "linear (f :: 'a ^'n \<Rightarrow> 'a::ring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
```
```  1560   by (vector linear_def Cart_eq ring_simps)
```
```  1561
```
```  1562 lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
```
```  1563   by (simp add: linear_def)
```
```  1564
```
```  1565 lemma linear_id: "linear id" by (simp add: linear_def id_def)
```
```  1566
```
```  1567 lemma linear_zero: "linear (\<lambda>x. 0::'a::semiring_1 ^ 'n)" by (simp add: linear_def)
```
```  1568
```
```  1569 lemma linear_compose_setsum:
```
```  1570   assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a :: 'a::semiring_1 ^ 'n \<Rightarrow> 'a ^ 'm)"
```
```  1571   shows "linear(\<lambda>x. setsum (\<lambda>a. f a x :: 'a::semiring_1 ^'m) S)"
```
```  1572   using lS
```
```  1573   apply (induct rule: finite_induct[OF fS])
```
```  1574   by (auto simp add: linear_zero intro: linear_compose_add)
```
```  1575
```
```  1576 lemma linear_vmul_component:
```
```  1577   fixes f:: "'a::semiring_1^'m \<Rightarrow> 'a^'n"
```
```  1578   assumes lf: "linear f"
```
```  1579   shows "linear (\<lambda>x. f x \$ k *s v)"
```
```  1580   using lf
```
```  1581   apply (auto simp add: linear_def )
```
```  1582   by (vector ring_simps)+
```
```  1583
```
```  1584 lemma linear_0: "linear f ==> f 0 = (0::'a::semiring_1 ^'n)"
```
```  1585   unfolding linear_def
```
```  1586   apply clarsimp
```
```  1587   apply (erule allE[where x="0::'a"])
```
```  1588   apply simp
```
```  1589   done
```
```  1590
```
```  1591 lemma linear_cmul: "linear f ==> f(c*s x) = c *s f x" by (simp add: linear_def)
```
```  1592
```
```  1593 lemma linear_neg: "linear (f :: 'a::ring_1 ^'n \<Rightarrow> _) ==> f (-x) = - f x"
```
```  1594   unfolding vector_sneg_minus1
```
```  1595   using linear_cmul[of f] by auto
```
```  1596
```
```  1597 lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def)
```
```  1598
```
```  1599 lemma linear_sub: "linear (f::'a::ring_1 ^'n \<Rightarrow> _) ==> f(x - y) = f x - f y"
```
```  1600   by (simp add: diff_def linear_add linear_neg)
```
```  1601
```
```  1602 lemma linear_setsum:
```
```  1603   fixes f:: "'a::semiring_1^'n \<Rightarrow> _"
```
```  1604   assumes lf: "linear f" and fS: "finite S"
```
```  1605   shows "f (setsum g S) = setsum (f o g) S"
```
```  1606 proof (induct rule: finite_induct[OF fS])
```
```  1607   case 1 thus ?case by (simp add: linear_0[OF lf])
```
```  1608 next
```
```  1609   case (2 x F)
```
```  1610   have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
```
```  1611     by simp
```
```  1612   also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
```
```  1613   also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
```
```  1614   finally show ?case .
```
```  1615 qed
```
```  1616
```
```  1617 lemma linear_setsum_mul:
```
```  1618   fixes f:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m"
```
```  1619   assumes lf: "linear f" and fS: "finite S"
```
```  1620   shows "f (setsum (\<lambda>i. c i *s v i) S) = setsum (\<lambda>i. c i *s f (v i)) S"
```
```  1621   using linear_setsum[OF lf fS, of "\<lambda>i. c i *s v i" , unfolded o_def]
```
```  1622   linear_cmul[OF lf] by simp
```
```  1623
```
```  1624 lemma linear_injective_0:
```
```  1625   assumes lf: "linear (f:: 'a::ring_1 ^ 'n \<Rightarrow> _)"
```
```  1626   shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
```
```  1627 proof-
```
```  1628   have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
```
```  1629   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
```
```  1630   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
```
```  1631     by (simp add: linear_sub[OF lf])
```
```  1632   also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
```
```  1633   finally show ?thesis .
```
```  1634 qed
```
```  1635
```
```  1636 lemma linear_bounded:
```
```  1637   fixes f:: "real ^'m::finite \<Rightarrow> real ^'n::finite"
```
```  1638   assumes lf: "linear f"
```
```  1639   shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
```
```  1640 proof-
```
```  1641   let ?S = "UNIV:: 'm set"
```
```  1642   let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
```
```  1643   have fS: "finite ?S" by simp
```
```  1644   {fix x:: "real ^ 'm"
```
```  1645     let ?g = "(\<lambda>i. (x\$i) *s (basis i) :: real ^ 'm)"
```
```  1646     have "norm (f x) = norm (f (setsum (\<lambda>i. (x\$i) *s (basis i)) ?S))"
```
```  1647       by (simp only:  basis_expansion)
```
```  1648     also have "\<dots> = norm (setsum (\<lambda>i. (x\$i) *s f (basis i))?S)"
```
```  1649       using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf]
```
```  1650       by auto
```
```  1651     finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x\$i) *s f (basis i))?S)" .
```
```  1652     {fix i assume i: "i \<in> ?S"
```
```  1653       from component_le_norm[of x i]
```
```  1654       have "norm ((x\$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x"
```
```  1655       unfolding norm_mul
```
```  1656       apply (simp only: mult_commute)
```
```  1657       apply (rule mult_mono)
```
```  1658       by (auto simp add: ring_simps norm_ge_zero) }
```
```  1659     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
```
```  1660     from real_setsum_norm_le[OF fS, of "\<lambda>i. (x\$i) *s (f (basis i))", OF th]
```
```  1661     have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
```
```  1662   then show ?thesis by blast
```
```  1663 qed
```
```  1664
```
```  1665 lemma linear_bounded_pos:
```
```  1666   fixes f:: "real ^'n::finite \<Rightarrow> real ^ 'm::finite"
```
```  1667   assumes lf: "linear f"
```
```  1668   shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
```
```  1669 proof-
```
```  1670   from linear_bounded[OF lf] obtain B where
```
```  1671     B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
```
```  1672   let ?K = "\<bar>B\<bar> + 1"
```
```  1673   have Kp: "?K > 0" by arith
```
```  1674     {assume C: "B < 0"
```
```  1675       have "norm (1::real ^ 'n) > 0" by (simp add: zero_less_norm_iff)
```
```  1676       with C have "B * norm (1:: real ^ 'n) < 0"
```
```  1677 	by (simp add: zero_compare_simps)
```
```  1678       with B[rule_format, of 1] norm_ge_zero[of "f 1"] have False by simp
```
```  1679     }
```
```  1680     then have Bp: "B \<ge> 0" by ferrack
```
```  1681     {fix x::"real ^ 'n"
```
```  1682       have "norm (f x) \<le> ?K *  norm x"
```
```  1683       using B[rule_format, of x] norm_ge_zero[of x] norm_ge_zero[of "f x"] Bp
```
```  1684       apply (auto simp add: ring_simps split add: abs_split)
```
```  1685       apply (erule order_trans, simp)
```
```  1686       done
```
```  1687   }
```
```  1688   then show ?thesis using Kp by blast
```
```  1689 qed
```
```  1690
```
```  1691 subsection{* Bilinear functions. *}
```
```  1692
```
```  1693 definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
```
```  1694
```
```  1695 lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
```
```  1696   by (simp add: bilinear_def linear_def)
```
```  1697 lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
```
```  1698   by (simp add: bilinear_def linear_def)
```
```  1699
```
```  1700 lemma bilinear_lmul: "bilinear h ==> h (c *s x) y = c *s (h x y)"
```
```  1701   by (simp add: bilinear_def linear_def)
```
```  1702
```
```  1703 lemma bilinear_rmul: "bilinear h ==> h x (c *s y) = c *s (h x y)"
```
```  1704   by (simp add: bilinear_def linear_def)
```
```  1705
```
```  1706 lemma bilinear_lneg: "bilinear h ==> h (- (x:: 'a::ring_1 ^ 'n)) y = -(h x y)"
```
```  1707   by (simp only: vector_sneg_minus1 bilinear_lmul)
```
```  1708
```
```  1709 lemma bilinear_rneg: "bilinear h ==> h x (- (y:: 'a::ring_1 ^ 'n)) = - h x y"
```
```  1710   by (simp only: vector_sneg_minus1 bilinear_rmul)
```
```  1711
```
```  1712 lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
```
```  1713   using add_imp_eq[of x y 0] by auto
```
```  1714
```
```  1715 lemma bilinear_lzero:
```
```  1716   fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h 0 x = 0"
```
```  1717   using bilinear_ladd[OF bh, of 0 0 x]
```
```  1718     by (simp add: eq_add_iff ring_simps)
```
```  1719
```
```  1720 lemma bilinear_rzero:
```
```  1721   fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h x 0 = 0"
```
```  1722   using bilinear_radd[OF bh, of x 0 0 ]
```
```  1723     by (simp add: eq_add_iff ring_simps)
```
```  1724
```
```  1725 lemma bilinear_lsub: "bilinear h ==> h (x - (y:: 'a::ring_1 ^ 'n)) z = h x z - h y z"
```
```  1726   by (simp  add: diff_def bilinear_ladd bilinear_lneg)
```
```  1727
```
```  1728 lemma bilinear_rsub: "bilinear h ==> h z (x - (y:: 'a::ring_1 ^ 'n)) = h z x - h z y"
```
```  1729   by (simp  add: diff_def bilinear_radd bilinear_rneg)
```
```  1730
```
```  1731 lemma bilinear_setsum:
```
```  1732   fixes h:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m \<Rightarrow> 'a ^ 'k"
```
```  1733   assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
```
```  1734   shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
```
```  1735 proof-
```
```  1736   have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
```
```  1737     apply (rule linear_setsum[unfolded o_def])
```
```  1738     using bh fS by (auto simp add: bilinear_def)
```
```  1739   also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
```
```  1740     apply (rule setsum_cong, simp)
```
```  1741     apply (rule linear_setsum[unfolded o_def])
```
```  1742     using bh fT by (auto simp add: bilinear_def)
```
```  1743   finally show ?thesis unfolding setsum_cartesian_product .
```
```  1744 qed
```
```  1745
```
```  1746 lemma bilinear_bounded:
```
```  1747   fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
```
```  1748   assumes bh: "bilinear h"
```
```  1749   shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
```
```  1750 proof-
```
```  1751   let ?M = "UNIV :: 'm set"
```
```  1752   let ?N = "UNIV :: 'n set"
```
```  1753   let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
```
```  1754   have fM: "finite ?M" and fN: "finite ?N" by simp_all
```
```  1755   {fix x:: "real ^ 'm" and  y :: "real^'n"
```
```  1756     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 ..
```
```  1757     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] ..
```
```  1758     finally have th: "norm (h x y) = \<dots>" .
```
```  1759     have "norm (h x y) \<le> ?B * norm x * norm y"
```
```  1760       apply (simp add: setsum_left_distrib th)
```
```  1761       apply (rule real_setsum_norm_le)
```
```  1762       using fN fM
```
```  1763       apply simp
```
```  1764       apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] norm_mul ring_simps)
```
```  1765       apply (rule mult_mono)
```
```  1766       apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
```
```  1767       apply (rule mult_mono)
```
```  1768       apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
```
```  1769       done}
```
```  1770   then show ?thesis by metis
```
```  1771 qed
```
```  1772
```
```  1773 lemma bilinear_bounded_pos:
```
```  1774   fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
```
```  1775   assumes bh: "bilinear h"
```
```  1776   shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
```
```  1777 proof-
```
```  1778   from bilinear_bounded[OF bh] obtain B where
```
```  1779     B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
```
```  1780   let ?K = "\<bar>B\<bar> + 1"
```
```  1781   have Kp: "?K > 0" by arith
```
```  1782   have KB: "B < ?K" by arith
```
```  1783   {fix x::"real ^'m" and y :: "real ^'n"
```
```  1784     from KB Kp
```
```  1785     have "B * norm x * norm y \<le> ?K * norm x * norm y"
```
```  1786       apply -
```
```  1787       apply (rule mult_right_mono, rule mult_right_mono)
```
```  1788       by (auto simp add: norm_ge_zero)
```
```  1789     then have "norm (h x y) \<le> ?K * norm x * norm y"
```
```  1790       using B[rule_format, of x y] by simp}
```
```  1791   with Kp show ?thesis by blast
```
```  1792 qed
```
```  1793
```
```  1794 subsection{* Adjoints. *}
```
```  1795
```
```  1796 definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
```
```  1797
```
```  1798 lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
```
```  1799
```
```  1800 lemma adjoint_works_lemma:
```
```  1801   fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1802   assumes lf: "linear f"
```
```  1803   shows "\<forall>x y. f x \<bullet> y = x \<bullet> adjoint f y"
```
```  1804 proof-
```
```  1805   let ?N = "UNIV :: 'n set"
```
```  1806   let ?M = "UNIV :: 'm set"
```
```  1807   have fN: "finite ?N" by simp
```
```  1808   have fM: "finite ?M" by simp
```
```  1809   {fix y:: "'a ^ 'm"
```
```  1810     let ?w = "(\<chi> i. (f (basis i) \<bullet> y)) :: 'a ^ 'n"
```
```  1811     {fix x
```
```  1812       have "f x \<bullet> y = f (setsum (\<lambda>i. (x\$i) *s basis i) ?N) \<bullet> y"
```
```  1813 	by (simp only: basis_expansion)
```
```  1814       also have "\<dots> = (setsum (\<lambda>i. (x\$i) *s f (basis i)) ?N) \<bullet> y"
```
```  1815 	unfolding linear_setsum[OF lf fN]
```
```  1816 	by (simp add: linear_cmul[OF lf])
```
```  1817       finally have "f x \<bullet> y = x \<bullet> ?w"
```
```  1818 	apply (simp only: )
```
```  1819 	apply (simp add: dot_def setsum_left_distrib setsum_right_distrib setsum_commute[of _ ?M ?N] ring_simps)
```
```  1820 	done}
```
```  1821   }
```
```  1822   then show ?thesis unfolding adjoint_def
```
```  1823     some_eq_ex[of "\<lambda>f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y"]
```
```  1824     using choice_iff[of "\<lambda>a b. \<forall>x. f x \<bullet> a = x \<bullet> b "]
```
```  1825     by metis
```
```  1826 qed
```
```  1827
```
```  1828 lemma adjoint_works:
```
```  1829   fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1830   assumes lf: "linear f"
```
```  1831   shows "x \<bullet> adjoint f y = f x \<bullet> y"
```
```  1832   using adjoint_works_lemma[OF lf] by metis
```
```  1833
```
```  1834
```
```  1835 lemma adjoint_linear:
```
```  1836   fixes f :: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1837   assumes lf: "linear f"
```
```  1838   shows "linear (adjoint f)"
```
```  1839   by (simp add: linear_def vector_eq_ldot[symmetric] dot_radd dot_rmult adjoint_works[OF lf])
```
```  1840
```
```  1841 lemma adjoint_clauses:
```
```  1842   fixes f:: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1843   assumes lf: "linear f"
```
```  1844   shows "x \<bullet> adjoint f y = f x \<bullet> y"
```
```  1845   and "adjoint f y \<bullet> x = y \<bullet> f x"
```
```  1846   by (simp_all add: adjoint_works[OF lf] dot_sym )
```
```  1847
```
```  1848 lemma adjoint_adjoint:
```
```  1849   fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1850   assumes lf: "linear f"
```
```  1851   shows "adjoint (adjoint f) = f"
```
```  1852   apply (rule ext)
```
```  1853   by (simp add: vector_eq_ldot[symmetric] adjoint_clauses[OF adjoint_linear[OF lf]] adjoint_clauses[OF lf])
```
```  1854
```
```  1855 lemma adjoint_unique:
```
```  1856   fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
```
```  1857   assumes lf: "linear f" and u: "\<forall>x y. f' x \<bullet> y = x \<bullet> f y"
```
```  1858   shows "f' = adjoint f"
```
```  1859   apply (rule ext)
```
```  1860   using u
```
```  1861   by (simp add: vector_eq_rdot[symmetric] adjoint_clauses[OF lf])
```
```  1862
```
```  1863 text{* Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"} *}
```
```  1864
```
```  1865 consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
```
```  1866
```
```  1867 defs (overloaded)
```
```  1868 matrix_matrix_mult_def: "(m:: ('a::semiring_1) ^'n^'m) \<star> (m' :: 'a ^'p^'n) \<equiv> (\<chi> i j. setsum (\<lambda>k. ((m\$i)\$k) * ((m'\$k)\$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
```
```  1869
```
```  1870 abbreviation
```
```  1871   matrix_matrix_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"  (infixl "**" 70)
```
```  1872   where "m ** m' == m\<star> m'"
```
```  1873
```
```  1874 defs (overloaded)
```
```  1875   matrix_vector_mult_def: "(m::('a::semiring_1) ^'n^'m) \<star> (x::'a ^'n) \<equiv> (\<chi> i. setsum (\<lambda>j. ((m\$i)\$j) * (x\$j)) (UNIV ::'n set)) :: 'a^'m"
```
```  1876
```
```  1877 abbreviation
```
```  1878   matrix_vector_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"  (infixl "*v" 70)
```
```  1879   where
```
```  1880   "m *v v == m \<star> v"
```
```  1881
```
```  1882 defs (overloaded)
```
```  1883   vector_matrix_mult_def: "(x::'a^'m) \<star> (m::('a::semiring_1) ^'n^'m) \<equiv> (\<chi> j. setsum (\<lambda>i. ((m\$i)\$j) * (x\$i)) (UNIV :: 'm set)) :: 'a^'n"
```
```  1884
```
```  1885 abbreviation
```
```  1886   vactor_matrix_mult' :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "  (infixl "v*" 70)
```
```  1887   where
```
```  1888   "v v* m == v \<star> m"
```
```  1889
```
```  1890 definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
```
```  1891 definition "(transp::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A\$j)\$i))"
```
```  1892 definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A\$i)\$j))"
```
```  1893 definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A\$i)\$j))"
```
```  1894 definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
```
```  1895 definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
```
```  1896
```
```  1897 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
```
```  1898 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A \<star> B) + (A \<star> C)"
```
```  1899   by (vector matrix_matrix_mult_def setsum_addf[symmetric] ring_simps)
```
```  1900
```
```  1901 lemma setsum_delta':
```
```  1902   assumes fS: "finite S" shows
```
```  1903   "setsum (\<lambda>k. if a = k then b k else 0) S =
```
```  1904      (if a\<in> S then b a else 0)"
```
```  1905   using setsum_delta[OF fS, of a b, symmetric]
```
```  1906   by (auto intro: setsum_cong)
```
```  1907
```
```  1908 lemma matrix_mul_lid:
```
```  1909   fixes A :: "'a::semiring_1 ^ 'm ^ 'n::finite"
```
```  1910   shows "mat 1 ** A = A"
```
```  1911   apply (simp add: matrix_matrix_mult_def mat_def)
```
```  1912   apply vector
```
```  1913   by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite]  mult_1_left mult_zero_left if_True UNIV_I)
```
```  1914
```
```  1915
```
```  1916 lemma matrix_mul_rid:
```
```  1917   fixes A :: "'a::semiring_1 ^ 'm::finite ^ 'n"
```
```  1918   shows "A ** mat 1 = A"
```
```  1919   apply (simp add: matrix_matrix_mult_def mat_def)
```
```  1920   apply vector
```
```  1921   by (auto simp only: cond_value_iff cond_application_beta setsum_delta[OF finite]  mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
```
```  1922
```
```  1923 lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
```
```  1924   apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
```
```  1925   apply (subst setsum_commute)
```
```  1926   apply simp
```
```  1927   done
```
```  1928
```
```  1929 lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
```
```  1930   apply (vector matrix_matrix_mult_def matrix_vector_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
```
```  1931   apply (subst setsum_commute)
```
```  1932   apply simp
```
```  1933   done
```
```  1934
```
```  1935 lemma matrix_vector_mul_lid: "mat 1 *v x = (x::'a::semiring_1 ^ 'n::finite)"
```
```  1936   apply (vector matrix_vector_mult_def mat_def)
```
```  1937   by (simp add: cond_value_iff cond_application_beta
```
```  1938     setsum_delta' cong del: if_weak_cong)
```
```  1939
```
```  1940 lemma matrix_transp_mul: "transp(A ** B) = transp B ** transp (A::'a::comm_semiring_1^'m^'n)"
```
```  1941   by (simp add: matrix_matrix_mult_def transp_def Cart_eq mult_commute)
```
```  1942
```
```  1943 lemma matrix_eq:
```
```  1944   fixes A B :: "'a::semiring_1 ^ 'n::finite ^ 'm"
```
```  1945   shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  1946   apply auto
```
```  1947   apply (subst Cart_eq)
```
```  1948   apply clarify
```
```  1949   apply (clarsimp simp add: matrix_vector_mult_def basis_def cond_value_iff cond_application_beta Cart_eq cong del: if_weak_cong)
```
```  1950   apply (erule_tac x="basis ia" in allE)
```
```  1951   apply (erule_tac x="i" in allE)
```
```  1952   by (auto simp add: basis_def cond_value_iff cond_application_beta setsum_delta[OF finite] cong del: if_weak_cong)
```
```  1953
```
```  1954 lemma matrix_vector_mul_component:
```
```  1955   shows "((A::'a::semiring_1^'n'^'m) *v x)\$k = (A\$k) \<bullet> x"
```
```  1956   by (simp add: matrix_vector_mult_def dot_def)
```
```  1957
```
```  1958 lemma dot_lmul_matrix: "((x::'a::comm_semiring_1 ^'n) v* A) \<bullet> y = x \<bullet> (A *v y)"
```
```  1959   apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib mult_ac)
```
```  1960   apply (subst setsum_commute)
```
```  1961   by simp
```
```  1962
```
```  1963 lemma transp_mat: "transp (mat n) = mat n"
```
```  1964   by (vector transp_def mat_def)
```
```  1965
```
```  1966 lemma transp_transp: "transp(transp A) = A"
```
```  1967   by (vector transp_def)
```
```  1968
```
```  1969 lemma row_transp:
```
```  1970   fixes A:: "'a::semiring_1^'n^'m"
```
```  1971   shows "row i (transp A) = column i A"
```
```  1972   by (simp add: row_def column_def transp_def Cart_eq)
```
```  1973
```
```  1974 lemma column_transp:
```
```  1975   fixes A:: "'a::semiring_1^'n^'m"
```
```  1976   shows "column i (transp A) = row i A"
```
```  1977   by (simp add: row_def column_def transp_def Cart_eq)
```
```  1978
```
```  1979 lemma rows_transp: "rows(transp (A::'a::semiring_1^'n^'m)) = columns A"
```
```  1980 by (auto simp add: rows_def columns_def row_transp intro: set_ext)
```
```  1981
```
```  1982 lemma columns_transp: "columns(transp (A::'a::semiring_1^'n^'m)) = rows A" by (metis transp_transp rows_transp)
```
```  1983
```
```  1984 text{* Two sometimes fruitful ways of looking at matrix-vector multiplication. *}
```
```  1985
```
```  1986 lemma matrix_mult_dot: "A *v x = (\<chi> i. A\$i \<bullet> x)"
```
```  1987   by (simp add: matrix_vector_mult_def dot_def)
```
```  1988
```
```  1989 lemma matrix_mult_vsum: "(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x\$i) *s column i A) (UNIV:: 'n set)"
```
```  1990   by (simp add: matrix_vector_mult_def Cart_eq column_def mult_commute)
```
```  1991
```
```  1992 lemma vector_componentwise:
```
```  1993   "(x::'a::ring_1^'n::finite) = (\<chi> j. setsum (\<lambda>i. (x\$i) * (basis i :: 'a^'n)\$j) (UNIV :: 'n set))"
```
```  1994   apply (subst basis_expansion[symmetric])
```
```  1995   by (vector Cart_eq setsum_component)
```
```  1996
```
```  1997 lemma linear_componentwise:
```
```  1998   fixes f:: "'a::ring_1 ^ 'm::finite \<Rightarrow> 'a ^ 'n"
```
```  1999   assumes lf: "linear f"
```
```  2000   shows "(f x)\$j = setsum (\<lambda>i. (x\$i) * (f (basis i)\$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
```
```  2001 proof-
```
```  2002   let ?M = "(UNIV :: 'm set)"
```
```  2003   let ?N = "(UNIV :: 'n set)"
```
```  2004   have fM: "finite ?M" by simp
```
```  2005   have "?rhs = (setsum (\<lambda>i.(x\$i) *s f (basis i) ) ?M)\$j"
```
```  2006     unfolding vector_smult_component[symmetric]
```
```  2007     unfolding setsum_component[of "(\<lambda>i.(x\$i) *s f (basis i :: 'a^'m))" ?M]
```
```  2008     ..
```
```  2009   then show ?thesis unfolding linear_setsum_mul[OF lf fM, symmetric] basis_expansion ..
```
```  2010 qed
```
```  2011
```
```  2012 text{* Inverse matrices  (not necessarily square) *}
```
```  2013
```
```  2014 definition "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
```
```  2015
```
```  2016 definition "matrix_inv(A:: 'a::semiring_1^'n^'m) =
```
```  2017         (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
```
```  2018
```
```  2019 text{* Correspondence between matrices and linear operators. *}
```
```  2020
```
```  2021 definition matrix:: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
```
```  2022 where "matrix f = (\<chi> i j. (f(basis j))\$i)"
```
```  2023
```
```  2024 lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::'a::comm_semiring_1 ^ 'n))"
```
```  2025   by (simp add: linear_def matrix_vector_mult_def Cart_eq ring_simps setsum_right_distrib setsum_addf)
```
```  2026
```
```  2027 lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n::finite)"
```
```  2028 apply (simp add: matrix_def matrix_vector_mult_def Cart_eq mult_commute)
```
```  2029 apply clarify
```
```  2030 apply (rule linear_componentwise[OF lf, symmetric])
```
```  2031 done
```
```  2032
```
```  2033 lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n::finite))" by (simp add: ext matrix_works)
```
```  2034
```
```  2035 lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n::finite)) = A"
```
```  2036   by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
```
```  2037
```
```  2038 lemma matrix_compose:
```
```  2039   assumes lf: "linear (f::'a::comm_ring_1^'n::finite \<Rightarrow> 'a^'m::finite)"
```
```  2040   and lg: "linear (g::'a::comm_ring_1^'m::finite \<Rightarrow> 'a^'k)"
```
```  2041   shows "matrix (g o f) = matrix g ** matrix f"
```
```  2042   using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
```
```  2043   by (simp  add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
```
```  2044
```
```  2045 lemma matrix_vector_column:"(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x\$i) *s ((transp A)\$i)) (UNIV:: 'n set)"
```
```  2046   by (simp add: matrix_vector_mult_def transp_def Cart_eq mult_commute)
```
```  2047
```
```  2048 lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n::finite^'m::finite) *v x) = (\<lambda>x. transp A *v x)"
```
```  2049   apply (rule adjoint_unique[symmetric])
```
```  2050   apply (rule matrix_vector_mul_linear)
```
```  2051   apply (simp add: transp_def dot_def matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
```
```  2052   apply (subst setsum_commute)
```
```  2053   apply (auto simp add: mult_ac)
```
```  2054   done
```
```  2055
```
```  2056 lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n::finite \<Rightarrow> 'a ^ 'm::finite)"
```
```  2057   shows "matrix(adjoint f) = transp(matrix f)"
```
```  2058   apply (subst matrix_vector_mul[OF lf])
```
```  2059   unfolding adjoint_matrix matrix_of_matrix_vector_mul ..
```
```  2060
```
```  2061 subsection{* Interlude: Some properties of real sets *}
```
```  2062
```
```  2063 lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
```
```  2064   shows "\<forall>n \<ge> m. d n < e m"
```
```  2065   using prems apply auto
```
```  2066   apply (erule_tac x="n" in allE)
```
```  2067   apply (erule_tac x="n" in allE)
```
```  2068   apply auto
```
```  2069   done
```
```  2070
```
```  2071
```
```  2072 lemma real_convex_bound_lt:
```
```  2073   assumes xa: "(x::real) < a" and ya: "y < a" and u: "0 <= u" and v: "0 <= v"
```
```  2074   and uv: "u + v = 1"
```
```  2075   shows "u * x + v * y < a"
```
```  2076 proof-
```
```  2077   have uv': "u = 0 \<longrightarrow> v \<noteq> 0" using u v uv by arith
```
```  2078   have "a = a * (u + v)" unfolding uv  by simp
```
```  2079   hence th: "u * a + v * a = a" by (simp add: ring_simps)
```
```  2080   from xa u have "u \<noteq> 0 \<Longrightarrow> u*x < u*a" by (simp add: mult_compare_simps)
```
```  2081   from ya v have "v \<noteq> 0 \<Longrightarrow> v * y < v * a" by (simp add: mult_compare_simps)
```
```  2082   from xa ya u v have "u * x + v * y < u * a + v * a"
```
```  2083     apply (cases "u = 0", simp_all add: uv')
```
```  2084     apply(rule mult_strict_left_mono)
```
```  2085     using uv' apply simp_all
```
```  2086
```
```  2087     apply (rule add_less_le_mono)
```
```  2088     apply(rule mult_strict_left_mono)
```
```  2089     apply simp_all
```
```  2090     apply (rule mult_left_mono)
```
```  2091     apply simp_all
```
```  2092     done
```
```  2093   thus ?thesis unfolding th .
```
```  2094 qed
```
```  2095
```
```  2096 lemma real_convex_bound_le:
```
```  2097   assumes xa: "(x::real) \<le> a" and ya: "y \<le> a" and u: "0 <= u" and v: "0 <= v"
```
```  2098   and uv: "u + v = 1"
```
```  2099   shows "u * x + v * y \<le> a"
```
```  2100 proof-
```
```  2101   from xa ya u v have "u * x + v * y \<le> u * a + v * a" by (simp add: add_mono mult_left_mono)
```
```  2102   also have "\<dots> \<le> (u + v) * a" by (simp add: ring_simps)
```
```  2103   finally show ?thesis unfolding uv by simp
```
```  2104 qed
```
```  2105
```
```  2106 lemma infinite_enumerate: assumes fS: "infinite S"
```
```  2107   shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
```
```  2108 unfolding subseq_def
```
```  2109 using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
```
```  2110
```
```  2111 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)"
```
```  2112 apply auto
```
```  2113 apply (rule_tac x="d/2" in exI)
```
```  2114 apply auto
```
```  2115 done
```
```  2116
```
```  2117
```
```  2118 lemma triangle_lemma:
```
```  2119   assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
```
```  2120   shows "x <= y + z"
```
```  2121 proof-
```
```  2122   have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y  by (simp add: zero_compare_simps)
```
```  2123   with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square ring_simps)
```
```  2124   from y z have yz: "y + z \<ge> 0" by arith
```
```  2125   from power2_le_imp_le[OF th yz] show ?thesis .
```
```  2126 qed
```
```  2127
```
```  2128
```
```  2129 lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
```
```  2130    (\<exists>x::'a ^ 'n. \<forall>i. P i (x\$i))" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  2131 proof-
```
```  2132   let ?S = "(UNIV :: 'n set)"
```
```  2133   {assume H: "?rhs"
```
```  2134     then have ?lhs by auto}
```
```  2135   moreover
```
```  2136   {assume H: "?lhs"
```
```  2137     then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
```
```  2138     let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
```
```  2139     {fix i
```
```  2140       from f have "P i (f i)" by metis
```
```  2141       then have "P i (?x\$i)" by auto
```
```  2142     }
```
```  2143     hence "\<forall>i. P i (?x\$i)" by metis
```
```  2144     hence ?rhs by metis }
```
```  2145   ultimately show ?thesis by metis
```
```  2146 qed
```
```  2147
```
```  2148 (* Supremum and infimum of real sets *)
```
```  2149
```
```  2150
```
```  2151 definition rsup:: "real set \<Rightarrow> real" where
```
```  2152   "rsup S = (SOME a. isLub UNIV S a)"
```
```  2153
```
```  2154 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)
```
```  2155
```
```  2156 lemma rsup: assumes Se: "S \<noteq> {}" and b: "\<exists>b. S *<= b"
```
```  2157   shows "isLub UNIV S (rsup S)"
```
```  2158 using Se b
```
```  2159 unfolding rsup_def
```
```  2160 apply clarify
```
```  2161 apply (rule someI_ex)
```
```  2162 apply (rule reals_complete)
```
```  2163 by (auto simp add: isUb_def setle_def)
```
```  2164
```
```  2165 lemma rsup_le: assumes Se: "S \<noteq> {}" and Sb: "S *<= b" shows "rsup S \<le> b"
```
```  2166 proof-
```
```  2167   from Sb have bu: "isUb UNIV S b" by (simp add: isUb_def setle_def)
```
```  2168   from rsup[OF Se] Sb have "isLub UNIV S (rsup S)"  by blast
```
```  2169   then show ?thesis using bu by (auto simp add: isLub_def leastP_def setle_def setge_def)
```
```  2170 qed
```
```  2171
```
```  2172 lemma rsup_finite_Max: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2173   shows "rsup S = Max S"
```
```  2174 using fS Se
```
```  2175 proof-
```
```  2176   let ?m = "Max S"
```
```  2177   from Max_ge[OF fS] have Sm: "\<forall> x\<in> S. x \<le> ?m" by metis
```
```  2178   with rsup[OF Se] have lub: "isLub UNIV S (rsup S)" by (metis setle_def)
```
```  2179   from Max_in[OF fS Se] lub have mrS: "?m \<le> rsup S"
```
```  2180     by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def)
```
```  2181   moreover
```
```  2182   have "rsup S \<le> ?m" using Sm lub
```
```  2183     by (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
```
```  2184   ultimately  show ?thesis by arith
```
```  2185 qed
```
```  2186
```
```  2187 lemma rsup_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2188   shows "rsup S \<in> S"
```
```  2189   using rsup_finite_Max[OF fS Se] Max_in[OF fS Se] by metis
```
```  2190
```
```  2191 lemma rsup_finite_Ub: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2192   shows "isUb S S (rsup S)"
```
```  2193   using rsup_finite_Max[OF fS Se] rsup_finite_in[OF fS Se] Max_ge[OF fS]
```
```  2194   unfolding isUb_def setle_def by metis
```
```  2195
```
```  2196 lemma rsup_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2197   shows "a \<le> rsup S \<longleftrightarrow> (\<exists> x \<in> S. a \<le> x)"
```
```  2198 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
```
```  2199
```
```  2200 lemma rsup_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2201   shows "a \<ge> rsup S \<longleftrightarrow> (\<forall> x \<in> S. a \<ge> x)"
```
```  2202 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
```
```  2203
```
```  2204 lemma rsup_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2205   shows "a < rsup S \<longleftrightarrow> (\<exists> x \<in> S. a < x)"
```
```  2206 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
```
```  2207
```
```  2208 lemma rsup_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2209   shows "a > rsup S \<longleftrightarrow> (\<forall> x \<in> S. a > x)"
```
```  2210 using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
```
```  2211
```
```  2212 lemma rsup_unique: assumes b: "S *<= b" and S: "\<forall>b' < b. \<exists>x \<in> S. b' < x"
```
```  2213   shows "rsup S = b"
```
```  2214 using b S
```
```  2215 unfolding setle_def rsup_alt
```
```  2216 apply -
```
```  2217 apply (rule some_equality)
```
```  2218 apply (metis  linorder_not_le order_eq_iff[symmetric])+
```
```  2219 done
```
```  2220
```
```  2221 lemma rsup_le_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. T *<= b) \<Longrightarrow> rsup S \<le> rsup T"
```
```  2222   apply (rule rsup_le)
```
```  2223   apply simp
```
```  2224   using rsup[of T] by (auto simp add: isLub_def leastP_def setge_def setle_def isUb_def)
```
```  2225
```
```  2226 lemma isUb_def': "isUb R S = (\<lambda>x. S *<= x \<and> x \<in> R)"
```
```  2227   apply (rule ext)
```
```  2228   by (metis isUb_def)
```
```  2229
```
```  2230 lemma UNIV_trivial: "UNIV x" using UNIV_I[of x] by (metis mem_def)
```
```  2231 lemma rsup_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
```
```  2232   shows "a \<le> rsup S \<and> rsup S \<le> b"
```
```  2233 proof-
```
```  2234   from rsup[OF Se] u have lub: "isLub UNIV S (rsup S)" by blast
```
```  2235   hence b: "rsup S \<le> b" using u by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
```
```  2236   from Se obtain y where y: "y \<in> S" by blast
```
```  2237   from lub l have "a \<le> rsup S" apply (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
```
```  2238     apply (erule ballE[where x=y])
```
```  2239     apply (erule ballE[where x=y])
```
```  2240     apply arith
```
```  2241     using y apply auto
```
```  2242     done
```
```  2243   with b show ?thesis by blast
```
```  2244 qed
```
```  2245
```
```  2246 lemma rsup_abs_le: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rsup S\<bar> \<le> a"
```
```  2247   unfolding abs_le_interval_iff  using rsup_bounds[of S "-a" a]
```
```  2248   by (auto simp add: setge_def setle_def)
```
```  2249
```
```  2250 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"
```
```  2251 proof-
```
```  2252   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
```
```  2253   show ?thesis using S b rsup_bounds[of S "l - e" "l+e"] unfolding th
```
```  2254     by  (auto simp add: setge_def setle_def)
```
```  2255 qed
```
```  2256
```
```  2257 definition rinf:: "real set \<Rightarrow> real" where
```
```  2258   "rinf S = (SOME a. isGlb UNIV S a)"
```
```  2259
```
```  2260 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)
```
```  2261
```
```  2262 lemma reals_complete_Glb: assumes Se: "\<exists>x. x \<in> S" and lb: "\<exists> y. isLb UNIV S y"
```
```  2263   shows "\<exists>(t::real). isGlb UNIV S t"
```
```  2264 proof-
```
```  2265   let ?M = "uminus ` S"
```
```  2266   from lb have th: "\<exists>y. isUb UNIV ?M y" apply (auto simp add: isUb_def isLb_def setle_def setge_def)
```
```  2267     by (rule_tac x="-y" in exI, auto)
```
```  2268   from Se have Me: "\<exists>x. x \<in> ?M" by blast
```
```  2269   from reals_complete[OF Me th] obtain t where t: "isLub UNIV ?M t" by blast
```
```  2270   have "isGlb UNIV S (- t)" using t
```
```  2271     apply (auto simp add: isLub_def isGlb_def leastP_def greatestP_def setle_def setge_def isUb_def isLb_def)
```
```  2272     apply (erule_tac x="-y" in allE)
```
```  2273     apply auto
```
```  2274     done
```
```  2275   then show ?thesis by metis
```
```  2276 qed
```
```  2277
```
```  2278 lemma rinf: assumes Se: "S \<noteq> {}" and b: "\<exists>b. b <=* S"
```
```  2279   shows "isGlb UNIV S (rinf S)"
```
```  2280 using Se b
```
```  2281 unfolding rinf_def
```
```  2282 apply clarify
```
```  2283 apply (rule someI_ex)
```
```  2284 apply (rule reals_complete_Glb)
```
```  2285 apply (auto simp add: isLb_def setle_def setge_def)
```
```  2286 done
```
```  2287
```
```  2288 lemma rinf_ge: assumes Se: "S \<noteq> {}" and Sb: "b <=* S" shows "rinf S \<ge> b"
```
```  2289 proof-
```
```  2290   from Sb have bu: "isLb UNIV S b" by (simp add: isLb_def setge_def)
```
```  2291   from rinf[OF Se] Sb have "isGlb UNIV S (rinf S)"  by blast
```
```  2292   then show ?thesis using bu by (auto simp add: isGlb_def greatestP_def setle_def setge_def)
```
```  2293 qed
```
```  2294
```
```  2295 lemma rinf_finite_Min: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2296   shows "rinf S = Min S"
```
```  2297 using fS Se
```
```  2298 proof-
```
```  2299   let ?m = "Min S"
```
```  2300   from Min_le[OF fS] have Sm: "\<forall> x\<in> S. x \<ge> ?m" by metis
```
```  2301   with rinf[OF Se] have glb: "isGlb UNIV S (rinf S)" by (metis setge_def)
```
```  2302   from Min_in[OF fS Se] glb have mrS: "?m \<ge> rinf S"
```
```  2303     by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def)
```
```  2304   moreover
```
```  2305   have "rinf S \<ge> ?m" using Sm glb
```
```  2306     by (auto simp add: isGlb_def greatestP_def isLb_def setle_def setge_def)
```
```  2307   ultimately  show ?thesis by arith
```
```  2308 qed
```
```  2309
```
```  2310 lemma rinf_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2311   shows "rinf S \<in> S"
```
```  2312   using rinf_finite_Min[OF fS Se] Min_in[OF fS Se] by metis
```
```  2313
```
```  2314 lemma rinf_finite_Lb: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2315   shows "isLb S S (rinf S)"
```
```  2316   using rinf_finite_Min[OF fS Se] rinf_finite_in[OF fS Se] Min_le[OF fS]
```
```  2317   unfolding isLb_def setge_def by metis
```
```  2318
```
```  2319 lemma rinf_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2320   shows "a \<le> rinf S \<longleftrightarrow> (\<forall> x \<in> S. a \<le> x)"
```
```  2321 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
```
```  2322
```
```  2323 lemma rinf_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2324   shows "a \<ge> rinf S \<longleftrightarrow> (\<exists> x \<in> S. a \<ge> x)"
```
```  2325 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
```
```  2326
```
```  2327 lemma rinf_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2328   shows "a < rinf S \<longleftrightarrow> (\<forall> x \<in> S. a < x)"
```
```  2329 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
```
```  2330
```
```  2331 lemma rinf_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
```
```  2332   shows "a > rinf S \<longleftrightarrow> (\<exists> x \<in> S. a > x)"
```
```  2333 using rinf_finite_Lb[OF fS Se] by (auto simp add: isLb_def setge_def)
```
```  2334
```
```  2335 lemma rinf_unique: assumes b: "b <=* S" and S: "\<forall>b' > b. \<exists>x \<in> S. b' > x"
```
```  2336   shows "rinf S = b"
```
```  2337 using b S
```
```  2338 unfolding setge_def rinf_alt
```
```  2339 apply -
```
```  2340 apply (rule some_equality)
```
```  2341 apply (metis  linorder_not_le order_eq_iff[symmetric])+
```
```  2342 done
```
```  2343
```
```  2344 lemma rinf_ge_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. b <=* T) \<Longrightarrow> rinf S >= rinf T"
```
```  2345   apply (rule rinf_ge)
```
```  2346   apply simp
```
```  2347   using rinf[of T] by (auto simp add: isGlb_def greatestP_def setge_def setle_def isLb_def)
```
```  2348
```
```  2349 lemma isLb_def': "isLb R S = (\<lambda>x. x <=* S \<and> x \<in> R)"
```
```  2350   apply (rule ext)
```
```  2351   by (metis isLb_def)
```
```  2352
```
```  2353 lemma rinf_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
```
```  2354   shows "a \<le> rinf S \<and> rinf S \<le> b"
```
```  2355 proof-
```
```  2356   from rinf[OF Se] l have lub: "isGlb UNIV S (rinf S)" by blast
```
```  2357   hence b: "a \<le> rinf S" using l by (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
```
```  2358   from Se obtain y where y: "y \<in> S" by blast
```
```  2359   from lub u have "b \<ge> rinf S" apply (auto simp add: isGlb_def greatestP_def setle_def setge_def isLb_def')
```
```  2360     apply (erule ballE[where x=y])
```
```  2361     apply (erule ballE[where x=y])
```
```  2362     apply arith
```
```  2363     using y apply auto
```
```  2364     done
```
```  2365   with b show ?thesis by blast
```
```  2366 qed
```
```  2367
```
```  2368 lemma rinf_abs_ge: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rinf S\<bar> \<le> a"
```
```  2369   unfolding abs_le_interval_iff  using rinf_bounds[of S "-a" a]
```
```  2370   by (auto simp add: setge_def setle_def)
```
```  2371
```
```  2372 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"
```
```  2373 proof-
```
```  2374   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
```
```  2375   show ?thesis using S b rinf_bounds[of S "l - e" "l+e"] unfolding th
```
```  2376     by  (auto simp add: setge_def setle_def)
```
```  2377 qed
```
```  2378
```
```  2379
```
```  2380
```
```  2381 subsection{* Operator norm. *}
```
```  2382
```
```  2383 definition "onorm f = rsup {norm (f x)| x. norm x = 1}"
```
```  2384
```
```  2385 lemma norm_bound_generalize:
```
```  2386   fixes f:: "real ^'n::finite \<Rightarrow> real^'m::finite"
```
```  2387   assumes lf: "linear f"
```
```  2388   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")
```
```  2389 proof-
```
```  2390   {assume H: ?rhs
```
```  2391     {fix x :: "real^'n" assume x: "norm x = 1"
```
```  2392       from H[rule_format, of x] x have "norm (f x) \<le> b" by simp}
```
```  2393     then have ?lhs by blast }
```
```  2394
```
```  2395   moreover
```
```  2396   {assume H: ?lhs
```
```  2397     from H[rule_format, of "basis arbitrary"]
```
```  2398     have bp: "b \<ge> 0" using norm_ge_zero[of "f (basis arbitrary)"]
```
```  2399       by (auto simp add: norm_basis elim: order_trans [OF norm_ge_zero])
```
```  2400     {fix x :: "real ^'n"
```
```  2401       {assume "x = 0"
```
```  2402 	then have "norm (f x) \<le> b * norm x" by (simp add: linear_0[OF lf] bp)}
```
```  2403       moreover
```
```  2404       {assume x0: "x \<noteq> 0"
```
```  2405 	hence n0: "norm x \<noteq> 0" by (metis norm_eq_zero)
```
```  2406 	let ?c = "1/ norm x"
```
```  2407 	have "norm (?c*s x) = 1" using x0 by (simp add: n0 norm_mul)
```
```  2408 	with H have "norm (f(?c*s x)) \<le> b" by blast
```
```  2409 	hence "?c * norm (f x) \<le> b"
```
```  2410 	  by (simp add: linear_cmul[OF lf] norm_mul)
```
```  2411 	hence "norm (f x) \<le> b * norm x"
```
```  2412 	  using n0 norm_ge_zero[of x] by (auto simp add: field_simps)}
```
```  2413       ultimately have "norm (f x) \<le> b * norm x" by blast}
```
```  2414     then have ?rhs by blast}
```
```  2415   ultimately show ?thesis by blast
```
```  2416 qed
```
```  2417
```
```  2418 lemma onorm:
```
```  2419   fixes f:: "real ^'n::finite \<Rightarrow> real ^'m::finite"
```
```  2420   assumes lf: "linear f"
```
```  2421   shows "norm (f x) <= onorm f * norm x"
```
```  2422   and "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
```
```  2423 proof-
```
```  2424   {
```
```  2425     let ?S = "{norm (f x) |x. norm x = 1}"
```
```  2426     have Se: "?S \<noteq> {}" using  norm_basis by auto
```
```  2427     from linear_bounded[OF lf] have b: "\<exists> b. ?S *<= b"
```
```  2428       unfolding norm_bound_generalize[OF lf, symmetric] by (auto simp add: setle_def)
```
```  2429     {from rsup[OF Se b, unfolded onorm_def[symmetric]]
```
```  2430       show "norm (f x) <= onorm f * norm x"
```
```  2431 	apply -
```
```  2432 	apply (rule spec[where x = x])
```
```  2433 	unfolding norm_bound_generalize[OF lf, symmetric]
```
```  2434 	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
```
```  2435     {
```
```  2436       show "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
```
```  2437 	using rsup[OF Se b, unfolded onorm_def[symmetric]]
```
```  2438 	unfolding norm_bound_generalize[OF lf, symmetric]
```
```  2439 	by (auto simp add: isLub_def isUb_def leastP_def setge_def setle_def)}
```
```  2440   }
```
```  2441 qed
```
```  2442
```
```  2443 lemma onorm_pos_le: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" shows "0 <= onorm f"
```
```  2444   using order_trans[OF norm_ge_zero onorm(1)[OF lf, of "basis arbitrary"], unfolded norm_basis] by simp
```
```  2445
```
```  2446 lemma onorm_eq_0: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)"
```
```  2447   shows "onorm f = 0 \<longleftrightarrow> (\<forall>x. f x = 0)"
```
```  2448   using onorm[OF lf]
```
```  2449   apply (auto simp add: onorm_pos_le)
```
```  2450   apply atomize
```
```  2451   apply (erule allE[where x="0::real"])
```
```  2452   using onorm_pos_le[OF lf]
```
```  2453   apply arith
```
```  2454   done
```
```  2455
```
```  2456 lemma onorm_const: "onorm(\<lambda>x::real^'n::finite. (y::real ^ 'm::finite)) = norm y"
```
```  2457 proof-
```
```  2458   let ?f = "\<lambda>x::real^'n. (y::real ^ 'm)"
```
```  2459   have th: "{norm (?f x)| x. norm x = 1} = {norm y}"
```
```  2460     by(auto intro: vector_choose_size set_ext)
```
```  2461   show ?thesis
```
```  2462     unfolding onorm_def th
```
```  2463     apply (rule rsup_unique) by (simp_all  add: setle_def)
```
```  2464 qed
```
```  2465
```
```  2466 lemma onorm_pos_lt: assumes lf: "linear (f::real ^ 'n::finite \<Rightarrow> real ^'m::finite)"
```
```  2467   shows "0 < onorm f \<longleftrightarrow> ~(\<forall>x. f x = 0)"
```
```  2468   unfolding onorm_eq_0[OF lf, symmetric]
```
```  2469   using onorm_pos_le[OF lf] by arith
```
```  2470
```
```  2471 lemma onorm_compose:
```
```  2472   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)"
```
```  2473   and lg: "linear (g::real^'k::finite \<Rightarrow> real^'n::finite)"
```
```  2474   shows "onorm (f o g) <= onorm f * onorm g"
```
```  2475   apply (rule onorm(2)[OF linear_compose[OF lg lf], rule_format])
```
```  2476   unfolding o_def
```
```  2477   apply (subst mult_assoc)
```
```  2478   apply (rule order_trans)
```
```  2479   apply (rule onorm(1)[OF lf])
```
```  2480   apply (rule mult_mono1)
```
```  2481   apply (rule onorm(1)[OF lg])
```
```  2482   apply (rule onorm_pos_le[OF lf])
```
```  2483   done
```
```  2484
```
```  2485 lemma onorm_neg_lemma: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real^'m::finite)"
```
```  2486   shows "onorm (\<lambda>x. - f x) \<le> onorm f"
```
```  2487   using onorm[OF linear_compose_neg[OF lf]] onorm[OF lf]
```
```  2488   unfolding norm_minus_cancel by metis
```
```  2489
```
```  2490 lemma onorm_neg: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real^'m::finite)"
```
```  2491   shows "onorm (\<lambda>x. - f x) = onorm f"
```
```  2492   using onorm_neg_lemma[OF lf] onorm_neg_lemma[OF linear_compose_neg[OF lf]]
```
```  2493   by simp
```
```  2494
```
```  2495 lemma onorm_triangle:
```
```  2496   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" and lg: "linear g"
```
```  2497   shows "onorm (\<lambda>x. f x + g x) <= onorm f + onorm g"
```
```  2498   apply(rule onorm(2)[OF linear_compose_add[OF lf lg], rule_format])
```
```  2499   apply (rule order_trans)
```
```  2500   apply (rule norm_triangle_ineq)
```
```  2501   apply (simp add: distrib)
```
```  2502   apply (rule add_mono)
```
```  2503   apply (rule onorm(1)[OF lf])
```
```  2504   apply (rule onorm(1)[OF lg])
```
```  2505   done
```
```  2506
```
```  2507 lemma onorm_triangle_le: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) <= e
```
```  2508   \<Longrightarrow> onorm(\<lambda>x. f x + g x) <= e"
```
```  2509   apply (rule order_trans)
```
```  2510   apply (rule onorm_triangle)
```
```  2511   apply assumption+
```
```  2512   done
```
```  2513
```
```  2514 lemma onorm_triangle_lt: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) < e
```
```  2515   ==> onorm(\<lambda>x. f x + g x) < e"
```
```  2516   apply (rule order_le_less_trans)
```
```  2517   apply (rule onorm_triangle)
```
```  2518   by assumption+
```
```  2519
```
```  2520 (* "lift" from 'a to 'a^1 and "drop" from 'a^1 to 'a -- FIXME: potential use of transfer *)
```
```  2521
```
```  2522 definition vec1:: "'a \<Rightarrow> 'a ^ 1" where "vec1 x = (\<chi> i. x)"
```
```  2523
```
```  2524 definition dest_vec1:: "'a ^1 \<Rightarrow> 'a"
```
```  2525   where "dest_vec1 x = (x\$1)"
```
```  2526
```
```  2527 lemma vec1_component[simp]: "(vec1 x)\$1 = x"
```
```  2528   by (simp add: vec1_def)
```
```  2529
```
```  2530 lemma vec1_dest_vec1[simp]: "vec1(dest_vec1 x) = x" "dest_vec1(vec1 y) = y"
```
```  2531   by (simp_all add: vec1_def dest_vec1_def Cart_eq forall_1)
```
```  2532
```
```  2533 lemma forall_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (vec1 x))" by (metis vec1_dest_vec1)
```
```  2534
```
```  2535 lemma exists_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(vec1 x))" by (metis vec1_dest_vec1)
```
```  2536
```
```  2537 lemma forall_dest_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P(dest_vec1 x))"  by (metis vec1_dest_vec1)
```
```  2538
```
```  2539 lemma exists_dest_vec1: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P(dest_vec1 x))"by (metis vec1_dest_vec1)
```
```  2540
```
```  2541 lemma vec1_eq[simp]:  "vec1 x = vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
```
```  2542
```
```  2543 lemma dest_vec1_eq[simp]: "dest_vec1 x = dest_vec1 y \<longleftrightarrow> x = y" by (metis vec1_dest_vec1)
```
```  2544
```
```  2545 lemma vec1_in_image_vec1: "vec1 x \<in> (vec1 ` S) \<longleftrightarrow> x \<in> S" by auto
```
```  2546
```
```  2547 lemma vec1_vec: "vec1 x = vec x" by (vector vec1_def)
```
```  2548
```
```  2549 lemma vec1_add: "vec1(x + y) = vec1 x + vec1 y" by (vector vec1_def)
```
```  2550 lemma vec1_sub: "vec1(x - y) = vec1 x - vec1 y" by (vector vec1_def)
```
```  2551 lemma vec1_cmul: "vec1(c* x) = c *s vec1 x " by (vector vec1_def)
```
```  2552 lemma vec1_neg: "vec1(- x) = - vec1 x " by (vector vec1_def)
```
```  2553
```
```  2554 lemma vec1_setsum: assumes fS: "finite S"
```
```  2555   shows "vec1(setsum f S) = setsum (vec1 o f) S"
```
```  2556   apply (induct rule: finite_induct[OF fS])
```
```  2557   apply (simp add: vec1_vec)
```
```  2558   apply (auto simp add: vec1_add)
```
```  2559   done
```
```  2560
```
```  2561 lemma dest_vec1_lambda: "dest_vec1(\<chi> i. x i) = x 1"
```
```  2562   by (simp add: dest_vec1_def)
```
```  2563
```
```  2564 lemma dest_vec1_vec: "dest_vec1(vec x) = x"
```
```  2565   by (simp add: vec1_vec[symmetric])
```
```  2566
```
```  2567 lemma dest_vec1_add: "dest_vec1(x + y) = dest_vec1 x + dest_vec1 y"
```
```  2568  by (metis vec1_dest_vec1 vec1_add)
```
```  2569
```
```  2570 lemma dest_vec1_sub: "dest_vec1(x - y) = dest_vec1 x - dest_vec1 y"
```
```  2571  by (metis vec1_dest_vec1 vec1_sub)
```
```  2572
```
```  2573 lemma dest_vec1_cmul: "dest_vec1(c*sx) = c * dest_vec1 x"
```
```  2574  by (metis vec1_dest_vec1 vec1_cmul)
```
```  2575
```
```  2576 lemma dest_vec1_neg: "dest_vec1(- x) = - dest_vec1 x"
```
```  2577  by (metis vec1_dest_vec1 vec1_neg)
```
```  2578
```
```  2579 lemma dest_vec1_0[simp]: "dest_vec1 0 = 0" by (metis vec_0 dest_vec1_vec)
```
```  2580
```
```  2581 lemma dest_vec1_sum: assumes fS: "finite S"
```
```  2582   shows "dest_vec1(setsum f S) = setsum (dest_vec1 o f) S"
```
```  2583   apply (induct rule: finite_induct[OF fS])
```
```  2584   apply (simp add: dest_vec1_vec)
```
```  2585   apply (auto simp add: dest_vec1_add)
```
```  2586   done
```
```  2587
```
```  2588 lemma norm_vec1: "norm(vec1 x) = abs(x)"
```
```  2589   by (simp add: vec1_def norm_real)
```
```  2590
```
```  2591 lemma dist_vec1: "dist(vec1 x) (vec1 y) = abs(x - y)"
```
```  2592   by (simp only: dist_real vec1_component)
```
```  2593 lemma abs_dest_vec1: "norm x = \<bar>dest_vec1 x\<bar>"
```
```  2594   by (metis vec1_dest_vec1 norm_vec1)
```
```  2595
```
```  2596 lemma linear_vmul_dest_vec1:
```
```  2597   fixes f:: "'a::semiring_1^'n \<Rightarrow> 'a^1"
```
```  2598   shows "linear f \<Longrightarrow> linear (\<lambda>x. dest_vec1(f x) *s v)"
```
```  2599   unfolding dest_vec1_def
```
```  2600   apply (rule linear_vmul_component)
```
```  2601   by auto
```
```  2602
```
```  2603 lemma linear_from_scalars:
```
```  2604   assumes lf: "linear (f::'a::comm_ring_1 ^1 \<Rightarrow> 'a^'n)"
```
```  2605   shows "f = (\<lambda>x. dest_vec1 x *s column 1 (matrix f))"
```
```  2606   apply (rule ext)
```
```  2607   apply (subst matrix_works[OF lf, symmetric])
```
```  2608   apply (auto simp add: Cart_eq matrix_vector_mult_def dest_vec1_def column_def  mult_commute UNIV_1)
```
```  2609   done
```
```  2610
```
```  2611 lemma linear_to_scalars: assumes lf: "linear (f::'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a^1)"
```
```  2612   shows "f = (\<lambda>x. vec1(row 1 (matrix f) \<bullet> x))"
```
```  2613   apply (rule ext)
```
```  2614   apply (subst matrix_works[OF lf, symmetric])
```
```  2615   apply (simp add: Cart_eq matrix_vector_mult_def vec1_def row_def dot_def mult_commute forall_1)
```
```  2616   done
```
```  2617
```
```  2618 lemma dest_vec1_eq_0: "dest_vec1 x = 0 \<longleftrightarrow> x = 0"
```
```  2619   by (simp add: dest_vec1_eq[symmetric])
```
```  2620
```
```  2621 lemma setsum_scalars: assumes fS: "finite S"
```
```  2622   shows "setsum f S = vec1 (setsum (dest_vec1 o f) S)"
```
```  2623   unfolding vec1_setsum[OF fS] by simp
```
```  2624
```
```  2625 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"
```
```  2626   apply (cases "dest_vec1 x \<le> dest_vec1 y")
```
```  2627   apply simp
```
```  2628   apply (subgoal_tac "dest_vec1 y \<le> dest_vec1 x")
```
```  2629   apply (auto)
```
```  2630   done
```
```  2631
```
```  2632 text{* Pasting vectors. *}
```
```  2633
```
```  2634 lemma linear_fstcart: "linear fstcart"
```
```  2635   by (auto simp add: linear_def Cart_eq)
```
```  2636
```
```  2637 lemma linear_sndcart: "linear sndcart"
```
```  2638   by (auto simp add: linear_def Cart_eq)
```
```  2639
```
```  2640 lemma fstcart_vec[simp]: "fstcart(vec x) = vec x"
```
```  2641   by (simp add: Cart_eq)
```
```  2642
```
```  2643 lemma fstcart_add[simp]:"fstcart(x + y) = fstcart (x::'a::{plus,times}^('b + 'c)) + fstcart y"
```
```  2644   by (simp add: Cart_eq)
```
```  2645
```
```  2646 lemma fstcart_cmul[simp]:"fstcart(c*s x) = c*s fstcart (x::'a::{plus,times}^('b + 'c))"
```
```  2647   by (simp add: Cart_eq)
```
```  2648
```
```  2649 lemma fstcart_neg[simp]:"fstcart(- x) = - fstcart (x::'a::ring_1^('b + 'c))"
```
```  2650   by (simp add: Cart_eq)
```
```  2651
```
```  2652 lemma fstcart_sub[simp]:"fstcart(x - y) = fstcart (x::'a::ring_1^('b + 'c)) - fstcart y"
```
```  2653   by (simp add: Cart_eq)
```
```  2654
```
```  2655 lemma fstcart_setsum:
```
```  2656   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
```
```  2657   assumes fS: "finite S"
```
```  2658   shows "fstcart (setsum f S) = setsum (\<lambda>i. fstcart (f i)) S"
```
```  2659   by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
```
```  2660
```
```  2661 lemma sndcart_vec[simp]: "sndcart(vec x) = vec x"
```
```  2662   by (simp add: Cart_eq)
```
```  2663
```
```  2664 lemma sndcart_add[simp]:"sndcart(x + y) = sndcart (x::'a::{plus,times}^('b + 'c)) + sndcart y"
```
```  2665   by (simp add: Cart_eq)
```
```  2666
```
```  2667 lemma sndcart_cmul[simp]:"sndcart(c*s x) = c*s sndcart (x::'a::{plus,times}^('b + 'c))"
```
```  2668   by (simp add: Cart_eq)
```
```  2669
```
```  2670 lemma sndcart_neg[simp]:"sndcart(- x) = - sndcart (x::'a::ring_1^('b + 'c))"
```
```  2671   by (simp add: Cart_eq)
```
```  2672
```
```  2673 lemma sndcart_sub[simp]:"sndcart(x - y) = sndcart (x::'a::ring_1^('b + 'c)) - sndcart y"
```
```  2674   by (simp add: Cart_eq)
```
```  2675
```
```  2676 lemma sndcart_setsum:
```
```  2677   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
```
```  2678   assumes fS: "finite S"
```
```  2679   shows "sndcart (setsum f S) = setsum (\<lambda>i. sndcart (f i)) S"
```
```  2680   by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
```
```  2681
```
```  2682 lemma pastecart_vec[simp]: "pastecart (vec x) (vec x) = vec x"
```
```  2683   by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
```
```  2684
```
```  2685 lemma pastecart_add[simp]:"pastecart (x1::'a::{plus,times}^_) y1 + pastecart x2 y2 = pastecart (x1 + x2) (y1 + y2)"
```
```  2686   by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
```
```  2687
```
```  2688 lemma pastecart_cmul[simp]: "pastecart (c *s (x1::'a::{plus,times}^_)) (c *s y1) = c *s pastecart x1 y1"
```
```  2689   by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
```
```  2690
```
```  2691 lemma pastecart_neg[simp]: "pastecart (- (x::'a::ring_1^_)) (- y) = - pastecart x y"
```
```  2692   unfolding vector_sneg_minus1 pastecart_cmul ..
```
```  2693
```
```  2694 lemma pastecart_sub: "pastecart (x1::'a::ring_1^_) y1 - pastecart x2 y2 = pastecart (x1 - x2) (y1 - y2)"
```
```  2695   by (simp add: diff_def pastecart_neg[symmetric] del: pastecart_neg)
```
```  2696
```
```  2697 lemma pastecart_setsum:
```
```  2698   fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
```
```  2699   assumes fS: "finite S"
```
```  2700   shows "pastecart (setsum f S) (setsum g S) = setsum (\<lambda>i. pastecart (f i) (g i)) S"
```
```  2701   by (simp  add: pastecart_eq fstcart_setsum[OF fS] sndcart_setsum[OF fS] fstcart_pastecart sndcart_pastecart)
```
```  2702
```
```  2703 lemma setsum_Plus:
```
```  2704   "\<lbrakk>finite A; finite B\<rbrakk> \<Longrightarrow>
```
```  2705     (\<Sum>x\<in>A <+> B. g x) = (\<Sum>x\<in>A. g (Inl x)) + (\<Sum>x\<in>B. g (Inr x))"
```
```  2706   unfolding Plus_def
```
```  2707   by (subst setsum_Un_disjoint, auto simp add: setsum_reindex)
```
```  2708
```
```  2709 lemma setsum_UNIV_sum:
```
```  2710   fixes g :: "'a::finite + 'b::finite \<Rightarrow> _"
```
```  2711   shows "(\<Sum>x\<in>UNIV. g x) = (\<Sum>x\<in>UNIV. g (Inl x)) + (\<Sum>x\<in>UNIV. g (Inr x))"
```
```  2712   apply (subst UNIV_Plus_UNIV [symmetric])
```
```  2713   apply (rule setsum_Plus [OF finite finite])
```
```  2714   done
```
```  2715
```
```  2716 lemma norm_fstcart: "norm(fstcart x) <= norm (x::real ^('n::finite + 'm::finite))"
```
```  2717 proof-
```
```  2718   have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
```
```  2719     by (simp add: pastecart_fst_snd)
```
```  2720   have th1: "fstcart x \<bullet> fstcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
```
```  2721     by (simp add: dot_def setsum_UNIV_sum pastecart_def setsum_nonneg)
```
```  2722   then show ?thesis
```
```  2723     unfolding th0
```
```  2724     unfolding real_vector_norm_def real_sqrt_le_iff id_def
```
```  2725     by (simp add: dot_def)
```
```  2726 qed
```
```  2727
```
```  2728 lemma dist_fstcart: "dist(fstcart (x::real^_)) (fstcart y) <= dist x y"
```
```  2729   unfolding dist_norm by (metis fstcart_sub[symmetric] norm_fstcart)
```
```  2730
```
```  2731 lemma norm_sndcart: "norm(sndcart x) <= norm (x::real ^('n::finite + 'm::finite))"
```
```  2732 proof-
```
```  2733   have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
```
```  2734     by (simp add: pastecart_fst_snd)
```
```  2735   have th1: "sndcart x \<bullet> sndcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
```
```  2736     by (simp add: dot_def setsum_UNIV_sum pastecart_def setsum_nonneg)
```
```  2737   then show ?thesis
```
```  2738     unfolding th0
```
```  2739     unfolding real_vector_norm_def real_sqrt_le_iff id_def
```
```  2740     by (simp add: dot_def)
```
```  2741 qed
```
```  2742
```
```  2743 lemma dist_sndcart: "dist(sndcart (x::real^_)) (sndcart y) <= dist x y"
```
```  2744   unfolding dist_norm by (metis sndcart_sub[symmetric] norm_sndcart)
```
```  2745
```
```  2746 lemma dot_pastecart: "(pastecart (x1::'a::{times,comm_monoid_add}^'n::finite) (x2::'a::{times,comm_monoid_add}^'m::finite)) \<bullet> (pastecart y1 y2) =  x1 \<bullet> y1 + x2 \<bullet> y2"
```
```  2747   by (simp add: dot_def setsum_UNIV_sum pastecart_def)
```
```  2748
```
```  2749 text {* TODO: move to NthRoot *}
```
```  2750 lemma sqrt_add_le_add_sqrt:
```
```  2751   assumes x: "0 \<le> x" and y: "0 \<le> y"
```
```  2752   shows "sqrt (x + y) \<le> sqrt x + sqrt y"
```
```  2753 apply (rule power2_le_imp_le)
```
```  2754 apply (simp add: real_sum_squared_expand add_nonneg_nonneg x y)
```
```  2755 apply (simp add: mult_nonneg_nonneg x y)
```
```  2756 apply (simp add: add_nonneg_nonneg x y)
```
```  2757 done
```
```  2758
```
```  2759 lemma norm_pastecart: "norm (pastecart x y) <= norm x + norm y"
```
```  2760   unfolding vector_norm_def setL2_def setsum_UNIV_sum
```
```  2761   by (simp add: sqrt_add_le_add_sqrt setsum_nonneg)
```
```  2762
```
```  2763 subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
```
```  2764
```
```  2765 definition hull :: "'a set set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "hull" 75) where
```
```  2766   "S hull s = Inter {t. t \<in> S \<and> s \<subseteq> t}"
```
```  2767
```
```  2768 lemma hull_same: "s \<in> S \<Longrightarrow> S hull s = s"
```
```  2769   unfolding hull_def by auto
```
```  2770
```
```  2771 lemma hull_in: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) \<in> S"
```
```  2772 unfolding hull_def subset_iff by auto
```
```  2773
```
```  2774 lemma hull_eq: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) = s \<longleftrightarrow> s \<in> S"
```
```  2775 using hull_same[of s S] hull_in[of S s] by metis
```
```  2776
```
```  2777
```
```  2778 lemma hull_hull: "S hull (S hull s) = S hull s"
```
```  2779   unfolding hull_def by blast
```
```  2780
```
```  2781 lemma hull_subset: "s \<subseteq> (S hull s)"
```
```  2782   unfolding hull_def by blast
```
```  2783
```
```  2784 lemma hull_mono: " s \<subseteq> t ==> (S hull s) \<subseteq> (S hull t)"
```
```  2785   unfolding hull_def by blast
```
```  2786
```
```  2787 lemma hull_antimono: "S \<subseteq> T ==> (T hull s) \<subseteq> (S hull s)"
```
```  2788   unfolding hull_def by blast
```
```  2789
```
```  2790 lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> t \<in> S ==> (S hull s) \<subseteq> t"
```
```  2791   unfolding hull_def by blast
```
```  2792
```
```  2793 lemma subset_hull: "t \<in> S ==> S hull s \<subseteq> t \<longleftrightarrow>  s \<subseteq> t"
```
```  2794   unfolding hull_def by blast
```
```  2795
```
```  2796 lemma hull_unique: "s \<subseteq> t \<Longrightarrow> t \<in> S \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> t' \<in> S ==> t \<subseteq> t')
```
```  2797            ==> (S hull s = t)"
```
```  2798 unfolding hull_def by auto
```
```  2799
```
```  2800 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"
```
```  2801   using hull_minimal[of S "{x. P x}" Q]
```
```  2802   by (auto simp add: subset_eq Collect_def mem_def)
```
```  2803
```
```  2804 lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S" by (metis hull_subset subset_eq)
```
```  2805
```
```  2806 lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
```
```  2807 unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
```
```  2808
```
```  2809 lemma hull_union: assumes T: "\<And>T. T \<subseteq> S ==> Inter T \<in> S"
```
```  2810   shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
```
```  2811 apply rule
```
```  2812 apply (rule hull_mono)
```
```  2813 unfolding Un_subset_iff
```
```  2814 apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
```
```  2815 apply (rule hull_minimal)
```
```  2816 apply (metis hull_union_subset)
```
```  2817 apply (metis hull_in T)
```
```  2818 done
```
```  2819
```
```  2820 lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
```
```  2821   unfolding hull_def by blast
```
```  2822
```
```  2823 lemma hull_redundant: "a \<in> (S hull s) ==> (S hull (insert a s) = S hull s)"
```
```  2824 by (metis hull_redundant_eq)
```
```  2825
```
```  2826 text{* Archimedian properties and useful consequences. *}
```
```  2827
```
```  2828 lemma real_arch_simple: "\<exists>n. x <= real (n::nat)"
```
```  2829   using reals_Archimedean2[of x] apply auto by (rule_tac x="Suc n" in exI, auto)
```
```  2830 lemmas real_arch_lt = reals_Archimedean2
```
```  2831
```
```  2832 lemmas real_arch = reals_Archimedean3
```
```  2833
```
```  2834 lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
```
```  2835   using reals_Archimedean
```
```  2836   apply (auto simp add: field_simps inverse_positive_iff_positive)
```
```  2837   apply (subgoal_tac "inverse (real n) > 0")
```
```  2838   apply arith
```
```  2839   apply simp
```
```  2840   done
```
```  2841
```
```  2842 lemma real_pow_lbound: "0 <= x ==> 1 + real n * x <= (1 + x) ^ n"
```
```  2843 proof(induct n)
```
```  2844   case 0 thus ?case by simp
```
```  2845 next
```
```  2846   case (Suc n)
```
```  2847   hence h: "1 + real n * x \<le> (1 + x) ^ n" by simp
```
```  2848   from h have p: "1 \<le> (1 + x) ^ n" using Suc.prems by simp
```
```  2849   from h have "1 + real n * x + x \<le> (1 + x) ^ n + x" by simp
```
```  2850   also have "\<dots> \<le> (1 + x) ^ Suc n" apply (subst diff_le_0_iff_le[symmetric])
```
```  2851     apply (simp add: ring_simps)
```
```  2852     using mult_left_mono[OF p Suc.prems] by simp
```
```  2853   finally show ?case  by (simp add: real_of_nat_Suc ring_simps)
```
```  2854 qed
```
```  2855
```
```  2856 lemma real_arch_pow: assumes x: "1 < (x::real)" shows "\<exists>n. y < x^n"
```
```  2857 proof-
```
```  2858   from x have x0: "x - 1 > 0" by arith
```
```  2859   from real_arch[OF x0, rule_format, of y]
```
```  2860   obtain n::nat where n:"y < real n * (x - 1)" by metis
```
```  2861   from x0 have x00: "x- 1 \<ge> 0" by arith
```
```  2862   from real_pow_lbound[OF x00, of n] n
```
```  2863   have "y < x^n" by auto
```
```  2864   then show ?thesis by metis
```
```  2865 qed
```
```  2866
```
```  2867 lemma real_arch_pow2: "\<exists>n. (x::real) < 2^ n"
```
```  2868   using real_arch_pow[of 2 x] by simp
```
```  2869
```
```  2870 lemma real_arch_pow_inv: assumes y: "(y::real) > 0" and x1: "x < 1"
```
```  2871   shows "\<exists>n. x^n < y"
```
```  2872 proof-
```
```  2873   {assume x0: "x > 0"
```
```  2874     from x0 x1 have ix: "1 < 1/x" by (simp add: field_simps)
```
```  2875     from real_arch_pow[OF ix, of "1/y"]
```
```  2876     obtain n where n: "1/y < (1/x)^n" by blast
```
```  2877     then
```
```  2878     have ?thesis using y x0 by (auto simp add: field_simps power_divide) }
```
```  2879   moreover
```
```  2880   {assume "\<not> x > 0" with y x1 have ?thesis apply auto by (rule exI[where x=1], auto)}
```
```  2881   ultimately show ?thesis by metis
```
```  2882 qed
```
```  2883
```
```  2884 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)"
```
```  2885   by (metis real_arch_inv)
```
```  2886
```
```  2887 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"
```
```  2888   apply (rule forall_pos_mono)
```
```  2889   apply auto
```
```  2890   apply (atomize)
```
```  2891   apply (erule_tac x="n - 1" in allE)
```
```  2892   apply auto
```
```  2893   done
```
```  2894
```
```  2895 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"
```
```  2896   shows "x = 0"
```
```  2897 proof-
```
```  2898   {assume "x \<noteq> 0" with x0 have xp: "x > 0" by arith
```
```  2899     from real_arch[OF xp, rule_format, of c] obtain n::nat where n: "c < real n * x"  by blast
```
```  2900     with xc[rule_format, of n] have "n = 0" by arith
```
```  2901     with n c have False by simp}
```
```  2902   then show ?thesis by blast
```
```  2903 qed
```
```  2904
```
```  2905 (* ------------------------------------------------------------------------- *)
```
```  2906 (* Relate max and min to sup and inf.                                        *)
```
```  2907 (* ------------------------------------------------------------------------- *)
```
```  2908
```
```  2909 lemma real_max_rsup: "max x y = rsup {x,y}"
```
```  2910 proof-
```
```  2911   have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
```
```  2912   from rsup_finite_le_iff[OF f, of "max x y"] have "rsup {x,y} \<le> max x y" by simp
```
```  2913   moreover
```
```  2914   have "max x y \<le> rsup {x,y}" using rsup_finite_ge_iff[OF f, of "max x y"]
```
```  2915     by (simp add: linorder_linear)
```
```  2916   ultimately show ?thesis by arith
```
```  2917 qed
```
```  2918
```
```  2919 lemma real_min_rinf: "min x y = rinf {x,y}"
```
```  2920 proof-
```
```  2921   have f: "finite {x, y}" "{x,y} \<noteq> {}"  by simp_all
```
```  2922   from rinf_finite_le_iff[OF f, of "min x y"] have "rinf {x,y} \<le> min x y"
```
```  2923     by (simp add: linorder_linear)
```
```  2924   moreover
```
```  2925   have "min x y \<le> rinf {x,y}" using rinf_finite_ge_iff[OF f, of "min x y"]
```
```  2926     by simp
```
```  2927   ultimately show ?thesis by arith
```
```  2928 qed
```
```  2929
```
```  2930 (* ------------------------------------------------------------------------- *)
```
```  2931 (* Geometric progression.                                                    *)
```
```  2932 (* ------------------------------------------------------------------------- *)
```
```  2933
```
```  2934 lemma sum_gp_basic: "((1::'a::{field}) - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
```
```  2935   (is "?lhs = ?rhs")
```
```  2936 proof-
```
```  2937   {assume x1: "x = 1" hence ?thesis by simp}
```
```  2938   moreover
```
```  2939   {assume x1: "x\<noteq>1"
```
```  2940     hence x1': "x - 1 \<noteq> 0" "1 - x \<noteq> 0" "x - 1 = - (1 - x)" "- (1 - x) \<noteq> 0" by auto
```
```  2941     from geometric_sum[OF x1, of "Suc n", unfolded x1']
```
```  2942     have "(- (1 - x)) * setsum (\<lambda>i. x^i) {0 .. n} = - (1 - x^(Suc n))"
```
```  2943       unfolding atLeastLessThanSuc_atLeastAtMost
```
```  2944       using x1' apply (auto simp only: field_simps)
```
```  2945       apply (simp add: ring_simps)
```
```  2946       done
```
```  2947     then have ?thesis by (simp add: ring_simps) }
```
```  2948   ultimately show ?thesis by metis
```
```  2949 qed
```
```  2950
```
```  2951 lemma sum_gp_multiplied: assumes mn: "m <= n"
```
```  2952   shows "((1::'a::{field}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
```
```  2953   (is "?lhs = ?rhs")
```
```  2954 proof-
```
```  2955   let ?S = "{0..(n - m)}"
```
```  2956   from mn have mn': "n - m \<ge> 0" by arith
```
```  2957   let ?f = "op + m"
```
```  2958   have i: "inj_on ?f ?S" unfolding inj_on_def by auto
```
```  2959   have f: "?f ` ?S = {m..n}"
```
```  2960     using mn apply (auto simp add: image_iff Bex_def) by arith
```
```  2961   have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)"
```
```  2962     by (rule ext, simp add: power_add power_mult)
```
```  2963   from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
```
```  2964   have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
```
```  2965   then show ?thesis unfolding sum_gp_basic using mn
```
```  2966     by (simp add: ring_simps power_add[symmetric])
```
```  2967 qed
```
```  2968
```
```  2969 lemma sum_gp: "setsum (op ^ (x::'a::{field})) {m .. n} =
```
```  2970    (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m)
```
```  2971                     else (x^ m - x^ (Suc n)) / (1 - x))"
```
```  2972 proof-
```
```  2973   {assume nm: "n < m" hence ?thesis by simp}
```
```  2974   moreover
```
```  2975   {assume "\<not> n < m" hence nm: "m \<le> n" by arith
```
```  2976     {assume x: "x = 1"  hence ?thesis by simp}
```
```  2977     moreover
```
```  2978     {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
```
```  2979       from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
```
```  2980     ultimately have ?thesis by metis
```
```  2981   }
```
```  2982   ultimately show ?thesis by metis
```
```  2983 qed
```
```  2984
```
```  2985 lemma sum_gp_offset: "setsum (op ^ (x::'a::{field})) {m .. m+n} =
```
```  2986   (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
```
```  2987   unfolding sum_gp[of x m "m + n"] power_Suc
```
```  2988   by (simp add: ring_simps power_add)
```
```  2989
```
```  2990
```
```  2991 subsection{* A bit of linear algebra. *}
```
```  2992
```
```  2993 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 )"
```
```  2994 definition "span S = (subspace hull S)"
```
```  2995 definition "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span(S - {a}))"
```
```  2996 abbreviation "independent s == ~(dependent s)"
```
```  2997
```
```  2998 (* Closure properties of subspaces.                                          *)
```
```  2999
```
```  3000 lemma subspace_UNIV[simp]: "subspace(UNIV)" by (simp add: subspace_def)
```
```  3001
```
```  3002 lemma subspace_0: "subspace S ==> 0 \<in> S" by (metis subspace_def)
```
```  3003
```
```  3004 lemma subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S ==> x + y \<in> S"
```
```  3005   by (metis subspace_def)
```
```  3006
```
```  3007 lemma subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *s x \<in> S"
```
```  3008   by (metis subspace_def)
```
```  3009
```
```  3010 lemma subspace_neg: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> - x \<in> S"
```
```  3011   by (metis vector_sneg_minus1 subspace_mul)
```
```  3012
```
```  3013 lemma subspace_sub: "subspace S \<Longrightarrow> (x::'a::ring_1^'n) \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
```
```  3014   by (metis diff_def subspace_add subspace_neg)
```
```  3015
```
```  3016 lemma subspace_setsum:
```
```  3017   assumes sA: "subspace A" and fB: "finite B"
```
```  3018   and f: "\<forall>x\<in> B. f x \<in> A"
```
```  3019   shows "setsum f B \<in> A"
```
```  3020   using  fB f sA
```
```  3021   apply(induct rule: finite_induct[OF fB])
```
```  3022   by (simp add: subspace_def sA, auto simp add: sA subspace_add)
```
```  3023
```
```  3024 lemma subspace_linear_image:
```
```  3025   assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and sS: "subspace S"
```
```  3026   shows "subspace(f ` S)"
```
```  3027   using lf sS linear_0[OF lf]
```
```  3028   unfolding linear_def subspace_def
```
```  3029   apply (auto simp add: image_iff)
```
```  3030   apply (rule_tac x="x + y" in bexI, auto)
```
```  3031   apply (rule_tac x="c*s x" in bexI, auto)
```
```  3032   done
```
```  3033
```
```  3034 lemma subspace_linear_preimage: "linear (f::'a::semiring_1^'n \<Rightarrow> _) ==> subspace S ==> subspace {x. f x \<in> S}"
```
```  3035   by (auto simp add: subspace_def linear_def linear_0[of f])
```
```  3036
```
```  3037 lemma subspace_trivial: "subspace {0::'a::semiring_1 ^_}"
```
```  3038   by (simp add: subspace_def)
```
```  3039
```
```  3040 lemma subspace_inter: "subspace A \<Longrightarrow> subspace B ==> subspace (A \<inter> B)"
```
```  3041   by (simp add: subspace_def)
```
```  3042
```
```  3043
```
```  3044 lemma span_mono: "A \<subseteq> B ==> span A \<subseteq> span B"
```
```  3045   by (metis span_def hull_mono)
```
```  3046
```
```  3047 lemma subspace_span: "subspace(span S)"
```
```  3048   unfolding span_def
```
```  3049   apply (rule hull_in[unfolded mem_def])
```
```  3050   apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
```
```  3051   apply auto
```
```  3052   apply (erule_tac x="X" in ballE)
```
```  3053   apply (simp add: mem_def)
```
```  3054   apply blast
```
```  3055   apply (erule_tac x="X" in ballE)
```
```  3056   apply (erule_tac x="X" in ballE)
```
```  3057   apply (erule_tac x="X" in ballE)
```
```  3058   apply (clarsimp simp add: mem_def)
```
```  3059   apply simp
```
```  3060   apply simp
```
```  3061   apply simp
```
```  3062   apply (erule_tac x="X" in ballE)
```
```  3063   apply (erule_tac x="X" in ballE)
```
```  3064   apply (simp add: mem_def)
```
```  3065   apply simp
```
```  3066   apply simp
```
```  3067   done
```
```  3068
```
```  3069 lemma span_clauses:
```
```  3070   "a \<in> S ==> a \<in> span S"
```
```  3071   "0 \<in> span S"
```
```  3072   "x\<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
```
```  3073   "x \<in> span S \<Longrightarrow> c *s x \<in> span S"
```
```  3074   by (metis span_def hull_subset subset_eq subspace_span subspace_def)+
```
```  3075
```
```  3076 lemma span_induct: assumes SP: "\<And>x. x \<in> S ==> P x"
```
```  3077   and P: "subspace P" and x: "x \<in> span S" shows "P x"
```
```  3078 proof-
```
```  3079   from SP have SP': "S \<subseteq> P" by (simp add: mem_def subset_eq)
```
```  3080   from P have P': "P \<in> subspace" by (simp add: mem_def)
```
```  3081   from x hull_minimal[OF SP' P', unfolded span_def[symmetric]]
```
```  3082   show "P x" by (metis mem_def subset_eq)
```
```  3083 qed
```
```  3084
```
```  3085 lemma span_empty: "span {} = {(0::'a::semiring_0 ^ 'n)}"
```
```  3086   apply (simp add: span_def)
```
```  3087   apply (rule hull_unique)
```
```  3088   apply (auto simp add: mem_def subspace_def)
```
```  3089   unfolding mem_def[of "0::'a^'n", symmetric]
```
```  3090   apply simp
```
```  3091   done
```
```  3092
```
```  3093 lemma independent_empty: "independent {}"
```
```  3094   by (simp add: dependent_def)
```
```  3095
```
```  3096 lemma independent_mono: "independent A \<Longrightarrow> B \<subseteq> A ==> independent B"
```
```  3097   apply (clarsimp simp add: dependent_def span_mono)
```
```  3098   apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
```
```  3099   apply force
```
```  3100   apply (rule span_mono)
```
```  3101   apply auto
```
```  3102   done
```
```  3103
```
```  3104 lemma span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
```
```  3105   by (metis order_antisym span_def hull_minimal mem_def)
```
```  3106
```
```  3107 lemma span_induct': assumes SP: "\<forall>x \<in> S. P x"
```
```  3108   and P: "subspace P" shows "\<forall>x \<in> span S. P x"
```
```  3109   using span_induct SP P by blast
```
```  3110
```
```  3111 inductive span_induct_alt_help for S:: "'a::semiring_1^'n \<Rightarrow> bool"
```
```  3112   where
```
```  3113   span_induct_alt_help_0: "span_induct_alt_help S 0"
```
```  3114   | 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)"
```
```  3115
```
```  3116 lemma span_induct_alt':
```
```  3117   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"
```
```  3118 proof-
```
```  3119   {fix x:: "'a^'n" assume x: "span_induct_alt_help S x"
```
```  3120     have "h x"
```
```  3121       apply (rule span_induct_alt_help.induct[OF x])
```
```  3122       apply (rule h0)
```
```  3123       apply (rule hS, assumption, assumption)
```
```  3124       done}
```
```  3125   note th0 = this
```
```  3126   {fix x assume x: "x \<in> span S"
```
```  3127
```
```  3128     have "span_induct_alt_help S x"
```
```  3129       proof(rule span_induct[where x=x and S=S])
```
```  3130 	show "x \<in> span S" using x .
```
```  3131       next
```
```  3132 	fix x assume xS : "x \<in> S"
```
```  3133 	  from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
```
```  3134 	  show "span_induct_alt_help S x" by simp
```
```  3135 	next
```
```  3136 	have "span_induct_alt_help S 0" by (rule span_induct_alt_help_0)
```
```  3137 	moreover
```
```  3138 	{fix x y assume h: "span_induct_alt_help S x" "span_induct_alt_help S y"
```
```  3139 	  from h
```
```  3140 	  have "span_induct_alt_help S (x + y)"
```
```  3141 	    apply (induct rule: span_induct_alt_help.induct)
```
```  3142 	    apply simp
```
```  3143 	    unfolding add_assoc
```
```  3144 	    apply (rule span_induct_alt_help_S)
```
```  3145 	    apply assumption
```
```  3146 	    apply simp
```
```  3147 	    done}
```
```  3148 	moreover
```
```  3149 	{fix c x assume xt: "span_induct_alt_help S x"
```
```  3150 	  then have "span_induct_alt_help S (c*s x)"
```
```  3151 	    apply (induct rule: span_induct_alt_help.induct)
```
```  3152 	    apply (simp add: span_induct_alt_help_0)
```
```  3153 	    apply (simp add: vector_smult_assoc vector_add_ldistrib)
```
```  3154 	    apply (rule span_induct_alt_help_S)
```
```  3155 	    apply assumption
```
```  3156 	    apply simp
```
```  3157 	    done
```
```  3158 	}
```
```  3159 	ultimately show "subspace (span_induct_alt_help S)"
```
```  3160 	  unfolding subspace_def mem_def Ball_def by blast
```
```  3161       qed}
```
```  3162   with th0 show ?thesis by blast
```
```  3163 qed
```
```  3164
```
```  3165 lemma span_induct_alt:
```
```  3166   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"
```
```  3167   shows "h x"
```
```  3168 using span_induct_alt'[of h S] h0 hS x by blast
```
```  3169
```
```  3170 (* Individual closure properties. *)
```
```  3171
```
```  3172 lemma span_superset: "x \<in> S ==> x \<in> span S" by (metis span_clauses)
```
```  3173
```
```  3174 lemma span_0: "0 \<in> span S" by (metis subspace_span subspace_0)
```
```  3175
```
```  3176 lemma span_add: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
```
```  3177   by (metis subspace_add subspace_span)
```
```  3178
```
```  3179 lemma span_mul: "x \<in> span S ==> (c *s x) \<in> span S"
```
```  3180   by (metis subspace_span subspace_mul)
```
```  3181
```
```  3182 lemma span_neg: "x \<in> span S ==> - (x::'a::ring_1^'n) \<in> span S"
```
```  3183   by (metis subspace_neg subspace_span)
```
```  3184
```
```  3185 lemma span_sub: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> y \<in> span S ==> x - y \<in> span S"
```
```  3186   by (metis subspace_span subspace_sub)
```
```  3187
```
```  3188 lemma span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S ==> setsum f A \<in> span S"
```
```  3189   apply (rule subspace_setsum)
```
```  3190   by (metis subspace_span subspace_setsum)+
```
```  3191
```
```  3192 lemma span_add_eq: "(x::'a::ring_1^'n) \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
```
```  3193   apply (auto simp only: span_add span_sub)
```
```  3194   apply (subgoal_tac "(x + y) - x \<in> span S", simp)
```
```  3195   by (simp only: span_add span_sub)
```
```  3196
```
```  3197 (* Mapping under linear image. *)
```
```  3198
```
```  3199 lemma span_linear_image: assumes lf: "linear (f::'a::semiring_1 ^ 'n => _)"
```
```  3200   shows "span (f ` S) = f ` (span S)"
```
```  3201 proof-
```
```  3202   {fix x
```
```  3203     assume x: "x \<in> span (f ` S)"
```
```  3204     have "x \<in> f ` span S"
```
```  3205       apply (rule span_induct[where x=x and S = "f ` S"])
```
```  3206       apply (clarsimp simp add: image_iff)
```
```  3207       apply (frule span_superset)
```
```  3208       apply blast
```
```  3209       apply (simp only: mem_def)
```
```  3210       apply (rule subspace_linear_image[OF lf])
```
```  3211       apply (rule subspace_span)
```
```  3212       apply (rule x)
```
```  3213       done}
```
```  3214   moreover
```
```  3215   {fix x assume x: "x \<in> span S"
```
```  3216     have th0:"(\<lambda>a. f a \<in> span (f ` S)) = {x. f x \<in> span (f ` S)}" apply (rule set_ext)
```
```  3217       unfolding mem_def Collect_def ..
```
```  3218     have "f x \<in> span (f ` S)"
```
```  3219       apply (rule span_induct[where S=S])
```
```  3220       apply (rule span_superset)
```
```  3221       apply simp
```
```  3222       apply (subst th0)
```
```  3223       apply (rule subspace_linear_preimage[OF lf subspace_span, of "f ` S"])
```
```  3224       apply (rule x)
```
```  3225       done}
```
```  3226   ultimately show ?thesis by blast
```
```  3227 qed
```
```  3228
```
```  3229 (* The key breakdown property. *)
```
```  3230
```
```  3231 lemma span_breakdown:
```
```  3232   assumes bS: "(b::'a::ring_1 ^ 'n) \<in> S" and aS: "a \<in> span S"
```
```  3233   shows "\<exists>k. a - k*s b \<in> span (S - {b})" (is "?P a")
```
```  3234 proof-
```
```  3235   {fix x assume xS: "x \<in> S"
```
```  3236     {assume ab: "x = b"
```
```  3237       then have "?P x"
```
```  3238 	apply simp
```
```  3239 	apply (rule exI[where x="1"], simp)
```
```  3240 	by (rule span_0)}
```
```  3241     moreover
```
```  3242     {assume ab: "x \<noteq> b"
```
```  3243       then have "?P x"  using xS
```
```  3244 	apply -
```
```  3245 	apply (rule exI[where x=0])
```
```  3246 	apply (rule span_superset)
```
```  3247 	by simp}
```
```  3248     ultimately have "?P x" by blast}
```
```  3249   moreover have "subspace ?P"
```
```  3250     unfolding subspace_def
```
```  3251     apply auto
```
```  3252     apply (simp add: mem_def)
```
```  3253     apply (rule exI[where x=0])
```
```  3254     using span_0[of "S - {b}"]
```
```  3255     apply (simp add: mem_def)
```
```  3256     apply (clarsimp simp add: mem_def)
```
```  3257     apply (rule_tac x="k + ka" in exI)
```
```  3258     apply (subgoal_tac "x + y - (k + ka) *s b = (x - k*s b) + (y - ka *s b)")
```
```  3259     apply (simp only: )
```
```  3260     apply (rule span_add[unfolded mem_def])
```
```  3261     apply assumption+
```
```  3262     apply (vector ring_simps)
```
```  3263     apply (clarsimp simp add: mem_def)
```
```  3264     apply (rule_tac x= "c*k" in exI)
```
```  3265     apply (subgoal_tac "c *s x - (c * k) *s b = c*s (x - k*s b)")
```
```  3266     apply (simp only: )
```
```  3267     apply (rule span_mul[unfolded mem_def])
```
```  3268     apply assumption
```
```  3269     by (vector ring_simps)
```
```  3270   ultimately show "?P a" using aS span_induct[where S=S and P= "?P"] by metis
```
```  3271 qed
```
```  3272
```
```  3273 lemma span_breakdown_eq:
```
```  3274   "(x::'a::ring_1^'n) \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. (x - k *s a) \<in> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  3275 proof-
```
```  3276   {assume x: "x \<in> span (insert a S)"
```
```  3277     from x span_breakdown[of "a" "insert a S" "x"]
```
```  3278     have ?rhs apply clarsimp
```
```  3279       apply (rule_tac x= "k" in exI)
```
```  3280       apply (rule set_rev_mp[of _ "span (S - {a})" _])
```
```  3281       apply assumption
```
```  3282       apply (rule span_mono)
```
```  3283       apply blast
```
```  3284       done}
```
```  3285   moreover
```
```  3286   { fix k assume k: "x - k *s a \<in> span S"
```
```  3287     have eq: "x = (x - k *s a) + k *s a" by vector
```
```  3288     have "(x - k *s a) + k *s a \<in> span (insert a S)"
```
```  3289       apply (rule span_add)
```
```  3290       apply (rule set_rev_mp[of _ "span S" _])
```
```  3291       apply (rule k)
```
```  3292       apply (rule span_mono)
```
```  3293       apply blast
```
```  3294       apply (rule span_mul)
```
```  3295       apply (rule span_superset)
```
```  3296       apply blast
```
```  3297       done
```
```  3298     then have ?lhs using eq by metis}
```
```  3299   ultimately show ?thesis by blast
```
```  3300 qed
```
```  3301
```
```  3302 (* Hence some "reversal" results.*)
```
```  3303
```
```  3304 lemma in_span_insert:
```
```  3305   assumes a: "(a::'a::field^'n) \<in> span (insert b S)" and na: "a \<notin> span S"
```
```  3306   shows "b \<in> span (insert a S)"
```
```  3307 proof-
```
```  3308   from span_breakdown[of b "insert b S" a, OF insertI1 a]
```
```  3309   obtain k where k: "a - k*s b \<in> span (S - {b})" by auto
```
```  3310   {assume k0: "k = 0"
```
```  3311     with k have "a \<in> span S"
```
```  3312       apply (simp)
```
```  3313       apply (rule set_rev_mp)
```
```  3314       apply assumption
```
```  3315       apply (rule span_mono)
```
```  3316       apply blast
```
```  3317       done
```
```  3318     with na  have ?thesis by blast}
```
```  3319   moreover
```
```  3320   {assume k0: "k \<noteq> 0"
```
```  3321     have eq: "b = (1/k) *s a - ((1/k) *s a - b)" by vector
```
```  3322     from k0 have eq': "(1/k) *s (a - k*s b) = (1/k) *s a - b"
```
```  3323       by (vector field_simps)
```
```  3324     from k have "(1/k) *s (a - k*s b) \<in> span (S - {b})"
```
```  3325       by (rule span_mul)
```
```  3326     hence th: "(1/k) *s a - b \<in> span (S - {b})"
```
```  3327       unfolding eq' .
```
```  3328
```
```  3329     from k
```
```  3330     have ?thesis
```
```  3331       apply (subst eq)
```
```  3332       apply (rule span_sub)
```
```  3333       apply (rule span_mul)
```
```  3334       apply (rule span_superset)
```
```  3335       apply blast
```
```  3336       apply (rule set_rev_mp)
```
```  3337       apply (rule th)
```
```  3338       apply (rule span_mono)
```
```  3339       using na by blast}
```
```  3340   ultimately show ?thesis by blast
```
```  3341 qed
```
```  3342
```
```  3343 lemma in_span_delete:
```
```  3344   assumes a: "(a::'a::field^'n) \<in> span S"
```
```  3345   and na: "a \<notin> span (S-{b})"
```
```  3346   shows "b \<in> span (insert a (S - {b}))"
```
```  3347   apply (rule in_span_insert)
```
```  3348   apply (rule set_rev_mp)
```
```  3349   apply (rule a)
```
```  3350   apply (rule span_mono)
```
```  3351   apply blast
```
```  3352   apply (rule na)
```
```  3353   done
```
```  3354
```
```  3355 (* Transitivity property. *)
```
```  3356
```
```  3357 lemma span_trans:
```
```  3358   assumes x: "(x::'a::ring_1^'n) \<in> span S" and y: "y \<in> span (insert x S)"
```
```  3359   shows "y \<in> span S"
```
```  3360 proof-
```
```  3361   from span_breakdown[of x "insert x S" y, OF insertI1 y]
```
```  3362   obtain k where k: "y -k*s x \<in> span (S - {x})" by auto
```
```  3363   have eq: "y = (y - k *s x) + k *s x" by vector
```
```  3364   show ?thesis
```
```  3365     apply (subst eq)
```
```  3366     apply (rule span_add)
```
```  3367     apply (rule set_rev_mp)
```
```  3368     apply (rule k)
```
```  3369     apply (rule span_mono)
```
```  3370     apply blast
```
```  3371     apply (rule span_mul)
```
```  3372     by (rule x)
```
```  3373 qed
```
```  3374
```
```  3375 (* ------------------------------------------------------------------------- *)
```
```  3376 (* An explicit expansion is sometimes needed.                                *)
```
```  3377 (* ------------------------------------------------------------------------- *)
```
```  3378
```
```  3379 lemma span_explicit:
```
```  3380   "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}"
```
```  3381   (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
```
```  3382 proof-
```
```  3383   {fix x assume x: "x \<in> ?E"
```
```  3384     then obtain S u where fS: "finite S" and SP: "S\<subseteq>P" and u: "setsum (\<lambda>v. u v *s v) S = x"
```
```  3385       by blast
```
```  3386     have "x \<in> span P"
```
```  3387       unfolding u[symmetric]
```
```  3388       apply (rule span_setsum[OF fS])
```
```  3389       using span_mono[OF SP]
```
```  3390       by (auto intro: span_superset span_mul)}
```
```  3391   moreover
```
```  3392   have "\<forall>x \<in> span P. x \<in> ?E"
```
```  3393     unfolding mem_def Collect_def
```
```  3394   proof(rule span_induct_alt')
```
```  3395     show "?h 0"
```
```  3396       apply (rule exI[where x="{}"]) by simp
```
```  3397   next
```
```  3398     fix c x y
```
```  3399     assume x: "x \<in> P" and hy: "?h y"
```
```  3400     from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P"
```
```  3401       and u: "setsum (\<lambda>v. u v *s v) S = y" by blast
```
```  3402     let ?S = "insert x S"
```
```  3403     let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c)
```
```  3404                   else u y"
```
```  3405     from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P" by blast+
```
```  3406     {assume xS: "x \<in> S"
```
```  3407       have S1: "S = (S - {x}) \<union> {x}"
```
```  3408 	and Sss:"finite (S - {x})" "finite {x}" "(S -{x}) \<inter> {x} = {}" using xS fS by auto
```
```  3409       have "setsum (\<lambda>v. ?u v *s v) ?S =(\<Sum>v\<in>S - {x}. u v *s v) + (u x + c) *s x"
```
```  3410 	using xS
```
```  3411 	by (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]]
```
```  3412 	  setsum_clauses(2)[OF fS] cong del: if_weak_cong)
```
```  3413       also have "\<dots> = (\<Sum>v\<in>S. u v *s v) + c *s x"
```
```  3414 	apply (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]])
```
```  3415 	by (vector ring_simps)
```
```  3416       also have "\<dots> = c*s x + y"
```
```  3417 	by (simp add: add_commute u)
```
```  3418       finally have "setsum (\<lambda>v. ?u v *s v) ?S = c*s x + y" .
```
```  3419     then have "?Q ?S ?u (c*s x + y)" using th0 by blast}
```
```  3420   moreover
```
```  3421   {assume xS: "x \<notin> S"
```
```  3422     have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *s v) = y"
```
```  3423       unfolding u[symmetric]
```
```  3424       apply (rule setsum_cong2)
```
```  3425       using xS by auto
```
```  3426     have "?Q ?S ?u (c*s x + y)" using fS xS th0
```
```  3427       by (simp add: th00 setsum_clauses add_commute cong del: if_weak_cong)}
```
```  3428   ultimately have "?Q ?S ?u (c*s x + y)"
```
```  3429     by (cases "x \<in> S", simp, simp)
```
```  3430     then show "?h (c*s x + y)"
```
```  3431       apply -
```
```  3432       apply (rule exI[where x="?S"])
```
```  3433       apply (rule exI[where x="?u"]) by metis
```
```  3434   qed
```
```  3435   ultimately show ?thesis by blast
```
```  3436 qed
```
```  3437
```
```  3438 lemma dependent_explicit:
```
```  3439   "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")
```
```  3440 proof-
```
```  3441   {assume dP: "dependent P"
```
```  3442     then obtain a S u where aP: "a \<in> P" and fS: "finite S"
```
```  3443       and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *s v) S = a"
```
```  3444       unfolding dependent_def span_explicit by blast
```
```  3445     let ?S = "insert a S"
```
```  3446     let ?u = "\<lambda>y. if y = a then - 1 else u y"
```
```  3447     let ?v = a
```
```  3448     from aP SP have aS: "a \<notin> S" by blast
```
```  3449     from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0" by auto
```
```  3450     have s0: "setsum (\<lambda>v. ?u v *s v) ?S = 0"
```
```  3451       using fS aS
```
```  3452       apply (simp add: vector_smult_lneg vector_smult_lid setsum_clauses ring_simps )
```
```  3453       apply (subst (2) ua[symmetric])
```
```  3454       apply (rule setsum_cong2)
```
```  3455       by auto
```
```  3456     with th0 have ?rhs
```
```  3457       apply -
```
```  3458       apply (rule exI[where x= "?S"])
```
```  3459       apply (rule exI[where x= "?u"])
```
```  3460       by clarsimp}
```
```  3461   moreover
```
```  3462   {fix S u v assume fS: "finite S"
```
```  3463       and SP: "S \<subseteq> P" and vS: "v \<in> S" and uv: "u v \<noteq> 0"
```
```  3464     and u: "setsum (\<lambda>v. u v *s v) S = 0"
```
```  3465     let ?a = v
```
```  3466     let ?S = "S - {v}"
```
```  3467     let ?u = "\<lambda>i. (- u i) / u v"
```
```  3468     have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"       using fS SP vS by auto
```
```  3469     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"
```
```  3470       using fS vS uv
```
```  3471       by (simp add: setsum_diff1 vector_smult_lneg divide_inverse
```
```  3472 	vector_smult_assoc field_simps)
```
```  3473     also have "\<dots> = ?a"
```
```  3474       unfolding setsum_cmul u
```
```  3475       using uv by (simp add: vector_smult_lneg)
```
```  3476     finally  have "setsum (\<lambda>v. ?u v *s v) ?S = ?a" .
```
```  3477     with th0 have ?lhs
```
```  3478       unfolding dependent_def span_explicit
```
```  3479       apply -
```
```  3480       apply (rule bexI[where x= "?a"])
```
```  3481       apply simp_all
```
```  3482       apply (rule exI[where x= "?S"])
```
```  3483       by auto}
```
```  3484   ultimately show ?thesis by blast
```
```  3485 qed
```
```  3486
```
```  3487
```
```  3488 lemma span_finite:
```
```  3489   assumes fS: "finite S"
```
```  3490   shows "span S = {(y::'a::semiring_1^'n). \<exists>u. setsum (\<lambda>v. u v *s v) S = y}"
```
```  3491   (is "_ = ?rhs")
```
```  3492 proof-
```
```  3493   {fix y assume y: "y \<in> span S"
```
```  3494     from y obtain S' u where fS': "finite S'" and SS': "S' \<subseteq> S" and
```
```  3495       u: "setsum (\<lambda>v. u v *s v) S' = y" unfolding span_explicit by blast
```
```  3496     let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
```
```  3497     from setsum_restrict_set[OF fS, of "\<lambda>v. u v *s v" S', symmetric] SS'
```
```  3498     have "setsum (\<lambda>v. ?u v *s v) S = setsum (\<lambda>v. u v *s v) S'"
```
```  3499       unfolding cond_value_iff cond_application_beta
```
```  3500       apply (simp add: cond_value_iff cong del: if_weak_cong)
```
```  3501       apply (rule setsum_cong)
```
```  3502       apply auto
```
```  3503       done
```
```  3504     hence "setsum (\<lambda>v. ?u v *s v) S = y" by (metis u)
```
```  3505     hence "y \<in> ?rhs" by auto}
```
```  3506   moreover
```
```  3507   {fix y u assume u: "setsum (\<lambda>v. u v *s v) S = y"
```
```  3508     then have "y \<in> span S" using fS unfolding span_explicit by auto}
```
```  3509   ultimately show ?thesis by blast
```
```  3510 qed
```
```  3511
```
```  3512
```
```  3513 (* Standard bases are a spanning set, and obviously finite.                  *)
```
```  3514
```
```  3515 lemma span_stdbasis:"span {basis i :: 'a::ring_1^'n::finite | i. i \<in> (UNIV :: 'n set)} = UNIV"
```
```  3516 apply (rule set_ext)
```
```  3517 apply auto
```
```  3518 apply (subst basis_expansion[symmetric])
```
```  3519 apply (rule span_setsum)
```
```  3520 apply simp
```
```  3521 apply auto
```
```  3522 apply (rule span_mul)
```
```  3523 apply (rule span_superset)
```
```  3524 apply (auto simp add: Collect_def mem_def)
```
```  3525 done
```
```  3526
```
```  3527 lemma has_size_stdbasis: "{basis i ::real ^'n::finite | i. i \<in> (UNIV :: 'n set)} hassize CARD('n)" (is "?S hassize ?n")
```
```  3528 proof-
```
```  3529   have eq: "?S = basis ` UNIV" by blast
```
```  3530   show ?thesis unfolding eq
```
```  3531     apply (rule hassize_image_inj[OF basis_inj])
```
```  3532     by (simp add: hassize_def)
```
```  3533 qed
```
```  3534
```
```  3535 lemma finite_stdbasis: "finite {basis i ::real^'n::finite |i. i\<in> (UNIV:: 'n set)}"
```
```  3536   using has_size_stdbasis[unfolded hassize_def]
```
```  3537   ..
```
```  3538
```
```  3539 lemma card_stdbasis: "card {basis i ::real^'n::finite |i. i\<in> (UNIV :: 'n set)} = CARD('n)"
```
```  3540   using has_size_stdbasis[unfolded hassize_def]
```
```  3541   ..
```
```  3542
```
```  3543 lemma independent_stdbasis_lemma:
```
```  3544   assumes x: "(x::'a::semiring_1 ^ 'n) \<in> span (basis ` S)"
```
```  3545   and iS: "i \<notin> S"
```
```  3546   shows "(x\$i) = 0"
```
```  3547 proof-
```
```  3548   let ?U = "UNIV :: 'n set"
```
```  3549   let ?B = "basis ` S"
```
```  3550   let ?P = "\<lambda>(x::'a^'n). \<forall>i\<in> ?U. i \<notin> S \<longrightarrow> x\$i =0"
```
```  3551  {fix x::"'a^'n" assume xS: "x\<in> ?B"
```
```  3552    from xS have "?P x" by auto}
```
```  3553  moreover
```
```  3554  have "subspace ?P"
```
```  3555    by (auto simp add: subspace_def Collect_def mem_def)
```
```  3556  ultimately show ?thesis
```
```  3557    using x span_induct[of ?B ?P x] iS by blast
```
```  3558 qed
```
```  3559
```
```  3560 lemma independent_stdbasis: "independent {basis i ::real^'n::finite |i. i\<in> (UNIV :: 'n set)}"
```
```  3561 proof-
```
```  3562   let ?I = "UNIV :: 'n set"
```
```  3563   let ?b = "basis :: _ \<Rightarrow> real ^'n"
```
```  3564   let ?B = "?b ` ?I"
```
```  3565   have eq: "{?b i|i. i \<in> ?I} = ?B"
```
```  3566     by auto
```
```  3567   {assume d: "dependent ?B"
```
```  3568     then obtain k where k: "k \<in> ?I" "?b k \<in> span (?B - {?b k})"
```
```  3569       unfolding dependent_def by auto
```
```  3570     have eq1: "?B - {?b k} = ?B - ?b ` {k}"  by simp
```
```  3571     have eq2: "?B - {?b k} = ?b ` (?I - {k})"
```
```  3572       unfolding eq1
```
```  3573       apply (rule inj_on_image_set_diff[symmetric])
```
```  3574       apply (rule basis_inj) using k(1) by auto
```
```  3575     from k(2) have th0: "?b k \<in> span (?b ` (?I - {k}))" unfolding eq2 .
```
```  3576     from independent_stdbasis_lemma[OF th0, of k, simplified]
```
```  3577     have False by simp}
```
```  3578   then show ?thesis unfolding eq dependent_def ..
```
```  3579 qed
```
```  3580
```
```  3581 (* This is useful for building a basis step-by-step.                         *)
```
```  3582
```
```  3583 lemma independent_insert:
```
```  3584   "independent(insert (a::'a::field ^'n) S) \<longleftrightarrow>
```
```  3585       (if a \<in> S then independent S
```
```  3586                 else independent S \<and> a \<notin> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  3587 proof-
```
```  3588   {assume aS: "a \<in> S"
```
```  3589     hence ?thesis using insert_absorb[OF aS] by simp}
```
```  3590   moreover
```
```  3591   {assume aS: "a \<notin> S"
```
```  3592     {assume i: ?lhs
```
```  3593       then have ?rhs using aS
```
```  3594 	apply simp
```
```  3595 	apply (rule conjI)
```
```  3596 	apply (rule independent_mono)
```
```  3597 	apply assumption
```
```  3598 	apply blast
```
```  3599 	by (simp add: dependent_def)}
```
```  3600     moreover
```
```  3601     {assume i: ?rhs
```
```  3602       have ?lhs using i aS
```
```  3603 	apply simp
```
```  3604 	apply (auto simp add: dependent_def)
```
```  3605 	apply (case_tac "aa = a", auto)
```
```  3606 	apply (subgoal_tac "insert a S - {aa} = insert a (S - {aa})")
```
```  3607 	apply simp
```
```  3608 	apply (subgoal_tac "a \<in> span (insert aa (S - {aa}))")
```
```  3609 	apply (subgoal_tac "insert aa (S - {aa}) = S")
```
```  3610 	apply simp
```
```  3611 	apply blast
```
```  3612 	apply (rule in_span_insert)
```
```  3613 	apply assumption
```
```  3614 	apply blast
```
```  3615 	apply blast
```
```  3616 	done}
```
```  3617     ultimately have ?thesis by blast}
```
```  3618   ultimately show ?thesis by blast
```
```  3619 qed
```
```  3620
```
```  3621 (* The degenerate case of the Exchange Lemma.  *)
```
```  3622
```
```  3623 lemma mem_delete: "x \<in> (A - {a}) \<longleftrightarrow> x \<noteq> a \<and> x \<in> A"
```
```  3624   by blast
```
```  3625
```
```  3626 lemma span_span: "span (span A) = span A"
```
```  3627   unfolding span_def hull_hull ..
```
```  3628
```
```  3629 lemma span_inc: "S \<subseteq> span S"
```
```  3630   by (metis subset_eq span_superset)
```
```  3631
```
```  3632 lemma spanning_subset_independent:
```
```  3633   assumes BA: "B \<subseteq> A" and iA: "independent (A::('a::field ^'n) set)"
```
```  3634   and AsB: "A \<subseteq> span B"
```
```  3635   shows "A = B"
```
```  3636 proof
```
```  3637   from BA show "B \<subseteq> A" .
```
```  3638 next
```
```  3639   from span_mono[OF BA] span_mono[OF AsB]
```
```  3640   have sAB: "span A = span B" unfolding span_span by blast
```
```  3641
```
```  3642   {fix x assume x: "x \<in> A"
```
```  3643     from iA have th0: "x \<notin> span (A - {x})"
```
```  3644       unfolding dependent_def using x by blast
```
```  3645     from x have xsA: "x \<in> span A" by (blast intro: span_superset)
```
```  3646     have "A - {x} \<subseteq> A" by blast
```
```  3647     hence th1:"span (A - {x}) \<subseteq> span A" by (metis span_mono)
```
```  3648     {assume xB: "x \<notin> B"
```
```  3649       from xB BA have "B \<subseteq> A -{x}" by blast
```
```  3650       hence "span B \<subseteq> span (A - {x})" by (metis span_mono)
```
```  3651       with th1 th0 sAB have "x \<notin> span A" by blast
```
```  3652       with x have False by (metis span_superset)}
```
```  3653     then have "x \<in> B" by blast}
```
```  3654   then show "A \<subseteq> B" by blast
```
```  3655 qed
```
```  3656
```
```  3657 (* The general case of the Exchange Lemma, the key to what follows.  *)
```
```  3658
```
```  3659 lemma exchange_lemma:
```
```  3660   assumes f:"finite (t:: ('a::field^'n) set)" and i: "independent s"
```
```  3661   and sp:"s \<subseteq> span t"
```
```  3662   shows "\<exists>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
```
```  3663 using f i sp
```
```  3664 proof(induct c\<equiv>"card(t - s)" arbitrary: s t rule: nat_less_induct)
```
```  3665   fix n:: nat and s t :: "('a ^'n) set"
```
```  3666   assume H: " \<forall>m<n. \<forall>(x:: ('a ^'n) set) xa.
```
```  3667                 finite xa \<longrightarrow>
```
```  3668                 independent x \<longrightarrow>
```
```  3669                 x \<subseteq> span xa \<longrightarrow>
```
```  3670                 m = card (xa - x) \<longrightarrow>
```
```  3671                 (\<exists>t'. (t' hassize card xa) \<and>
```
```  3672                       x \<subseteq> t' \<and> t' \<subseteq> x \<union> xa \<and> x \<subseteq> span t')"
```
```  3673     and ft: "finite t" and s: "independent s" and sp: "s \<subseteq> span t"
```
```  3674     and n: "n = card (t - s)"
```
```  3675   let ?P = "\<lambda>t'. (t' hassize card t) \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
```
```  3676   let ?ths = "\<exists>t'. ?P t'"
```
```  3677   {assume st: "s \<subseteq> t"
```
```  3678     from st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
```
```  3679       by (auto simp add: hassize_def intro: span_superset)}
```
```  3680   moreover
```
```  3681   {assume st: "t \<subseteq> s"
```
```  3682
```
```  3683     from spanning_subset_independent[OF st s sp]
```
```  3684       st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
```
```  3685       by (auto simp add: hassize_def intro: span_superset)}
```
```  3686   moreover
```
```  3687   {assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
```
```  3688     from st(2) obtain b where b: "b \<in> t" "b \<notin> s" by blast
```
```  3689       from b have "t - {b} - s \<subset> t - s" by blast
```
```  3690       then have cardlt: "card (t - {b} - s) < n" using n ft
```
```  3691  	by (auto intro: psubset_card_mono)
```
```  3692       from b ft have ct0: "card t \<noteq> 0" by auto
```
```  3693     {assume stb: "s \<subseteq> span(t -{b})"
```
```  3694       from ft have ftb: "finite (t -{b})" by auto
```
```  3695       from H[rule_format, OF cardlt ftb s stb]
```
```  3696       obtain u where u: "u hassize card (t-{b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u" by blast
```
```  3697       let ?w = "insert b u"
```
```  3698       have th0: "s \<subseteq> insert b u" using u by blast
```
```  3699       from u(3) b have "u \<subseteq> s \<union> t" by blast
```
```  3700       then have th1: "insert b u \<subseteq> s \<union> t" using u b by blast
```
```  3701       have bu: "b \<notin> u" using b u by blast
```
```  3702       from u(1) have fu: "finite u" by (simp add: hassize_def)
```
```  3703       from u(1) ft b have "u hassize (card t - 1)" by auto
```
```  3704       then
```
```  3705       have th2: "insert b u hassize card t"
```
```  3706 	using  card_insert_disjoint[OF fu bu] ct0 by (auto simp add: hassize_def)
```
```  3707       from u(4) have "s \<subseteq> span u" .
```
```  3708       also have "\<dots> \<subseteq> span (insert b u)" apply (rule span_mono) by blast
```
```  3709       finally have th3: "s \<subseteq> span (insert b u)" .      from th0 th1 th2 th3 have th: "?P ?w"  by blast
```
```  3710       from th have ?ths by blast}
```
```  3711     moreover
```
```  3712     {assume stb: "\<not> s \<subseteq> span(t -{b})"
```
```  3713       from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})" by blast
```
```  3714       have ab: "a \<noteq> b" using a b by blast
```
```  3715       have at: "a \<notin> t" using a ab span_superset[of a "t- {b}"] by auto
```
```  3716       have mlt: "card ((insert a (t - {b})) - s) < n"
```
```  3717 	using cardlt ft n  a b by auto
```
```  3718       have ft': "finite (insert a (t - {b}))" using ft by auto
```
```  3719       {fix x assume xs: "x \<in> s"
```
```  3720 	have t: "t \<subseteq> (insert b (insert a (t -{b})))" using b by auto
```
```  3721 	from b(1) have "b \<in> span t" by (simp add: span_superset)
```
```  3722 	have bs: "b \<in> span (insert a (t - {b}))"
```
```  3723 	  by (metis in_span_delete a sp mem_def subset_eq)
```
```  3724 	from xs sp have "x \<in> span t" by blast
```
```  3725 	with span_mono[OF t]
```
```  3726 	have x: "x \<in> span (insert b (insert a (t - {b})))" ..
```
```  3727 	from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))"  .}
```
```  3728       then have sp': "s \<subseteq> span (insert a (t - {b}))" by blast
```
```  3729
```
```  3730       from H[rule_format, OF mlt ft' s sp' refl] obtain u where
```
```  3731 	u: "u hassize card (insert a (t -{b}))" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t -{b})"
```
```  3732 	"s \<subseteq> span u" by blast
```
```  3733       from u a b ft at ct0 have "?P u" by (auto simp add: hassize_def)
```
```  3734       then have ?ths by blast }
```
```  3735     ultimately have ?ths by blast
```
```  3736   }
```
```  3737   ultimately
```
```  3738   show ?ths  by blast
```
```  3739 qed
```
```  3740
```
```  3741 (* This implies corresponding size bounds.                                   *)
```
```  3742
```
```  3743 lemma independent_span_bound:
```
```  3744   assumes f: "finite t" and i: "independent (s::('a::field^'n) set)" and sp:"s \<subseteq> span t"
```
```  3745   shows "finite s \<and> card s \<le> card t"
```
```  3746   by (metis exchange_lemma[OF f i sp] hassize_def finite_subset card_mono)
```
```  3747
```
```  3748 lemma finite_Atleast_Atmost[simp]: "finite {f x |x. x\<in> {(i::'a::finite_intvl_succ) .. j}}"
```
```  3749 proof-
```
```  3750   have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
```
```  3751   show ?thesis unfolding eq
```
```  3752     apply (rule finite_imageI)
```
```  3753     apply (rule finite_intvl)
```
```  3754     done
```
```  3755 qed
```
```  3756
```
```  3757 lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> (UNIV::'a::finite set)}"
```
```  3758 proof-
```
```  3759   have eq: "{f x |x. x\<in> UNIV} = f ` UNIV" by auto
```
```  3760   show ?thesis unfolding eq
```
```  3761     apply (rule finite_imageI)
```
```  3762     apply (rule finite)
```
```  3763     done
```
```  3764 qed
```
```  3765
```
```  3766
```
```  3767 lemma independent_bound:
```
```  3768   fixes S:: "(real^'n::finite) set"
```
```  3769   shows "independent S \<Longrightarrow> finite S \<and> card S <= CARD('n)"
```
```  3770   apply (subst card_stdbasis[symmetric])
```
```  3771   apply (rule independent_span_bound)
```
```  3772   apply (rule finite_Atleast_Atmost_nat)
```
```  3773   apply assumption
```
```  3774   unfolding span_stdbasis
```
```  3775   apply (rule subset_UNIV)
```
```  3776   done
```
```  3777
```
```  3778 lemma dependent_biggerset: "(finite (S::(real ^'n::finite) set) ==> card S > CARD('n)) ==> dependent S"
```
```  3779   by (metis independent_bound not_less)
```
```  3780
```
```  3781 (* Hence we can create a maximal independent subset.                         *)
```
```  3782
```
```  3783 lemma maximal_independent_subset_extend:
```
```  3784   assumes sv: "(S::(real^'n::finite) set) \<subseteq> V" and iS: "independent S"
```
```  3785   shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
```
```  3786   using sv iS
```
```  3787 proof(induct d\<equiv> "CARD('n) - card S" arbitrary: S rule: nat_less_induct)
```
```  3788   fix n and S:: "(real^'n) set"
```
```  3789   assume H: "\<forall>m<n. \<forall>S \<subseteq> V. independent S \<longrightarrow> m = CARD('n) - card S \<longrightarrow>
```
```  3790               (\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B)"
```
```  3791     and sv: "S \<subseteq> V" and i: "independent S" and n: "n = CARD('n) - card S"
```
```  3792   let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
```
```  3793   let ?ths = "\<exists>x. ?P x"
```
```  3794   let ?d = "CARD('n)"
```
```  3795   {assume "V \<subseteq> span S"
```
```  3796     then have ?ths  using sv i by blast }
```
```  3797   moreover
```
```  3798   {assume VS: "\<not> V \<subseteq> span S"
```
```  3799     from VS obtain a where a: "a \<in> V" "a \<notin> span S" by blast
```
```  3800     from a have aS: "a \<notin> S" by (auto simp add: span_superset)
```
```  3801     have th0: "insert a S \<subseteq> V" using a sv by blast
```
```  3802     from independent_insert[of a S]  i a
```
```  3803     have th1: "independent (insert a S)" by auto
```
```  3804     have mlt: "?d - card (insert a S) < n"
```
```  3805       using aS a n independent_bound[OF th1]
```
```  3806       by auto
```
```  3807
```
```  3808     from H[rule_format, OF mlt th0 th1 refl]
```
```  3809     obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B"
```
```  3810       by blast
```
```  3811     from B have "?P B" by auto
```
```  3812     then have ?ths by blast}
```
```  3813   ultimately show ?ths by blast
```
```  3814 qed
```
```  3815
```
```  3816 lemma maximal_independent_subset:
```
```  3817   "\<exists>(B:: (real ^'n::finite) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
```
```  3818   by (metis maximal_independent_subset_extend[of "{}:: (real ^'n) set"] empty_subsetI independent_empty)
```
```  3819
```
```  3820 (* Notion of dimension.                                                      *)
```
```  3821
```
```  3822 definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n))"
```
```  3823
```
```  3824 lemma basis_exists:  "\<exists>B. (B :: (real ^'n::finite) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize dim V)"
```
```  3825 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)"]
```
```  3826 unfolding hassize_def
```
```  3827 using maximal_independent_subset[of V] independent_bound
```
```  3828 by auto
```
```  3829
```
```  3830 (* Consequences of independence or spanning for cardinality.                 *)
```
```  3831
```
```  3832 lemma independent_card_le_dim: "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B \<le> dim V"
```
```  3833 by (metis basis_exists[of V] independent_span_bound[where ?'a=real] hassize_def subset_trans)
```
```  3834
```
```  3835 lemma span_card_ge_dim:  "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
```
```  3836   by (metis basis_exists[of V] independent_span_bound hassize_def subset_trans)
```
```  3837
```
```  3838 lemma basis_card_eq_dim:
```
```  3839   "B \<subseteq> (V:: (real ^'n::finite) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
```
```  3840   by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_mono)
```
```  3841
```
```  3842 lemma dim_unique: "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> B hassize n \<Longrightarrow> dim V = n"
```
```  3843   by (metis basis_card_eq_dim hassize_def)
```
```  3844
```
```  3845 (* More lemmas about dimension.                                              *)
```
```  3846
```
```  3847 lemma dim_univ: "dim (UNIV :: (real^'n::finite) set) = CARD('n)"
```
```  3848   apply (rule dim_unique[of "{basis i |i. i\<in> (UNIV :: 'n set)}"])
```
```  3849   by (auto simp only: span_stdbasis has_size_stdbasis independent_stdbasis)
```
```  3850
```
```  3851 lemma dim_subset:
```
```  3852   "(S:: (real ^'n::finite) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
```
```  3853   using basis_exists[of T] basis_exists[of S]
```
```  3854   by (metis independent_span_bound[where ?'a = real and ?'n = 'n] subset_eq hassize_def)
```
```  3855
```
```  3856 lemma dim_subset_univ: "dim (S:: (real^'n::finite) set) \<le> CARD('n)"
```
```  3857   by (metis dim_subset subset_UNIV dim_univ)
```
```  3858
```
```  3859 (* Converses to those.                                                       *)
```
```  3860
```
```  3861 lemma card_ge_dim_independent:
```
```  3862   assumes BV:"(B::(real ^'n::finite) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
```
```  3863   shows "V \<subseteq> span B"
```
```  3864 proof-
```
```  3865   {fix a assume aV: "a \<in> V"
```
```  3866     {assume aB: "a \<notin> span B"
```
```  3867       then have iaB: "independent (insert a B)" using iB aV  BV by (simp add: independent_insert)
```
```  3868       from aV BV have th0: "insert a B \<subseteq> V" by blast
```
```  3869       from aB have "a \<notin>B" by (auto simp add: span_superset)
```
```  3870       with independent_card_le_dim[OF th0 iaB] dVB  have False by auto}
```
```  3871     then have "a \<in> span B"  by blast}
```
```  3872   then show ?thesis by blast
```
```  3873 qed
```
```  3874
```
```  3875 lemma card_le_dim_spanning:
```
```  3876   assumes BV: "(B:: (real ^'n::finite) set) \<subseteq> V" and VB: "V \<subseteq> span B"
```
```  3877   and fB: "finite B" and dVB: "dim V \<ge> card B"
```
```  3878   shows "independent B"
```
```  3879 proof-
```
```  3880   {fix a assume a: "a \<in> B" "a \<in> span (B -{a})"
```
```  3881     from a fB have c0: "card B \<noteq> 0" by auto
```
```  3882     from a fB have cb: "card (B -{a}) = card B - 1" by auto
```
```  3883     from BV a have th0: "B -{a} \<subseteq> V" by blast
```
```  3884     {fix x assume x: "x \<in> V"
```
```  3885       from a have eq: "insert a (B -{a}) = B" by blast
```
```  3886       from x VB have x': "x \<in> span B" by blast
```
```  3887       from span_trans[OF a(2), unfolded eq, OF x']
```
```  3888       have "x \<in> span (B -{a})" . }
```
```  3889     then have th1: "V \<subseteq> span (B -{a})" by blast
```
```  3890     have th2: "finite (B -{a})" using fB by auto
```
```  3891     from span_card_ge_dim[OF th0 th1 th2]
```
```  3892     have c: "dim V \<le> card (B -{a})" .
```
```  3893     from c c0 dVB cb have False by simp}
```
```  3894   then show ?thesis unfolding dependent_def by blast
```
```  3895 qed
```
```  3896
```
```  3897 lemma card_eq_dim: "(B:: (real ^'n::finite) set) \<subseteq> V \<Longrightarrow> B hassize dim V \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
```
```  3898   by (metis hassize_def order_eq_iff card_le_dim_spanning
```
```  3899     card_ge_dim_independent)
```
```  3900
```
```  3901 (* ------------------------------------------------------------------------- *)
```
```  3902 (* More general size bound lemmas.                                           *)
```
```  3903 (* ------------------------------------------------------------------------- *)
```
```  3904
```
```  3905 lemma independent_bound_general:
```
```  3906   "independent (S:: (real^'n::finite) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
```
```  3907   by (metis independent_card_le_dim independent_bound subset_refl)
```
```  3908
```
```  3909 lemma dependent_biggerset_general: "(finite (S:: (real^'n::finite) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
```
```  3910   using independent_bound_general[of S] by (metis linorder_not_le)
```
```  3911
```
```  3912 lemma dim_span: "dim (span (S:: (real ^'n::finite) set)) = dim S"
```
```  3913 proof-
```
```  3914   have th0: "dim S \<le> dim (span S)"
```
```  3915     by (auto simp add: subset_eq intro: dim_subset span_superset)
```
```  3916   from basis_exists[of S]
```
```  3917   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
```
```  3918   from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
```
```  3919   have bSS: "B \<subseteq> span S" using B(1) by (metis subset_eq span_inc)
```
```  3920   have sssB: "span S \<subseteq> span B" using span_mono[OF B(3)] by (simp add: span_span)
```
```  3921   from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
```
```  3922     using fB(2)  by arith
```
```  3923 qed
```
```  3924
```
```  3925 lemma subset_le_dim: "(S:: (real ^'n::finite) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
```
```  3926   by (metis dim_span dim_subset)
```
```  3927
```
```  3928 lemma span_eq_dim: "span (S:: (real ^'n::finite) set) = span T ==> dim S = dim T"
```
```  3929   by (metis dim_span)
```
```  3930
```
```  3931 lemma spans_image:
```
```  3932   assumes lf: "linear (f::'a::semiring_1^'n \<Rightarrow> _)" and VB: "V \<subseteq> span B"
```
```  3933   shows "f ` V \<subseteq> span (f ` B)"
```
```  3934   unfolding span_linear_image[OF lf]
```
```  3935   by (metis VB image_mono)
```
```  3936
```
```  3937 lemma dim_image_le:
```
```  3938   fixes f :: "real^'n::finite \<Rightarrow> real^'m::finite"
```
```  3939   assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S:: (real ^'n::finite) set)"
```
```  3940 proof-
```
```  3941   from basis_exists[of S] obtain B where
```
```  3942     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
```
```  3943   from B have fB: "finite B" "card B = dim S" unfolding hassize_def by blast+
```
```  3944   have "dim (f ` S) \<le> card (f ` B)"
```
```  3945     apply (rule span_card_ge_dim)
```
```  3946     using lf B fB by (auto simp add: span_linear_image spans_image subset_image_iff)
```
```  3947   also have "\<dots> \<le> dim S" using card_image_le[OF fB(1)] fB by simp
```
```  3948   finally show ?thesis .
```
```  3949 qed
```
```  3950
```
```  3951 (* Relation between bases and injectivity/surjectivity of map.               *)
```
```  3952
```
```  3953 lemma spanning_surjective_image:
```
```  3954   assumes us: "UNIV \<subseteq> span (S:: ('a::semiring_1 ^'n) set)"
```
```  3955   and lf: "linear f" and sf: "surj f"
```
```  3956   shows "UNIV \<subseteq> span (f ` S)"
```
```  3957 proof-
```
```  3958   have "UNIV \<subseteq> f ` UNIV" using sf by (auto simp add: surj_def)
```
```  3959   also have " \<dots> \<subseteq> span (f ` S)" using spans_image[OF lf us] .
```
```  3960 finally show ?thesis .
```
```  3961 qed
```
```  3962
```
```  3963 lemma independent_injective_image:
```
```  3964   assumes iS: "independent (S::('a::semiring_1^'n) set)" and lf: "linear f" and fi: "inj f"
```
```  3965   shows "independent (f ` S)"
```
```  3966 proof-
```
```  3967   {fix a assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
```
```  3968     have eq: "f ` S - {f a} = f ` (S - {a})" using fi
```
```  3969       by (auto simp add: inj_on_def)
```
```  3970     from a have "f a \<in> f ` span (S -{a})"
```
```  3971       unfolding eq span_linear_image[OF lf, of "S - {a}"]  by blast
```
```  3972     hence "a \<in> span (S -{a})" using fi by (auto simp add: inj_on_def)
```
```  3973     with a(1) iS  have False by (simp add: dependent_def) }
```
```  3974   then show ?thesis unfolding dependent_def by blast
```
```  3975 qed
```
```  3976
```
```  3977 (* ------------------------------------------------------------------------- *)
```
```  3978 (* Picking an orthogonal replacement for a spanning set.                     *)
```
```  3979 (* ------------------------------------------------------------------------- *)
```
```  3980     (* FIXME : Move to some general theory ?*)
```
```  3981 definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
```
```  3982
```
```  3983 lemma vector_sub_project_orthogonal: "(b::'a::ordered_field^'n::finite) \<bullet> (x - ((b \<bullet> x) / (b\<bullet>b)) *s b) = 0"
```
```  3984   apply (cases "b = 0", simp)
```
```  3985   apply (simp add: dot_rsub dot_rmult)
```
```  3986   unfolding times_divide_eq_right[symmetric]
```
```  3987   by (simp add: field_simps dot_eq_0)
```
```  3988
```
```  3989 lemma basis_orthogonal:
```
```  3990   fixes B :: "(real ^'n::finite) set"
```
```  3991   assumes fB: "finite B"
```
```  3992   shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
```
```  3993   (is " \<exists>C. ?P B C")
```
```  3994 proof(induct rule: finite_induct[OF fB])
```
```  3995   case 1 thus ?case apply (rule exI[where x="{}"]) by (auto simp add: pairwise_def)
```
```  3996 next
```
```  3997   case (2 a B)
```
```  3998   note fB = `finite B` and aB = `a \<notin> B`
```
```  3999   from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C`
```
```  4000   obtain C where C: "finite C" "card C \<le> card B"
```
```  4001     "span C = span B" "pairwise orthogonal C" by blast
```
```  4002   let ?a = "a - setsum (\<lambda>x. (x\<bullet>a / (x\<bullet>x)) *s x) C"
```
```  4003   let ?C = "insert ?a C"
```
```  4004   from C(1) have fC: "finite ?C" by simp
```
```  4005   from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)" by (simp add: card_insert_if)
```
```  4006   {fix x k
```
```  4007     have th0: "\<And>(a::'b::comm_ring) b c. a - (b - c) = c + (a - b)" by (simp add: ring_simps)
```
```  4008     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"
```
```  4009       apply (simp only: vector_ssub_ldistrib th0)
```
```  4010       apply (rule span_add_eq)
```
```  4011       apply (rule span_mul)
```
```  4012       apply (rule span_setsum[OF C(1)])
```
```  4013       apply clarify
```
```  4014       apply (rule span_mul)
```
```  4015       by (rule span_superset)}
```
```  4016   then have SC: "span ?C = span (insert a B)"
```
```  4017     unfolding expand_set_eq span_breakdown_eq C(3)[symmetric] by auto
```
```  4018   thm pairwise_def
```
```  4019   {fix x y assume xC: "x \<in> ?C" and yC: "y \<in> ?C" and xy: "x \<noteq> y"
```
```  4020     {assume xa: "x = ?a" and ya: "y = ?a"
```
```  4021       have "orthogonal x y" using xa ya xy by blast}
```
```  4022     moreover
```
```  4023     {assume xa: "x = ?a" and ya: "y \<noteq> ?a" "y \<in> C"
```
```  4024       from ya have Cy: "C = insert y (C - {y})" by blast
```
```  4025       have fth: "finite (C - {y})" using C by simp
```
```  4026       have "orthogonal x y"
```
```  4027 	using xa ya
```
```  4028 	unfolding orthogonal_def xa dot_lsub dot_rsub diff_eq_0_iff_eq
```
```  4029 	apply simp
```
```  4030 	apply (subst Cy)
```
```  4031 	using C(1) fth
```
```  4032 	apply (simp only: setsum_clauses)
```
```  4033 	thm dot_ladd
```
```  4034 	apply (auto simp add: dot_ladd dot_radd dot_lmult dot_rmult dot_eq_0 dot_sym[of y a] dot_lsum[OF fth])
```
```  4035 	apply (rule setsum_0')
```
```  4036 	apply clarsimp
```
```  4037 	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
```
```  4038 	by auto}
```
```  4039     moreover
```
```  4040     {assume xa: "x \<noteq> ?a" "x \<in> C" and ya: "y = ?a"
```
```  4041       from xa have Cx: "C = insert x (C - {x})" by blast
```
```  4042       have fth: "finite (C - {x})" using C by simp
```
```  4043       have "orthogonal x y"
```
```  4044 	using xa ya
```
```  4045 	unfolding orthogonal_def ya dot_rsub dot_lsub diff_eq_0_iff_eq
```
```  4046 	apply simp
```
```  4047 	apply (subst Cx)
```
```  4048 	using C(1) fth
```
```  4049 	apply (simp only: setsum_clauses)
```
```  4050 	apply (subst dot_sym[of x])
```
```  4051 	apply (auto simp add: dot_radd dot_rmult dot_eq_0 dot_sym[of x a] dot_rsum[OF fth])
```
```  4052 	apply (rule setsum_0')
```
```  4053 	apply clarsimp
```
```  4054 	apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
```
```  4055 	by auto}
```
```  4056     moreover
```
```  4057     {assume xa: "x \<in> C" and ya: "y \<in> C"
```
```  4058       have "orthogonal x y" using xa ya xy C(4) unfolding pairwise_def by blast}
```
```  4059     ultimately have "orthogonal x y" using xC yC by blast}
```
```  4060   then have CPO: "pairwise orthogonal ?C" unfolding pairwise_def by blast
```
```  4061   from fC cC SC CPO have "?P (insert a B) ?C" by blast
```
```  4062   then show ?case by blast
```
```  4063 qed
```
```  4064
```
```  4065 lemma orthogonal_basis_exists:
```
```  4066   fixes V :: "(real ^'n::finite) set"
```
```  4067   shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (B hassize dim V) \<and> pairwise orthogonal B"
```
```  4068 proof-
```
```  4069   from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "B hassize dim V" by blast
```
```  4070   from B have fB: "finite B" "card B = dim V" by (simp_all add: hassize_def)
```
```  4071   from basis_orthogonal[OF fB(1)] obtain C where
```
```  4072     C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C" by blast
```
```  4073   from C B
```
```  4074   have CSV: "C \<subseteq> span V" by (metis span_inc span_mono subset_trans)
```
```  4075   from span_mono[OF B(3)]  C have SVC: "span V \<subseteq> span C" by (simp add: span_span)
```
```  4076   from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
```
```  4077   have iC: "independent C" by (simp add: dim_span)
```
```  4078   from C fB have "card C \<le> dim V" by simp
```
```  4079   moreover have "dim V \<le> card C" using span_card_ge_dim[OF CSV SVC C(1)]
```
```  4080     by (simp add: dim_span)
```
```  4081   ultimately have CdV: "C hassize dim V" unfolding hassize_def using C(1) by simp
```
```  4082   from C B CSV CdV iC show ?thesis by auto
```
```  4083 qed
```
```  4084
```
```  4085 lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
```
```  4086   by (metis set_eq_subset span_mono span_span span_inc) (* FIXME: slow *)
```
```  4087
```
```  4088 (* ------------------------------------------------------------------------- *)
```
```  4089 (* Low-dimensional subset is in a hyperplane (weak orthogonal complement).   *)
```
```  4090 (* ------------------------------------------------------------------------- *)
```
```  4091
```
```  4092 lemma span_not_univ_orthogonal:
```
```  4093   assumes sU: "span S \<noteq> UNIV"
```
```  4094   shows "\<exists>(a:: real ^'n::finite). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
```
```  4095 proof-
```
```  4096   from sU obtain a where a: "a \<notin> span S" by blast
```
```  4097   from orthogonal_basis_exists obtain B where
```
```  4098     B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "B hassize dim S" "pairwise orthogonal B"
```
```  4099     by blast
```
```  4100   from B have fB: "finite B" "card B = dim S" by (simp_all add: hassize_def)
```
```  4101   from span_mono[OF B(2)] span_mono[OF B(3)]
```
```  4102   have sSB: "span S = span B" by (simp add: span_span)
```
```  4103   let ?a = "a - setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B"
```
```  4104   have "setsum (\<lambda>b. (a\<bullet>b / (b\<bullet>b)) *s b) B \<in> span S"
```
```  4105     unfolding sSB
```
```  4106     apply (rule span_setsum[OF fB(1)])
```
```  4107     apply clarsimp
```
```  4108     apply (rule span_mul)
```
```  4109     by (rule span_superset)
```
```  4110   with a have a0:"?a  \<noteq> 0" by auto
```
```  4111   have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
```
```  4112   proof(rule span_induct')
```
```  4113     show "subspace (\<lambda>x. ?a \<bullet> x = 0)"
```
```  4114       by (auto simp add: subspace_def mem_def dot_radd dot_rmult)
```
```  4115   next
```
```  4116     {fix x assume x: "x \<in> B"
```
```  4117       from x have B': "B = insert x (B - {x})" by blast
```
```  4118       have fth: "finite (B - {x})" using fB by simp
```
```  4119       have "?a \<bullet> x = 0"
```
```  4120 	apply (subst B') using fB fth
```
```  4121 	unfolding setsum_clauses(2)[OF fth]
```
```  4122 	apply simp
```
```  4123 	apply (clarsimp simp add: dot_lsub dot_ladd dot_lmult dot_lsum dot_eq_0)
```
```  4124 	apply (rule setsum_0', rule ballI)
```
```  4125 	unfolding dot_sym
```
```  4126 	by (auto simp add: x field_simps dot_eq_0 intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])}
```
```  4127     then show "\<forall>x \<in> B. ?a \<bullet> x = 0" by blast
```
```  4128   qed
```
```  4129   with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"])
```
```  4130 qed
```
```  4131
```
```  4132 lemma span_not_univ_subset_hyperplane:
```
```  4133   assumes SU: "span S \<noteq> (UNIV ::(real^'n::finite) set)"
```
```  4134   shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
```
```  4135   using span_not_univ_orthogonal[OF SU] by auto
```
```  4136
```
```  4137 lemma lowdim_subset_hyperplane:
```
```  4138   assumes d: "dim S < CARD('n::finite)"
```
```  4139   shows "\<exists>(a::real ^'n::finite). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
```
```  4140 proof-
```
```  4141   {assume "span S = UNIV"
```
```  4142     hence "dim (span S) = dim (UNIV :: (real ^'n) set)" by simp
```
```  4143     hence "dim S = CARD('n)" by (simp add: dim_span dim_univ)
```
```  4144     with d have False by arith}
```
```  4145   hence th: "span S \<noteq> UNIV" by blast
```
```  4146   from span_not_univ_subset_hyperplane[OF th] show ?thesis .
```
```  4147 qed
```
```  4148
```
```  4149 (* We can extend a linear basis-basis injection to the whole set.            *)
```
```  4150
```
```  4151 lemma linear_indep_image_lemma:
```
```  4152   assumes lf: "linear f" and fB: "finite B"
```
```  4153   and ifB: "independent (f ` B)"
```
```  4154   and fi: "inj_on f B" and xsB: "x \<in> span B"
```
```  4155   and fx: "f (x::'a::field^'n) = 0"
```
```  4156   shows "x = 0"
```
```  4157   using fB ifB fi xsB fx
```
```  4158 proof(induct arbitrary: x rule: finite_induct[OF fB])
```
```  4159   case 1 thus ?case by (auto simp add:  span_empty)
```
```  4160 next
```
```  4161   case (2 a b x)
```
```  4162   have fb: "finite b" using "2.prems" by simp
```
```  4163   have th0: "f ` b \<subseteq> f ` (insert a b)"
```
```  4164     apply (rule image_mono) by blast
```
```  4165   from independent_mono[ OF "2.prems"(2) th0]
```
```  4166   have ifb: "independent (f ` b)"  .
```
```  4167   have fib: "inj_on f b"
```
```  4168     apply (rule subset_inj_on [OF "2.prems"(3)])
```
```  4169     by blast
```
```  4170   from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
```
```  4171   obtain k where k: "x - k*s a \<in> span (b -{a})" by blast
```
```  4172   have "f (x - k*s a) \<in> span (f ` b)"
```
```  4173     unfolding span_linear_image[OF lf]
```
```  4174     apply (rule imageI)
```
```  4175     using k span_mono[of "b-{a}" b] by blast
```
```  4176   hence "f x - k*s f a \<in> span (f ` b)"
```
```  4177     by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
```
```  4178   hence th: "-k *s f a \<in> span (f ` b)"
```
```  4179     using "2.prems"(5) by (simp add: vector_smult_lneg)
```
```  4180   {assume k0: "k = 0"
```
```  4181     from k0 k have "x \<in> span (b -{a})" by simp
```
```  4182     then have "x \<in> span b" using span_mono[of "b-{a}" b]
```
```  4183       by blast}
```
```  4184   moreover
```
```  4185   {assume k0: "k \<noteq> 0"
```
```  4186     from span_mul[OF th, of "- 1/ k"] k0
```
```  4187     have th1: "f a \<in> span (f ` b)"
```
```  4188       by (auto simp add: vector_smult_assoc)
```
```  4189     from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
```
```  4190     have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
```
```  4191     from "2.prems"(2)[unfolded dependent_def bex_simps(10), rule_format, of "f a"]
```
```  4192     have "f a \<notin> span (f ` b)" using tha
```
```  4193       using "2.hyps"(2)
```
```  4194       "2.prems"(3) by auto
```
```  4195     with th1 have False by blast
```
```  4196     then have "x \<in> span b" by blast}
```
```  4197   ultimately have xsb: "x \<in> span b" by blast
```
```  4198   from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)]
```
```  4199   show "x = 0" .
```
```  4200 qed
```
```  4201
```
```  4202 (* We can extend a linear mapping from basis.                                *)
```
```  4203
```
```  4204 lemma linear_independent_extend_lemma:
```
```  4205   assumes fi: "finite B" and ib: "independent B"
```
```  4206   shows "\<exists>g. (\<forall>x\<in> span B. \<forall>y\<in> span B. g ((x::'a::field^'n) + y) = g x + g y)
```
```  4207            \<and> (\<forall>x\<in> span B. \<forall>c. g (c*s x) = c *s g x)
```
```  4208            \<and> (\<forall>x\<in> B. g x = f x)"
```
```  4209 using ib fi
```
```  4210 proof(induct rule: finite_induct[OF fi])
```
```  4211   case 1 thus ?case by (auto simp add: span_empty)
```
```  4212 next
```
```  4213   case (2 a b)
```
```  4214   from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
```
```  4215     by (simp_all add: independent_insert)
```
```  4216   from "2.hyps"(3)[OF ibf] obtain g where
```
```  4217     g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
```
```  4218     "\<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
```
```  4219   let ?h = "\<lambda>z. SOME k. (z - k *s a) \<in> span b"
```
```  4220   {fix z assume z: "z \<in> span (insert a b)"
```
```  4221     have th0: "z - ?h z *s a \<in> span b"
```
```  4222       apply (rule someI_ex)
```
```  4223       unfolding span_breakdown_eq[symmetric]
```
```  4224       using z .
```
```  4225     {fix k assume k: "z - k *s a \<in> span b"
```
```  4226       have eq: "z - ?h z *s a - (z - k*s a) = (k - ?h z) *s a"
```
```  4227 	by (simp add: ring_simps vector_sadd_rdistrib[symmetric])
```
```  4228       from span_sub[OF th0 k]
```
```  4229       have khz: "(k - ?h z) *s a \<in> span b" by (simp add: eq)
```
```  4230       {assume "k \<noteq> ?h z" hence k0: "k - ?h z \<noteq> 0" by simp
```
```  4231 	from k0 span_mul[OF khz, of "1 /(k - ?h z)"]
```
```  4232 	have "a \<in> span b" by (simp add: vector_smult_assoc)
```
```  4233 	with "2.prems"(1) "2.hyps"(2) have False
```
```  4234 	  by (auto simp add: dependent_def)}
```
```  4235       then have "k = ?h z" by blast}
```
```  4236     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}
```
```  4237   note h = this
```
```  4238   let ?g = "\<lambda>z. ?h z *s f a + g (z - ?h z *s a)"
```
```  4239   {fix x y assume x: "x \<in> span (insert a b)" and y: "y \<in> span (insert a b)"
```
```  4240     have tha: "\<And>(x::'a^'n) y a k l. (x + y) - (k + l) *s a = (x - k *s a) + (y - l *s a)"
```
```  4241       by (vector ring_simps)
```
```  4242     have addh: "?h (x + y) = ?h x + ?h y"
```
```  4243       apply (rule conjunct2[OF h, rule_format, symmetric])
```
```  4244       apply (rule span_add[OF x y])
```
```  4245       unfolding tha
```
```  4246       by (metis span_add x y conjunct1[OF h, rule_format])
```
```  4247     have "?g (x + y) = ?g x + ?g y"
```
```  4248       unfolding addh tha
```
```  4249       g(1)[rule_format,OF conjunct1[OF h, OF x] conjunct1[OF h, OF y]]
```
```  4250       by (simp add: vector_sadd_rdistrib)}
```
```  4251   moreover
```
```  4252   {fix x:: "'a^'n" and c:: 'a  assume x: "x \<in> span (insert a b)"
```
```  4253     have tha: "\<And>(x::'a^'n) c k a. c *s x - (c * k) *s a = c *s (x - k *s a)"
```
```  4254       by (vector ring_simps)
```
```  4255     have hc: "?h (c *s x) = c * ?h x"
```
```  4256       apply (rule conjunct2[OF h, rule_format, symmetric])
```
```  4257       apply (metis span_mul x)
```
```  4258       by (metis tha span_mul x conjunct1[OF h])
```
```  4259     have "?g (c *s x) = c*s ?g x"
```
```  4260       unfolding hc tha g(2)[rule_format, OF conjunct1[OF h, OF x]]
```
```  4261       by (vector ring_simps)}
```
```  4262   moreover
```
```  4263   {fix x assume x: "x \<in> (insert a b)"
```
```  4264     {assume xa: "x = a"
```
```  4265       have ha1: "1 = ?h a"
```
```  4266 	apply (rule conjunct2[OF h, rule_format])
```
```  4267 	apply (metis span_superset insertI1)
```
```  4268 	using conjunct1[OF h, OF span_superset, OF insertI1]
```
```  4269 	by (auto simp add: span_0)
```
```  4270
```
```  4271       from xa ha1[symmetric] have "?g x = f x"
```
```  4272 	apply simp
```
```  4273 	using g(2)[rule_format, OF span_0, of 0]
```
```  4274 	by simp}
```
```  4275     moreover
```
```  4276     {assume xb: "x \<in> b"
```
```  4277       have h0: "0 = ?h x"
```
```  4278 	apply (rule conjunct2[OF h, rule_format])
```
```  4279 	apply (metis  span_superset insertI1 xb x)
```
```  4280 	apply simp
```
```  4281 	apply (metis span_superset xb)
```
```  4282 	done
```
```  4283       have "?g x = f x"
```
```  4284 	by (simp add: h0[symmetric] g(3)[rule_format, OF xb])}
```
```  4285     ultimately have "?g x = f x" using x by blast }
```
```  4286   ultimately show ?case apply - apply (rule exI[where x="?g"]) by blast
```
```  4287 qed
```
```  4288
```
```  4289 lemma linear_independent_extend:
```
```  4290   assumes iB: "independent (B:: (real ^'n::finite) set)"
```
```  4291   shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
```
```  4292 proof-
```
```  4293   from maximal_independent_subset_extend[of B UNIV] iB
```
```  4294   obtain C where C: "B \<subseteq> C" "independent C" "\<And>x. x \<in> span C" by auto
```
```  4295
```
```  4296   from C(2) independent_bound[of C] linear_independent_extend_lemma[of C f]
```
```  4297   obtain g where g: "(\<forall>x\<in> span C. \<forall>y\<in> span C. g (x + y) = g x + g y)
```
```  4298            \<and> (\<forall>x\<in> span C. \<forall>c. g (c*s x) = c *s g x)
```
```  4299            \<and> (\<forall>x\<in> C. g x = f x)" by blast
```
```  4300   from g show ?thesis unfolding linear_def using C
```
```  4301     apply clarsimp by blast
```
```  4302 qed
```
```  4303
```
```  4304 (* Can construct an isomorphism between spaces of same dimension.            *)
```
```  4305
```
```  4306 lemma card_le_inj: assumes fA: "finite A" and fB: "finite B"
```
```  4307   and c: "card A \<le> card B" shows "(\<exists>f. f ` A \<subseteq> B \<and> inj_on f A)"
```
```  4308 using fB c
```
```  4309 proof(induct arbitrary: B rule: finite_induct[OF fA])
```
```  4310   case 1 thus ?case by simp
```
```  4311 next
```
```  4312   case (2 x s t)
```
```  4313   thus ?case
```
```  4314   proof(induct rule: finite_induct[OF "2.prems"(1)])
```
```  4315     case 1    then show ?case by simp
```
```  4316   next
```
```  4317     case (2 y t)
```
```  4318     from "2.prems"(1,2,5) "2.hyps"(1,2) have cst:"card s \<le> card t" by simp
```
```  4319     from "2.prems"(3) [OF "2.hyps"(1) cst] obtain f where
```
```  4320       f: "f ` s \<subseteq> t \<and> inj_on f s" by blast
```
```  4321     from f "2.prems"(2) "2.hyps"(2) show ?case
```
```  4322       apply -
```
```  4323       apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
```
```  4324       by (auto simp add: inj_on_def)
```
```  4325   qed
```
```  4326 qed
```
```  4327
```
```  4328 lemma card_subset_eq: assumes fB: "finite B" and AB: "A \<subseteq> B" and
```
```  4329   c: "card A = card B"
```
```  4330   shows "A = B"
```
```  4331 proof-
```
```  4332   from fB AB have fA: "finite A" by (auto intro: finite_subset)
```
```  4333   from fA fB have fBA: "finite (B - A)" by auto
```
```  4334   have e: "A \<inter> (B - A) = {}" by blast
```
```  4335   have eq: "A \<union> (B - A) = B" using AB by blast
```
```  4336   from card_Un_disjoint[OF fA fBA e, unfolded eq c]
```
```  4337   have "card (B - A) = 0" by arith
```
```  4338   hence "B - A = {}" unfolding card_eq_0_iff using fA fB by simp
```
```  4339   with AB show "A = B" by blast
```
```  4340 qed
```
```  4341
```
```  4342 lemma subspace_isomorphism:
```
```  4343   assumes s: "subspace (S:: (real ^'n::finite) set)"
```
```  4344   and t: "subspace (T :: (real ^ 'm::finite) set)"
```
```  4345   and d: "dim S = dim T"
```
```  4346   shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
```
```  4347 proof-
```
```  4348   from basis_exists[of S] obtain B where
```
```  4349     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
```
```  4350   from basis_exists[of T] obtain C where
```
```  4351     C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "C hassize dim T" by blast
```
```  4352   from B(4) C(4) card_le_inj[of B C] d obtain f where
```
```  4353     f: "f ` B \<subseteq> C" "inj_on f B" unfolding hassize_def by auto
```
```  4354   from linear_independent_extend[OF B(2)] obtain g where
```
```  4355     g: "linear g" "\<forall>x\<in> B. g x = f x" by blast
```
```  4356   from B(4) have fB: "finite B" by (simp add: hassize_def)
```
```  4357   from C(4) have fC: "finite C" by (simp add: hassize_def)
```
```  4358   from inj_on_iff_eq_card[OF fB, of f] f(2)
```
```  4359   have "card (f ` B) = card B" by simp
```
```  4360   with B(4) C(4) have ceq: "card (f ` B) = card C" using d
```
```  4361     by (simp add: hassize_def)
```
```  4362   have "g ` B = f ` B" using g(2)
```
```  4363     by (auto simp add: image_iff)
```
```  4364   also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
```
```  4365   finally have gBC: "g ` B = C" .
```
```  4366   have gi: "inj_on g B" using f(2) g(2)
```
```  4367     by (auto simp add: inj_on_def)
```
```  4368   note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
```
```  4369   {fix x y assume x: "x \<in> S" and y: "y \<in> S" and gxy:"g x = g y"
```
```  4370     from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B" by blast+
```
```  4371     from gxy have th0: "g (x - y) = 0" by (simp add: linear_sub[OF g(1)])
```
```  4372     have th1: "x - y \<in> span B" using x' y' by (metis span_sub)
```
```  4373     have "x=y" using g0[OF th1 th0] by simp }
```
```  4374   then have giS: "inj_on g S"
```
```  4375     unfolding inj_on_def by blast
```
```  4376   from span_subspace[OF B(1,3) s]
```
```  4377   have "g ` S = span (g ` B)" by (simp add: span_linear_image[OF g(1)])
```
```  4378   also have "\<dots> = span C" unfolding gBC ..
```
```  4379   also have "\<dots> = T" using span_subspace[OF C(1,3) t] .
```
```  4380   finally have gS: "g ` S = T" .
```
```  4381   from g(1) gS giS show ?thesis by blast
```
```  4382 qed
```
```  4383
```
```  4384 (* linear functions are equal on a subspace if they are on a spanning set.   *)
```
```  4385
```
```  4386 lemma subspace_kernel:
```
```  4387   assumes lf: "linear (f::'a::semiring_1 ^'n \<Rightarrow> _)"
```
```  4388   shows "subspace {x. f x = 0}"
```
```  4389 apply (simp add: subspace_def)
```
```  4390 by (simp add: linear_add[OF lf] linear_cmul[OF lf] linear_0[OF lf])
```
```  4391
```
```  4392 lemma linear_eq_0_span:
```
```  4393   assumes lf: "linear f" and f0: "\<forall>x\<in>B. f x = 0"
```
```  4394   shows "\<forall>x \<in> span B. f x = (0::'a::semiring_1 ^'n)"
```
```  4395 proof
```
```  4396   fix x assume x: "x \<in> span B"
```
```  4397   let ?P = "\<lambda>x. f x = 0"
```
```  4398   from subspace_kernel[OF lf] have "subspace ?P" unfolding Collect_def .
```
```  4399   with x f0 span_induct[of B "?P" x] show "f x = 0" by blast
```
```  4400 qed
```
```  4401
```
```  4402 lemma linear_eq_0:
```
```  4403   assumes lf: "linear f" and SB: "S \<subseteq> span B" and f0: "\<forall>x\<in>B. f x = 0"
```
```  4404   shows "\<forall>x \<in> S. f x = (0::'a::semiring_1^'n)"
```
```  4405   by (metis linear_eq_0_span[OF lf] subset_eq SB f0)
```
```  4406
```
```  4407 lemma linear_eq:
```
```  4408   assumes lf: "linear (f::'a::ring_1^'n \<Rightarrow> _)" and lg: "linear g" and S: "S \<subseteq> span B"
```
```  4409   and fg: "\<forall> x\<in> B. f x = g x"
```
```  4410   shows "\<forall>x\<in> S. f x = g x"
```
```  4411 proof-
```
```  4412   let ?h = "\<lambda>x. f x - g x"
```
```  4413   from fg have fg': "\<forall>x\<in> B. ?h x = 0" by simp
```
```  4414   from linear_eq_0[OF linear_compose_sub[OF lf lg] S fg']
```
```  4415   show ?thesis by simp
```
```  4416 qed
```
```  4417
```
```  4418 lemma linear_eq_stdbasis:
```
```  4419   assumes lf: "linear (f::'a::ring_1^'m::finite \<Rightarrow> 'a^'n::finite)" and lg: "linear g"
```
```  4420   and fg: "\<forall>i. f (basis i) = g(basis i)"
```
```  4421   shows "f = g"
```
```  4422 proof-
```
```  4423   let ?U = "UNIV :: 'm set"
```
```  4424   let ?I = "{basis i:: 'a^'m|i. i \<in> ?U}"
```
```  4425   {fix x assume x: "x \<in> (UNIV :: ('a^'m) set)"
```
```  4426     from equalityD2[OF span_stdbasis]
```
```  4427     have IU: " (UNIV :: ('a^'m) set) \<subseteq> span ?I" by blast
```
```  4428     from linear_eq[OF lf lg IU] fg x
```
```  4429     have "f x = g x" unfolding Collect_def  Ball_def mem_def by metis}
```
```  4430   then show ?thesis by (auto intro: ext)
```
```  4431 qed
```
```  4432
```
```  4433 (* Similar results for bilinear functions.                                   *)
```
```  4434
```
```  4435 lemma bilinear_eq:
```
```  4436   assumes bf: "bilinear (f:: 'a::ring^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)"
```
```  4437   and bg: "bilinear g"
```
```  4438   and SB: "S \<subseteq> span B" and TC: "T \<subseteq> span C"
```
```  4439   and fg: "\<forall>x\<in> B. \<forall>y\<in> C. f x y = g x y"
```
```  4440   shows "\<forall>x\<in>S. \<forall>y\<in>T. f x y = g x y "
```
```  4441 proof-
```
```  4442   let ?P = "\<lambda>x. \<forall>y\<in> span C. f x y = g x y"
```
```  4443   from bf bg have sp: "subspace ?P"
```
```  4444     unfolding bilinear_def linear_def subspace_def bf bg
```
```  4445     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])
```
```  4446
```
```  4447   have "\<forall>x \<in> span B. \<forall>y\<in> span C. f x y = g x y"
```
```  4448     apply -
```
```  4449     apply (rule ballI)
```
```  4450     apply (rule span_induct[of B ?P])
```
```  4451     defer
```
```  4452     apply (rule sp)
```
```  4453     apply assumption
```
```  4454     apply (clarsimp simp add: Ball_def)
```
```  4455     apply (rule_tac P="\<lambda>y. f xa y = g xa y" and S=C in span_induct)
```
```  4456     using fg
```
```  4457     apply (auto simp add: subspace_def)
```
```  4458     using bf bg unfolding bilinear_def linear_def
```
```  4459     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])
```
```  4460   then show ?thesis using SB TC by (auto intro: ext)
```
```  4461 qed
```
```  4462
```
```  4463 lemma bilinear_eq_stdbasis:
```
```  4464   assumes bf: "bilinear (f:: 'a::ring_1^'m::finite \<Rightarrow> 'a^'n::finite \<Rightarrow> 'a^'p)"
```
```  4465   and bg: "bilinear g"
```
```  4466   and fg: "\<forall>i j. f (basis i) (basis j) = g (basis i) (basis j)"
```
```  4467   shows "f = g"
```
```  4468 proof-
```
```  4469   from fg have th: "\<forall>x \<in> {basis i| i. i\<in> (UNIV :: 'm set)}. \<forall>y\<in>  {basis j |j. j \<in> (UNIV :: 'n set)}. f x y = g x y" by blast
```
```  4470   from bilinear_eq[OF bf bg equalityD2[OF span_stdbasis] equalityD2[OF span_stdbasis] th] show ?thesis by (blast intro: ext)
```
```  4471 qed
```
```  4472
```
```  4473 (* Detailed theorems about left and right invertibility in general case.     *)
```
```  4474
```
```  4475 lemma left_invertible_transp:
```
```  4476   "(\<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)"
```
```  4477   by (metis matrix_transp_mul transp_mat transp_transp)
```
```  4478
```
```  4479 lemma right_invertible_transp:
```
```  4480   "(\<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)"
```
```  4481   by (metis matrix_transp_mul transp_mat transp_transp)
```
```  4482
```
```  4483 lemma linear_injective_left_inverse:
```
```  4484   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" and fi: "inj f"
```
```  4485   shows "\<exists>g. linear g \<and> g o f = id"
```
```  4486 proof-
```
```  4487   from linear_independent_extend[OF independent_injective_image, OF independent_stdbasis, OF lf fi]
```
```  4488   obtain h:: "real ^'m \<Rightarrow> real ^'n" where h: "linear h" " \<forall>x \<in> f ` {basis i|i. i \<in> (UNIV::'n set)}. h x = inv f x" by blast
```
```  4489   from h(2)
```
```  4490   have th: "\<forall>i. (h \<circ> f) (basis i) = id (basis i)"
```
```  4491     using inv_o_cancel[OF fi, unfolded stupid_ext[symmetric] id_def o_def]
```
```  4492     by auto
```
```  4493
```
```  4494   from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
```
```  4495   have "h o f = id" .
```
```  4496   then show ?thesis using h(1) by blast
```
```  4497 qed
```
```  4498
```
```  4499 lemma linear_surjective_right_inverse:
```
```  4500   assumes lf: "linear (f:: real ^'m::finite \<Rightarrow> real ^'n::finite)" and sf: "surj f"
```
```  4501   shows "\<exists>g. linear g \<and> f o g = id"
```
```  4502 proof-
```
```  4503   from linear_independent_extend[OF independent_stdbasis]
```
```  4504   obtain h:: "real ^'n \<Rightarrow> real ^'m" where
```
```  4505     h: "linear h" "\<forall> x\<in> {basis i| i. i\<in> (UNIV :: 'n set)}. h x = inv f x" by blast
```
```  4506   from h(2)
```
```  4507   have th: "\<forall>i. (f o h) (basis i) = id (basis i)"
```
```  4508     using sf
```
```  4509     apply (auto simp add: surj_iff o_def stupid_ext[symmetric])
```
```  4510     apply (erule_tac x="basis i" in allE)
```
```  4511     by auto
```
```  4512
```
```  4513   from linear_eq_stdbasis[OF linear_compose[OF h(1) lf] linear_id th]
```
```  4514   have "f o h = id" .
```
```  4515   then show ?thesis using h(1) by blast
```
```  4516 qed
```
```  4517
```
```  4518 lemma matrix_left_invertible_injective:
```
```  4519 "(\<exists>B. (B::real^'m^'n) ** (A::real^'n::finite^'m::finite) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
```
```  4520 proof-
```
```  4521   {fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
```
```  4522     from xy have "B*v (A *v x) = B *v (A*v y)" by simp
```
```  4523     hence "x = y"
```
```  4524       unfolding matrix_vector_mul_assoc B matrix_vector_mul_lid .}
```
```  4525   moreover
```
```  4526   {assume A: "\<forall>x y. A *v x = A *v y \<longrightarrow> x = y"
```
```  4527     hence i: "inj (op *v A)" unfolding inj_on_def by auto
```
```  4528     from linear_injective_left_inverse[OF matrix_vector_mul_linear i]
```
```  4529     obtain g where g: "linear g" "g o op *v A = id" by blast
```
```  4530     have "matrix g ** A = mat 1"
```
```  4531       unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
```
```  4532       using g(2) by (simp add: o_def id_def stupid_ext)
```
```  4533     then have "\<exists>B. (B::real ^'m^'n) ** A = mat 1" by blast}
```
```  4534   ultimately show ?thesis by blast
```
```  4535 qed
```
```  4536
```
```  4537 lemma matrix_left_invertible_ker:
```
```  4538   "(\<exists>B. (B::real ^'m::finite^'n::finite) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
```
```  4539   unfolding matrix_left_invertible_injective
```
```  4540   using linear_injective_0[OF matrix_vector_mul_linear, of A]
```
```  4541   by (simp add: inj_on_def)
```
```  4542
```
```  4543 lemma matrix_right_invertible_surjective:
```
```  4544 "(\<exists>B. (A::real^'n::finite^'m::finite) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
```
```  4545 proof-
```
```  4546   {fix B :: "real ^'m^'n"  assume AB: "A ** B = mat 1"
```
```  4547     {fix x :: "real ^ 'm"
```
```  4548       have "A *v (B *v x) = x"
```
```  4549 	by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB)}
```
```  4550     hence "surj (op *v A)" unfolding surj_def by metis }
```
```  4551   moreover
```
```  4552   {assume sf: "surj (op *v A)"
```
```  4553     from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
```
```  4554     obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "op *v A o g = id"
```
```  4555       by blast
```
```  4556
```
```  4557     have "A ** (matrix g) = mat 1"
```
```  4558       unfolding matrix_eq  matrix_vector_mul_lid
```
```  4559 	matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
```
```  4560       using g(2) unfolding o_def stupid_ext[symmetric] id_def
```
```  4561       .
```
```  4562     hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
```
```  4563   }
```
```  4564   ultimately show ?thesis unfolding surj_def by blast
```
```  4565 qed
```
```  4566
```
```  4567 lemma matrix_left_invertible_independent_columns:
```
```  4568   fixes A :: "real^'n::finite^'m::finite"
```
```  4569   shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s column i A) (UNIV :: 'n set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
```
```  4570    (is "?lhs \<longleftrightarrow> ?rhs")
```
```  4571 proof-
```
```  4572   let ?U = "UNIV :: 'n set"
```
```  4573   {assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
```
```  4574     {fix c i assume c: "setsum (\<lambda>i. c i *s column i A) ?U = 0"
```
```  4575       and i: "i \<in> ?U"
```
```  4576       let ?x = "\<chi> i. c i"
```
```  4577       have th0:"A *v ?x = 0"
```
```  4578 	using c
```
```  4579 	unfolding matrix_mult_vsum Cart_eq
```
```  4580 	by auto
```
```  4581       from k[rule_format, OF th0] i
```
```  4582       have "c i = 0" by (vector Cart_eq)}
```
```  4583     hence ?rhs by blast}
```
```  4584   moreover
```
```  4585   {assume H: ?rhs
```
```  4586     {fix x assume x: "A *v x = 0"
```
```  4587       let ?c = "\<lambda>i. ((x\$i ):: real)"
```
```  4588       from H[rule_format, of ?c, unfolded matrix_mult_vsum[symmetric], OF x]
```
```  4589       have "x = 0" by vector}}
```
```  4590   ultimately show ?thesis unfolding matrix_left_invertible_ker by blast
```
```  4591 qed
```
```  4592
```
```  4593 lemma matrix_right_invertible_independent_rows:
```
```  4594   fixes A :: "real^'n::finite^'m::finite"
```
```  4595   shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s row i A) (UNIV :: 'm set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
```
```  4596   unfolding left_invertible_transp[symmetric]
```
```  4597     matrix_left_invertible_independent_columns
```
```  4598   by (simp add: column_transp)
```
```  4599
```
```  4600 lemma matrix_right_invertible_span_columns:
```
```  4601   "(\<exists>(B::real ^'n::finite^'m::finite). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow> span (columns A) = UNIV" (is "?lhs = ?rhs")
```
```  4602 proof-
```
```  4603   let ?U = "UNIV :: 'm set"
```
```  4604   have fU: "finite ?U" by simp
```
```  4605   have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). setsum (\<lambda>i. (x\$i) *s column i A) ?U = y)"
```
```  4606     unfolding matrix_right_invertible_surjective matrix_mult_vsum surj_def
```
```  4607     apply (subst eq_commute) ..
```
```  4608   have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
```
```  4609   {assume h: ?lhs
```
```  4610     {fix x:: "real ^'n"
```
```  4611 	from h[unfolded lhseq, rule_format, of x] obtain y:: "real ^'m"
```
```  4612 	  where y: "setsum (\<lambda>i. (y\$i) *s column i A) ?U = x" by blast
```
```  4613 	have "x \<in> span (columns A)"
```
```  4614 	  unfolding y[symmetric]
```
```  4615 	  apply (rule span_setsum[OF fU])
```
```  4616 	  apply clarify
```
```  4617 	  apply (rule span_mul)
```
```  4618 	  apply (rule span_superset)
```
```  4619 	  unfolding columns_def
```
```  4620 	  by blast}
```
```  4621     then have ?rhs unfolding rhseq by blast}
```
```  4622   moreover
```
```  4623   {assume h:?rhs
```
```  4624     let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). setsum (\<lambda>i. (x\$i) *s column i A) ?U = y"
```
```  4625     {fix y have "?P y"
```
```  4626       proof(rule span_induct_alt[of ?P "columns A"])
```
```  4627 	show "\<exists>x\<Colon>real ^ 'm. setsum (\<lambda>i. (x\$i) *s column i A) ?U = 0"
```
```  4628 	  apply (rule exI[where x=0])
```
```  4629 	  by (simp add: zero_index vector_smult_lzero)
```
```  4630       next
```
```  4631 	fix c y1 y2 assume y1: "y1 \<in> columns A" and y2: "?P y2"
```
```  4632 	from y1 obtain i where i: "i \<in> ?U" "y1 = column i A"
```
```  4633 	  unfolding columns_def by blast
```
```  4634 	from y2 obtain x:: "real ^'m" where
```
```  4635 	  x: "setsum (\<lambda>i. (x\$i) *s column i A) ?U = y2" by blast
```
```  4636 	let ?x = "(\<chi> j. if j = i then c + (x\$i) else (x\$j))::real^'m"
```
```  4637 	show "?P (c*s y1 + y2)"
```
```  4638 	  proof(rule exI[where x= "?x"], vector, auto simp add: i x[symmetric] cond_value_iff right_distrib cond_application_beta cong del: if_weak_cong)
```
```  4639 	    fix j
```
```  4640 	    have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x\$i)) * ((column xa A)\$j)
```
```  4641            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)
```
```  4642 	      by (simp add: ring_simps)
```
```  4643 	    have "setsum (\<lambda>xa. if xa = i then (c + (x\$i)) * ((column xa A)\$j)
```
```  4644            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"
```
```  4645 	      apply (rule setsum_cong[OF refl])
```
```  4646 	      using th by blast
```
```  4647 	    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"
```
```  4648 	      by (simp add: setsum_addf)
```
```  4649 	    also have "\<dots> = c * ((column i A)\$j) + setsum (\<lambda>xa. ((x\$xa) * ((column xa A)\$j))) ?U"
```
```  4650 	      unfolding setsum_delta[OF fU]
```
```  4651 	      using i(1) by simp
```
```  4652 	    finally show "setsum (\<lambda>xa. if xa = i then (c + (x\$i)) * ((column xa A)\$j)
```
```  4653            else (x\$xa) * ((column xa A\$j))) ?U = c * ((column i A)\$j) + setsum (\<lambda>xa. ((x\$xa) * ((column xa A)\$j))) ?U" .
```
```  4654 	  qed
```
```  4655 	next
```
```  4656 	  show "y \<in> span (columns A)" unfolding h by blast
```
```  4657 	qed}
```
```  4658     then have ?lhs unfolding lhseq ..}
```
```  4659   ultimately show ?thesis by blast
```
```  4660 qed
```
```  4661
```
```  4662 lemma matrix_left_invertible_span_rows:
```
```  4663   "(\<exists>(B::real^'m::finite^'n::finite). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
```
```  4664   unfolding right_invertible_transp[symmetric]
```
```  4665   unfolding columns_transp[symmetric]
```
```  4666   unfolding matrix_right_invertible_span_columns
```
```  4667  ..
```
```  4668
```
```  4669 (* An injective map real^'n->real^'n is also surjective.                       *)
```
```  4670
```
```  4671 lemma linear_injective_imp_surjective:
```
```  4672   assumes lf: "linear (f:: real ^'n::finite \<Rightarrow> real ^'n)" and fi: "inj f"
```
```  4673   shows "surj f"
```
```  4674 proof-
```
```  4675   let ?U = "UNIV :: (real ^'n) set"
```
```  4676   from basis_exists[of ?U] obtain B
```
```  4677     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U"
```
```  4678     by blast
```
```  4679   from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
```
```  4680   have th: "?U \<subseteq> span (f ` B)"
```
```  4681     apply (rule card_ge_dim_independent)
```
```  4682     apply blast
```
```  4683     apply (rule independent_injective_image[OF B(2) lf fi])
```
```  4684     apply (rule order_eq_refl)
```
```  4685     apply (rule sym)
```
```  4686     unfolding d
```
```  4687     apply (rule card_image)
```
```  4688     apply (rule subset_inj_on[OF fi])
```
```  4689     by blast
```
```  4690   from th show ?thesis
```
```  4691     unfolding span_linear_image[OF lf] surj_def
```
```  4692     using B(3) by blast
```
```  4693 qed
```
```  4694
```
```  4695 (* And vice versa.                                                           *)
```
```  4696
```
```  4697 lemma surjective_iff_injective_gen:
```
```  4698   assumes fS: "finite S" and fT: "finite T" and c: "card S = card T"
```
```  4699   and ST: "f ` S \<subseteq> T"
```
```  4700   shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  4701 proof-
```
```  4702   {assume h: "?lhs"
```
```  4703     {fix x y assume x: "x \<in> S" and y: "y \<in> S" and f: "f x = f y"
```
```  4704       from x fS have S0: "card S \<noteq> 0" by auto
```
```  4705       {assume xy: "x \<noteq> y"
```
```  4706 	have th: "card S \<le> card (f ` (S - {y}))"
```
```  4707 	  unfolding c
```
```  4708 	  apply (rule card_mono)
```
```  4709 	  apply (rule finite_imageI)
```
```  4710 	  using fS apply simp
```
```  4711 	  using h xy x y f unfolding subset_eq image_iff
```
```  4712 	  apply auto
```
```  4713 	  apply (case_tac "xa = f x")
```
```  4714 	  apply (rule bexI[where x=x])
```
```  4715 	  apply auto
```
```  4716 	  done
```
```  4717 	also have " \<dots> \<le> card (S -{y})"
```
```  4718 	  apply (rule card_image_le)
```
```  4719 	  using fS by simp
```
```  4720 	also have "\<dots> \<le> card S - 1" using y fS by simp
```
```  4721 	finally have False  using S0 by arith }
```
```  4722       then have "x = y" by blast}
```
```  4723     then have ?rhs unfolding inj_on_def by blast}
```
```  4724   moreover
```
```  4725   {assume h: ?rhs
```
```  4726     have "f ` S = T"
```
```  4727       apply (rule card_subset_eq[OF fT ST])
```
```  4728       unfolding card_image[OF h] using c .
```
```  4729     then have ?lhs by blast}
```
```  4730   ultimately show ?thesis by blast
```
```  4731 qed
```
```  4732
```
```  4733 lemma linear_surjective_imp_injective:
```
```  4734   assumes lf: "linear (f::real ^'n::finite => real ^'n)" and sf: "surj f"
```
```  4735   shows "inj f"
```
```  4736 proof-
```
```  4737   let ?U = "UNIV :: (real ^'n) set"
```
```  4738   from basis_exists[of ?U] obtain B
```
```  4739     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "B hassize dim ?U"
```
```  4740     by blast
```
```  4741   {fix x assume x: "x \<in> span B" and fx: "f x = 0"
```
```  4742     from B(4) have fB: "finite B" by (simp add: hassize_def)
```
```  4743     from B(4) have d: "dim ?U = card B" by (simp add: hassize_def)
```
```  4744     have fBi: "independent (f ` B)"
```
```  4745       apply (rule card_le_dim_spanning[of "f ` B" ?U])
```
```  4746       apply blast
```
```  4747       using sf B(3)
```
```  4748       unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
```
```  4749       apply blast
```
```  4750       using fB apply (blast intro: finite_imageI)
```
```  4751       unfolding d
```
```  4752       apply (rule card_image_le)
```
```  4753       apply (rule fB)
```
```  4754       done
```
```  4755     have th0: "dim ?U \<le> card (f ` B)"
```
```  4756       apply (rule span_card_ge_dim)
```
```  4757       apply blast
```
```  4758       unfolding span_linear_image[OF lf]
```
```  4759       apply (rule subset_trans[where B = "f ` UNIV"])
```
```  4760       using sf unfolding surj_def apply blast
```
```  4761       apply (rule image_mono)
```
```  4762       apply (rule B(3))
```
```  4763       apply (metis finite_imageI fB)
```
```  4764       done
```
```  4765
```
```  4766     moreover have "card (f ` B) \<le> card B"
```
```  4767       by (rule card_image_le, rule fB)
```
```  4768     ultimately have th1: "card B = card (f ` B)" unfolding d by arith
```
```  4769     have fiB: "inj_on f B"
```
```  4770       unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric] by blast
```
```  4771     from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
```
```  4772     have "x = 0" by blast}
```
```  4773   note th = this
```
```  4774   from th show ?thesis unfolding linear_injective_0[OF lf]
```
```  4775     using B(3) by blast
```
```  4776 qed
```
```  4777
```
```  4778 (* Hence either is enough for isomorphism.                                   *)
```
```  4779
```
```  4780 lemma left_right_inverse_eq:
```
```  4781   assumes fg: "f o g = id" and gh: "g o h = id"
```
```  4782   shows "f = h"
```
```  4783 proof-
```
```  4784   have "f = f o (g o h)" unfolding gh by simp
```
```  4785   also have "\<dots> = (f o g) o h" by (simp add: o_assoc)
```
```  4786   finally show "f = h" unfolding fg by simp
```
```  4787 qed
```
```  4788
```
```  4789 lemma isomorphism_expand:
```
```  4790   "f o g = id \<and> g o f = id \<longleftrightarrow> (\<forall>x. f(g x) = x) \<and> (\<forall>x. g(f x) = x)"
```
```  4791   by (simp add: expand_fun_eq o_def id_def)
```
```  4792
```
```  4793 lemma linear_injective_isomorphism:
```
```  4794   assumes lf: "linear (f :: real^'n::finite \<Rightarrow> real ^'n)" and fi: "inj f"
```
```  4795   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
```
```  4796 unfolding isomorphism_expand[symmetric]
```
```  4797 using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]] linear_injective_left_inverse[OF lf fi]
```
```  4798 by (metis left_right_inverse_eq)
```
```  4799
```
```  4800 lemma linear_surjective_isomorphism:
```
```  4801   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and sf: "surj f"
```
```  4802   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
```
```  4803 unfolding isomorphism_expand[symmetric]
```
```  4804 using linear_surjective_right_inverse[OF lf sf] linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
```
```  4805 by (metis left_right_inverse_eq)
```
```  4806
```
```  4807 (* Left and right inverses are the same for R^N->R^N.                        *)
```
```  4808
```
```  4809 lemma linear_inverse_left:
```
```  4810   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and lf': "linear f'"
```
```  4811   shows "f o f' = id \<longleftrightarrow> f' o f = id"
```
```  4812 proof-
```
```  4813   {fix f f':: "real ^'n \<Rightarrow> real ^'n"
```
```  4814     assume lf: "linear f" "linear f'" and f: "f o f' = id"
```
```  4815     from f have sf: "surj f"
```
```  4816
```
```  4817       apply (auto simp add: o_def stupid_ext[symmetric] id_def surj_def)
```
```  4818       by metis
```
```  4819     from linear_surjective_isomorphism[OF lf(1) sf] lf f
```
```  4820     have "f' o f = id" unfolding stupid_ext[symmetric] o_def id_def
```
```  4821       by metis}
```
```  4822   then show ?thesis using lf lf' by metis
```
```  4823 qed
```
```  4824
```
```  4825 (* Moreover, a one-sided inverse is automatically linear.                    *)
```
```  4826
```
```  4827 lemma left_inverse_linear:
```
```  4828   assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and gf: "g o f = id"
```
```  4829   shows "linear g"
```
```  4830 proof-
```
```  4831   from gf have fi: "inj f" apply (auto simp add: inj_on_def o_def id_def stupid_ext[symmetric])
```
```  4832     by metis
```
```  4833   from linear_injective_isomorphism[OF lf fi]
```
```  4834   obtain h:: "real ^'n \<Rightarrow> real ^'n" where
```
```  4835     h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
```
```  4836   have "h = g" apply (rule ext) using gf h(2,3)
```
```  4837     apply (simp add: o_def id_def stupid_ext[symmetric])
```
```  4838     by metis
```
```  4839   with h(1) show ?thesis by blast
```
```  4840 qed
```
```  4841
```
```  4842 lemma right_inverse_linear:
```
```  4843   assumes lf: "linear (f:: real ^'n::finite \<Rightarrow> real ^'n)" and gf: "f o g = id"
```
```  4844   shows "linear g"
```
```  4845 proof-
```
```  4846   from gf have fi: "surj f" apply (auto simp add: surj_def o_def id_def stupid_ext[symmetric])
```
```  4847     by metis
```
```  4848   from linear_surjective_isomorphism[OF lf fi]
```
```  4849   obtain h:: "real ^'n \<Rightarrow> real ^'n" where
```
```  4850     h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
```
```  4851   have "h = g" apply (rule ext) using gf h(2,3)
```
```  4852     apply (simp add: o_def id_def stupid_ext[symmetric])
```
```  4853     by metis
```
```  4854   with h(1) show ?thesis by blast
```
```  4855 qed
```
```  4856
```
```  4857 (* The same result in terms of square matrices.                              *)
```
```  4858
```
```  4859 lemma matrix_left_right_inverse:
```
```  4860   fixes A A' :: "real ^'n::finite^'n"
```
```  4861   shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
```
```  4862 proof-
```
```  4863   {fix A A' :: "real ^'n^'n" assume AA': "A ** A' = mat 1"
```
```  4864     have sA: "surj (op *v A)"
```
```  4865       unfolding surj_def
```
```  4866       apply clarify
```
```  4867       apply (rule_tac x="(A' *v y)" in exI)
```
```  4868       by (simp add: matrix_vector_mul_assoc AA' matrix_vector_mul_lid)
```
```  4869     from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
```
```  4870     obtain f' :: "real ^'n \<Rightarrow> real ^'n"
```
```  4871       where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
```
```  4872     have th: "matrix f' ** A = mat 1"
```
```  4873       by (simp add: matrix_eq matrix_works[OF f'(1)] matrix_vector_mul_assoc[symmetric] matrix_vector_mul_lid f'(2)[rule_format])
```
```  4874     hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
```
```  4875     hence "matrix f' = A'" by (simp add: matrix_mul_assoc[symmetric] AA' matrix_mul_rid matrix_mul_lid)
```
```  4876     hence "matrix f' ** A = A' ** A" by simp
```
```  4877     hence "A' ** A = mat 1" by (simp add: th)}
```
```  4878   then show ?thesis by blast
```
```  4879 qed
```
```  4880
```
```  4881 (* Considering an n-element vector as an n-by-1 or 1-by-n matrix.            *)
```
```  4882
```
```  4883 definition "rowvector v = (\<chi> i j. (v\$j))"
```
```  4884
```
```  4885 definition "columnvector v = (\<chi> i j. (v\$i))"
```
```  4886
```
```  4887 lemma transp_columnvector:
```
```  4888  "transp(columnvector v) = rowvector v"
```
```  4889   by (simp add: transp_def rowvector_def columnvector_def Cart_eq)
```
```  4890
```
```  4891 lemma transp_rowvector: "transp(rowvector v) = columnvector v"
```
```  4892   by (simp add: transp_def columnvector_def rowvector_def Cart_eq)
```
```  4893
```
```  4894 lemma dot_rowvector_columnvector:
```
```  4895   "columnvector (A *v v) = A ** columnvector v"
```
```  4896   by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
```
```  4897
```
```  4898 lemma dot_matrix_product: "(x::'a::semiring_1^'n::finite) \<bullet> y = (((rowvector x ::'a^'n^1) ** (columnvector y :: 'a^1^'n))\$1)\$1"
```
```  4899   by (vector matrix_matrix_mult_def rowvector_def columnvector_def dot_def)
```
```  4900
```
```  4901 lemma dot_matrix_vector_mul:
```
```  4902   fixes A B :: "real ^'n::finite ^'n" and x y :: "real ^'n"
```
```  4903   shows "(A *v x) \<bullet> (B *v y) =
```
```  4904       (((rowvector x :: real^'n^1) ** ((transp A ** B) ** (columnvector y :: real ^1^'n)))\$1)\$1"
```
```  4905 unfolding dot_matrix_product transp_columnvector[symmetric]
```
```  4906   dot_rowvector_columnvector matrix_transp_mul matrix_mul_assoc ..
```
```  4907
```
```  4908 (* Infinity norm.                                                            *)
```
```  4909
```
```  4910 definition "infnorm (x::real^'n::finite) = rsup {abs(x\$i) |i. i\<in> (UNIV :: 'n set)}"
```
```  4911
```
```  4912 lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> (UNIV :: 'n set)"
```
```  4913   by auto
```
```  4914
```
```  4915 lemma infnorm_set_image:
```
```  4916   "{abs(x\$i) |i. i\<in> (UNIV :: 'n set)} =
```
```  4917   (\<lambda>i. abs(x\$i)) ` (UNIV :: 'n set)" by blast
```
```  4918
```
```  4919 lemma infnorm_set_lemma:
```
```  4920   shows "finite {abs((x::'a::abs ^'n::finite)\$i) |i. i\<in> (UNIV :: 'n set)}"
```
```  4921   and "{abs(x\$i) |i. i\<in> (UNIV :: 'n::finite set)} \<noteq> {}"
```
```  4922   unfolding infnorm_set_image
```
```  4923   by (auto intro: finite_imageI)
```
```  4924
```
```  4925 lemma infnorm_pos_le: "0 \<le> infnorm (x::real^'n::finite)"
```
```  4926   unfolding infnorm_def
```
```  4927   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
```
```  4928   unfolding infnorm_set_image
```
```  4929   by auto
```
```  4930
```
```  4931 lemma infnorm_triangle: "infnorm ((x::real^'n::finite) + y) \<le> infnorm x + infnorm y"
```
```  4932 proof-
```
```  4933   have th: "\<And>x y (z::real). x - y <= z \<longleftrightarrow> x - z <= y" by arith
```
```  4934   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
```
```  4935   have th2: "\<And>x (y::real). abs(x + y) - abs(x) <= abs(y)" by arith
```
```  4936   show ?thesis
```
```  4937   unfolding infnorm_def
```
```  4938   unfolding rsup_finite_le_iff[ OF infnorm_set_lemma]
```
```  4939   apply (subst diff_le_eq[symmetric])
```
```  4940   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
```
```  4941   unfolding infnorm_set_image bex_simps
```
```  4942   apply (subst th)
```
```  4943   unfolding th1
```
```  4944   unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
```
```  4945
```
```  4946   unfolding infnorm_set_image ball_simps bex_simps
```
```  4947   apply simp
```
```  4948   apply (metis th2)
```
```  4949   done
```
```  4950 qed
```
```  4951
```
```  4952 lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::real ^'n::finite) = 0"
```
```  4953 proof-
```
```  4954   have "infnorm x <= 0 \<longleftrightarrow> x = 0"
```
```  4955     unfolding infnorm_def
```
```  4956     unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
```
```  4957     unfolding infnorm_set_image ball_simps
```
```  4958     by vector
```
```  4959   then show ?thesis using infnorm_pos_le[of x] by simp
```
```  4960 qed
```
```  4961
```
```  4962 lemma infnorm_0: "infnorm 0 = 0"
```
```  4963   by (simp add: infnorm_eq_0)
```
```  4964
```
```  4965 lemma infnorm_neg: "infnorm (- x) = infnorm x"
```
```  4966   unfolding infnorm_def
```
```  4967   apply (rule cong[of "rsup" "rsup"])
```
```  4968   apply blast
```
```  4969   apply (rule set_ext)
```
```  4970   apply auto
```
```  4971   done
```
```  4972
```
```  4973 lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
```
```  4974 proof-
```
```  4975   have "y - x = - (x - y)" by simp
```
```  4976   then show ?thesis  by (metis infnorm_neg)
```
```  4977 qed
```
```  4978
```
```  4979 lemma real_abs_sub_infnorm: "\<bar> infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
```
```  4980 proof-
```
```  4981   have th: "\<And>(nx::real) n ny. nx <= n + ny \<Longrightarrow> ny <= n + nx ==> \<bar>nx - ny\<bar> <= n"
```
```  4982     by arith
```
```  4983   from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
```
```  4984   have ths: "infnorm x \<le> infnorm (x - y) + infnorm y"
```
```  4985     "infnorm y \<le> infnorm (x - y) + infnorm x"
```
```  4986     by (simp_all add: ring_simps infnorm_neg diff_def[symmetric])
```
```  4987   from th[OF ths]  show ?thesis .
```
```  4988 qed
```
```  4989
```
```  4990 lemma real_abs_infnorm: " \<bar>infnorm x\<bar> = infnorm x"
```
```  4991   using infnorm_pos_le[of x] by arith
```
```  4992
```
```  4993 lemma component_le_infnorm:
```
```  4994   shows "\<bar>x\$i\<bar> \<le> infnorm (x::real^'n::finite)"
```
```  4995 proof-
```
```  4996   let ?U = "UNIV :: 'n set"
```
```  4997   let ?S = "{\<bar>x\$i\<bar> |i. i\<in> ?U}"
```
```  4998   have fS: "finite ?S" unfolding image_Collect[symmetric]
```
```  4999     apply (rule finite_imageI) unfolding Collect_def mem_def by simp
```
```  5000   have S0: "?S \<noteq> {}" by blast
```
```  5001   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
```
```  5002   from rsup_finite_in[OF fS S0] rsup_finite_Ub[OF fS S0]
```
```  5003   show ?thesis unfolding infnorm_def isUb_def setle_def
```
```  5004     unfolding infnorm_set_image ball_simps by auto
```
```  5005 qed
```
```  5006
```
```  5007 lemma infnorm_mul_lemma: "infnorm(a *s x) <= \<bar>a\<bar> * infnorm x"
```
```  5008   apply (subst infnorm_def)
```
```  5009   unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
```
```  5010   unfolding infnorm_set_image ball_simps
```
```  5011   apply (simp add: abs_mult)
```
```  5012   apply (rule allI)
```
```  5013   apply (cut_tac component_le_infnorm[of x])
```
```  5014   apply (rule mult_mono)
```
```  5015   apply auto
```
```  5016   done
```
```  5017
```
```  5018 lemma infnorm_mul: "infnorm(a *s x) = abs a * infnorm x"
```
```  5019 proof-
```
```  5020   {assume a0: "a = 0" hence ?thesis by (simp add: infnorm_0) }
```
```  5021   moreover
```
```  5022   {assume a0: "a \<noteq> 0"
```
```  5023     from a0 have th: "(1/a) *s (a *s x) = x"
```
```  5024       by (simp add: vector_smult_assoc)
```
```  5025     from a0 have ap: "\<bar>a\<bar> > 0" by arith
```
```  5026     from infnorm_mul_lemma[of "1/a" "a *s x"]
```
```  5027     have "infnorm x \<le> 1/\<bar>a\<bar> * infnorm (a*s x)"
```
```  5028       unfolding th by simp
```
```  5029     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)
```
```  5030     then have "\<bar>a\<bar> * infnorm x \<le> infnorm (a*s x)"
```
```  5031       using ap by (simp add: field_simps)
```
```  5032     with infnorm_mul_lemma[of a x] have ?thesis by arith }
```
```  5033   ultimately show ?thesis by blast
```
```  5034 qed
```
```  5035
```
```  5036 lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
```
```  5037   using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
```
```  5038
```
```  5039 (* Prove that it differs only up to a bound from Euclidean norm.             *)
```
```  5040
```
```  5041 lemma infnorm_le_norm: "infnorm x \<le> norm x"
```
```  5042   unfolding infnorm_def rsup_finite_le_iff[OF infnorm_set_lemma]
```
```  5043   unfolding infnorm_set_image  ball_simps
```
```  5044   by (metis component_le_norm)
```
```  5045 lemma card_enum: "card {1 .. n} = n" by auto
```
```  5046 lemma norm_le_infnorm: "norm(x) <= sqrt(real CARD('n)) * infnorm(x::real ^'n::finite)"
```
```  5047 proof-
```
```  5048   let ?d = "CARD('n)"
```
```  5049   have "real ?d \<ge> 0" by simp
```
```  5050   hence d2: "(sqrt (real ?d))^2 = real ?d"
```
```  5051     by (auto intro: real_sqrt_pow2)
```
```  5052   have th: "sqrt (real ?d) * infnorm x \<ge> 0"
```
```  5053     by (simp add: zero_le_mult_iff real_sqrt_ge_0_iff infnorm_pos_le)
```
```  5054   have th1: "x\<bullet>x \<le> (sqrt (real ?d) * infnorm x)^2"
```
```  5055     unfolding power_mult_distrib d2
```
```  5056     apply (subst power2_abs[symmetric])
```
```  5057     unfolding real_of_nat_def dot_def power2_eq_square[symmetric]
```
```  5058     apply (subst power2_abs[symmetric])
```
```  5059     apply (rule setsum_bounded)
```
```  5060     apply (rule power_mono)
```
```  5061     unfolding abs_of_nonneg[OF infnorm_pos_le]
```
```  5062     unfolding infnorm_def  rsup_finite_ge_iff[OF infnorm_set_lemma]
```
```  5063     unfolding infnorm_set_image bex_simps
```
```  5064     apply blast
```
```  5065     by (rule abs_ge_zero)
```
```  5066   from real_le_lsqrt[OF dot_pos_le th th1]
```
```  5067   show ?thesis unfolding real_vector_norm_def id_def .
```
```  5068 qed
```
```  5069
```
```  5070 (* Equality in Cauchy-Schwarz and triangle inequalities.                     *)
```
```  5071
```
```  5072 lemma norm_cauchy_schwarz_eq: "(x::real ^'n::finite) \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *s y = norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  5073 proof-
```
```  5074   {assume h: "x = 0"
```
```  5075     hence ?thesis by simp}
```
```  5076   moreover
```
```  5077   {assume h: "y = 0"
```
```  5078     hence ?thesis by simp}
```
```  5079   moreover
```
```  5080   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
```
```  5081     from dot_eq_0[of "norm y *s x - norm x *s y"]
```
```  5082     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)"
```
```  5083       using x y
```
```  5084       unfolding dot_rsub dot_lsub dot_lmult dot_rmult
```
```  5085       unfolding norm_pow_2[symmetric] power2_eq_square diff_eq_0_iff_eq apply (simp add: dot_sym)
```
```  5086       apply (simp add: ring_simps)
```
```  5087       apply metis
```
```  5088       done
```
```  5089     also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" using x y
```
```  5090       by (simp add: ring_simps dot_sym)
```
```  5091     also have "\<dots> \<longleftrightarrow> ?lhs" using x y
```
```  5092       apply simp
```
```  5093       by metis
```
```  5094     finally have ?thesis by blast}
```
```  5095   ultimately show ?thesis by blast
```
```  5096 qed
```
```  5097
```
```  5098 lemma norm_cauchy_schwarz_abs_eq:
```
```  5099   fixes x y :: "real ^ 'n::finite"
```
```  5100   shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
```
```  5101                 norm x *s y = norm y *s x \<or> norm(x) *s y = - norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
```
```  5102 proof-
```
```  5103   have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a" by arith
```
```  5104   have "?rhs \<longleftrightarrow> norm x *s y = norm y *s x \<or> norm (- x) *s y = norm y *s (- x)"
```
```  5105     apply simp by vector
```
```  5106   also have "\<dots> \<longleftrightarrow>(x \<bullet> y = norm x * norm y \<or>
```
```  5107      (-x) \<bullet> y = norm x * norm y)"
```
```  5108     unfolding norm_cauchy_schwarz_eq[symmetric]
```
```  5109     unfolding norm_minus_cancel
```
```  5110       norm_mul by blast
```
```  5111   also have "\<dots> \<longleftrightarrow> ?lhs"
```
```  5112     unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] dot_lneg
```
```  5113     by arith
```
```  5114   finally show ?thesis ..
```
```  5115 qed
```
```  5116
```
```  5117 lemma norm_triangle_eq:
```
```  5118   fixes x y :: "real ^ 'n::finite"
```
```  5119   shows "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *s y = norm y *s x"
```
```  5120 proof-
```
```  5121   {assume x: "x =0 \<or> y =0"
```
```  5122     hence ?thesis by (cases "x=0", simp_all)}
```
```  5123   moreover
```
```  5124   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
```
```  5125     hence "norm x \<noteq> 0" "norm y \<noteq> 0"
```
```  5126       by simp_all
```
```  5127     hence n: "norm x > 0" "norm y > 0"
```
```  5128       using norm_ge_zero[of x] norm_ge_zero[of y]
```
```  5129       by arith+
```
```  5130     have th: "\<And>(a::real) b c. a + b + c \<noteq> 0 ==> (a = b + c \<longleftrightarrow> a^2 = (b + c)^2)" by algebra
```
```  5131     have "norm(x + y) = norm x + norm y \<longleftrightarrow> norm(x + y)^ 2 = (norm x + norm y) ^2"
```
```  5132       apply (rule th) using n norm_ge_zero[of "x + y"]
```
```  5133       by arith
```
```  5134     also have "\<dots> \<longleftrightarrow> norm x *s y = norm y *s x"
```
```  5135       unfolding norm_cauchy_schwarz_eq[symmetric]
```
```  5136       unfolding norm_pow_2 dot_ladd dot_radd
```
```  5137       by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym ring_simps)
```
```  5138     finally have ?thesis .}
```
```  5139   ultimately show ?thesis by blast
```
```  5140 qed
```
```  5141
```
```  5142 (* Collinearity.*)
```
```  5143
```
```  5144 definition "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *s u)"
```
```  5145
```
```  5146 lemma collinear_empty:  "collinear {}" by (simp add: collinear_def)
```
```  5147
```
```  5148 lemma collinear_sing: "collinear {(x::'a::ring_1^'n)}"
```
```  5149   apply (simp add: collinear_def)
```
```  5150   apply (rule exI[where x=0])
```
```  5151   by simp
```
```  5152
```
```  5153 lemma collinear_2: "collinear {(x::'a::ring_1^'n),y}"
```
```  5154   apply (simp add: collinear_def)
```
```  5155   apply (rule exI[where x="x - y"])
```
```  5156   apply auto
```
```  5157   apply (rule exI[where x=0], simp)
```
```  5158   apply (rule exI[where x=1], simp)
```
```  5159   apply (rule exI[where x="- 1"], simp add: vector_sneg_minus1[symmetric])
```
```  5160   apply (rule exI[where x=0], simp)
```
```  5161   done
```
```  5162
```
```  5163 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")
```
```  5164 proof-
```
```  5165   {assume "x=0 \<or> y = 0" hence ?thesis
```
```  5166       by (cases "x = 0", simp_all add: collinear_2 insert_commute)}
```
```  5167   moreover
```
```  5168   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
```
```  5169     {assume h: "?lhs"
```
```  5170       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
```
```  5171       from u[rule_format, of x 0] u[rule_format, of y 0]
```
```  5172       obtain cx and cy where
```
```  5173 	cx: "x = cx*s u" and cy: "y = cy*s u"
```
```  5174 	by auto
```
```  5175       from cx x have cx0: "cx \<noteq> 0" by auto
```
```  5176       from cy y have cy0: "cy \<noteq> 0" by auto
```
```  5177       let ?d = "cy / cx"
```
```  5178       from cx cy cx0 have "y = ?d *s x"
```
```  5179 	by (simp add: vector_smult_assoc)
```
```  5180       hence ?rhs using x y by blast}
```
```  5181     moreover
```
```  5182     {assume h: "?rhs"
```
```  5183       then obtain c where c: "y = c*s x" using x y by blast
```
```  5184       have ?lhs unfolding collinear_def c
```
```  5185 	apply (rule exI[where x=x])
```
```  5186 	apply auto
```
```  5187 	apply (rule exI[where x="- 1"], simp only: vector_smult_lneg vector_smult_lid)
```
```  5188 	apply (rule exI[where x= "-c"], simp only: vector_smult_lneg)
```
```  5189 	apply (rule exI[where x=1], simp)
```
```  5190 	apply (rule exI[where x="1 - c"], simp add: vector_smult_lneg vector_sub_rdistrib)
```
```  5191 	apply (rule exI[where x="c - 1"], simp add: vector_smult_lneg vector_sub_rdistrib)
```
```  5192 	done}
```
```  5193     ultimately have ?thesis by blast}
```
```  5194   ultimately show ?thesis by blast
```
```  5195 qed
```
```  5196
```
```  5197 lemma norm_cauchy_schwarz_equal:
```
```  5198   fixes x y :: "real ^ 'n::finite"
```
```  5199   shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {(0::real^'n),x,y}"
```
```  5200 unfolding norm_cauchy_schwarz_abs_eq
```
```  5201 apply (cases "x=0", simp_all add: collinear_2)
```
```  5202 apply (cases "y=0", simp_all add: collinear_2 insert_commute)
```
```  5203 unfolding collinear_lemma
```
```  5204 apply simp
```
```  5205 apply (subgoal_tac "norm x \<noteq> 0")
```
```  5206 apply (subgoal_tac "norm y \<noteq> 0")
```
```  5207 apply (rule iffI)
```
```  5208 apply (cases "norm x *s y = norm y *s x")
```
```  5209 apply (rule exI[where x="(1/norm x) * norm y"])
```
```  5210 apply (drule sym)
```
```  5211 unfolding vector_smult_assoc[symmetric]
```
```  5212 apply (simp add: vector_smult_assoc field_simps)
```
```  5213 apply (rule exI[where x="(1/norm x) * - norm y"])
```
```  5214 apply clarify
```
```  5215 apply (drule sym)
```
```  5216 unfolding vector_smult_assoc[symmetric]
```
```  5217 apply (simp add: vector_smult_assoc field_simps)
```
```  5218 apply (erule exE)
```
```  5219 apply (erule ssubst)
```
```  5220 unfolding vector_smult_assoc
```
```  5221 unfolding norm_mul
```
```  5222 apply (subgoal_tac "norm x * c = \<bar>c\<bar> * norm x \<or> norm x * c = - \<bar>c\<bar> * norm x")
```
```  5223 apply (case_tac "c <= 0", simp add: ring_simps)
```
```  5224 apply (simp add: ring_simps)
```
```  5225 apply (case_tac "c <= 0", simp add: ring_simps)
```
```  5226 apply (simp add: ring_simps)
```
```  5227 apply simp
```
```  5228 apply simp
```
```  5229 done
```
```  5230
```
```  5231 end
```