src/HOL/Library/Euclidean_Space.thy
author huffman
Tue Jun 09 11:12:08 2009 -0700 (2009-06-09)
changeset 31538 16068eb224c0
parent 31518 feaf9071f8b9
parent 31529 689f5dae1f41
child 31566 eff95000aae7
permissions -rw-r--r--
merged
chaieb@29842
     1
(* Title:      Library/Euclidean_Space
chaieb@29842
     2
   Author:     Amine Chaieb, University of Cambridge
chaieb@29842
     3
*)
chaieb@29842
     4
chaieb@29842
     5
header {* (Real) Vectors in Euclidean space, and elementary linear algebra.*}
chaieb@29842
     6
chaieb@29842
     7
theory Euclidean_Space
haftmann@30661
     8
imports
haftmann@30665
     9
  Complex_Main "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
chaieb@29842
    10
  Finite_Cartesian_Product Glbs Infinite_Set Numeral_Type
huffman@30045
    11
  Inner_Product
chaieb@31118
    12
uses "positivstellensatz.ML" ("normarith.ML")
chaieb@29842
    13
begin
chaieb@29842
    14
chaieb@29842
    15
text{* Some common special cases.*}
chaieb@29842
    16
huffman@30582
    17
lemma forall_1: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
huffman@30582
    18
  by (metis num1_eq_iff)
huffman@30582
    19
huffman@30582
    20
lemma exhaust_2:
huffman@30582
    21
  fixes x :: 2 shows "x = 1 \<or> x = 2"
huffman@30582
    22
proof (induct x)
huffman@30582
    23
  case (of_int z)
huffman@30582
    24
  then have "0 <= z" and "z < 2" by simp_all
huffman@30582
    25
  then have "z = 0 | z = 1" by arith
huffman@30582
    26
  then show ?case by auto
chaieb@29842
    27
qed
chaieb@29842
    28
huffman@30582
    29
lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
huffman@30582
    30
  by (metis exhaust_2)
huffman@30582
    31
huffman@30582
    32
lemma exhaust_3:
huffman@30582
    33
  fixes x :: 3 shows "x = 1 \<or> x = 2 \<or> x = 3"
huffman@30582
    34
proof (induct x)
huffman@30582
    35
  case (of_int z)
huffman@30582
    36
  then have "0 <= z" and "z < 3" by simp_all
huffman@30582
    37
  then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
huffman@30582
    38
  then show ?case by auto
chaieb@29842
    39
qed
chaieb@29842
    40
huffman@30582
    41
lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
huffman@30582
    42
  by (metis exhaust_3)
huffman@30582
    43
huffman@30582
    44
lemma UNIV_1: "UNIV = {1::1}"
huffman@30582
    45
  by (auto simp add: num1_eq_iff)
huffman@30582
    46
huffman@30582
    47
lemma UNIV_2: "UNIV = {1::2, 2::2}"
huffman@30582
    48
  using exhaust_2 by auto
huffman@30582
    49
huffman@30582
    50
lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
huffman@30582
    51
  using exhaust_3 by auto
huffman@30582
    52
huffman@30582
    53
lemma setsum_1: "setsum f (UNIV::1 set) = f 1"
huffman@30582
    54
  unfolding UNIV_1 by simp
huffman@30582
    55
huffman@30582
    56
lemma setsum_2: "setsum f (UNIV::2 set) = f 1 + f 2"
huffman@30582
    57
  unfolding UNIV_2 by simp
huffman@30582
    58
huffman@30582
    59
lemma setsum_3: "setsum f (UNIV::3 set) = f 1 + f 2 + f 3"
huffman@30582
    60
  unfolding UNIV_3 by (simp add: add_ac)
chaieb@29842
    61
huffman@29906
    62
subsection{* Basic componentwise operations on vectors. *}
chaieb@29842
    63
chaieb@29842
    64
instantiation "^" :: (plus,type) plus
chaieb@29842
    65
begin
huffman@30489
    66
definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) + (y$i)))"
chaieb@29842
    67
instance ..
chaieb@29842
    68
end
chaieb@29842
    69
chaieb@29842
    70
instantiation "^" :: (times,type) times
chaieb@29842
    71
begin
huffman@30489
    72
  definition vector_mult_def : "op * \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
chaieb@29842
    73
  instance ..
chaieb@29842
    74
end
chaieb@29842
    75
chaieb@29842
    76
instantiation "^" :: (minus,type) minus begin
chaieb@29842
    77
  definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) - (y$i)))"
chaieb@29842
    78
instance ..
chaieb@29842
    79
end
chaieb@29842
    80
chaieb@29842
    81
instantiation "^" :: (uminus,type) uminus begin
chaieb@29842
    82
  definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x$i)))"
chaieb@29842
    83
instance ..
chaieb@29842
    84
end
chaieb@29842
    85
instantiation "^" :: (zero,type) zero begin
huffman@30489
    86
  definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
chaieb@29842
    87
instance ..
chaieb@29842
    88
end
chaieb@29842
    89
chaieb@29842
    90
instantiation "^" :: (one,type) one begin
huffman@30489
    91
  definition vector_one_def : "1 \<equiv> (\<chi> i. 1)"
chaieb@29842
    92
instance ..
chaieb@29842
    93
end
chaieb@29842
    94
chaieb@29842
    95
instantiation "^" :: (ord,type) ord
chaieb@29842
    96
 begin
chaieb@29842
    97
definition vector_less_eq_def:
huffman@30582
    98
  "less_eq (x :: 'a ^'b) y = (ALL i. x$i <= y$i)"
huffman@30582
    99
definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i. x$i < y$i)"
huffman@30489
   100
chaieb@29842
   101
instance by (intro_classes)
chaieb@29842
   102
end
chaieb@29842
   103
huffman@30039
   104
instantiation "^" :: (scaleR, type) scaleR
huffman@30039
   105
begin
huffman@30489
   106
definition vector_scaleR_def: "scaleR = (\<lambda> r x.  (\<chi> i. scaleR r (x$i)))"
huffman@30039
   107
instance ..
huffman@30039
   108
end
huffman@30039
   109
huffman@30039
   110
text{* Also the scalar-vector multiplication. *}
chaieb@29842
   111
himmelma@31275
   112
definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
chaieb@29842
   113
  where "c *s x = (\<chi> i. c * (x$i))"
chaieb@29842
   114
himmelma@31275
   115
text{* Constant Vectors *} 
chaieb@29842
   116
chaieb@29842
   117
definition "vec x = (\<chi> i. x)"
chaieb@29842
   118
chaieb@29842
   119
text{* Dot products. *}
chaieb@29842
   120
chaieb@29842
   121
definition dot :: "'a::{comm_monoid_add, times} ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" (infix "\<bullet>" 70) where
huffman@30582
   122
  "x \<bullet> y = setsum (\<lambda>i. x$i * y$i) UNIV"
huffman@30582
   123
chaieb@29842
   124
lemma dot_1[simp]: "(x::'a::{comm_monoid_add, times}^1) \<bullet> y = (x$1) * (y$1)"
huffman@30582
   125
  by (simp add: dot_def setsum_1)
chaieb@29842
   126
chaieb@29842
   127
lemma dot_2[simp]: "(x::'a::{comm_monoid_add, times}^2) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2)"
huffman@30582
   128
  by (simp add: dot_def setsum_2)
chaieb@29842
   129
chaieb@29842
   130
lemma dot_3[simp]: "(x::'a::{comm_monoid_add, times}^3) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2) + (x$3) * (y$3)"
huffman@30582
   131
  by (simp add: dot_def setsum_3)
chaieb@29842
   132
huffman@29906
   133
subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
chaieb@29842
   134
chaieb@29842
   135
method_setup vector = {*
chaieb@29842
   136
let
huffman@30489
   137
  val ss1 = HOL_basic_ss addsimps [@{thm dot_def}, @{thm setsum_addf} RS sym,
huffman@30489
   138
  @{thm setsum_subtractf} RS sym, @{thm setsum_right_distrib},
chaieb@29842
   139
  @{thm setsum_left_distrib}, @{thm setsum_negf} RS sym]
huffman@30489
   140
  val ss2 = @{simpset} addsimps
huffman@30489
   141
             [@{thm vector_add_def}, @{thm vector_mult_def},
huffman@30489
   142
              @{thm vector_minus_def}, @{thm vector_uminus_def},
huffman@30489
   143
              @{thm vector_one_def}, @{thm vector_zero_def}, @{thm vec_def},
huffman@30039
   144
              @{thm vector_scaleR_def},
huffman@30582
   145
              @{thm Cart_lambda_beta}, @{thm vector_scalar_mult_def}]
huffman@30489
   146
 fun vector_arith_tac ths =
chaieb@29842
   147
   simp_tac ss1
chaieb@29842
   148
   THEN' (fn i => rtac @{thm setsum_cong2} i
huffman@30489
   149
         ORELSE rtac @{thm setsum_0'} i
chaieb@29842
   150
         ORELSE simp_tac (HOL_basic_ss addsimps [@{thm "Cart_eq"}]) i)
chaieb@29842
   151
   (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
chaieb@29842
   152
   THEN' asm_full_simp_tac (ss2 addsimps ths)
chaieb@29842
   153
 in
wenzelm@30549
   154
  Attrib.thms >> (fn ths => K (SIMPLE_METHOD' (vector_arith_tac ths)))
wenzelm@30549
   155
 end
chaieb@29842
   156
*} "Lifts trivial vector statements to real arith statements"
chaieb@29842
   157
chaieb@29842
   158
lemma vec_0[simp]: "vec 0 = 0" by (vector vector_zero_def)
chaieb@29842
   159
lemma vec_1[simp]: "vec 1 = 1" by (vector vector_one_def)
chaieb@29842
   160
chaieb@29842
   161
chaieb@29842
   162
chaieb@29842
   163
text{* Obvious "component-pushing". *}
chaieb@29842
   164
huffman@30582
   165
lemma vec_component [simp]: "(vec x :: 'a ^ 'n)$i = x"
huffman@30489
   166
  by (vector vec_def)
huffman@30489
   167
huffman@30582
   168
lemma vector_add_component [simp]:
huffman@30582
   169
  fixes x y :: "'a::{plus} ^ 'n"
chaieb@29842
   170
  shows "(x + y)$i = x$i + y$i"
huffman@30582
   171
  by vector
huffman@30582
   172
huffman@30582
   173
lemma vector_minus_component [simp]:
huffman@30582
   174
  fixes x y :: "'a::{minus} ^ 'n"
chaieb@29842
   175
  shows "(x - y)$i = x$i - y$i"
huffman@30582
   176
  by vector
huffman@30582
   177
huffman@30582
   178
lemma vector_mult_component [simp]:
huffman@30582
   179
  fixes x y :: "'a::{times} ^ 'n"
chaieb@29842
   180
  shows "(x * y)$i = x$i * y$i"
huffman@30582
   181
  by vector
huffman@30582
   182
huffman@30582
   183
lemma vector_smult_component [simp]:
huffman@30582
   184
  fixes y :: "'a::{times} ^ 'n"
chaieb@29842
   185
  shows "(c *s y)$i = c * (y$i)"
huffman@30582
   186
  by vector
huffman@30582
   187
huffman@30582
   188
lemma vector_uminus_component [simp]:
huffman@30582
   189
  fixes x :: "'a::{uminus} ^ 'n"
chaieb@29842
   190
  shows "(- x)$i = - (x$i)"
huffman@30582
   191
  by vector
huffman@30582
   192
huffman@30582
   193
lemma vector_scaleR_component [simp]:
huffman@30039
   194
  fixes x :: "'a::scaleR ^ 'n"
huffman@30039
   195
  shows "(scaleR r x)$i = scaleR r (x$i)"
huffman@30582
   196
  by vector
huffman@30039
   197
chaieb@29842
   198
lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
chaieb@29842
   199
huffman@30039
   200
lemmas vector_component =
huffman@30039
   201
  vec_component vector_add_component vector_mult_component
huffman@30039
   202
  vector_smult_component vector_minus_component vector_uminus_component
huffman@30039
   203
  vector_scaleR_component cond_component
chaieb@29842
   204
chaieb@29842
   205
subsection {* Some frequently useful arithmetic lemmas over vectors. *}
chaieb@29842
   206
huffman@30489
   207
instance "^" :: (semigroup_add,type) semigroup_add
chaieb@29842
   208
  apply (intro_classes) by (vector add_assoc)
chaieb@29842
   209
chaieb@29842
   210
huffman@30489
   211
instance "^" :: (monoid_add,type) monoid_add
huffman@30489
   212
  apply (intro_classes) by vector+
huffman@30489
   213
huffman@30489
   214
instance "^" :: (group_add,type) group_add
huffman@30489
   215
  apply (intro_classes) by (vector algebra_simps)+
huffman@30489
   216
huffman@30489
   217
instance "^" :: (ab_semigroup_add,type) ab_semigroup_add
chaieb@29842
   218
  apply (intro_classes) by (vector add_commute)
chaieb@29842
   219
chaieb@29842
   220
instance "^" :: (comm_monoid_add,type) comm_monoid_add
chaieb@29842
   221
  apply (intro_classes) by vector
chaieb@29842
   222
huffman@30489
   223
instance "^" :: (ab_group_add,type) ab_group_add
chaieb@29842
   224
  apply (intro_classes) by vector+
chaieb@29842
   225
huffman@30489
   226
instance "^" :: (cancel_semigroup_add,type) cancel_semigroup_add
chaieb@29842
   227
  apply (intro_classes)
chaieb@29842
   228
  by (vector Cart_eq)+
chaieb@29842
   229
chaieb@29842
   230
instance "^" :: (cancel_ab_semigroup_add,type) cancel_ab_semigroup_add
chaieb@29842
   231
  apply (intro_classes)
chaieb@29842
   232
  by (vector Cart_eq)
chaieb@29842
   233
huffman@30039
   234
instance "^" :: (real_vector, type) real_vector
huffman@30039
   235
  by default (vector scaleR_left_distrib scaleR_right_distrib)+
huffman@30039
   236
huffman@30489
   237
instance "^" :: (semigroup_mult,type) semigroup_mult
chaieb@29842
   238
  apply (intro_classes) by (vector mult_assoc)
chaieb@29842
   239
huffman@30489
   240
instance "^" :: (monoid_mult,type) monoid_mult
chaieb@29842
   241
  apply (intro_classes) by vector+
chaieb@29842
   242
huffman@30489
   243
instance "^" :: (ab_semigroup_mult,type) ab_semigroup_mult
chaieb@29842
   244
  apply (intro_classes) by (vector mult_commute)
chaieb@29842
   245
huffman@30489
   246
instance "^" :: (ab_semigroup_idem_mult,type) ab_semigroup_idem_mult
chaieb@29842
   247
  apply (intro_classes) by (vector mult_idem)
chaieb@29842
   248
huffman@30489
   249
instance "^" :: (comm_monoid_mult,type) comm_monoid_mult
chaieb@29842
   250
  apply (intro_classes) by vector
chaieb@29842
   251
chaieb@29842
   252
fun vector_power :: "('a::{one,times} ^'n) \<Rightarrow> nat \<Rightarrow> 'a^'n" where
chaieb@29842
   253
  "vector_power x 0 = 1"
chaieb@29842
   254
  | "vector_power x (Suc n) = x * vector_power x n"
chaieb@29842
   255
chaieb@29842
   256
instance "^" :: (semiring,type) semiring
chaieb@29842
   257
  apply (intro_classes) by (vector ring_simps)+
chaieb@29842
   258
chaieb@29842
   259
instance "^" :: (semiring_0,type) semiring_0
chaieb@29842
   260
  apply (intro_classes) by (vector ring_simps)+
chaieb@29842
   261
instance "^" :: (semiring_1,type) semiring_1
huffman@30582
   262
  apply (intro_classes) by vector
chaieb@29842
   263
instance "^" :: (comm_semiring,type) comm_semiring
chaieb@29842
   264
  apply (intro_classes) by (vector ring_simps)+
chaieb@29842
   265
huffman@30489
   266
instance "^" :: (comm_semiring_0,type) comm_semiring_0 by (intro_classes)
huffman@29905
   267
instance "^" :: (cancel_comm_monoid_add, type) cancel_comm_monoid_add ..
huffman@30489
   268
instance "^" :: (semiring_0_cancel,type) semiring_0_cancel by (intro_classes)
huffman@30489
   269
instance "^" :: (comm_semiring_0_cancel,type) comm_semiring_0_cancel by (intro_classes)
huffman@30489
   270
instance "^" :: (ring,type) ring by (intro_classes)
huffman@30489
   271
instance "^" :: (semiring_1_cancel,type) semiring_1_cancel by (intro_classes)
chaieb@29842
   272
instance "^" :: (comm_semiring_1,type) comm_semiring_1 by (intro_classes)
huffman@30039
   273
huffman@30039
   274
instance "^" :: (ring_1,type) ring_1 ..
huffman@30039
   275
huffman@30039
   276
instance "^" :: (real_algebra,type) real_algebra
huffman@30039
   277
  apply intro_classes
huffman@30039
   278
  apply (simp_all add: vector_scaleR_def ring_simps)
huffman@30039
   279
  apply vector
huffman@30039
   280
  apply vector
huffman@30039
   281
  done
huffman@30039
   282
huffman@30039
   283
instance "^" :: (real_algebra_1,type) real_algebra_1 ..
huffman@30039
   284
huffman@30489
   285
lemma of_nat_index:
huffman@30582
   286
  "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
chaieb@29842
   287
  apply (induct n)
chaieb@29842
   288
  apply vector
chaieb@29842
   289
  apply vector
chaieb@29842
   290
  done
huffman@30489
   291
lemma zero_index[simp]:
huffman@30582
   292
  "(0 :: 'a::zero ^'n)$i = 0" by vector
chaieb@29842
   293
huffman@30489
   294
lemma one_index[simp]:
huffman@30582
   295
  "(1 :: 'a::one ^'n)$i = 1" by vector
chaieb@29842
   296
chaieb@29842
   297
lemma one_plus_of_nat_neq_0: "(1::'a::semiring_char_0) + of_nat n \<noteq> 0"
chaieb@29842
   298
proof-
chaieb@29842
   299
  have "(1::'a) + of_nat n = 0 \<longleftrightarrow> of_nat 1 + of_nat n = (of_nat 0 :: 'a)" by simp
huffman@30489
   300
  also have "\<dots> \<longleftrightarrow> 1 + n = 0" by (simp only: of_nat_add[symmetric] of_nat_eq_iff)
huffman@30489
   301
  finally show ?thesis by simp
chaieb@29842
   302
qed
chaieb@29842
   303
huffman@30489
   304
instance "^" :: (semiring_char_0,type) semiring_char_0
huffman@30489
   305
proof (intro_classes)
chaieb@29842
   306
  fix m n ::nat
chaieb@29842
   307
  show "(of_nat m :: 'a^'b) = of_nat n \<longleftrightarrow> m = n"
huffman@30582
   308
    by (simp add: Cart_eq of_nat_index)
chaieb@29842
   309
qed
chaieb@29842
   310
chaieb@29842
   311
instance "^" :: (comm_ring_1,type) comm_ring_1 by intro_classes
huffman@30039
   312
instance "^" :: (ring_char_0,type) ring_char_0 by intro_classes
chaieb@29842
   313
huffman@30489
   314
lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
chaieb@29842
   315
  by (vector mult_assoc)
huffman@30489
   316
lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
chaieb@29842
   317
  by (vector ring_simps)
huffman@30489
   318
lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
chaieb@29842
   319
  by (vector ring_simps)
chaieb@29842
   320
lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
chaieb@29842
   321
lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
huffman@30489
   322
lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
chaieb@29842
   323
  by (vector ring_simps)
chaieb@29842
   324
lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
chaieb@29842
   325
lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
chaieb@29842
   326
lemma vector_sneg_minus1: "-x = (- (1::'a::ring_1)) *s x" by vector
chaieb@29842
   327
lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
huffman@30489
   328
lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
chaieb@29842
   329
  by (vector ring_simps)
chaieb@29842
   330
huffman@30489
   331
lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
huffman@30582
   332
  by (simp add: Cart_eq)
chaieb@29842
   333
huffman@31493
   334
subsection {* Topological space *}
huffman@31493
   335
huffman@31493
   336
instantiation "^" :: (topological_space, finite) topological_space
huffman@31493
   337
begin
huffman@31493
   338
huffman@31493
   339
definition open_vector_def:
huffman@31493
   340
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@31493
   341
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@31493
   342
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@31493
   343
huffman@31493
   344
instance proof
huffman@31493
   345
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@31493
   346
    unfolding open_vector_def by auto
huffman@31493
   347
next
huffman@31493
   348
  fix S T :: "('a ^ 'b) set"
huffman@31493
   349
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@31493
   350
    unfolding open_vector_def
huffman@31493
   351
    apply clarify
huffman@31493
   352
    apply (drule (1) bspec)+
huffman@31493
   353
    apply (clarify, rename_tac Sa Ta)
huffman@31493
   354
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@31493
   355
    apply (simp add: open_Int)
huffman@31493
   356
    done
huffman@31493
   357
next
huffman@31493
   358
  fix K :: "('a ^ 'b) set set"
huffman@31493
   359
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@31493
   360
    unfolding open_vector_def
huffman@31493
   361
    apply clarify
huffman@31493
   362
    apply (drule (1) bspec)
huffman@31493
   363
    apply (drule (1) bspec)
huffman@31493
   364
    apply clarify
huffman@31493
   365
    apply (rule_tac x=A in exI)
huffman@31493
   366
    apply fast
huffman@31493
   367
    done
huffman@31493
   368
qed
huffman@31493
   369
huffman@31493
   370
end
huffman@31493
   371
huffman@31493
   372
lemma tendsto_Cart_nth:
huffman@31493
   373
  fixes f :: "'a \<Rightarrow> 'b::topological_space ^ 'n::finite"
huffman@31493
   374
  assumes "((\<lambda>x. f x) ---> a) net"
huffman@31493
   375
  shows "((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@31493
   376
proof (rule topological_tendstoI)
huffman@31493
   377
  fix S :: "'b set" assume "open S" "a $ i \<in> S"
huffman@31493
   378
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@31493
   379
    unfolding open_vector_def
huffman@31493
   380
    apply simp_all
huffman@31493
   381
    apply clarify
huffman@31493
   382
    apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI)
huffman@31493
   383
    apply simp
huffman@31493
   384
    done
huffman@31493
   385
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@31493
   386
    by (rule topological_tendstoD)
huffman@31493
   387
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@31493
   388
    by simp
huffman@31493
   389
qed
huffman@31493
   390
huffman@30040
   391
subsection {* Square root of sum of squares *}
huffman@30040
   392
huffman@30040
   393
definition
huffman@30040
   394
  "setL2 f A = sqrt (\<Sum>i\<in>A. (f i)\<twosuperior>)"
huffman@30040
   395
huffman@30040
   396
lemma setL2_cong:
huffman@30040
   397
  "\<lbrakk>A = B; \<And>x. x \<in> B \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
huffman@30040
   398
  unfolding setL2_def by simp
huffman@30040
   399
huffman@30040
   400
lemma strong_setL2_cong:
huffman@30040
   401
  "\<lbrakk>A = B; \<And>x. x \<in> B =simp=> f x = g x\<rbrakk> \<Longrightarrow> setL2 f A = setL2 g B"
huffman@30040
   402
  unfolding setL2_def simp_implies_def by simp
huffman@30040
   403
huffman@30040
   404
lemma setL2_infinite [simp]: "\<not> finite A \<Longrightarrow> setL2 f A = 0"
huffman@30040
   405
  unfolding setL2_def by simp
huffman@30040
   406
huffman@30040
   407
lemma setL2_empty [simp]: "setL2 f {} = 0"
huffman@30040
   408
  unfolding setL2_def by simp
huffman@30040
   409
huffman@30040
   410
lemma setL2_insert [simp]:
huffman@30040
   411
  "\<lbrakk>finite F; a \<notin> F\<rbrakk> \<Longrightarrow>
huffman@30040
   412
    setL2 f (insert a F) = sqrt ((f a)\<twosuperior> + (setL2 f F)\<twosuperior>)"
huffman@30040
   413
  unfolding setL2_def by (simp add: setsum_nonneg)
huffman@30040
   414
huffman@30040
   415
lemma setL2_nonneg [simp]: "0 \<le> setL2 f A"
huffman@30040
   416
  unfolding setL2_def by (simp add: setsum_nonneg)
huffman@30040
   417
huffman@30040
   418
lemma setL2_0': "\<forall>a\<in>A. f a = 0 \<Longrightarrow> setL2 f A = 0"
huffman@30040
   419
  unfolding setL2_def by simp
huffman@30040
   420
huffman@31493
   421
lemma setL2_constant: "setL2 (\<lambda>x. y) A = sqrt (of_nat (card A)) * \<bar>y\<bar>"
huffman@31493
   422
  unfolding setL2_def by (simp add: real_sqrt_mult)
huffman@31493
   423
huffman@30040
   424
lemma setL2_mono:
huffman@30040
   425
  assumes "\<And>i. i \<in> K \<Longrightarrow> f i \<le> g i"
huffman@30040
   426
  assumes "\<And>i. i \<in> K \<Longrightarrow> 0 \<le> f i"
huffman@30040
   427
  shows "setL2 f K \<le> setL2 g K"
huffman@30040
   428
  unfolding setL2_def
huffman@30040
   429
  by (simp add: setsum_nonneg setsum_mono power_mono prems)
huffman@30040
   430
huffman@31493
   431
lemma setL2_strict_mono:
huffman@31493
   432
  assumes "finite K" and "K \<noteq> {}"
huffman@31493
   433
  assumes "\<And>i. i \<in> K \<Longrightarrow> f i < g i"
huffman@31493
   434
  assumes "\<And>i. i \<in> K \<Longrightarrow> 0 \<le> f i"
huffman@31493
   435
  shows "setL2 f K < setL2 g K"
huffman@31493
   436
  unfolding setL2_def
huffman@31493
   437
  by (simp add: setsum_strict_mono power_strict_mono assms)
huffman@31493
   438
huffman@30040
   439
lemma setL2_right_distrib:
huffman@30040
   440
  "0 \<le> r \<Longrightarrow> r * setL2 f A = setL2 (\<lambda>x. r * f x) A"
huffman@30040
   441
  unfolding setL2_def
huffman@30040
   442
  apply (simp add: power_mult_distrib)
huffman@30040
   443
  apply (simp add: setsum_right_distrib [symmetric])
huffman@30040
   444
  apply (simp add: real_sqrt_mult setsum_nonneg)
huffman@30040
   445
  done
huffman@30040
   446
huffman@30040
   447
lemma setL2_left_distrib:
huffman@30040
   448
  "0 \<le> r \<Longrightarrow> setL2 f A * r = setL2 (\<lambda>x. f x * r) A"
huffman@30040
   449
  unfolding setL2_def
huffman@30040
   450
  apply (simp add: power_mult_distrib)
huffman@30040
   451
  apply (simp add: setsum_left_distrib [symmetric])
huffman@30040
   452
  apply (simp add: real_sqrt_mult setsum_nonneg)
huffman@30040
   453
  done
huffman@30040
   454
huffman@30040
   455
lemma setsum_nonneg_eq_0_iff:
huffman@30040
   456
  fixes f :: "'a \<Rightarrow> 'b::pordered_ab_group_add"
huffman@30040
   457
  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)"
huffman@30040
   458
  apply (induct set: finite, simp)
huffman@30040
   459
  apply (simp add: add_nonneg_eq_0_iff setsum_nonneg)
huffman@30040
   460
  done
huffman@30040
   461
huffman@30040
   462
lemma setL2_eq_0_iff: "finite A \<Longrightarrow> setL2 f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
huffman@30040
   463
  unfolding setL2_def
huffman@30040
   464
  by (simp add: setsum_nonneg setsum_nonneg_eq_0_iff)
huffman@30040
   465
huffman@30040
   466
lemma setL2_triangle_ineq:
huffman@30040
   467
  shows "setL2 (\<lambda>i. f i + g i) A \<le> setL2 f A + setL2 g A"
huffman@30040
   468
proof (cases "finite A")
huffman@30040
   469
  case False
huffman@30040
   470
  thus ?thesis by simp
huffman@30040
   471
next
huffman@30040
   472
  case True
huffman@30040
   473
  thus ?thesis
huffman@30040
   474
  proof (induct set: finite)
huffman@30040
   475
    case empty
huffman@30040
   476
    show ?case by simp
huffman@30040
   477
  next
huffman@30040
   478
    case (insert x F)
huffman@30040
   479
    hence "sqrt ((f x + g x)\<twosuperior> + (setL2 (\<lambda>i. f i + g i) F)\<twosuperior>) \<le>
huffman@30040
   480
           sqrt ((f x + g x)\<twosuperior> + (setL2 f F + setL2 g F)\<twosuperior>)"
huffman@30040
   481
      by (intro real_sqrt_le_mono add_left_mono power_mono insert
huffman@30040
   482
                setL2_nonneg add_increasing zero_le_power2)
huffman@30040
   483
    also have
huffman@30040
   484
      "\<dots> \<le> sqrt ((f x)\<twosuperior> + (setL2 f F)\<twosuperior>) + sqrt ((g x)\<twosuperior> + (setL2 g F)\<twosuperior>)"
huffman@30040
   485
      by (rule real_sqrt_sum_squares_triangle_ineq)
huffman@30040
   486
    finally show ?case
huffman@30040
   487
      using insert by simp
huffman@30040
   488
  qed
huffman@30040
   489
qed
huffman@30040
   490
huffman@30040
   491
lemma sqrt_sum_squares_le_sum:
huffman@30040
   492
  "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (x\<twosuperior> + y\<twosuperior>) \<le> x + y"
huffman@30040
   493
  apply (rule power2_le_imp_le)
huffman@30040
   494
  apply (simp add: power2_sum)
huffman@30040
   495
  apply (simp add: mult_nonneg_nonneg)
huffman@30040
   496
  apply (simp add: add_nonneg_nonneg)
huffman@30040
   497
  done
huffman@30040
   498
huffman@30040
   499
lemma setL2_le_setsum [rule_format]:
huffman@30040
   500
  "(\<forall>i\<in>A. 0 \<le> f i) \<longrightarrow> setL2 f A \<le> setsum f A"
huffman@30040
   501
  apply (cases "finite A")
huffman@30040
   502
  apply (induct set: finite)
huffman@30040
   503
  apply simp
huffman@30040
   504
  apply clarsimp
huffman@30040
   505
  apply (erule order_trans [OF sqrt_sum_squares_le_sum])
huffman@30040
   506
  apply simp
huffman@30040
   507
  apply simp
huffman@30040
   508
  apply simp
huffman@30040
   509
  done
huffman@30040
   510
huffman@30040
   511
lemma sqrt_sum_squares_le_sum_abs: "sqrt (x\<twosuperior> + y\<twosuperior>) \<le> \<bar>x\<bar> + \<bar>y\<bar>"
huffman@30040
   512
  apply (rule power2_le_imp_le)
huffman@30040
   513
  apply (simp add: power2_sum)
huffman@30040
   514
  apply (simp add: mult_nonneg_nonneg)
huffman@30040
   515
  apply (simp add: add_nonneg_nonneg)
huffman@30040
   516
  done
huffman@30040
   517
huffman@30040
   518
lemma setL2_le_setsum_abs: "setL2 f A \<le> (\<Sum>i\<in>A. \<bar>f i\<bar>)"
huffman@30040
   519
  apply (cases "finite A")
huffman@30040
   520
  apply (induct set: finite)
huffman@30040
   521
  apply simp
huffman@30040
   522
  apply simp
huffman@30040
   523
  apply (rule order_trans [OF sqrt_sum_squares_le_sum_abs])
huffman@30040
   524
  apply simp
huffman@30040
   525
  apply simp
huffman@30040
   526
  done
huffman@30040
   527
huffman@30040
   528
lemma setL2_mult_ineq_lemma:
huffman@30040
   529
  fixes a b c d :: real
huffman@30040
   530
  shows "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
huffman@30040
   531
proof -
huffman@30040
   532
  have "0 \<le> (a * d - b * c)\<twosuperior>" by simp
huffman@30040
   533
  also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * d) * (b * c)"
huffman@30040
   534
    by (simp only: power2_diff power_mult_distrib)
huffman@30040
   535
  also have "\<dots> = a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior> - 2 * (a * c) * (b * d)"
huffman@30040
   536
    by simp
huffman@30040
   537
  finally show "2 * (a * c) * (b * d) \<le> a\<twosuperior> * d\<twosuperior> + b\<twosuperior> * c\<twosuperior>"
huffman@30040
   538
    by simp
huffman@30040
   539
qed
huffman@30040
   540
huffman@30040
   541
lemma setL2_mult_ineq: "(\<Sum>i\<in>A. \<bar>f i\<bar> * \<bar>g i\<bar>) \<le> setL2 f A * setL2 g A"
huffman@30040
   542
  apply (cases "finite A")
huffman@30040
   543
  apply (induct set: finite)
huffman@30040
   544
  apply simp
huffman@30040
   545
  apply (rule power2_le_imp_le, simp)
huffman@30040
   546
  apply (rule order_trans)
huffman@30040
   547
  apply (rule power_mono)
huffman@30040
   548
  apply (erule add_left_mono)
huffman@30040
   549
  apply (simp add: add_nonneg_nonneg mult_nonneg_nonneg setsum_nonneg)
huffman@30040
   550
  apply (simp add: power2_sum)
huffman@30040
   551
  apply (simp add: power_mult_distrib)
huffman@30040
   552
  apply (simp add: right_distrib left_distrib)
huffman@30040
   553
  apply (rule ord_le_eq_trans)
huffman@30040
   554
  apply (rule setL2_mult_ineq_lemma)
huffman@30040
   555
  apply simp
huffman@30040
   556
  apply (intro mult_nonneg_nonneg setL2_nonneg)
huffman@30040
   557
  apply simp
huffman@30040
   558
  done
huffman@30040
   559
huffman@30040
   560
lemma member_le_setL2: "\<lbrakk>finite A; i \<in> A\<rbrakk> \<Longrightarrow> f i \<le> setL2 f A"
huffman@30040
   561
  apply (rule_tac s="insert i (A - {i})" and t="A" in subst)
huffman@30040
   562
  apply fast
huffman@30040
   563
  apply (subst setL2_insert)
huffman@30040
   564
  apply simp
huffman@30040
   565
  apply simp
huffman@30040
   566
  apply simp
huffman@30040
   567
  done
huffman@30040
   568
huffman@31344
   569
subsection {* Metric *}
huffman@31344
   570
huffman@31493
   571
(* TODO: move somewhere else *)
huffman@31493
   572
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)"
huffman@31493
   573
apply (induct set: finite, simp_all)
huffman@31493
   574
apply (clarify, rename_tac y)
huffman@31493
   575
apply (rule_tac x="f(x:=y)" in exI, simp)
huffman@31493
   576
done
huffman@31493
   577
huffman@31344
   578
instantiation "^" :: (metric_space, finite) metric_space
huffman@31344
   579
begin
huffman@31344
   580
huffman@31344
   581
definition dist_vector_def:
huffman@31344
   582
  "dist (x::'a^'b) (y::'a^'b) = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@31344
   583
huffman@31493
   584
lemma dist_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@31493
   585
unfolding dist_vector_def
huffman@31493
   586
by (rule member_le_setL2) simp_all
huffman@31416
   587
huffman@31344
   588
instance proof
huffman@31344
   589
  fix x y :: "'a ^ 'b"
huffman@31344
   590
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@31344
   591
    unfolding dist_vector_def
huffman@31344
   592
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@31344
   593
next
huffman@31344
   594
  fix x y z :: "'a ^ 'b"
huffman@31344
   595
  show "dist x y \<le> dist x z + dist y z"
huffman@31344
   596
    unfolding dist_vector_def
huffman@31344
   597
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@31344
   598
    apply (simp add: setL2_mono dist_triangle2)
huffman@31344
   599
    done
huffman@31416
   600
next
huffman@31493
   601
  (* FIXME: long proof! *)
huffman@31492
   602
  fix S :: "('a ^ 'b) set"
huffman@31492
   603
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@31493
   604
    unfolding open_vector_def open_dist
huffman@31493
   605
    apply safe
huffman@31493
   606
     apply (drule (1) bspec)
huffman@31493
   607
     apply clarify
huffman@31493
   608
     apply (subgoal_tac "\<exists>e>0. \<forall>i y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@31493
   609
      apply clarify
huffman@31493
   610
      apply (rule_tac x=e in exI, clarify)
huffman@31493
   611
      apply (drule spec, erule mp, clarify)
huffman@31493
   612
      apply (drule spec, drule spec, erule mp)
huffman@31493
   613
      apply (erule le_less_trans [OF dist_nth_le])
huffman@31493
   614
     apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@31493
   615
      apply (drule finite_choice [OF finite], clarify)
huffman@31493
   616
      apply (rule_tac x="Min (range f)" in exI, simp)
huffman@31493
   617
     apply clarify
huffman@31493
   618
     apply (drule_tac x=i in spec, clarify)
huffman@31493
   619
     apply (erule (1) bspec)
huffman@31493
   620
    apply (drule (1) bspec, clarify)
huffman@31493
   621
    apply (subgoal_tac "\<exists>r. (\<forall>i::'b. 0 < r i) \<and> e = setL2 r UNIV")
huffman@31493
   622
     apply clarify
huffman@31493
   623
     apply (rule_tac x="\<lambda>i. {y. dist y (x$i) < r i}" in exI)
huffman@31493
   624
     apply (rule conjI)
huffman@31493
   625
      apply clarify
huffman@31493
   626
      apply (rule conjI)
huffman@31493
   627
       apply (clarify, rename_tac y)
huffman@31493
   628
       apply (rule_tac x="r i - dist y (x$i)" in exI, rule conjI, simp)
huffman@31493
   629
       apply clarify
huffman@31493
   630
       apply (simp only: less_diff_eq)
huffman@31493
   631
       apply (erule le_less_trans [OF dist_triangle])
huffman@31493
   632
      apply simp
huffman@31493
   633
     apply clarify
huffman@31493
   634
     apply (drule spec, erule mp)
huffman@31493
   635
     apply (simp add: dist_vector_def setL2_strict_mono)
huffman@31493
   636
    apply (rule_tac x="\<lambda>i. e / sqrt (of_nat CARD('b))" in exI)
huffman@31493
   637
    apply (simp add: divide_pos_pos setL2_constant)
huffman@31493
   638
    done
huffman@31344
   639
qed
huffman@31344
   640
huffman@31344
   641
end
huffman@31344
   642
huffman@31389
   643
lemma LIMSEQ_Cart_nth:
huffman@31389
   644
  "(X ----> a) \<Longrightarrow> (\<lambda>n. X n $ i) ----> a $ i"
huffman@31389
   645
unfolding LIMSEQ_conv_tendsto by (rule tendsto_Cart_nth)
huffman@31389
   646
huffman@31389
   647
lemma LIM_Cart_nth:
huffman@31389
   648
  "(f -- x --> y) \<Longrightarrow> (\<lambda>x. f x $ i) -- x --> y $ i"
huffman@31389
   649
unfolding LIM_conv_tendsto by (rule tendsto_Cart_nth)
huffman@31389
   650
huffman@31389
   651
lemma Cauchy_Cart_nth:
huffman@31406
   652
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@31406
   653
unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le])
huffman@31389
   654
huffman@31389
   655
lemma LIMSEQ_vector:
huffman@31389
   656
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n::finite"
huffman@31389
   657
  assumes X: "\<And>i. (\<lambda>n. X n $ i) ----> (a $ i)"
huffman@31389
   658
  shows "X ----> a"
huffman@31389
   659
proof (rule metric_LIMSEQ_I)
huffman@31389
   660
  fix r :: real assume "0 < r"
huffman@31389
   661
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@31389
   662
    by (simp add: divide_pos_pos)
huffman@31389
   663
  def N \<equiv> "\<lambda>i. LEAST N. \<forall>n\<ge>N. dist (X n $ i) (a $ i) < ?s"
huffman@31389
   664
  def M \<equiv> "Max (range N)"
huffman@31389
   665
  have "\<And>i. \<exists>N. \<forall>n\<ge>N. dist (X n $ i) (a $ i) < ?s"
huffman@31389
   666
    using X `0 < ?s` by (rule metric_LIMSEQ_D)
huffman@31389
   667
  hence "\<And>i. \<forall>n\<ge>N i. dist (X n $ i) (a $ i) < ?s"
huffman@31389
   668
    unfolding N_def by (rule LeastI_ex)
huffman@31389
   669
  hence M: "\<And>i. \<forall>n\<ge>M. dist (X n $ i) (a $ i) < ?s"
huffman@31389
   670
    unfolding M_def by simp
huffman@31389
   671
  {
huffman@31389
   672
    fix n :: nat assume "M \<le> n"
huffman@31389
   673
    have "dist (X n) a = setL2 (\<lambda>i. dist (X n $ i) (a $ i)) UNIV"
huffman@31389
   674
      unfolding dist_vector_def ..
huffman@31389
   675
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X n $ i) (a $ i)) UNIV"
huffman@31389
   676
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@31389
   677
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@31389
   678
      by (rule setsum_strict_mono, simp_all add: M `M \<le> n`)
huffman@31389
   679
    also have "\<dots> = r"
huffman@31389
   680
      by simp
huffman@31389
   681
    finally have "dist (X n) a < r" .
huffman@31389
   682
  }
huffman@31389
   683
  hence "\<forall>n\<ge>M. dist (X n) a < r"
huffman@31389
   684
    by simp
huffman@31389
   685
  then show "\<exists>M. \<forall>n\<ge>M. dist (X n) a < r" ..
huffman@31389
   686
qed
huffman@31389
   687
huffman@31389
   688
lemma Cauchy_vector:
huffman@31389
   689
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n::finite"
huffman@31389
   690
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@31389
   691
  shows "Cauchy (\<lambda>n. X n)"
huffman@31389
   692
proof (rule metric_CauchyI)
huffman@31389
   693
  fix r :: real assume "0 < r"
huffman@31389
   694
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@31389
   695
    by (simp add: divide_pos_pos)
huffman@31389
   696
  def N \<equiv> "\<lambda>i. LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@31389
   697
  def M \<equiv> "Max (range N)"
huffman@31389
   698
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@31389
   699
    using X `0 < ?s` by (rule metric_CauchyD)
huffman@31389
   700
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@31389
   701
    unfolding N_def by (rule LeastI_ex)
huffman@31389
   702
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@31389
   703
    unfolding M_def by simp
huffman@31389
   704
  {
huffman@31389
   705
    fix m n :: nat
huffman@31389
   706
    assume "M \<le> m" "M \<le> n"
huffman@31389
   707
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@31389
   708
      unfolding dist_vector_def ..
huffman@31389
   709
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@31389
   710
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@31389
   711
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@31389
   712
      by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
huffman@31389
   713
    also have "\<dots> = r"
huffman@31389
   714
      by simp
huffman@31389
   715
    finally have "dist (X m) (X n) < r" .
huffman@31389
   716
  }
huffman@31389
   717
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@31389
   718
    by simp
huffman@31389
   719
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@31389
   720
qed
huffman@31389
   721
huffman@31406
   722
instance "^" :: (complete_space, finite) complete_space
huffman@31406
   723
proof
huffman@31406
   724
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
huffman@31406
   725
  have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
huffman@31406
   726
    using Cauchy_Cart_nth [OF `Cauchy X`]
huffman@31406
   727
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@31406
   728
  hence "X ----> Cart_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@31406
   729
    by (simp add: LIMSEQ_vector)
huffman@31406
   730
  then show "convergent X"
huffman@31406
   731
    by (rule convergentI)
huffman@31406
   732
qed
huffman@31406
   733
huffman@30040
   734
subsection {* Norms *}
huffman@30040
   735
huffman@30582
   736
instantiation "^" :: (real_normed_vector, finite) real_normed_vector
huffman@30040
   737
begin
huffman@30040
   738
huffman@30040
   739
definition vector_norm_def:
huffman@30582
   740
  "norm (x::'a^'b) = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@30040
   741
huffman@30040
   742
definition vector_sgn_def:
huffman@30040
   743
  "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@30040
   744
huffman@30040
   745
instance proof
huffman@30040
   746
  fix a :: real and x y :: "'a ^ 'b"
huffman@30040
   747
  show "0 \<le> norm x"
huffman@30040
   748
    unfolding vector_norm_def
huffman@30040
   749
    by (rule setL2_nonneg)
huffman@30040
   750
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@30040
   751
    unfolding vector_norm_def
huffman@30040
   752
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@30040
   753
  show "norm (x + y) \<le> norm x + norm y"
huffman@30040
   754
    unfolding vector_norm_def
huffman@30040
   755
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@30582
   756
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@30040
   757
    done
huffman@30040
   758
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@30040
   759
    unfolding vector_norm_def
huffman@30582
   760
    by (simp add: norm_scaleR setL2_right_distrib)
huffman@30040
   761
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@30040
   762
    by (rule vector_sgn_def)
huffman@31289
   763
  show "dist x y = norm (x - y)"
huffman@31344
   764
    unfolding dist_vector_def vector_norm_def
huffman@31344
   765
    by (simp add: dist_norm)
huffman@30040
   766
qed
huffman@30040
   767
huffman@30040
   768
end
huffman@30040
   769
huffman@31389
   770
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@31389
   771
unfolding vector_norm_def
huffman@31389
   772
by (rule member_le_setL2) simp_all
huffman@31389
   773
huffman@31389
   774
interpretation Cart_nth: bounded_linear "\<lambda>x. x $ i"
huffman@31389
   775
apply default
huffman@31389
   776
apply (rule vector_add_component)
huffman@31389
   777
apply (rule vector_scaleR_component)
huffman@31389
   778
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@31389
   779
done
huffman@31389
   780
huffman@31406
   781
instance "^" :: (banach, finite) banach ..
huffman@31406
   782
huffman@30045
   783
subsection {* Inner products *}
huffman@30045
   784
huffman@30582
   785
instantiation "^" :: (real_inner, finite) real_inner
huffman@30045
   786
begin
huffman@30045
   787
huffman@30045
   788
definition vector_inner_def:
huffman@30582
   789
  "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@30045
   790
huffman@30045
   791
instance proof
huffman@30045
   792
  fix r :: real and x y z :: "'a ^ 'b"
huffman@30045
   793
  show "inner x y = inner y x"
huffman@30045
   794
    unfolding vector_inner_def
huffman@30045
   795
    by (simp add: inner_commute)
huffman@30045
   796
  show "inner (x + y) z = inner x z + inner y z"
huffman@30045
   797
    unfolding vector_inner_def
huffman@30582
   798
    by (simp add: inner_left_distrib setsum_addf)
huffman@30045
   799
  show "inner (scaleR r x) y = r * inner x y"
huffman@30045
   800
    unfolding vector_inner_def
huffman@30582
   801
    by (simp add: inner_scaleR_left setsum_right_distrib)
huffman@30045
   802
  show "0 \<le> inner x x"
huffman@30045
   803
    unfolding vector_inner_def
huffman@30045
   804
    by (simp add: setsum_nonneg)
huffman@30045
   805
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@30045
   806
    unfolding vector_inner_def
huffman@30045
   807
    by (simp add: Cart_eq setsum_nonneg_eq_0_iff)
huffman@30045
   808
  show "norm x = sqrt (inner x x)"
huffman@30045
   809
    unfolding vector_inner_def vector_norm_def setL2_def
huffman@30045
   810
    by (simp add: power2_norm_eq_inner)
huffman@30045
   811
qed
huffman@30045
   812
huffman@30045
   813
end
huffman@30045
   814
chaieb@29842
   815
subsection{* Properties of the dot product.  *}
chaieb@29842
   816
huffman@30489
   817
lemma dot_sym: "(x::'a:: {comm_monoid_add, ab_semigroup_mult} ^ 'n) \<bullet> y = y \<bullet> x"
chaieb@29842
   818
  by (vector mult_commute)
chaieb@29842
   819
lemma dot_ladd: "((x::'a::ring ^ 'n) + y) \<bullet> z = (x \<bullet> z) + (y \<bullet> z)"
chaieb@29842
   820
  by (vector ring_simps)
huffman@30489
   821
lemma dot_radd: "x \<bullet> (y + (z::'a::ring ^ 'n)) = (x \<bullet> y) + (x \<bullet> z)"
chaieb@29842
   822
  by (vector ring_simps)
huffman@30489
   823
lemma dot_lsub: "((x::'a::ring ^ 'n) - y) \<bullet> z = (x \<bullet> z) - (y \<bullet> z)"
chaieb@29842
   824
  by (vector ring_simps)
huffman@30489
   825
lemma dot_rsub: "(x::'a::ring ^ 'n) \<bullet> (y - z) = (x \<bullet> y) - (x \<bullet> z)"
chaieb@29842
   826
  by (vector ring_simps)
chaieb@29842
   827
lemma dot_lmult: "(c *s x) \<bullet> y = (c::'a::ring) * (x \<bullet> y)" by (vector ring_simps)
chaieb@29842
   828
lemma dot_rmult: "x \<bullet> (c *s y) = (c::'a::comm_ring) * (x \<bullet> y)" by (vector ring_simps)
chaieb@29842
   829
lemma dot_lneg: "(-x) \<bullet> (y::'a::ring ^ 'n) = -(x \<bullet> y)" by vector
chaieb@29842
   830
lemma dot_rneg: "(x::'a::ring ^ 'n) \<bullet> (-y) = -(x \<bullet> y)" by vector
chaieb@29842
   831
lemma dot_lzero[simp]: "0 \<bullet> x = (0::'a::{comm_monoid_add, mult_zero})" by vector
chaieb@29842
   832
lemma dot_rzero[simp]: "x \<bullet> 0 = (0::'a::{comm_monoid_add, mult_zero})" by vector
chaieb@29842
   833
lemma dot_pos_le[simp]: "(0::'a\<Colon>ordered_ring_strict) <= x \<bullet> x"
chaieb@29842
   834
  by (simp add: dot_def setsum_nonneg)
chaieb@29842
   835
chaieb@29842
   836
lemma setsum_squares_eq_0_iff: assumes fS: "finite F" and fp: "\<forall>x \<in> F. f x \<ge> (0 ::'a::pordered_ab_group_add)" shows "setsum f F = 0 \<longleftrightarrow> (ALL x:F. f x = 0)"
chaieb@29842
   837
using fS fp setsum_nonneg[OF fp]
chaieb@29842
   838
proof (induct set: finite)
chaieb@29842
   839
  case empty thus ?case by simp
chaieb@29842
   840
next
chaieb@29842
   841
  case (insert x F)
chaieb@29842
   842
  from insert.prems have Fx: "f x \<ge> 0" and Fp: "\<forall> a \<in> F. f a \<ge> 0" by simp_all
chaieb@29842
   843
  from insert.hyps Fp setsum_nonneg[OF Fp]
chaieb@29842
   844
  have h: "setsum f F = 0 \<longleftrightarrow> (\<forall>a \<in>F. f a = 0)" by metis
haftmann@31034
   845
  from add_nonneg_eq_0_iff[OF Fx  setsum_nonneg[OF Fp]] insert.hyps(1,2)
chaieb@29842
   846
  show ?case by (simp add: h)
chaieb@29842
   847
qed
chaieb@29842
   848
huffman@30582
   849
lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) = 0"
huffman@30582
   850
  by (simp add: dot_def setsum_squares_eq_0_iff Cart_eq)
huffman@30582
   851
huffman@30582
   852
lemma dot_pos_lt[simp]: "(0 < x \<bullet> x) \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) \<noteq> 0" using dot_eq_0[of x] dot_pos_le[of x]
huffman@30489
   853
  by (auto simp add: le_less)
chaieb@29842
   854
huffman@30040
   855
subsection{* The collapse of the general concepts to dimension one. *}
chaieb@29842
   856
chaieb@29842
   857
lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
huffman@30582
   858
  by (simp add: Cart_eq forall_1)
chaieb@29842
   859
chaieb@29842
   860
lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
chaieb@29842
   861
  apply auto
chaieb@29842
   862
  apply (erule_tac x= "x$1" in allE)
chaieb@29842
   863
  apply (simp only: vector_one[symmetric])
chaieb@29842
   864
  done
chaieb@29842
   865
huffman@30040
   866
lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
huffman@30582
   867
  by (simp add: vector_norm_def UNIV_1)
huffman@30040
   868
huffman@30489
   869
lemma norm_real: "norm(x::real ^ 1) = abs(x$1)"
huffman@30040
   870
  by (simp add: norm_vector_1)
chaieb@29842
   871
chaieb@29842
   872
lemma dist_real: "dist(x::real ^ 1) y = abs((x$1) - (y$1))"
huffman@31289
   873
  by (auto simp add: norm_real dist_norm)
chaieb@29842
   874
chaieb@29842
   875
subsection {* A connectedness or intermediate value lemma with several applications. *}
chaieb@29842
   876
chaieb@29842
   877
lemma connected_real_lemma:
huffman@30582
   878
  fixes f :: "real \<Rightarrow> real ^ 'n::finite"
chaieb@29842
   879
  assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
chaieb@29842
   880
  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"
chaieb@29842
   881
  and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
chaieb@29842
   882
  and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
chaieb@29842
   883
  and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
chaieb@29842
   884
  shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
chaieb@29842
   885
proof-
chaieb@29842
   886
  let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
huffman@30489
   887
  have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa)
huffman@30489
   888
  have Sub: "\<exists>y. isUb UNIV ?S y"
chaieb@29842
   889
    apply (rule exI[where x= b])
huffman@30489
   890
    using ab fb e12 by (auto simp add: isUb_def setle_def)
huffman@30489
   891
  from reals_complete[OF Se Sub] obtain l where
chaieb@29842
   892
    l: "isLub UNIV ?S l"by blast
chaieb@29842
   893
  have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
huffman@30489
   894
    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
chaieb@29842
   895
    by (metis linorder_linear)
chaieb@29842
   896
  have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
chaieb@29842
   897
    apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
chaieb@29842
   898
    by (metis linorder_linear not_le)
chaieb@29842
   899
    have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
chaieb@29842
   900
    have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
chaieb@29842
   901
    have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by dlo
chaieb@29842
   902
    {assume le2: "f l \<in> e2"
chaieb@29842
   903
      from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
chaieb@29842
   904
      hence lap: "l - a > 0" using alb by arith
huffman@30489
   905
      from e2[rule_format, OF le2] obtain e where
chaieb@29842
   906
	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
huffman@30489
   907
      from dst[OF alb e(1)] obtain d where
chaieb@29842
   908
	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
huffman@30489
   909
      have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" using lap d(1)
chaieb@29842
   910
	apply ferrack by arith
chaieb@29842
   911
      then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
chaieb@29842
   912
      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
chaieb@29842
   913
      from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
chaieb@29842
   914
      moreover
chaieb@29842
   915
      have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
chaieb@29842
   916
      ultimately have False using e12 alb d' by auto}
chaieb@29842
   917
    moreover
chaieb@29842
   918
    {assume le1: "f l \<in> e1"
chaieb@29842
   919
    from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
chaieb@29842
   920
      hence blp: "b - l > 0" using alb by arith
huffman@30489
   921
      from e1[rule_format, OF le1] obtain e where
chaieb@29842
   922
	e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
huffman@30489
   923
      from dst[OF alb e(1)] obtain d where
chaieb@29842
   924
	d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
huffman@30489
   925
      have "\<exists>d'. d' < d \<and> d' >0" using d(1) by dlo
chaieb@29842
   926
      then obtain d' where d': "d' > 0" "d' < d" by metis
chaieb@29842
   927
      from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
chaieb@29842
   928
      hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
chaieb@29842
   929
      with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
huffman@30489
   930
      with l d' have False
chaieb@29842
   931
	by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
chaieb@29842
   932
    ultimately show ?thesis using alb by metis
chaieb@29842
   933
qed
chaieb@29842
   934
huffman@29881
   935
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"} *}
chaieb@29842
   936
chaieb@29842
   937
lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
chaieb@29842
   938
proof-
huffman@30489
   939
  have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith
chaieb@29842
   940
  thus ?thesis by (simp add: ring_simps power2_eq_square)
chaieb@29842
   941
qed
chaieb@29842
   942
chaieb@29842
   943
lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
huffman@31340
   944
  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)
chaieb@29842
   945
  apply (rule_tac x="s" in exI)
chaieb@29842
   946
  apply auto
chaieb@29842
   947
  apply (erule_tac x=y in allE)
chaieb@29842
   948
  apply auto
chaieb@29842
   949
  done
chaieb@29842
   950
chaieb@29842
   951
lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
chaieb@29842
   952
  using real_sqrt_le_iff[of x "y^2"] by simp
chaieb@29842
   953
chaieb@29842
   954
lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
chaieb@29842
   955
  using real_sqrt_le_mono[of "x^2" y] by simp
chaieb@29842
   956
chaieb@29842
   957
lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
chaieb@29842
   958
  using real_sqrt_less_mono[of "x^2" y] by simp
chaieb@29842
   959
huffman@30489
   960
lemma sqrt_even_pow2: assumes n: "even n"
chaieb@29842
   961
  shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
chaieb@29842
   962
proof-
huffman@30489
   963
  from n obtain m where m: "n = 2*m" unfolding even_nat_equiv_def2
huffman@30489
   964
    by (auto simp add: nat_number)
chaieb@29842
   965
  from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
chaieb@29842
   966
    by (simp only: power_mult[symmetric] mult_commute)
huffman@30489
   967
  then show ?thesis  using m by simp
chaieb@29842
   968
qed
chaieb@29842
   969
chaieb@29842
   970
lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
chaieb@29842
   971
  apply (cases "x = 0", simp_all)
chaieb@29842
   972
  using sqrt_divide_self_eq[of x]
chaieb@29842
   973
  apply (simp add: inverse_eq_divide real_sqrt_ge_0_iff field_simps)
chaieb@29842
   974
  done
chaieb@29842
   975
chaieb@29842
   976
text{* Hence derive more interesting properties of the norm. *}
chaieb@29842
   977
huffman@30582
   978
text {*
huffman@30582
   979
  This type-specific version is only here
huffman@30582
   980
  to make @{text normarith.ML} happy.
huffman@30582
   981
*}
huffman@30582
   982
lemma norm_0: "norm (0::real ^ _) = 0"
huffman@30040
   983
  by (rule norm_zero)
huffman@30040
   984
chaieb@30263
   985
lemma norm_mul[simp]: "norm(a *s x) = abs(a) * norm x"
huffman@30040
   986
  by (simp add: vector_norm_def vector_component setL2_right_distrib
huffman@30040
   987
           abs_mult cong: strong_setL2_cong)
chaieb@29842
   988
lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (x \<bullet> x = (0::real))"
huffman@30040
   989
  by (simp add: vector_norm_def dot_def setL2_def power2_eq_square)
huffman@30040
   990
lemma real_vector_norm_def: "norm x = sqrt (x \<bullet> x)"
huffman@30040
   991
  by (simp add: vector_norm_def setL2_def dot_def power2_eq_square)
chaieb@29842
   992
lemma norm_pow_2: "norm x ^ 2 = x \<bullet> x"
huffman@30040
   993
  by (simp add: real_vector_norm_def)
huffman@30582
   994
lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n::finite)" by (metis norm_eq_zero)
chaieb@30263
   995
lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
chaieb@29842
   996
  by vector
chaieb@30263
   997
lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
chaieb@29842
   998
  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
chaieb@30263
   999
lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
chaieb@29842
  1000
  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
chaieb@29842
  1001
lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
chaieb@29842
  1002
  by (metis vector_mul_lcancel)
chaieb@29842
  1003
lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
chaieb@29842
  1004
  by (metis vector_mul_rcancel)
huffman@30582
  1005
lemma norm_cauchy_schwarz:
huffman@30582
  1006
  fixes x y :: "real ^ 'n::finite"
huffman@30582
  1007
  shows "x \<bullet> y <= norm x * norm y"
chaieb@29842
  1008
proof-
chaieb@29842
  1009
  {assume "norm x = 0"
huffman@30041
  1010
    hence ?thesis by (simp add: dot_lzero dot_rzero)}
chaieb@29842
  1011
  moreover
huffman@30489
  1012
  {assume "norm y = 0"
huffman@30041
  1013
    hence ?thesis by (simp add: dot_lzero dot_rzero)}
chaieb@29842
  1014
  moreover
chaieb@29842
  1015
  {assume h: "norm x \<noteq> 0" "norm y \<noteq> 0"
chaieb@29842
  1016
    let ?z = "norm y *s x - norm x *s y"
huffman@30041
  1017
    from h have p: "norm x * norm y > 0" by (metis norm_ge_zero le_less zero_compare_simps)
chaieb@29842
  1018
    from dot_pos_le[of ?z]
chaieb@29842
  1019
    have "(norm x * norm y) * (x \<bullet> y) \<le> norm x ^2 * norm y ^2"
chaieb@29842
  1020
      apply (simp add: dot_rsub dot_lsub dot_lmult dot_rmult ring_simps)
chaieb@29842
  1021
      by (simp add: norm_pow_2[symmetric] power2_eq_square dot_sym)
chaieb@29842
  1022
    hence "x\<bullet>y \<le> (norm x ^2 * norm y ^2) / (norm x * norm y)" using p
chaieb@29842
  1023
      by (simp add: field_simps)
chaieb@29842
  1024
    hence ?thesis using h by (simp add: power2_eq_square)}
chaieb@29842
  1025
  ultimately show ?thesis by metis
chaieb@29842
  1026
qed
chaieb@29842
  1027
huffman@30582
  1028
lemma norm_cauchy_schwarz_abs:
huffman@30582
  1029
  fixes x y :: "real ^ 'n::finite"
huffman@30582
  1030
  shows "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
chaieb@29842
  1031
  using norm_cauchy_schwarz[of x y] norm_cauchy_schwarz[of x "-y"]
huffman@30041
  1032
  by (simp add: real_abs_def dot_rneg)
chaieb@29842
  1033
huffman@31398
  1034
lemma norm_triangle_sub:
huffman@31398
  1035
  fixes x y :: "'a::real_normed_vector"
huffman@31398
  1036
  shows "norm x \<le> norm y  + norm (x - y)"
huffman@30041
  1037
  using norm_triangle_ineq[of "y" "x - y"] by (simp add: ring_simps)
huffman@31398
  1038
huffman@30582
  1039
lemma norm_triangle_le: "norm(x::real ^'n::finite) + norm y <= e ==> norm(x + y) <= e"
huffman@30041
  1040
  by (metis order_trans norm_triangle_ineq)
huffman@30582
  1041
lemma norm_triangle_lt: "norm(x::real ^'n::finite) + norm(y) < e ==> norm(x + y) < e"
huffman@30041
  1042
  by (metis basic_trans_rules(21) norm_triangle_ineq)
chaieb@29842
  1043
huffman@30582
  1044
lemma setsum_delta:
huffman@30582
  1045
  assumes fS: "finite S"
huffman@30582
  1046
  shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
huffman@30582
  1047
proof-
huffman@30582
  1048
  let ?f = "(\<lambda>k. if k=a then b k else 0)"
huffman@30582
  1049
  {assume a: "a \<notin> S"
huffman@30582
  1050
    hence "\<forall> k\<in> S. ?f k = 0" by simp
huffman@30582
  1051
    hence ?thesis  using a by simp}
huffman@30582
  1052
  moreover
huffman@30582
  1053
  {assume a: "a \<in> S"
huffman@30582
  1054
    let ?A = "S - {a}"
huffman@30582
  1055
    let ?B = "{a}"
huffman@30582
  1056
    have eq: "S = ?A \<union> ?B" using a by blast
huffman@30582
  1057
    have dj: "?A \<inter> ?B = {}" by simp
huffman@30582
  1058
    from fS have fAB: "finite ?A" "finite ?B" by auto
huffman@30582
  1059
    have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
huffman@30582
  1060
      using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
huffman@30582
  1061
      by simp
huffman@30582
  1062
    then have ?thesis  using a by simp}
huffman@30582
  1063
  ultimately show ?thesis by blast
huffman@30582
  1064
qed
huffman@30582
  1065
huffman@30582
  1066
lemma component_le_norm: "\<bar>x$i\<bar> <= norm (x::real ^ 'n::finite)"
huffman@30040
  1067
  apply (simp add: vector_norm_def)
huffman@30040
  1068
  apply (rule member_le_setL2, simp_all)
huffman@30040
  1069
  done
huffman@30040
  1070
huffman@30582
  1071
lemma norm_bound_component_le: "norm(x::real ^ 'n::finite) <= e
huffman@30582
  1072
                ==> \<bar>x$i\<bar> <= e"
chaieb@29842
  1073
  by (metis component_le_norm order_trans)
chaieb@29842
  1074
huffman@30582
  1075
lemma norm_bound_component_lt: "norm(x::real ^ 'n::finite) < e
huffman@30582
  1076
                ==> \<bar>x$i\<bar> < e"
chaieb@29842
  1077
  by (metis component_le_norm basic_trans_rules(21))
chaieb@29842
  1078
huffman@30582
  1079
lemma norm_le_l1: "norm (x:: real ^'n::finite) <= setsum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
huffman@30040
  1080
  by (simp add: vector_norm_def setL2_le_setsum)
chaieb@29842
  1081
huffman@30582
  1082
lemma real_abs_norm: "\<bar>norm x\<bar> = norm (x :: real ^ _)"
huffman@30040
  1083
  by (rule abs_norm_cancel)
huffman@30582
  1084
lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n::finite) - norm y\<bar> <= norm(x - y)"
huffman@30040
  1085
  by (rule norm_triangle_ineq3)
huffman@30582
  1086
lemma norm_le: "norm(x::real ^ _) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
chaieb@29842
  1087
  by (simp add: real_vector_norm_def)
huffman@30582
  1088
lemma norm_lt: "norm(x::real ^ _) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
chaieb@29842
  1089
  by (simp add: real_vector_norm_def)
huffman@30582
  1090
lemma norm_eq: "norm (x::real ^ _) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
chaieb@29842
  1091
  by (simp add: order_eq_iff norm_le)
huffman@30582
  1092
lemma norm_eq_1: "norm(x::real ^ _) = 1 \<longleftrightarrow> x \<bullet> x = 1"
chaieb@29842
  1093
  by (simp add: real_vector_norm_def)
chaieb@29842
  1094
chaieb@29842
  1095
text{* Squaring equations and inequalities involving norms.  *}
chaieb@29842
  1096
chaieb@29842
  1097
lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
huffman@30582
  1098
  by (simp add: real_vector_norm_def)
chaieb@29842
  1099
chaieb@29842
  1100
lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
huffman@30040
  1101
  by (auto simp add: real_vector_norm_def)
chaieb@29842
  1102
chaieb@29842
  1103
lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
chaieb@29842
  1104
proof-
chaieb@29842
  1105
  have "x^2 \<le> y^2 \<longleftrightarrow> (x -y) * (y + x) \<le> 0" by (simp add: ring_simps power2_eq_square)
chaieb@29842
  1106
  also have "\<dots> \<longleftrightarrow> \<bar>x\<bar> \<le> \<bar>y\<bar>" apply (simp add: zero_compare_simps real_abs_def not_less) by arith
chaieb@29842
  1107
finally show ?thesis ..
chaieb@29842
  1108
qed
chaieb@29842
  1109
chaieb@29842
  1110
lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
huffman@30040
  1111
  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
huffman@30041
  1112
  using norm_ge_zero[of x]
chaieb@29842
  1113
  apply arith
chaieb@29842
  1114
  done
chaieb@29842
  1115
huffman@30489
  1116
lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2"
huffman@30040
  1117
  apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
huffman@30041
  1118
  using norm_ge_zero[of x]
chaieb@29842
  1119
  apply arith
chaieb@29842
  1120
  done
chaieb@29842
  1121
chaieb@29842
  1122
lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
chaieb@29842
  1123
  by (metis not_le norm_ge_square)
chaieb@29842
  1124
lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
chaieb@29842
  1125
  by (metis norm_le_square not_less)
chaieb@29842
  1126
chaieb@29842
  1127
text{* Dot product in terms of the norm rather than conversely. *}
chaieb@29842
  1128
chaieb@29842
  1129
lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
chaieb@29842
  1130
  by (simp add: norm_pow_2 dot_ladd dot_radd dot_sym)
chaieb@29842
  1131
chaieb@29842
  1132
lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
chaieb@29842
  1133
  by (simp add: norm_pow_2 dot_ladd dot_radd dot_lsub dot_rsub dot_sym)
chaieb@29842
  1134
chaieb@29842
  1135
chaieb@29842
  1136
text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
chaieb@29842
  1137
huffman@30582
  1138
lemma vector_eq: "(x:: real ^ 'n::finite) = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y\<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@29842
  1139
proof
chaieb@29842
  1140
  assume "?lhs" then show ?rhs by simp
chaieb@29842
  1141
next
chaieb@29842
  1142
  assume ?rhs
chaieb@29842
  1143
  then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y\<bullet> y = 0" by simp
huffman@30489
  1144
  hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
chaieb@29842
  1145
    by (simp add: dot_rsub dot_lsub dot_sym)
chaieb@29842
  1146
  then have "(x - y) \<bullet> (x - y) = 0" by (simp add: ring_simps dot_lsub dot_rsub)
chaieb@29842
  1147
  then show "x = y" by (simp add: dot_eq_0)
chaieb@29842
  1148
qed
chaieb@29842
  1149
chaieb@29842
  1150
chaieb@29842
  1151
subsection{* General linear decision procedure for normed spaces. *}
chaieb@29842
  1152
huffman@31445
  1153
lemma norm_cmul_rule_thm:
huffman@31445
  1154
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1155
  shows "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(scaleR c x)"
huffman@31445
  1156
  unfolding norm_scaleR
huffman@31445
  1157
  apply (erule mult_mono1)
huffman@31445
  1158
  apply simp
chaieb@29842
  1159
  done
chaieb@29842
  1160
chaieb@30263
  1161
  (* FIXME: Move all these theorems into the ML code using lemma antiquotation *)
huffman@31445
  1162
lemma norm_add_rule_thm:
huffman@31445
  1163
  fixes x1 x2 :: "'a::real_normed_vector"
huffman@31445
  1164
  shows "norm x1 \<le> b1 \<Longrightarrow> norm x2 \<le> b2 \<Longrightarrow> norm (x1 + x2) \<le> b1 + b2"
huffman@31445
  1165
  by (rule order_trans [OF norm_triangle_ineq add_mono])
chaieb@29842
  1166
chaieb@29842
  1167
lemma ge_iff_diff_ge_0: "(a::'a::ordered_ring) \<ge> b == a - b \<ge> 0"
chaieb@29842
  1168
  by (simp add: ring_simps)
chaieb@29842
  1169
huffman@31445
  1170
lemma pth_1:
huffman@31445
  1171
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1172
  shows "x == scaleR 1 x" by simp
huffman@31445
  1173
huffman@31445
  1174
lemma pth_2:
huffman@31445
  1175
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1176
  shows "x - y == x + -y" by (atomize (full)) simp
huffman@31445
  1177
huffman@31445
  1178
lemma pth_3:
huffman@31445
  1179
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1180
  shows "- x == scaleR (-1) x" by simp
huffman@31445
  1181
huffman@31445
  1182
lemma pth_4:
huffman@31445
  1183
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1184
  shows "scaleR 0 x == 0" and "scaleR c 0 = (0::'a)" by simp_all
huffman@31445
  1185
huffman@31445
  1186
lemma pth_5:
huffman@31445
  1187
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1188
  shows "scaleR c (scaleR d x) == scaleR (c * d) x" by simp
huffman@31445
  1189
huffman@31445
  1190
lemma pth_6:
huffman@31445
  1191
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1192
  shows "scaleR c (x + y) == scaleR c x + scaleR c y"
huffman@31445
  1193
  by (simp add: scaleR_right_distrib)
huffman@31445
  1194
huffman@31445
  1195
lemma pth_7:
huffman@31445
  1196
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1197
  shows "0 + x == x" and "x + 0 == x" by simp_all
huffman@31445
  1198
huffman@31445
  1199
lemma pth_8:
huffman@31445
  1200
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1201
  shows "scaleR c x + scaleR d x == scaleR (c + d) x"
huffman@31445
  1202
  by (simp add: scaleR_left_distrib)
huffman@31445
  1203
huffman@31445
  1204
lemma pth_9:
huffman@31445
  1205
  fixes x :: "'a::real_normed_vector" shows
huffman@31445
  1206
  "(scaleR c x + z) + scaleR d x == scaleR (c + d) x + z"
huffman@31445
  1207
  "scaleR c x + (scaleR d x + z) == scaleR (c + d) x + z"
huffman@31445
  1208
  "(scaleR c x + w) + (scaleR d x + z) == scaleR (c + d) x + (w + z)"
huffman@31445
  1209
  by (simp_all add: algebra_simps)
huffman@31445
  1210
huffman@31445
  1211
lemma pth_a:
huffman@31445
  1212
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1213
  shows "scaleR 0 x + y == y" by simp
huffman@31445
  1214
huffman@31445
  1215
lemma pth_b:
huffman@31445
  1216
  fixes x :: "'a::real_normed_vector" shows
huffman@31445
  1217
  "scaleR c x + scaleR d y == scaleR c x + scaleR d y"
huffman@31445
  1218
  "(scaleR c x + z) + scaleR d y == scaleR c x + (z + scaleR d y)"
huffman@31445
  1219
  "scaleR c x + (scaleR d y + z) == scaleR c x + (scaleR d y + z)"
huffman@31445
  1220
  "(scaleR c x + w) + (scaleR d y + z) == scaleR c x + (w + (scaleR d y + z))"
huffman@31445
  1221
  by (simp_all add: algebra_simps)
huffman@31445
  1222
huffman@31445
  1223
lemma pth_c:
huffman@31445
  1224
  fixes x :: "'a::real_normed_vector" shows
huffman@31445
  1225
  "scaleR c x + scaleR d y == scaleR d y + scaleR c x"
huffman@31445
  1226
  "(scaleR c x + z) + scaleR d y == scaleR d y + (scaleR c x + z)"
huffman@31445
  1227
  "scaleR c x + (scaleR d y + z) == scaleR d y + (scaleR c x + z)"
huffman@31445
  1228
  "(scaleR c x + w) + (scaleR d y + z) == scaleR d y + ((scaleR c x + w) + z)"
huffman@31445
  1229
  by (simp_all add: algebra_simps)
huffman@31445
  1230
huffman@31445
  1231
lemma pth_d:
huffman@31445
  1232
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1233
  shows "x + 0 == x" by simp
huffman@31445
  1234
huffman@31445
  1235
lemma norm_imp_pos_and_ge:
huffman@31445
  1236
  fixes x :: "'a::real_normed_vector"
huffman@31445
  1237
  shows "norm x == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
huffman@31445
  1238
  by atomize auto
chaieb@29842
  1239
chaieb@29842
  1240
lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
chaieb@29842
  1241
huffman@30489
  1242
lemma norm_pths:
huffman@31445
  1243
  fixes x :: "'a::real_normed_vector" shows
huffman@31445
  1244
  "x = y \<longleftrightarrow> norm (x - y) \<le> 0"
chaieb@29842
  1245
  "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
huffman@30041
  1246
  using norm_ge_zero[of "x - y"] by auto
chaieb@29842
  1247
huffman@31344
  1248
lemma vector_dist_norm:
huffman@31445
  1249
  fixes x :: "'a::real_normed_vector"
huffman@31344
  1250
  shows "dist x y = norm (x - y)"
huffman@31344
  1251
  by (rule dist_norm)
huffman@31344
  1252
chaieb@29842
  1253
use "normarith.ML"
chaieb@29842
  1254
wenzelm@30549
  1255
method_setup norm = {* Scan.succeed (SIMPLE_METHOD' o NormArith.norm_arith_tac)
chaieb@29842
  1256
*} "Proves simple linear statements about vector norms"
chaieb@29842
  1257
chaieb@29842
  1258
chaieb@29842
  1259
text{* Hence more metric properties. *}
chaieb@29842
  1260
huffman@31289
  1261
lemma dist_triangle_alt:
huffman@31289
  1262
  fixes x y z :: "'a::metric_space"
huffman@31289
  1263
  shows "dist y z <= dist x y + dist x z"
huffman@31285
  1264
using dist_triangle [of y z x] by (simp add: dist_commute)
huffman@31285
  1265
huffman@31289
  1266
lemma dist_pos_lt:
huffman@31289
  1267
  fixes x y :: "'a::metric_space"
huffman@31289
  1268
  shows "x \<noteq> y ==> 0 < dist x y"
huffman@31289
  1269
by (simp add: zero_less_dist_iff)
huffman@31289
  1270
huffman@31289
  1271
lemma dist_nz:
huffman@31289
  1272
  fixes x y :: "'a::metric_space"
huffman@31289
  1273
  shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
huffman@31289
  1274
by (simp add: zero_less_dist_iff)
huffman@31289
  1275
huffman@31289
  1276
lemma dist_triangle_le:
huffman@31289
  1277
  fixes x y z :: "'a::metric_space"
huffman@31289
  1278
  shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
huffman@31285
  1279
by (rule order_trans [OF dist_triangle2])
huffman@31285
  1280
huffman@31289
  1281
lemma dist_triangle_lt:
huffman@31289
  1282
  fixes x y z :: "'a::metric_space"
huffman@31289
  1283
  shows "dist x z + dist y z < e ==> dist x y < e"
huffman@31285
  1284
by (rule le_less_trans [OF dist_triangle2])
huffman@31285
  1285
huffman@31285
  1286
lemma dist_triangle_half_l:
huffman@31289
  1287
  fixes x1 x2 y :: "'a::metric_space"
huffman@31289
  1288
  shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
huffman@31285
  1289
by (rule dist_triangle_lt [where z=y], simp)
huffman@31285
  1290
huffman@31285
  1291
lemma dist_triangle_half_r:
huffman@31289
  1292
  fixes x1 x2 y :: "'a::metric_space"
huffman@31289
  1293
  shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
huffman@31285
  1294
by (rule dist_triangle_half_l, simp_all add: dist_commute)
chaieb@29842
  1295
huffman@31289
  1296
lemma dist_triangle_add:
huffman@31289
  1297
  fixes x y x' y' :: "'a::real_normed_vector"
huffman@31289
  1298
  shows "dist (x + y) (x' + y') <= dist x x' + dist y y'"
huffman@31445
  1299
  by norm
huffman@30489
  1300
huffman@30489
  1301
lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
huffman@31289
  1302
  unfolding dist_norm vector_ssub_ldistrib[symmetric] norm_mul ..
huffman@30489
  1303
huffman@31285
  1304
lemma dist_triangle_add_half:
huffman@31289
  1305
  fixes x x' y y' :: "'a::real_normed_vector"
huffman@31289
  1306
  shows "dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 \<Longrightarrow> dist(x + y) (x' + y') < e"
huffman@31445
  1307
  by norm
chaieb@29842
  1308
huffman@30582
  1309
lemma setsum_component [simp]:
huffman@30582
  1310
  fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
huffman@30582
  1311
  shows "(setsum f S)$i = setsum (\<lambda>x. (f x)$i) S"
huffman@30582
  1312
  by (cases "finite S", induct S set: finite, simp_all)
huffman@30582
  1313
chaieb@29842
  1314
lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)$i ) S)"
huffman@30582
  1315
  by (simp add: Cart_eq)
chaieb@29842
  1316
huffman@30489
  1317
lemma setsum_clauses:
chaieb@29842
  1318
  shows "setsum f {} = 0"
chaieb@29842
  1319
  and "finite S \<Longrightarrow> setsum f (insert x S) =
chaieb@29842
  1320
                 (if x \<in> S then setsum f S else f x + setsum f S)"
chaieb@29842
  1321
  by (auto simp add: insert_absorb)
chaieb@29842
  1322
huffman@30489
  1323
lemma setsum_cmul:
chaieb@29842
  1324
  fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
chaieb@29842
  1325
  shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
huffman@30582
  1326
  by (simp add: Cart_eq setsum_right_distrib)
chaieb@29842
  1327
huffman@30489
  1328
lemma setsum_norm:
chaieb@29842
  1329
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
chaieb@29842
  1330
  assumes fS: "finite S"
chaieb@29842
  1331
  shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
chaieb@29842
  1332
proof(induct rule: finite_induct[OF fS])
huffman@30041
  1333
  case 1 thus ?case by simp
chaieb@29842
  1334
next
chaieb@29842
  1335
  case (2 x S)
chaieb@29842
  1336
  from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
chaieb@29842
  1337
  also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
chaieb@29842
  1338
    using "2.hyps" by simp
chaieb@29842
  1339
  finally  show ?case  using "2.hyps" by simp
chaieb@29842
  1340
qed
chaieb@29842
  1341
huffman@30489
  1342
lemma real_setsum_norm:
huffman@30582
  1343
  fixes f :: "'a \<Rightarrow> real ^'n::finite"
chaieb@29842
  1344
  assumes fS: "finite S"
chaieb@29842
  1345
  shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
chaieb@29842
  1346
proof(induct rule: finite_induct[OF fS])
huffman@30040
  1347
  case 1 thus ?case by simp
chaieb@29842
  1348
next
chaieb@29842
  1349
  case (2 x S)
huffman@30040
  1350
  from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
chaieb@29842
  1351
  also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
chaieb@29842
  1352
    using "2.hyps" by simp
chaieb@29842
  1353
  finally  show ?case  using "2.hyps" by simp
chaieb@29842
  1354
qed
chaieb@29842
  1355
huffman@30489
  1356
lemma setsum_norm_le:
chaieb@29842
  1357
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
chaieb@29842
  1358
  assumes fS: "finite S"
chaieb@29842
  1359
  and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
chaieb@29842
  1360
  shows "norm (setsum f S) \<le> setsum g S"
chaieb@29842
  1361
proof-
huffman@30489
  1362
  from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
chaieb@29842
  1363
    by - (rule setsum_mono, simp)
chaieb@29842
  1364
  then show ?thesis using setsum_norm[OF fS, of f] fg
chaieb@29842
  1365
    by arith
chaieb@29842
  1366
qed
chaieb@29842
  1367
huffman@30489
  1368
lemma real_setsum_norm_le:
huffman@30582
  1369
  fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
chaieb@29842
  1370
  assumes fS: "finite S"
chaieb@29842
  1371
  and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
chaieb@29842
  1372
  shows "norm (setsum f S) \<le> setsum g S"
chaieb@29842
  1373
proof-
huffman@30489
  1374
  from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
chaieb@29842
  1375
    by - (rule setsum_mono, simp)
chaieb@29842
  1376
  then show ?thesis using real_setsum_norm[OF fS, of f] fg
chaieb@29842
  1377
    by arith
chaieb@29842
  1378
qed
chaieb@29842
  1379
chaieb@29842
  1380
lemma setsum_norm_bound:
chaieb@29842
  1381
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
chaieb@29842
  1382
  assumes fS: "finite S"
chaieb@29842
  1383
  and K: "\<forall>x \<in> S. norm (f x) \<le> K"
chaieb@29842
  1384
  shows "norm (setsum f S) \<le> of_nat (card S) * K"
chaieb@29842
  1385
  using setsum_norm_le[OF fS K] setsum_constant[symmetric]
chaieb@29842
  1386
  by simp
chaieb@29842
  1387
chaieb@29842
  1388
lemma real_setsum_norm_bound:
huffman@30582
  1389
  fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
chaieb@29842
  1390
  assumes fS: "finite S"
chaieb@29842
  1391
  and K: "\<forall>x \<in> S. norm (f x) \<le> K"
chaieb@29842
  1392
  shows "norm (setsum f S) \<le> of_nat (card S) * K"
chaieb@29842
  1393
  using real_setsum_norm_le[OF fS K] setsum_constant[symmetric]
chaieb@29842
  1394
  by simp
chaieb@29842
  1395
chaieb@29842
  1396
lemma setsum_vmul:
chaieb@29842
  1397
  fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector,semiring, mult_zero}"
chaieb@29842
  1398
  assumes fS: "finite S"
chaieb@29842
  1399
  shows "setsum f S *s v = setsum (\<lambda>x. f x *s v) S"
chaieb@29842
  1400
proof(induct rule: finite_induct[OF fS])
chaieb@29842
  1401
  case 1 then show ?case by (simp add: vector_smult_lzero)
chaieb@29842
  1402
next
chaieb@29842
  1403
  case (2 x F)
huffman@30489
  1404
  from "2.hyps" have "setsum f (insert x F) *s v = (f x + setsum f F) *s v"
chaieb@29842
  1405
    by simp
huffman@30489
  1406
  also have "\<dots> = f x *s v + setsum f F *s v"
chaieb@29842
  1407
    by (simp add: vector_sadd_rdistrib)
chaieb@29842
  1408
  also have "\<dots> = setsum (\<lambda>x. f x *s v) (insert x F)" using "2.hyps" by simp
chaieb@29842
  1409
  finally show ?case .
chaieb@29842
  1410
qed
chaieb@29842
  1411
chaieb@29842
  1412
(* FIXME : Problem thm setsum_vmul[of _ "f:: 'a \<Rightarrow> real ^'n"]  ---
chaieb@29842
  1413
 Get rid of *s and use real_vector instead! Also prove that ^ creates a real_vector !! *)
chaieb@29842
  1414
chaieb@29842
  1415
    (* FIXME: Here too need stupid finiteness assumption on T!!! *)
chaieb@29842
  1416
lemma setsum_group:
chaieb@29842
  1417
  assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
chaieb@29842
  1418
  shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
huffman@30489
  1419
chaieb@29842
  1420
apply (subst setsum_image_gen[OF fS, of g f])
chaieb@30263
  1421
apply (rule setsum_mono_zero_right[OF fT fST])
chaieb@29842
  1422
by (auto intro: setsum_0')
chaieb@29842
  1423
chaieb@29842
  1424
lemma vsum_norm_allsubsets_bound:
huffman@30582
  1425
  fixes f:: "'a \<Rightarrow> real ^'n::finite"
huffman@30489
  1426
  assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
huffman@30582
  1427
  shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
chaieb@29842
  1428
proof-
huffman@30582
  1429
  let ?d = "real CARD('n)"
chaieb@29842
  1430
  let ?nf = "\<lambda>x. norm (f x)"
huffman@30582
  1431
  let ?U = "UNIV :: 'n set"
chaieb@29842
  1432
  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"
chaieb@29842
  1433
    by (rule setsum_commute)
chaieb@29842
  1434
  have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
chaieb@29842
  1435
  have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $ i\<bar>) ?U) P"
chaieb@29842
  1436
    apply (rule setsum_mono)
chaieb@29842
  1437
    by (rule norm_le_l1)
chaieb@29842
  1438
  also have "\<dots> \<le> 2 * ?d * e"
chaieb@29842
  1439
    unfolding th0 th1
chaieb@29842
  1440
  proof(rule setsum_bounded)
chaieb@29842
  1441
    fix i assume i: "i \<in> ?U"
chaieb@29842
  1442
    let ?Pp = "{x. x\<in> P \<and> f x $ i \<ge> 0}"
chaieb@29842
  1443
    let ?Pn = "{x. x \<in> P \<and> f x $ i < 0}"
chaieb@29842
  1444
    have thp: "P = ?Pp \<union> ?Pn" by auto
chaieb@29842
  1445
    have thp0: "?Pp \<inter> ?Pn ={}" by auto
chaieb@29842
  1446
    have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
chaieb@29842
  1447
    have Ppe:"setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pp \<le> e"
huffman@30582
  1448
      using component_le_norm[of "setsum (\<lambda>x. f x) ?Pp" i]  fPs[OF PpP]
huffman@30582
  1449
      by (auto intro: abs_le_D1)
chaieb@29842
  1450
    have Pne: "setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pn \<le> e"
huffman@30582
  1451
      using component_le_norm[of "setsum (\<lambda>x. - f x) ?Pn" i]  fPs[OF PnP]
huffman@30582
  1452
      by (auto simp add: setsum_negf intro: abs_le_D1)
huffman@30489
  1453
    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"
chaieb@29842
  1454
      apply (subst thp)
huffman@30489
  1455
      apply (rule setsum_Un_zero)
chaieb@29842
  1456
      using fP thp0 by auto
chaieb@29842
  1457
    also have "\<dots> \<le> 2*e" using Pne Ppe by arith
chaieb@29842
  1458
    finally show "setsum (\<lambda>x. \<bar>f x $ i\<bar>) P \<le> 2*e" .
chaieb@29842
  1459
  qed
chaieb@29842
  1460
  finally show ?thesis .
chaieb@29842
  1461
qed
chaieb@29842
  1462
chaieb@29842
  1463
lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> (y::'a::{comm_ring}^'n) = setsum (\<lambda>x. f x \<bullet> y) S "
chaieb@30263
  1464
  by (induct rule: finite_induct, auto simp add: dot_lzero dot_ladd dot_radd)
chaieb@29842
  1465
chaieb@29842
  1466
lemma dot_rsum: "finite S \<Longrightarrow> (y::'a::{comm_ring}^'n) \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
chaieb@29842
  1467
  by (induct rule: finite_induct, auto simp add: dot_rzero dot_radd)
chaieb@29842
  1468
chaieb@29842
  1469
subsection{* Basis vectors in coordinate directions. *}
chaieb@29842
  1470
chaieb@29842
  1471
chaieb@29842
  1472
definition "basis k = (\<chi> i. if i = k then 1 else 0)"
chaieb@29842
  1473
huffman@30582
  1474
lemma basis_component [simp]: "basis k $ i = (if k=i then 1 else 0)"
huffman@30582
  1475
  unfolding basis_def by simp
huffman@30582
  1476
huffman@30489
  1477
lemma delta_mult_idempotent:
chaieb@29842
  1478
  "(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)
chaieb@29842
  1479
chaieb@29842
  1480
lemma norm_basis:
huffman@30582
  1481
  shows "norm (basis k :: real ^'n::finite) = 1"
chaieb@29842
  1482
  apply (simp add: basis_def real_vector_norm_def dot_def)
chaieb@29842
  1483
  apply (vector delta_mult_idempotent)
huffman@30582
  1484
  using setsum_delta[of "UNIV :: 'n set" "k" "\<lambda>k. 1::real"]
chaieb@29842
  1485
  apply auto
chaieb@29842
  1486
  done
chaieb@29842
  1487
huffman@30582
  1488
lemma norm_basis_1: "norm(basis 1 :: real ^'n::{finite,one}) = 1"
huffman@30582
  1489
  by (rule norm_basis)
huffman@30582
  1490
huffman@30582
  1491
lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n::finite). norm x = c"
huffman@30582
  1492
  apply (rule exI[where x="c *s basis arbitrary"])
huffman@30582
  1493
  by (simp only: norm_mul norm_basis)
chaieb@29842
  1494
huffman@30489
  1495
lemma vector_choose_dist: assumes e: "0 <= e"
huffman@30582
  1496
  shows "\<exists>(y::real^'n::finite). dist x y = e"
chaieb@29842
  1497
proof-
chaieb@29842
  1498
  from vector_choose_size[OF e] obtain c:: "real ^'n"  where "norm c = e"
chaieb@29842
  1499
    by blast
huffman@31289
  1500
  then have "dist x (x - c) = e" by (simp add: dist_norm)
chaieb@29842
  1501
  then show ?thesis by blast
chaieb@29842
  1502
qed
chaieb@29842
  1503
huffman@30582
  1504
lemma basis_inj: "inj (basis :: 'n \<Rightarrow> real ^'n::finite)"
huffman@30582
  1505
  by (simp add: inj_on_def Cart_eq)
chaieb@29842
  1506
chaieb@29842
  1507
lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
chaieb@29842
  1508
  by auto
chaieb@29842
  1509
chaieb@29842
  1510
lemma basis_expansion:
huffman@30582
  1511
  "setsum (\<lambda>i. (x$i) *s basis i) UNIV = (x::('a::ring_1) ^'n::finite)" (is "?lhs = ?rhs" is "setsum ?f ?S = _")
huffman@30582
  1512
  by (auto simp add: Cart_eq cond_value_iff setsum_delta[of "?S", where ?'b = "'a", simplified] cong del: if_weak_cong)
chaieb@29842
  1513
huffman@30489
  1514
lemma basis_expansion_unique:
huffman@30582
  1515
  "setsum (\<lambda>i. f i *s basis i) UNIV = (x::('a::comm_ring_1) ^'n::finite) \<longleftrightarrow> (\<forall>i. f i = x$i)"
huffman@30582
  1516
  by (simp add: Cart_eq setsum_delta cond_value_iff cong del: if_weak_cong)
chaieb@29842
  1517
chaieb@29842
  1518
lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
chaieb@29842
  1519
  by auto
chaieb@29842
  1520
chaieb@29842
  1521
lemma dot_basis:
huffman@30582
  1522
  shows "basis i \<bullet> x = x$i" "x \<bullet> (basis i :: 'a^'n::finite) = (x$i :: 'a::semiring_1)"
huffman@30582
  1523
  by (auto simp add: dot_def basis_def cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
huffman@30582
  1524
huffman@30582
  1525
lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> False"
huffman@30582
  1526
  by (auto simp add: Cart_eq)
chaieb@29842
  1527
huffman@30489
  1528
lemma basis_nonzero:
chaieb@29842
  1529
  shows "basis k \<noteq> (0:: 'a::semiring_1 ^'n)"
huffman@30582
  1530
  by (simp add: basis_eq_0)
huffman@30582
  1531
huffman@30582
  1532
lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n::finite)"
chaieb@29842
  1533
  apply (auto simp add: Cart_eq dot_basis)
chaieb@29842
  1534
  apply (erule_tac x="basis i" in allE)
chaieb@29842
  1535
  apply (simp add: dot_basis)
chaieb@29842
  1536
  apply (subgoal_tac "y = z")
chaieb@29842
  1537
  apply simp
huffman@30582
  1538
  apply (simp add: Cart_eq)
chaieb@29842
  1539
  done
chaieb@29842
  1540
huffman@30582
  1541
lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n::finite)"
chaieb@29842
  1542
  apply (auto simp add: Cart_eq dot_basis)
chaieb@29842
  1543
  apply (erule_tac x="basis i" in allE)
chaieb@29842
  1544
  apply (simp add: dot_basis)
chaieb@29842
  1545
  apply (subgoal_tac "x = y")
chaieb@29842
  1546
  apply simp
huffman@30582
  1547
  apply (simp add: Cart_eq)
chaieb@29842
  1548
  done
chaieb@29842
  1549
chaieb@29842
  1550
subsection{* Orthogonality. *}
chaieb@29842
  1551
chaieb@29842
  1552
definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
chaieb@29842
  1553
chaieb@29842
  1554
lemma orthogonal_basis:
huffman@30582
  1555
  shows "orthogonal (basis i :: 'a^'n::finite) x \<longleftrightarrow> x$i = (0::'a::ring_1)"
huffman@30582
  1556
  by (auto simp add: orthogonal_def dot_def basis_def cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
chaieb@29842
  1557
chaieb@29842
  1558
lemma orthogonal_basis_basis:
huffman@30582
  1559
  shows "orthogonal (basis i :: 'a::ring_1^'n::finite) (basis j) \<longleftrightarrow> i \<noteq> j"
huffman@30582
  1560
  unfolding orthogonal_basis[of i] basis_component[of j] by simp
chaieb@29842
  1561
chaieb@29842
  1562
  (* FIXME : Maybe some of these require less than comm_ring, but not all*)
chaieb@29842
  1563
lemma orthogonal_clauses:
chaieb@29842
  1564
  "orthogonal a (0::'a::comm_ring ^'n)"
chaieb@29842
  1565
  "orthogonal a x ==> orthogonal a (c *s x)"
chaieb@29842
  1566
  "orthogonal a x ==> orthogonal a (-x)"
chaieb@29842
  1567
  "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x + y)"
chaieb@29842
  1568
  "orthogonal a x \<Longrightarrow> orthogonal a y ==> orthogonal a (x - y)"
chaieb@29842
  1569
  "orthogonal 0 a"
chaieb@29842
  1570
  "orthogonal x a ==> orthogonal (c *s x) a"
chaieb@29842
  1571
  "orthogonal x a ==> orthogonal (-x) a"
chaieb@29842
  1572
  "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x + y) a"
chaieb@29842
  1573
  "orthogonal x a \<Longrightarrow> orthogonal y a ==> orthogonal (x - y) a"
chaieb@29842
  1574
  unfolding orthogonal_def dot_rneg dot_rmult dot_radd dot_rsub
chaieb@29842
  1575
  dot_lzero dot_rzero dot_lneg dot_lmult dot_ladd dot_lsub
chaieb@29842
  1576
  by simp_all
chaieb@29842
  1577
chaieb@29842
  1578
lemma orthogonal_commute: "orthogonal (x::'a::{ab_semigroup_mult,comm_monoid_add} ^'n)y \<longleftrightarrow> orthogonal y x"
chaieb@29842
  1579
  by (simp add: orthogonal_def dot_sym)
chaieb@29842
  1580
chaieb@29842
  1581
subsection{* Explicit vector construction from lists. *}
chaieb@29842
  1582
huffman@30582
  1583
primrec from_nat :: "nat \<Rightarrow> 'a::{monoid_add,one}"
huffman@30582
  1584
where "from_nat 0 = 0" | "from_nat (Suc n) = 1 + from_nat n"
huffman@30582
  1585
huffman@30582
  1586
lemma from_nat [simp]: "from_nat = of_nat"
huffman@30582
  1587
by (rule ext, induct_tac x, simp_all)
huffman@30582
  1588
huffman@30582
  1589
primrec
huffman@30582
  1590
  list_fun :: "nat \<Rightarrow> _ list \<Rightarrow> _ \<Rightarrow> _"
huffman@30582
  1591
where
huffman@30582
  1592
  "list_fun n [] = (\<lambda>x. 0)"
huffman@30582
  1593
| "list_fun n (x # xs) = fun_upd (list_fun (Suc n) xs) (from_nat n) x"
huffman@30582
  1594
huffman@30582
  1595
definition "vector l = (\<chi> i. list_fun 1 l i)"
huffman@30582
  1596
(*definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"*)
chaieb@29842
  1597
chaieb@29842
  1598
lemma vector_1: "(vector[x]) $1 = x"
huffman@30582
  1599
  unfolding vector_def by simp
chaieb@29842
  1600
chaieb@29842
  1601
lemma vector_2:
chaieb@29842
  1602
 "(vector[x,y]) $1 = x"
chaieb@29842
  1603
 "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
huffman@30582
  1604
  unfolding vector_def by simp_all
chaieb@29842
  1605
chaieb@29842
  1606
lemma vector_3:
chaieb@29842
  1607
 "(vector [x,y,z] ::('a::zero)^3)$1 = x"
chaieb@29842
  1608
 "(vector [x,y,z] ::('a::zero)^3)$2 = y"
chaieb@29842
  1609
 "(vector [x,y,z] ::('a::zero)^3)$3 = z"
huffman@30582
  1610
  unfolding vector_def by simp_all
chaieb@29842
  1611
chaieb@29842
  1612
lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
chaieb@29842
  1613
  apply auto
chaieb@29842
  1614
  apply (erule_tac x="v$1" in allE)
chaieb@29842
  1615
  apply (subgoal_tac "vector [v$1] = v")
chaieb@29842
  1616
  apply simp
huffman@30582
  1617
  apply (vector vector_def)
huffman@30582
  1618
  apply (simp add: forall_1)
huffman@30582
  1619
  done
chaieb@29842
  1620
chaieb@29842
  1621
lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
chaieb@29842
  1622
  apply auto
chaieb@29842
  1623
  apply (erule_tac x="v$1" in allE)
chaieb@29842
  1624
  apply (erule_tac x="v$2" in allE)
chaieb@29842
  1625
  apply (subgoal_tac "vector [v$1, v$2] = v")
chaieb@29842
  1626
  apply simp
huffman@30582
  1627
  apply (vector vector_def)
huffman@30582
  1628
  apply (simp add: forall_2)
chaieb@29842
  1629
  done
chaieb@29842
  1630
chaieb@29842
  1631
lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
chaieb@29842
  1632
  apply auto
chaieb@29842
  1633
  apply (erule_tac x="v$1" in allE)
chaieb@29842
  1634
  apply (erule_tac x="v$2" in allE)
chaieb@29842
  1635
  apply (erule_tac x="v$3" in allE)
chaieb@29842
  1636
  apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
chaieb@29842
  1637
  apply simp
huffman@30582
  1638
  apply (vector vector_def)
huffman@30582
  1639
  apply (simp add: forall_3)
chaieb@29842
  1640
  done
chaieb@29842
  1641
chaieb@29842
  1642
subsection{* Linear functions. *}
chaieb@29842
  1643
chaieb@29842
  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)"
chaieb@29842
  1645
chaieb@29842
  1646
lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. (c::'a::comm_semiring) *s f x)"
huffman@30582
  1647
  by (vector linear_def Cart_eq ring_simps)
chaieb@29842
  1648
chaieb@29842
  1649
lemma linear_compose_neg: "linear (f :: 'a ^'n \<Rightarrow> 'a::comm_ring ^'m) ==> linear (\<lambda>x. -(f(x)))" by (vector linear_def Cart_eq)
chaieb@29842
  1650
chaieb@29842
  1651
lemma linear_compose_add: "linear (f :: 'a ^'n \<Rightarrow> 'a::semiring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
chaieb@29842
  1652
  by (vector linear_def Cart_eq ring_simps)
chaieb@29842
  1653
chaieb@29842
  1654
lemma linear_compose_sub: "linear (f :: 'a ^'n \<Rightarrow> 'a::ring_1 ^'m) \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
chaieb@29842
  1655
  by (vector linear_def Cart_eq ring_simps)
chaieb@29842
  1656
chaieb@29842
  1657
lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
chaieb@29842
  1658
  by (simp add: linear_def)
chaieb@29842
  1659
chaieb@29842
  1660
lemma linear_id: "linear id" by (simp add: linear_def id_def)
chaieb@29842
  1661
chaieb@29842
  1662
lemma linear_zero: "linear (\<lambda>x. 0::'a::semiring_1 ^ 'n)" by (simp add: linear_def)
chaieb@29842
  1663
chaieb@29842
  1664
lemma linear_compose_setsum:
chaieb@29842
  1665
  assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a :: 'a::semiring_1 ^ 'n \<Rightarrow> 'a ^ 'm)"
chaieb@29842
  1666
  shows "linear(\<lambda>x. setsum (\<lambda>a. f a x :: 'a::semiring_1 ^'m) S)"
chaieb@29842
  1667
  using lS
chaieb@29842
  1668
  apply (induct rule: finite_induct[OF fS])
chaieb@29842
  1669
  by (auto simp add: linear_zero intro: linear_compose_add)
chaieb@29842
  1670
chaieb@29842
  1671
lemma linear_vmul_component:
chaieb@29842
  1672
  fixes f:: "'a::semiring_1^'m \<Rightarrow> 'a^'n"
huffman@30582
  1673
  assumes lf: "linear f"
chaieb@29842
  1674
  shows "linear (\<lambda>x. f x $ k *s v)"
huffman@30582
  1675
  using lf
chaieb@29842
  1676
  apply (auto simp add: linear_def )
chaieb@29842
  1677
  by (vector ring_simps)+
chaieb@29842
  1678
chaieb@29842
  1679
lemma linear_0: "linear f ==> f 0 = (0::'a::semiring_1 ^'n)"
chaieb@29842
  1680
  unfolding linear_def
chaieb@29842
  1681
  apply clarsimp
chaieb@29842
  1682
  apply (erule allE[where x="0::'a"])
chaieb@29842
  1683
  apply simp
chaieb@29842
  1684
  done
chaieb@29842
  1685
chaieb@29842
  1686
lemma linear_cmul: "linear f ==> f(c*s x) = c *s f x" by (simp add: linear_def)
chaieb@29842
  1687
chaieb@29842
  1688
lemma linear_neg: "linear (f :: 'a::ring_1 ^'n \<Rightarrow> _) ==> f (-x) = - f x"
chaieb@29842
  1689
  unfolding vector_sneg_minus1
huffman@30489
  1690
  using linear_cmul[of f] by auto
huffman@30489
  1691
huffman@30489
  1692
lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def)
chaieb@29842
  1693
chaieb@29842
  1694
lemma linear_sub: "linear (f::'a::ring_1 ^'n \<Rightarrow> _) ==> f(x - y) = f x - f y"
chaieb@29842
  1695
  by (simp add: diff_def linear_add linear_neg)
chaieb@29842
  1696
huffman@30489
  1697
lemma linear_setsum:
chaieb@29842
  1698
  fixes f:: "'a::semiring_1^'n \<Rightarrow> _"
chaieb@29842
  1699
  assumes lf: "linear f" and fS: "finite S"
chaieb@29842
  1700
  shows "f (setsum g S) = setsum (f o g) S"
chaieb@29842
  1701
proof (induct rule: finite_induct[OF fS])
chaieb@29842
  1702
  case 1 thus ?case by (simp add: linear_0[OF lf])
chaieb@29842
  1703
next
chaieb@29842
  1704
  case (2 x F)
chaieb@29842
  1705
  have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
chaieb@29842
  1706
    by simp
chaieb@29842
  1707
  also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
chaieb@29842
  1708
  also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
chaieb@29842
  1709
  finally show ?case .
chaieb@29842
  1710
qed
chaieb@29842
  1711
chaieb@29842
  1712
lemma linear_setsum_mul:
chaieb@29842
  1713
  fixes f:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m"
chaieb@29842
  1714
  assumes lf: "linear f" and fS: "finite S"
chaieb@29842
  1715
  shows "f (setsum (\<lambda>i. c i *s v i) S) = setsum (\<lambda>i. c i *s f (v i)) S"
chaieb@29842
  1716
  using linear_setsum[OF lf fS, of "\<lambda>i. c i *s v i" , unfolded o_def]
huffman@30489
  1717
  linear_cmul[OF lf] by simp
chaieb@29842
  1718
chaieb@29842
  1719
lemma linear_injective_0:
chaieb@29842
  1720
  assumes lf: "linear (f:: 'a::ring_1 ^ 'n \<Rightarrow> _)"
chaieb@29842
  1721
  shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
chaieb@29842
  1722
proof-
chaieb@29842
  1723
  have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
chaieb@29842
  1724
  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
huffman@30489
  1725
  also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
chaieb@29842
  1726
    by (simp add: linear_sub[OF lf])
chaieb@29842
  1727
  also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
chaieb@29842
  1728
  finally show ?thesis .
chaieb@29842
  1729
qed
chaieb@29842
  1730
chaieb@29842
  1731
lemma linear_bounded:
huffman@30582
  1732
  fixes f:: "real ^'m::finite \<Rightarrow> real ^'n::finite"
chaieb@29842
  1733
  assumes lf: "linear f"
chaieb@29842
  1734
  shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
chaieb@29842
  1735
proof-
huffman@30582
  1736
  let ?S = "UNIV:: 'm set"
chaieb@29842
  1737
  let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
chaieb@29842
  1738
  have fS: "finite ?S" by simp
chaieb@29842
  1739
  {fix x:: "real ^ 'm"
huffman@30582
  1740
    let ?g = "(\<lambda>i. (x$i) *s (basis i) :: real ^ 'm)"
chaieb@29842
  1741
    have "norm (f x) = norm (f (setsum (\<lambda>i. (x$i) *s (basis i)) ?S))"
chaieb@29842
  1742
      by (simp only:  basis_expansion)
chaieb@29842
  1743
    also have "\<dots> = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)"
chaieb@29842
  1744
      using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf]
chaieb@29842
  1745
      by auto
chaieb@29842
  1746
    finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)" .
chaieb@29842
  1747
    {fix i assume i: "i \<in> ?S"
huffman@30582
  1748
      from component_le_norm[of x i]
chaieb@29842
  1749
      have "norm ((x$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x"
chaieb@29842
  1750
      unfolding norm_mul
chaieb@29842
  1751
      apply (simp only: mult_commute)
chaieb@29842
  1752
      apply (rule mult_mono)
huffman@30041
  1753
      by (auto simp add: ring_simps norm_ge_zero) }
chaieb@29842
  1754
    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
chaieb@29842
  1755
    from real_setsum_norm_le[OF fS, of "\<lambda>i. (x$i) *s (f (basis i))", OF th]
chaieb@29842
  1756
    have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
chaieb@29842
  1757
  then show ?thesis by blast
chaieb@29842
  1758
qed
chaieb@29842
  1759
chaieb@29842
  1760
lemma linear_bounded_pos:
huffman@30582
  1761
  fixes f:: "real ^'n::finite \<Rightarrow> real ^ 'm::finite"
chaieb@29842
  1762
  assumes lf: "linear f"
chaieb@29842
  1763
  shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
chaieb@29842
  1764
proof-
huffman@30489
  1765
  from linear_bounded[OF lf] obtain B where
chaieb@29842
  1766
    B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
chaieb@29842
  1767
  let ?K = "\<bar>B\<bar> + 1"
chaieb@29842
  1768
  have Kp: "?K > 0" by arith
chaieb@29842
  1769
    {assume C: "B < 0"
huffman@30041
  1770
      have "norm (1::real ^ 'n) > 0" by (simp add: zero_less_norm_iff)
chaieb@29842
  1771
      with C have "B * norm (1:: real ^ 'n) < 0"
chaieb@29842
  1772
	by (simp add: zero_compare_simps)
huffman@30041
  1773
      with B[rule_format, of 1] norm_ge_zero[of "f 1"] have False by simp
chaieb@29842
  1774
    }
chaieb@29842
  1775
    then have Bp: "B \<ge> 0" by ferrack
chaieb@29842
  1776
    {fix x::"real ^ 'n"
chaieb@29842
  1777
      have "norm (f x) \<le> ?K *  norm x"
huffman@30041
  1778
      using B[rule_format, of x] norm_ge_zero[of x] norm_ge_zero[of "f x"] Bp
huffman@30040
  1779
      apply (auto simp add: ring_simps split add: abs_split)
huffman@30040
  1780
      apply (erule order_trans, simp)
huffman@30040
  1781
      done
chaieb@29842
  1782
  }
chaieb@29842
  1783
  then show ?thesis using Kp by blast
chaieb@29842
  1784
qed
chaieb@29842
  1785
huffman@31529
  1786
lemma smult_conv_scaleR: "c *s x = scaleR c x"
huffman@31529
  1787
  unfolding vector_scalar_mult_def vector_scaleR_def by simp
huffman@31529
  1788
huffman@31529
  1789
lemma linear_conv_bounded_linear:
huffman@31529
  1790
  fixes f :: "real ^ _ \<Rightarrow> real ^ _"
huffman@31529
  1791
  shows "linear f \<longleftrightarrow> bounded_linear f"
huffman@31529
  1792
proof
huffman@31529
  1793
  assume "linear f"
huffman@31529
  1794
  show "bounded_linear f"
huffman@31529
  1795
  proof
huffman@31529
  1796
    fix x y show "f (x + y) = f x + f y"
huffman@31529
  1797
      using `linear f` unfolding linear_def by simp
huffman@31529
  1798
  next
huffman@31529
  1799
    fix r x show "f (scaleR r x) = scaleR r (f x)"
huffman@31529
  1800
      using `linear f` unfolding linear_def
huffman@31529
  1801
      by (simp add: smult_conv_scaleR)
huffman@31529
  1802
  next
huffman@31529
  1803
    have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
huffman@31529
  1804
      using `linear f` by (rule linear_bounded)
huffman@31529
  1805
    thus "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
huffman@31529
  1806
      by (simp add: mult_commute)
huffman@31529
  1807
  qed
huffman@31529
  1808
next
huffman@31529
  1809
  assume "bounded_linear f"
huffman@31529
  1810
  then interpret f: bounded_linear f .
huffman@31529
  1811
  show "linear f"
huffman@31529
  1812
    unfolding linear_def smult_conv_scaleR
huffman@31529
  1813
    by (simp add: f.add f.scaleR)
huffman@31529
  1814
qed
huffman@31529
  1815
chaieb@29842
  1816
subsection{* Bilinear functions. *}
chaieb@29842
  1817
chaieb@29842
  1818
definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
chaieb@29842
  1819
chaieb@29842
  1820
lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
chaieb@29842
  1821
  by (simp add: bilinear_def linear_def)
chaieb@29842
  1822
lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
chaieb@29842
  1823
  by (simp add: bilinear_def linear_def)
chaieb@29842
  1824
chaieb@29842
  1825
lemma bilinear_lmul: "bilinear h ==> h (c *s x) y = c *s (h x y)"
chaieb@29842
  1826
  by (simp add: bilinear_def linear_def)
chaieb@29842
  1827
chaieb@29842
  1828
lemma bilinear_rmul: "bilinear h ==> h x (c *s y) = c *s (h x y)"
chaieb@29842
  1829
  by (simp add: bilinear_def linear_def)
chaieb@29842
  1830
chaieb@29842
  1831
lemma bilinear_lneg: "bilinear h ==> h (- (x:: 'a::ring_1 ^ 'n)) y = -(h x y)"
chaieb@29842
  1832
  by (simp only: vector_sneg_minus1 bilinear_lmul)
chaieb@29842
  1833
chaieb@29842
  1834
lemma bilinear_rneg: "bilinear h ==> h x (- (y:: 'a::ring_1 ^ 'n)) = - h x y"
chaieb@29842
  1835
  by (simp only: vector_sneg_minus1 bilinear_rmul)
chaieb@29842
  1836
chaieb@29842
  1837
lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
chaieb@29842
  1838
  using add_imp_eq[of x y 0] by auto
huffman@30489
  1839
huffman@30489
  1840
lemma bilinear_lzero:
chaieb@29842
  1841
  fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h 0 x = 0"
huffman@30489
  1842
  using bilinear_ladd[OF bh, of 0 0 x]
chaieb@29842
  1843
    by (simp add: eq_add_iff ring_simps)
chaieb@29842
  1844
huffman@30489
  1845
lemma bilinear_rzero:
chaieb@29842
  1846
  fixes h :: "'a::ring^'n \<Rightarrow> _" assumes bh: "bilinear h" shows "h x 0 = 0"
huffman@30489
  1847
  using bilinear_radd[OF bh, of x 0 0 ]
chaieb@29842
  1848
    by (simp add: eq_add_iff ring_simps)
chaieb@29842
  1849
chaieb@29842
  1850
lemma bilinear_lsub: "bilinear h ==> h (x - (y:: 'a::ring_1 ^ 'n)) z = h x z - h y z"
chaieb@29842
  1851
  by (simp  add: diff_def bilinear_ladd bilinear_lneg)
chaieb@29842
  1852
chaieb@29842
  1853
lemma bilinear_rsub: "bilinear h ==> h z (x - (y:: 'a::ring_1 ^ 'n)) = h z x - h z y"
chaieb@29842
  1854
  by (simp  add: diff_def bilinear_radd bilinear_rneg)
chaieb@29842
  1855
chaieb@29842
  1856
lemma bilinear_setsum:
chaieb@29842
  1857
  fixes h:: "'a ^'n \<Rightarrow> 'a::semiring_1^'m \<Rightarrow> 'a ^ 'k"
chaieb@29842
  1858
  assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
chaieb@29842
  1859
  shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
huffman@30489
  1860
proof-
chaieb@29842
  1861
  have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
chaieb@29842
  1862
    apply (rule linear_setsum[unfolded o_def])
chaieb@29842
  1863
    using bh fS by (auto simp add: bilinear_def)
chaieb@29842
  1864
  also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
chaieb@29842
  1865
    apply (rule setsum_cong, simp)
chaieb@29842
  1866
    apply (rule linear_setsum[unfolded o_def])
chaieb@29842
  1867
    using bh fT by (auto simp add: bilinear_def)
chaieb@29842
  1868
  finally show ?thesis unfolding setsum_cartesian_product .
chaieb@29842
  1869
qed
chaieb@29842
  1870
chaieb@29842
  1871
lemma bilinear_bounded:
huffman@30582
  1872
  fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
chaieb@29842
  1873
  assumes bh: "bilinear h"
chaieb@29842
  1874
  shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@30489
  1875
proof-
huffman@30582
  1876
  let ?M = "UNIV :: 'm set"
huffman@30582
  1877
  let ?N = "UNIV :: 'n set"
chaieb@29842
  1878
  let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
chaieb@29842
  1879
  have fM: "finite ?M" and fN: "finite ?N" by simp_all
chaieb@29842
  1880
  {fix x:: "real ^ 'm" and  y :: "real^'n"
chaieb@29842
  1881
    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 ..
chaieb@29842
  1882
    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] ..
chaieb@29842
  1883
    finally have th: "norm (h x y) = \<dots>" .
chaieb@29842
  1884
    have "norm (h x y) \<le> ?B * norm x * norm y"
chaieb@29842
  1885
      apply (simp add: setsum_left_distrib th)
chaieb@29842
  1886
      apply (rule real_setsum_norm_le)
chaieb@29842
  1887
      using fN fM
chaieb@29842
  1888
      apply simp
chaieb@29842
  1889
      apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] norm_mul ring_simps)
chaieb@29842
  1890
      apply (rule mult_mono)
huffman@30041
  1891
      apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
chaieb@29842
  1892
      apply (rule mult_mono)
huffman@30041
  1893
      apply (auto simp add: norm_ge_zero zero_le_mult_iff component_le_norm)
chaieb@29842
  1894
      done}
chaieb@29842
  1895
  then show ?thesis by metis
chaieb@29842
  1896
qed
chaieb@29842
  1897
chaieb@29842
  1898
lemma bilinear_bounded_pos:
huffman@30582
  1899
  fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
chaieb@29842
  1900
  assumes bh: "bilinear h"
chaieb@29842
  1901
  shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
chaieb@29842
  1902
proof-
huffman@30489
  1903
  from bilinear_bounded[OF bh] obtain B where
chaieb@29842
  1904
    B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
chaieb@29842
  1905
  let ?K = "\<bar>B\<bar> + 1"
chaieb@29842
  1906
  have Kp: "?K > 0" by arith
chaieb@29842
  1907
  have KB: "B < ?K" by arith
chaieb@29842
  1908
  {fix x::"real ^'m" and y :: "real ^'n"
chaieb@29842
  1909
    from KB Kp
chaieb@29842
  1910
    have "B * norm x * norm y \<le> ?K * norm x * norm y"
huffman@30489
  1911
      apply -
chaieb@29842
  1912
      apply (rule mult_right_mono, rule mult_right_mono)
huffman@30041
  1913
      by (auto simp add: norm_ge_zero)
chaieb@29842
  1914
    then have "norm (h x y) \<le> ?K * norm x * norm y"
huffman@30489
  1915
      using B[rule_format, of x y] by simp}
chaieb@29842
  1916
  with Kp show ?thesis by blast
chaieb@29842
  1917
qed
chaieb@29842
  1918
huffman@31529
  1919
lemma bilinear_conv_bounded_bilinear:
huffman@31529
  1920
  fixes h :: "real ^ _ \<Rightarrow> real ^ _ \<Rightarrow> real ^ _"
huffman@31529
  1921
  shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
huffman@31529
  1922
proof
huffman@31529
  1923
  assume "bilinear h"
huffman@31529
  1924
  show "bounded_bilinear h"
huffman@31529
  1925
  proof
huffman@31529
  1926
    fix x y z show "h (x + y) z = h x z + h y z"
huffman@31529
  1927
      using `bilinear h` unfolding bilinear_def linear_def by simp
huffman@31529
  1928
  next
huffman@31529
  1929
    fix x y z show "h x (y + z) = h x y + h x z"
huffman@31529
  1930
      using `bilinear h` unfolding bilinear_def linear_def by simp
huffman@31529
  1931
  next
huffman@31529
  1932
    fix r x y show "h (scaleR r x) y = scaleR r (h x y)"
huffman@31529
  1933
      using `bilinear h` unfolding bilinear_def linear_def
huffman@31529
  1934
      by (simp add: smult_conv_scaleR)
huffman@31529
  1935
  next
huffman@31529
  1936
    fix r x y show "h x (scaleR r y) = scaleR r (h x y)"
huffman@31529
  1937
      using `bilinear h` unfolding bilinear_def linear_def
huffman@31529
  1938
      by (simp add: smult_conv_scaleR)
huffman@31529
  1939
  next
huffman@31529
  1940
    have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@31529
  1941
      using `bilinear h` by (rule bilinear_bounded)
huffman@31529
  1942
    thus "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
huffman@31529
  1943
      by (simp add: mult_ac)
huffman@31529
  1944
  qed
huffman@31529
  1945
next
huffman@31529
  1946
  assume "bounded_bilinear h"
huffman@31529
  1947
  then interpret h: bounded_bilinear h .
huffman@31529
  1948
  show "bilinear h"
huffman@31529
  1949
    unfolding bilinear_def linear_conv_bounded_linear
huffman@31529
  1950
    using h.bounded_linear_left h.bounded_linear_right
huffman@31529
  1951
    by simp
huffman@31529
  1952
qed
huffman@31529
  1953
chaieb@29842
  1954
subsection{* Adjoints. *}
chaieb@29842
  1955
chaieb@29842
  1956
definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
chaieb@29842
  1957
chaieb@29842
  1958
lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
chaieb@29842
  1959
chaieb@29842
  1960
lemma adjoint_works_lemma:
huffman@30582
  1961
  fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  1962
  assumes lf: "linear f"
chaieb@29842
  1963
  shows "\<forall>x y. f x \<bullet> y = x \<bullet> adjoint f y"
chaieb@29842
  1964
proof-
huffman@30582
  1965
  let ?N = "UNIV :: 'n set"
huffman@30582
  1966
  let ?M = "UNIV :: 'm set"
chaieb@29842
  1967
  have fN: "finite ?N" by simp
chaieb@29842
  1968
  have fM: "finite ?M" by simp
chaieb@29842
  1969
  {fix y:: "'a ^ 'm"
chaieb@29842
  1970
    let ?w = "(\<chi> i. (f (basis i) \<bullet> y)) :: 'a ^ 'n"
chaieb@29842
  1971
    {fix x
chaieb@29842
  1972
      have "f x \<bullet> y = f (setsum (\<lambda>i. (x$i) *s basis i) ?N) \<bullet> y"
chaieb@29842
  1973
	by (simp only: basis_expansion)
chaieb@29842
  1974
      also have "\<dots> = (setsum (\<lambda>i. (x$i) *s f (basis i)) ?N) \<bullet> y"
huffman@30489
  1975
	unfolding linear_setsum[OF lf fN]
chaieb@29842
  1976
	by (simp add: linear_cmul[OF lf])
chaieb@29842
  1977
      finally have "f x \<bullet> y = x \<bullet> ?w"
chaieb@29842
  1978
	apply (simp only: )
huffman@30582
  1979
	apply (simp add: dot_def setsum_left_distrib setsum_right_distrib setsum_commute[of _ ?M ?N] ring_simps)
chaieb@29842
  1980
	done}
chaieb@29842
  1981
  }
huffman@30489
  1982
  then show ?thesis unfolding adjoint_def
chaieb@29842
  1983
    some_eq_ex[of "\<lambda>f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y"]
chaieb@29842
  1984
    using choice_iff[of "\<lambda>a b. \<forall>x. f x \<bullet> a = x \<bullet> b "]
chaieb@29842
  1985
    by metis
chaieb@29842
  1986
qed
chaieb@29842
  1987
chaieb@29842
  1988
lemma adjoint_works:
huffman@30582
  1989
  fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  1990
  assumes lf: "linear f"
chaieb@29842
  1991
  shows "x \<bullet> adjoint f y = f x \<bullet> y"
chaieb@29842
  1992
  using adjoint_works_lemma[OF lf] by metis
chaieb@29842
  1993
chaieb@29842
  1994
chaieb@29842
  1995
lemma adjoint_linear:
huffman@30582
  1996
  fixes f :: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  1997
  assumes lf: "linear f"
chaieb@29842
  1998
  shows "linear (adjoint f)"
chaieb@29842
  1999
  by (simp add: linear_def vector_eq_ldot[symmetric] dot_radd dot_rmult adjoint_works[OF lf])
chaieb@29842
  2000
chaieb@29842
  2001
lemma adjoint_clauses:
huffman@30582
  2002
  fixes f:: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  2003
  assumes lf: "linear f"
chaieb@29842
  2004
  shows "x \<bullet> adjoint f y = f x \<bullet> y"
chaieb@29842
  2005
  and "adjoint f y \<bullet> x = y \<bullet> f x"
chaieb@29842
  2006
  by (simp_all add: adjoint_works[OF lf] dot_sym )
chaieb@29842
  2007
chaieb@29842
  2008
lemma adjoint_adjoint:
huffman@30582
  2009
  fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  2010
  assumes lf: "linear f"
chaieb@29842
  2011
  shows "adjoint (adjoint f) = f"
chaieb@29842
  2012
  apply (rule ext)
chaieb@29842
  2013
  by (simp add: vector_eq_ldot[symmetric] adjoint_clauses[OF adjoint_linear[OF lf]] adjoint_clauses[OF lf])
chaieb@29842
  2014
chaieb@29842
  2015
lemma adjoint_unique:
huffman@30582
  2016
  fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
chaieb@29842
  2017
  assumes lf: "linear f" and u: "\<forall>x y. f' x \<bullet> y = x \<bullet> f y"
chaieb@29842
  2018
  shows "f' = adjoint f"
chaieb@29842
  2019
  apply (rule ext)
chaieb@29842
  2020
  using u
chaieb@29842
  2021
  by (simp add: vector_eq_rdot[symmetric] adjoint_clauses[OF lf])
chaieb@29842
  2022
huffman@29881
  2023
text{* Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"} *}
chaieb@29842
  2024
chaieb@29842
  2025
consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
chaieb@29842
  2026
huffman@30489
  2027
defs (overloaded)
huffman@30582
  2028
matrix_matrix_mult_def: "(m:: ('a::semiring_1) ^'n^'m) \<star> (m' :: 'a ^'p^'n) \<equiv> (\<chi> i j. setsum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
chaieb@29842
  2029
huffman@30489
  2030
abbreviation
chaieb@29842
  2031
  matrix_matrix_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"  (infixl "**" 70)
chaieb@29842
  2032
  where "m ** m' == m\<star> m'"
chaieb@29842
  2033
huffman@30489
  2034
defs (overloaded)
huffman@30582
  2035
  matrix_vector_mult_def: "(m::('a::semiring_1) ^'n^'m) \<star> (x::'a ^'n) \<equiv> (\<chi> i. setsum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
chaieb@29842
  2036
huffman@30489
  2037
abbreviation
chaieb@29842
  2038
  matrix_vector_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"  (infixl "*v" 70)
huffman@30489
  2039
  where
chaieb@29842
  2040
  "m *v v == m \<star> v"
chaieb@29842
  2041
huffman@30489
  2042
defs (overloaded)
huffman@30582
  2043
  vector_matrix_mult_def: "(x::'a^'m) \<star> (m::('a::semiring_1) ^'n^'m) \<equiv> (\<chi> j. setsum (\<lambda>i. ((m$i)$j) * (x$i)) (UNIV :: 'm set)) :: 'a^'n"
chaieb@29842
  2044
huffman@30489
  2045
abbreviation
chaieb@29842
  2046
  vactor_matrix_mult' :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "  (infixl "v*" 70)
huffman@30489
  2047
  where
chaieb@29842
  2048
  "v v* m == v \<star> m"
chaieb@29842
  2049
huffman@30582
  2050
definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
chaieb@29842
  2051
definition "(transp::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
huffman@30582
  2052
definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
huffman@30582
  2053
definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
huffman@30582
  2054
definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
huffman@30582
  2055
definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
chaieb@29842
  2056
chaieb@29842
  2057
lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
chaieb@29842
  2058
lemma matrix_add_ldistrib: "(A ** (B + C)) = (A \<star> B) + (A \<star> C)"
chaieb@29842
  2059
  by (vector matrix_matrix_mult_def setsum_addf[symmetric] ring_simps)
chaieb@29842
  2060
huffman@30489
  2061
lemma setsum_delta':
huffman@30489
  2062
  assumes fS: "finite S" shows
huffman@30489
  2063
  "setsum (\<lambda>k. if a = k then b k else 0) S =
chaieb@29842
  2064
     (if a\<in> S then b a else 0)"
huffman@30489
  2065
  using setsum_delta[OF fS, of a b, symmetric]
chaieb@29842
  2066
  by (auto intro: setsum_cong)
chaieb@29842
  2067
huffman@30582
  2068
lemma matrix_mul_lid:
huffman@30582
  2069
  fixes A :: "'a::semiring_1 ^ 'm ^ 'n::finite"
huffman@30582
  2070
  shows "mat 1 ** A = A"
chaieb@29842
  2071
  apply (simp add: matrix_matrix_mult_def mat_def)
chaieb@29842
  2072
  apply vector
huffman@30582
  2073
  by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite]  mult_1_left mult_zero_left if_True UNIV_I)
huffman@30582
  2074
huffman@30582
  2075
huffman@30582
  2076
lemma matrix_mul_rid:
huffman@30582
  2077
  fixes A :: "'a::semiring_1 ^ 'm::finite ^ 'n"
huffman@30582
  2078
  shows "A ** mat 1 = A"
chaieb@29842
  2079
  apply (simp add: matrix_matrix_mult_def mat_def)
chaieb@29842
  2080
  apply vector
huffman@30582
  2081
  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)
chaieb@29842
  2082
chaieb@29842
  2083
lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
chaieb@29842
  2084
  apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
chaieb@29842
  2085
  apply (subst setsum_commute)
chaieb@29842
  2086
  apply simp
chaieb@29842
  2087
  done
chaieb@29842
  2088
chaieb@29842
  2089
lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
chaieb@29842
  2090
  apply (vector matrix_matrix_mult_def matrix_vector_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
chaieb@29842
  2091
  apply (subst setsum_commute)
chaieb@29842
  2092
  apply simp
chaieb@29842
  2093
  done
chaieb@29842
  2094
huffman@30582
  2095
lemma matrix_vector_mul_lid: "mat 1 *v x = (x::'a::semiring_1 ^ 'n::finite)"
chaieb@29842
  2096
  apply (vector matrix_vector_mult_def mat_def)
huffman@30489
  2097
  by (simp add: cond_value_iff cond_application_beta
chaieb@29842
  2098
    setsum_delta' cong del: if_weak_cong)
chaieb@29842
  2099
chaieb@29842
  2100
lemma matrix_transp_mul: "transp(A ** B) = transp B ** transp (A::'a::comm_semiring_1^'m^'n)"
huffman@30582
  2101
  by (simp add: matrix_matrix_mult_def transp_def Cart_eq mult_commute)
huffman@30582
  2102
huffman@30582
  2103
lemma matrix_eq:
huffman@30582
  2104
  fixes A B :: "'a::semiring_1 ^ 'n::finite ^ 'm"
huffman@30582
  2105
  shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@29842
  2106
  apply auto
chaieb@29842
  2107
  apply (subst Cart_eq)
chaieb@29842
  2108
  apply clarify
huffman@30582
  2109
  apply (clarsimp simp add: matrix_vector_mult_def basis_def cond_value_iff cond_application_beta Cart_eq cong del: if_weak_cong)
chaieb@29842
  2110
  apply (erule_tac x="basis ia" in allE)
huffman@30582
  2111
  apply (erule_tac x="i" in allE)
huffman@30582
  2112
  by (auto simp add: basis_def cond_value_iff cond_application_beta setsum_delta[OF finite] cong del: if_weak_cong)
chaieb@29842
  2113
huffman@30489
  2114
lemma matrix_vector_mul_component:
chaieb@29842
  2115
  shows "((A::'a::semiring_1^'n'^'m) *v x)$k = (A$k) \<bullet> x"
huffman@30582
  2116
  by (simp add: matrix_vector_mult_def dot_def)
chaieb@29842
  2117
chaieb@29842
  2118
lemma dot_lmul_matrix: "((x::'a::comm_semiring_1 ^'n) v* A) \<bullet> y = x \<bullet> (A *v y)"
huffman@30582
  2119
  apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib mult_ac)
chaieb@29842
  2120
  apply (subst setsum_commute)
chaieb@29842
  2121
  by simp
chaieb@29842
  2122
chaieb@29842
  2123
lemma transp_mat: "transp (mat n) = mat n"
chaieb@29842
  2124
  by (vector transp_def mat_def)
chaieb@29842
  2125
chaieb@29842
  2126
lemma transp_transp: "transp(transp A) = A"
chaieb@29842
  2127
  by (vector transp_def)
chaieb@29842
  2128
huffman@30489
  2129
lemma row_transp:
chaieb@29842
  2130
  fixes A:: "'a::semiring_1^'n^'m"
chaieb@29842
  2131
  shows "row i (transp A) = column i A"
huffman@30582
  2132
  by (simp add: row_def column_def transp_def Cart_eq)
chaieb@29842
  2133
chaieb@29842
  2134
lemma column_transp:
chaieb@29842
  2135
  fixes A:: "'a::semiring_1^'n^'m"
chaieb@29842
  2136
  shows "column i (transp A) = row i A"
huffman@30582
  2137
  by (simp add: row_def column_def transp_def Cart_eq)
chaieb@29842
  2138
chaieb@29842
  2139
lemma rows_transp: "rows(transp (A::'a::semiring_1^'n^'m)) = columns A"
huffman@30582
  2140
by (auto simp add: rows_def columns_def row_transp intro: set_ext)
chaieb@29842
  2141
chaieb@29842
  2142
lemma columns_transp: "columns(transp (A::'a::semiring_1^'n^'m)) = rows A" by (metis transp_transp rows_transp)
chaieb@29842
  2143
chaieb@29842
  2144
text{* Two sometimes fruitful ways of looking at matrix-vector multiplication. *}
chaieb@29842
  2145
chaieb@29842
  2146
lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
chaieb@29842
  2147
  by (simp add: matrix_vector_mult_def dot_def)
chaieb@29842
  2148
huffman@30582
  2149
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)"
huffman@30582
  2150
  by (simp add: matrix_vector_mult_def Cart_eq column_def mult_commute)
chaieb@29842
  2151
chaieb@29842
  2152
lemma vector_componentwise:
huffman@30582
  2153
  "(x::'a::ring_1^'n::finite) = (\<chi> j. setsum (\<lambda>i. (x$i) * (basis i :: 'a^'n)$j) (UNIV :: 'n set))"
chaieb@29842
  2154
  apply (subst basis_expansion[symmetric])
huffman@30582
  2155
  by (vector Cart_eq setsum_component)
chaieb@29842
  2156
chaieb@29842
  2157
lemma linear_componentwise:
huffman@30582
  2158
  fixes f:: "'a::ring_1 ^ 'm::finite \<Rightarrow> 'a ^ 'n"
huffman@30582
  2159
  assumes lf: "linear f"
huffman@30582
  2160
  shows "(f x)$j = setsum (\<lambda>i. (x$i) * (f (basis i)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
chaieb@29842
  2161
proof-
huffman@30582
  2162
  let ?M = "(UNIV :: 'm set)"
huffman@30582
  2163
  let ?N = "(UNIV :: 'n set)"
chaieb@29842
  2164
  have fM: "finite ?M" by simp
chaieb@29842
  2165
  have "?rhs = (setsum (\<lambda>i.(x$i) *s f (basis i) ) ?M)$j"
huffman@30582
  2166
    unfolding vector_smult_component[symmetric]
huffman@30582
  2167
    unfolding setsum_component[of "(\<lambda>i.(x$i) *s f (basis i :: 'a^'m))" ?M]
chaieb@29842
  2168
    ..
chaieb@29842
  2169
  then show ?thesis unfolding linear_setsum_mul[OF lf fM, symmetric] basis_expansion ..
chaieb@29842
  2170
qed
chaieb@29842
  2171
chaieb@29842
  2172
text{* Inverse matrices  (not necessarily square) *}
chaieb@29842
  2173
chaieb@29842
  2174
definition "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
chaieb@29842
  2175
chaieb@29842
  2176
definition "matrix_inv(A:: 'a::semiring_1^'n^'m) =
chaieb@29842
  2177
        (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
chaieb@29842
  2178
chaieb@29842
  2179
text{* Correspondence between matrices and linear operators. *}
chaieb@29842
  2180
chaieb@29842
  2181
definition matrix:: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
chaieb@29842
  2182
where "matrix f = (\<chi> i j. (f(basis j))$i)"
chaieb@29842
  2183
chaieb@29842
  2184
lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::'a::comm_semiring_1 ^ 'n))"
huffman@30582
  2185
  by (simp add: linear_def matrix_vector_mult_def Cart_eq ring_simps setsum_right_distrib setsum_addf)
huffman@30582
  2186
huffman@30582
  2187
lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n::finite)"
huffman@30582
  2188
apply (simp add: matrix_def matrix_vector_mult_def Cart_eq mult_commute)
chaieb@29842
  2189
apply clarify
chaieb@29842
  2190
apply (rule linear_componentwise[OF lf, symmetric])
chaieb@29842
  2191
done
chaieb@29842
  2192
huffman@30582
  2193
lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n::finite))" by (simp add: ext matrix_works)
huffman@30582
  2194
huffman@30582
  2195
lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n::finite)) = A"
chaieb@29842
  2196
  by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
chaieb@29842
  2197
huffman@30489
  2198
lemma matrix_compose:
huffman@30582
  2199
  assumes lf: "linear (f::'a::comm_ring_1^'n::finite \<Rightarrow> 'a^'m::finite)"
huffman@30582
  2200
  and lg: "linear (g::'a::comm_ring_1^'m::finite \<Rightarrow> 'a^'k)"
chaieb@29842
  2201
  shows "matrix (g o f) = matrix g ** matrix f"
chaieb@29842
  2202
  using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
chaieb@29842
  2203
  by (simp  add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
chaieb@29842
  2204
huffman@30582
  2205
lemma matrix_vector_column:"(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s ((transp A)$i)) (UNIV:: 'n set)"
huffman@30582
  2206
  by (simp add: matrix_vector_mult_def transp_def Cart_eq mult_commute)
huffman@30582
  2207
huffman@30582
  2208
lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n::finite^'m::finite) *v x) = (\<lambda>x. transp A *v x)"
chaieb@29842
  2209
  apply (rule adjoint_unique[symmetric])
chaieb@29842
  2210
  apply (rule matrix_vector_mul_linear)
huffman@30582
  2211
  apply (simp add: transp_def dot_def matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
chaieb@29842
  2212
  apply (subst setsum_commute)
chaieb@29842
  2213
  apply (auto simp add: mult_ac)
chaieb@29842
  2214
  done
chaieb@29842
  2215
huffman@30582
  2216
lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n::finite \<Rightarrow> 'a ^ 'm::finite)"
chaieb@29842
  2217
  shows "matrix(adjoint f) = transp(matrix f)"
chaieb@29842
  2218
  apply (subst matrix_vector_mul[OF lf])
chaieb@29842
  2219
  unfolding adjoint_matrix matrix_of_matrix_vector_mul ..
chaieb@29842
  2220
chaieb@29842
  2221
subsection{* Interlude: Some properties of real sets *}
chaieb@29842
  2222
chaieb@29842
  2223
lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
chaieb@29842
  2224
  shows "\<forall>n \<ge> m. d n < e m"
chaieb@29842
  2225
  using prems apply auto
chaieb@29842
  2226
  apply (erule_tac x="n" in allE)
chaieb@29842
  2227
  apply (erule_tac x="n" in allE)
chaieb@29842
  2228
  apply auto
chaieb@29842
  2229
  done
chaieb@29842
  2230
chaieb@29842
  2231
huffman@30489
  2232
lemma real_convex_bound_lt:
chaieb@29842
  2233
  assumes xa: "(x::real) < a" and ya: "y < a" and u: "0 <= u" and v: "0 <= v"
huffman@30489
  2234
  and uv: "u + v = 1"
chaieb@29842
  2235
  shows "u * x + v * y < a"
chaieb@29842
  2236
proof-
chaieb@29842
  2237
  have uv': "u = 0 \<longrightarrow> v \<noteq> 0" using u v uv by arith
chaieb@29842
  2238
  have "a = a * (u + v)" unfolding uv  by simp
chaieb@29842
  2239
  hence th: "u * a + v * a = a" by (simp add: ring_simps)
chaieb@29842
  2240
  from xa u have "u \<noteq> 0 \<Longrightarrow> u*x < u*a" by (simp add: mult_compare_simps)
chaieb@29842
  2241
  from ya v have "v \<noteq> 0 \<Longrightarrow> v * y < v * a" by (simp add: mult_compare_simps)
chaieb@29842
  2242
  from xa ya u v have "u * x + v * y < u * a + v * a"
chaieb@29842
  2243
    apply (cases "u = 0", simp_all add: uv')
chaieb@29842
  2244
    apply(rule mult_strict_left_mono)
chaieb@29842
  2245
    using uv' apply simp_all
huffman@30489
  2246
chaieb@29842
  2247
    apply (rule add_less_le_mono)
chaieb@29842
  2248
    apply(rule mult_strict_left_mono)
chaieb@29842
  2249
    apply simp_all
chaieb@29842
  2250
    apply (rule mult_left_mono)
chaieb@29842
  2251
    apply simp_all
chaieb@29842
  2252
    done
chaieb@29842
  2253
  thus ?thesis unfolding th .
chaieb@29842
  2254
qed
chaieb@29842
  2255
huffman@30489
  2256
lemma real_convex_bound_le:
chaieb@29842
  2257
  assumes xa: "(x::real) \<le> a" and ya: "y \<le> a" and u: "0 <= u" and v: "0 <= v"
huffman@30489
  2258
  and uv: "u + v = 1"
chaieb@29842
  2259
  shows "u * x + v * y \<le> a"
chaieb@29842
  2260
proof-
chaieb@29842
  2261
  from xa ya u v have "u * x + v * y \<le> u * a + v * a" by (simp add: add_mono mult_left_mono)
chaieb@29842
  2262
  also have "\<dots> \<le> (u + v) * a" by (simp add: ring_simps)
chaieb@29842
  2263
  finally show ?thesis unfolding uv by simp
chaieb@29842
  2264
qed
chaieb@29842
  2265
chaieb@29842
  2266
lemma infinite_enumerate: assumes fS: "infinite S"
chaieb@29842
  2267
  shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
chaieb@29842
  2268
unfolding subseq_def
chaieb@29842
  2269
using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
chaieb@29842
  2270
chaieb@29842
  2271
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)"
chaieb@29842
  2272
apply auto
chaieb@29842
  2273
apply (rule_tac x="d/2" in exI)
chaieb@29842
  2274
apply auto
chaieb@29842
  2275
done
chaieb@29842
  2276
chaieb@29842
  2277
huffman@30489
  2278
lemma triangle_lemma:
chaieb@29842
  2279
  assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
chaieb@29842
  2280
  shows "x <= y + z"
chaieb@29842
  2281
proof-
chaieb@29842
  2282
  have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y  by (simp add: zero_compare_simps)
chaieb@29842
  2283
  with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square ring_simps)
chaieb@29842
  2284
  from y z have yz: "y + z \<ge> 0" by arith
chaieb@29842
  2285
  from power2_le_imp_le[OF th yz] show ?thesis .
chaieb@29842
  2286
qed
chaieb@29842
  2287
chaieb@29842
  2288
huffman@30582
  2289
lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
huffman@30582
  2290
   (\<exists>x::'a ^ 'n. \<forall>i. P i (x$i))" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@29842
  2291
proof-
huffman@30582
  2292
  let ?S = "(UNIV :: 'n set)"
chaieb@29842
  2293
  {assume H: "?rhs"
chaieb@29842
  2294
    then have ?lhs by auto}
chaieb@29842
  2295
  moreover
chaieb@29842
  2296
  {assume H: "?lhs"
huffman@30582
  2297
    then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
chaieb@29842
  2298
    let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
huffman@30582
  2299
    {fix i
huffman@30582
  2300
      from f have "P i (f i)" by metis
huffman@30582
  2301
      then have "P i (?x$i)" by auto
chaieb@29842
  2302
    }
huffman@30582
  2303
    hence "\<forall>i. P i (?x$i)" by metis
chaieb@29842
  2304
    hence ?rhs by metis }
chaieb@29842
  2305
  ultimately show ?thesis by metis
huffman@30489
  2306
qed
chaieb@29842
  2307
chaieb@29842
  2308
(* Supremum and infimum of real sets *)
chaieb@29842
  2309
chaieb@29842
  2310
chaieb@29842
  2311
definition rsup:: "real set \<Rightarrow> real" where
chaieb@29842
  2312
  "rsup S = (SOME a. isLub UNIV S a)"
chaieb@29842
  2313
chaieb@29842
  2314
lemma rsup_alt: "rsup S = (SOME a. (\<forall>x \<in> S. x \<le> a) \<and> (\<forall>b. (\<forall>x \<in> S. x \<le> b) \<longrightarrow> a \<le> b))"  by (auto simp  add: isLub_def rsup_def leastP_def isUb_def setle_def setge_def)
chaieb@29842
  2315
chaieb@29842
  2316
lemma rsup: assumes Se: "S \<noteq> {}" and b: "\<exists>b. S *<= b"
chaieb@29842
  2317
  shows "isLub UNIV S (rsup S)"
chaieb@29842
  2318
using Se b
chaieb@29842
  2319
unfolding rsup_def
chaieb@29842
  2320
apply clarify
chaieb@29842
  2321
apply (rule someI_ex)
chaieb@29842
  2322
apply (rule reals_complete)
chaieb@29842
  2323
by (auto simp add: isUb_def setle_def)
chaieb@29842
  2324
chaieb@29842
  2325
lemma rsup_le: assumes Se: "S \<noteq> {}" and Sb: "S *<= b" shows "rsup S \<le> b"
chaieb@29842
  2326
proof-
chaieb@29842
  2327
  from Sb have bu: "isUb UNIV S b" by (simp add: isUb_def setle_def)
huffman@30489
  2328
  from rsup[OF Se] Sb have "isLub UNIV S (rsup S)"  by blast
chaieb@29842
  2329
  then show ?thesis using bu by (auto simp add: isLub_def leastP_def setle_def setge_def)
chaieb@29842
  2330
qed
chaieb@29842
  2331
chaieb@29842
  2332
lemma rsup_finite_Max: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2333
  shows "rsup S = Max S"
chaieb@29842
  2334
using fS Se
chaieb@29842
  2335
proof-
chaieb@29842
  2336
  let ?m = "Max S"
chaieb@29842
  2337
  from Max_ge[OF fS] have Sm: "\<forall> x\<in> S. x \<le> ?m" by metis
chaieb@29842
  2338
  with rsup[OF Se] have lub: "isLub UNIV S (rsup S)" by (metis setle_def)
huffman@30489
  2339
  from Max_in[OF fS Se] lub have mrS: "?m \<le> rsup S"
chaieb@29842
  2340
    by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def)
huffman@30489
  2341
  moreover
chaieb@29842
  2342
  have "rsup S \<le> ?m" using Sm lub
chaieb@29842
  2343
    by (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
huffman@30489
  2344
  ultimately  show ?thesis by arith
chaieb@29842
  2345
qed
chaieb@29842
  2346
chaieb@29842
  2347
lemma rsup_finite_in: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2348
  shows "rsup S \<in> S"
chaieb@29842
  2349
  using rsup_finite_Max[OF fS Se] Max_in[OF fS Se] by metis
chaieb@29842
  2350
chaieb@29842
  2351
lemma rsup_finite_Ub: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2352
  shows "isUb S S (rsup S)"
chaieb@29842
  2353
  using rsup_finite_Max[OF fS Se] rsup_finite_in[OF fS Se] Max_ge[OF fS]
chaieb@29842
  2354
  unfolding isUb_def setle_def by metis
chaieb@29842
  2355
chaieb@29842
  2356
lemma rsup_finite_ge_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2357
  shows "a \<le> rsup S \<longleftrightarrow> (\<exists> x \<in> S. a \<le> x)"
chaieb@29842
  2358
using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
chaieb@29842
  2359
chaieb@29842
  2360
lemma rsup_finite_le_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2361
  shows "a \<ge> rsup S \<longleftrightarrow> (\<forall> x \<in> S. a \<ge> x)"
chaieb@29842
  2362
using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
chaieb@29842
  2363
chaieb@29842
  2364
lemma rsup_finite_gt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2365
  shows "a < rsup S \<longleftrightarrow> (\<exists> x \<in> S. a < x)"
chaieb@29842
  2366
using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
chaieb@29842
  2367
chaieb@29842
  2368
lemma rsup_finite_lt_iff: assumes fS: "finite S" and Se: "S \<noteq> {}"
chaieb@29842
  2369
  shows "a > rsup S \<longleftrightarrow> (\<forall> x \<in> S. a > x)"
chaieb@29842
  2370
using rsup_finite_Ub[OF fS Se] by (auto simp add: isUb_def setle_def)
chaieb@29842
  2371
chaieb@29842
  2372
lemma rsup_unique: assumes b: "S *<= b" and S: "\<forall>b' < b. \<exists>x \<in> S. b' < x"
chaieb@29842
  2373
  shows "rsup S = b"
huffman@30489
  2374
using b S
chaieb@29842
  2375
unfolding setle_def rsup_alt
chaieb@29842
  2376
apply -
chaieb@29842
  2377
apply (rule some_equality)
chaieb@29842
  2378
apply (metis  linorder_not_le order_eq_iff[symmetric])+
chaieb@29842
  2379
done
chaieb@29842
  2380
chaieb@29842
  2381
lemma rsup_le_subset: "S\<noteq>{} \<Longrightarrow> S \<subseteq> T \<Longrightarrow> (\<exists>b. T *<= b) \<Longrightarrow> rsup S \<le> rsup T"
chaieb@29842
  2382
  apply (rule rsup_le)
chaieb@29842
  2383
  apply simp
chaieb@29842
  2384
  using rsup[of T] by (auto simp add: isLub_def leastP_def setge_def setle_def isUb_def)
chaieb@29842
  2385
chaieb@29842
  2386
lemma isUb_def': "isUb R S = (\<lambda>x. S *<= x \<and> x \<in> R)"
chaieb@29842
  2387
  apply (rule ext)
chaieb@29842
  2388
  by (metis isUb_def)
chaieb@29842
  2389
chaieb@29842
  2390
lemma UNIV_trivial: "UNIV x" using UNIV_I[of x] by (metis mem_def)
chaieb@29842
  2391
lemma rsup_bounds: assumes Se: "S \<noteq> {}" and l: "a <=* S" and u: "S *<= b"
chaieb@29842
  2392
  shows "a \<le> rsup S \<and> rsup S \<le> b"
chaieb@29842
  2393
proof-
chaieb@29842
  2394
  from rsup[OF Se] u have lub: "isLub UNIV S (rsup S)" by blast
chaieb@29842
  2395
  hence b: "rsup S \<le> b" using u by (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
chaieb@29842
  2396
  from Se obtain y where y: "y \<in> S" by blast
chaieb@29842
  2397
  from lub l have "a \<le> rsup S" apply (auto simp add: isLub_def leastP_def setle_def setge_def isUb_def')
chaieb@29842
  2398
    apply (erule ballE[where x=y])
chaieb@29842
  2399
    apply (erule ballE[where x=y])
chaieb@29842
  2400
    apply arith
chaieb@29842
  2401
    using y apply auto
chaieb@29842
  2402
    done
chaieb@29842
  2403
  with b show ?thesis by blast
chaieb@29842
  2404
qed
chaieb@29842
  2405
chaieb@29842
  2406
lemma rsup_abs_le: "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>rsup S\<bar> \<le> a"
chaieb@29842
  2407
  unfolding abs_le_interval_iff  using rsup_bounds[of S "-a" a]
chaieb@29842
  2408
  by (auto simp add: setge_def setle_def)
chaieb@29842
  2409
chaieb@29842
  2410
lemma rsup_asclose: assumes S:"S \<noteq> {}" and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e" shows "\<bar>rsup S - l\<bar> \<le> e"
chaieb@29842
  2411
proof-
chaieb@29842
  2412
  have th: "\<And>(x::real) l e. \<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e" by arith
huffman@30489
  2413
  show ?thesis using S b rsup_bounds[of S "l - e" "l+e"] unfolding th
chaieb@29842
  2414
    by  (auto simp add: setge_def setle_def)
chaieb@29842
  2415
qed
chaieb@29842
  2416
chaieb@29842
  2417
definition rinf:: "real set \<Rightarrow> real" where
chaieb@29842
  2418
  "rinf S = (SOME a. isGlb UNIV S a)"
chaieb@29842
  2419
chaieb@29842
  2420
lemma rinf_alt: "rinf S = (SOME a. (\<forall>x \<in> S. x \<ge> a) \<and> (\<forall>b. (\<forall>x \<in> S. x \<ge> b) \<longrightarrow> a \<ge> b))"  by (auto simp  add: isGlb_def rinf_def greatestP_def isLb_def setle_def setge_def)
chaieb@29842
  2421
chaieb@29842
  2422
lemma reals_complete_Glb: assumes Se: "\<exists>x. x \<in> S" and lb: "\<exists> y. isLb UNIV S y"
chaieb@29842
  2423
  shows "\<exists>(t::real). isGlb UNIV S t"
chaieb@29842
  2424
proof-
chaieb@29842
  2425
  let ?M = "uminus ` S"
chaieb@29842
  2426
  from lb have th: "\<exists>y. isUb UNIV ?M y" apply (auto simp add: isUb_def isLb_def setle_def setge_def)
chaieb@29842
  2427
    by (rule_tac x="-y" in exI, auto)
chaieb@29842
  2428
  from Se have Me: "\<exists>x. x \<in> ?M" by blast
chaieb@29842
  2429
  from reals_complete[OF Me th] obtain t where t: "isLub UNIV ?M t" by blast
chaieb@29842
  2430
  have "isGlb UNIV S (- t)" using t
chaieb@29842
  2431
    apply (auto simp add: isLub_def isGlb_def leastP_def greatestP_def setle_def setge_def isUb_def isLb_def)