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