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