src/HOL/Analysis/Finite_Cartesian_Product.thy
author immler
Thu May 03 15:07:14 2018 +0200 (12 months ago)
changeset 68073 fad29d2a17a5
parent 68072 493b818e8e10
child 68074 8d50467f7555
permissions -rw-r--r--
merged; resolved conflicts manually (esp. lemmas that have been moved from Linear_Algebra and Cartesian_Euclidean_Space)
hoelzl@63627
     1
(*  Title:      HOL/Analysis/Finite_Cartesian_Product.thy
wenzelm@35253
     2
    Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
*)
himmelma@33175
     4
nipkow@67968
     5
section \<open>Definition of finite Cartesian product types\<close>
himmelma@33175
     6
himmelma@33175
     7
theory Finite_Cartesian_Product
wenzelm@41413
     8
imports
huffman@44135
     9
  Euclidean_Space
wenzelm@41413
    10
  L2_Norm
wenzelm@66453
    11
  "HOL-Library.Numeral_Type"
wenzelm@66453
    12
  "HOL-Library.Countable_Set"
himmelma@33175
    13
begin
himmelma@33175
    14
nipkow@67968
    15
subsection \<open>Finite Cartesian products, with indexing and lambdas\<close>
himmelma@33175
    16
wenzelm@67731
    17
typedef ('a, 'b) vec = "UNIV :: ('b::finite \<Rightarrow> 'a) set"
huffman@44136
    18
  morphisms vec_nth vec_lambda ..
himmelma@33175
    19
lp15@67673
    20
declare vec_lambda_inject [simplified, simp]
lp15@67673
    21
immler@67685
    22
bundle vec_syntax begin
wenzelm@35254
    23
notation
huffman@44136
    24
  vec_nth (infixl "$" 90) and
huffman@44136
    25
  vec_lambda (binder "\<chi>" 10)
immler@67685
    26
end
immler@67685
    27
immler@67685
    28
bundle no_vec_syntax begin
immler@67685
    29
no_notation
immler@67685
    30
  vec_nth (infixl "$" 90) and
immler@67685
    31
  vec_lambda (binder "\<chi>" 10)
immler@67685
    32
end
immler@67685
    33
immler@67685
    34
unbundle vec_syntax
himmelma@33175
    35
wenzelm@67731
    36
text \<open>
wenzelm@67731
    37
  Concrete syntax for \<open>('a, 'b) vec\<close>:
wenzelm@67731
    38
    \<^item> \<open>'a^'b\<close> becomes \<open>('a, 'b::finite) vec\<close>
wenzelm@67731
    39
    \<^item> \<open>'a^'b::_\<close> becomes \<open>('a, 'b) vec\<close> without extra sort-constraint
wenzelm@67731
    40
\<close>
wenzelm@67732
    41
syntax "_vec_type" :: "type \<Rightarrow> type \<Rightarrow> type" (infixl "^" 15)
wenzelm@60420
    42
parse_translation \<open>
wenzelm@52143
    43
  let
wenzelm@52143
    44
    fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
wenzelm@52143
    45
    fun finite_vec_tr [t, u] =
wenzelm@52143
    46
      (case Term_Position.strip_positions u of
wenzelm@52143
    47
        v as Free (x, _) =>
wenzelm@52143
    48
          if Lexicon.is_tid x then
wenzelm@52143
    49
            vec t (Syntax.const @{syntax_const "_ofsort"} $ v $
wenzelm@52143
    50
              Syntax.const @{class_syntax finite})
wenzelm@52143
    51
          else vec t u
wenzelm@52143
    52
      | _ => vec t u)
wenzelm@52143
    53
  in
wenzelm@67732
    54
    [(@{syntax_const "_vec_type"}, K finite_vec_tr)]
wenzelm@52143
    55
  end
wenzelm@60420
    56
\<close>
hoelzl@34290
    57
huffman@44136
    58
lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
huffman@44136
    59
  by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
himmelma@33175
    60
huffman@44136
    61
lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
huffman@44136
    62
  by (simp add: vec_lambda_inverse)
himmelma@33175
    63
huffman@44136
    64
lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
huffman@44136
    65
  by (auto simp add: vec_eq_iff)
himmelma@33175
    66
lp15@67673
    67
lemma vec_lambda_eta [simp]: "(\<chi> i. (g$i)) = g"
huffman@44136
    68
  by (simp add: vec_eq_iff)
himmelma@33175
    69
eberlm@66281
    70
subsection \<open>Cardinality of vectors\<close>
eberlm@66281
    71
eberlm@66281
    72
instance vec :: (finite, finite) finite
eberlm@66281
    73
proof
eberlm@66281
    74
  show "finite (UNIV :: ('a, 'b) vec set)"
eberlm@66281
    75
  proof (subst bij_betw_finite)
eberlm@66281
    76
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    77
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
    78
    have "finite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    79
      by (intro finite_PiE) auto
eberlm@66281
    80
    also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
eberlm@66281
    81
      by auto
eberlm@66281
    82
    finally show "finite \<dots>" .
eberlm@66281
    83
  qed
eberlm@66281
    84
qed
eberlm@66281
    85
eberlm@66281
    86
lemma countable_PiE:
eberlm@66281
    87
  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
eberlm@66281
    88
  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
eberlm@66281
    89
eberlm@66281
    90
instance vec :: (countable, finite) countable
eberlm@66281
    91
proof
eberlm@66281
    92
  have "countable (UNIV :: ('a, 'b) vec set)"
eberlm@66281
    93
  proof (rule countableI_bij2)
eberlm@66281
    94
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    95
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
    96
    have "countable (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    97
      by (intro countable_PiE) auto
eberlm@66281
    98
    also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
eberlm@66281
    99
      by auto
eberlm@66281
   100
    finally show "countable \<dots>" .
eberlm@66281
   101
  qed
eberlm@66281
   102
  thus "\<exists>t::('a, 'b) vec \<Rightarrow> nat. inj t"
eberlm@66281
   103
    by (auto elim!: countableE)
eberlm@66281
   104
qed
eberlm@66281
   105
eberlm@66281
   106
lemma infinite_UNIV_vec:
eberlm@66281
   107
  assumes "infinite (UNIV :: 'a set)"
wenzelm@67731
   108
  shows   "infinite (UNIV :: ('a^'b) set)"
eberlm@66281
   109
proof (subst bij_betw_finite)
eberlm@66281
   110
  show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   111
    by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
   112
  have "infinite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" (is "infinite ?A")
eberlm@66281
   113
  proof
eberlm@66281
   114
    assume "finite ?A"
eberlm@66281
   115
    hence "finite ((\<lambda>f. f undefined) ` ?A)"
eberlm@66281
   116
      by (rule finite_imageI)
eberlm@66281
   117
    also have "(\<lambda>f. f undefined) ` ?A = UNIV"
eberlm@66281
   118
      by auto
eberlm@66281
   119
    finally show False 
eberlm@66281
   120
      using \<open>infinite (UNIV :: 'a set)\<close> by contradiction
eberlm@66281
   121
  qed
eberlm@66281
   122
  also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
eberlm@66281
   123
    by auto
eberlm@66281
   124
  finally show "infinite (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" .
eberlm@66281
   125
qed
eberlm@66281
   126
eberlm@66281
   127
lemma CARD_vec [simp]:
wenzelm@67731
   128
  "CARD('a^'b) = CARD('a) ^ CARD('b)"
eberlm@66281
   129
proof (cases "finite (UNIV :: 'a set)")
eberlm@66281
   130
  case True
eberlm@66281
   131
  show ?thesis
eberlm@66281
   132
  proof (subst bij_betw_same_card)
eberlm@66281
   133
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   134
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
   135
    have "CARD('a) ^ CARD('b) = card (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   136
      (is "_ = card ?A")
eberlm@66281
   137
      by (subst card_PiE) (auto simp: prod_constant)
eberlm@66281
   138
    
eberlm@66281
   139
    also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
eberlm@66281
   140
      by auto
eberlm@66281
   141
    finally show "card \<dots> = CARD('a) ^ CARD('b)" ..
eberlm@66281
   142
  qed
eberlm@66281
   143
qed (simp_all add: infinite_UNIV_vec)
huffman@36591
   144
lp15@67683
   145
lemma countable_vector:
lp15@67683
   146
  fixes B:: "'n::finite \<Rightarrow> 'a set"
lp15@67683
   147
  assumes "\<And>i. countable (B i)"
lp15@67683
   148
  shows "countable {V. \<forall>i::'n::finite. V $ i \<in> B i}"
lp15@67683
   149
proof -
lp15@67683
   150
  have "f \<in> ($) ` {V. \<forall>i. V $ i \<in> B i}" if "f \<in> Pi\<^sub>E UNIV B" for f
lp15@67683
   151
  proof -
lp15@67683
   152
    have "\<exists>W. (\<forall>i. W $ i \<in> B i) \<and> ($) W = f"
lp15@67683
   153
      by (metis that PiE_iff UNIV_I vec_lambda_inverse)
lp15@67683
   154
    then show "f \<in> ($) ` {v. \<forall>i. v $ i \<in> B i}"
lp15@67683
   155
      by blast
lp15@67683
   156
  qed
lp15@67683
   157
  then have "Pi\<^sub>E UNIV B = vec_nth ` {V. \<forall>i::'n. V $ i \<in> B i}"
lp15@67683
   158
    by blast
lp15@67683
   159
  then have "countable (vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
lp15@67683
   160
    by (metis finite_class.finite_UNIV countable_PiE assms)
lp15@67683
   161
  then have "countable (vec_lambda ` vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
lp15@67683
   162
    by auto
lp15@67683
   163
  then show ?thesis
lp15@67683
   164
    by (simp add: image_comp o_def vec_nth_inverse)
lp15@67683
   165
qed
lp15@67683
   166
wenzelm@60420
   167
subsection \<open>Group operations and class instances\<close>
huffman@36591
   168
huffman@44136
   169
instantiation vec :: (zero, finite) zero
huffman@36591
   170
begin
huffman@44136
   171
  definition "0 \<equiv> (\<chi> i. 0)"
huffman@36591
   172
  instance ..
himmelma@33175
   173
end
huffman@36591
   174
huffman@44136
   175
instantiation vec :: (plus, finite) plus
huffman@36591
   176
begin
nipkow@67399
   177
  definition "(+) \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
huffman@36591
   178
  instance ..
huffman@36591
   179
end
huffman@36591
   180
huffman@44136
   181
instantiation vec :: (minus, finite) minus
huffman@36591
   182
begin
nipkow@67399
   183
  definition "(-) \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
huffman@36591
   184
  instance ..
huffman@36591
   185
end
huffman@36591
   186
huffman@44136
   187
instantiation vec :: (uminus, finite) uminus
huffman@36591
   188
begin
huffman@44136
   189
  definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
huffman@36591
   190
  instance ..
huffman@36591
   191
end
huffman@36591
   192
huffman@36591
   193
lemma zero_index [simp]: "0 $ i = 0"
huffman@44136
   194
  unfolding zero_vec_def by simp
huffman@36591
   195
huffman@36591
   196
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@44136
   197
  unfolding plus_vec_def by simp
huffman@36591
   198
huffman@36591
   199
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@44136
   200
  unfolding minus_vec_def by simp
huffman@36591
   201
huffman@36591
   202
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@44136
   203
  unfolding uminus_vec_def by simp
huffman@36591
   204
huffman@44136
   205
instance vec :: (semigroup_add, finite) semigroup_add
wenzelm@61169
   206
  by standard (simp add: vec_eq_iff add.assoc)
huffman@36591
   207
huffman@44136
   208
instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
wenzelm@61169
   209
  by standard (simp add: vec_eq_iff add.commute)
huffman@36591
   210
huffman@44136
   211
instance vec :: (monoid_add, finite) monoid_add
wenzelm@61169
   212
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   213
huffman@44136
   214
instance vec :: (comm_monoid_add, finite) comm_monoid_add
wenzelm@61169
   215
  by standard (simp add: vec_eq_iff)
huffman@36591
   216
huffman@44136
   217
instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
wenzelm@61169
   218
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   219
huffman@44136
   220
instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
wenzelm@61169
   221
  by standard (simp_all add: vec_eq_iff diff_diff_eq)
huffman@36591
   222
huffman@44136
   223
instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   224
huffman@44136
   225
instance vec :: (group_add, finite) group_add
wenzelm@61169
   226
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   227
huffman@44136
   228
instance vec :: (ab_group_add, finite) ab_group_add
wenzelm@61169
   229
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   230
huffman@36591
   231
immler@68072
   232
subsection\<open>Basic componentwise operations on vectors\<close>
immler@68072
   233
immler@68072
   234
instantiation vec :: (times, finite) times
immler@68072
   235
begin
immler@68072
   236
immler@68072
   237
definition "( * ) \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
immler@68072
   238
instance ..
immler@68072
   239
immler@68072
   240
end
immler@68072
   241
immler@68072
   242
instantiation vec :: (one, finite) one
immler@68072
   243
begin
immler@68072
   244
immler@68072
   245
definition "1 \<equiv> (\<chi> i. 1)"
immler@68072
   246
instance ..
immler@68072
   247
immler@68072
   248
end
immler@68072
   249
immler@68072
   250
instantiation vec :: (ord, finite) ord
immler@68072
   251
begin
immler@68072
   252
immler@68072
   253
definition "x \<le> y \<longleftrightarrow> (\<forall>i. x$i \<le> y$i)"
immler@68072
   254
definition "x < (y::'a^'b) \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
immler@68072
   255
instance ..
immler@68072
   256
immler@68072
   257
end
immler@68072
   258
immler@68072
   259
text\<open>The ordering on one-dimensional vectors is linear.\<close>
immler@68072
   260
immler@68072
   261
class cart_one =
immler@68072
   262
  assumes UNIV_one: "card (UNIV :: 'a set) = Suc 0"
immler@68072
   263
begin
immler@68072
   264
immler@68072
   265
subclass finite
immler@68072
   266
proof
immler@68072
   267
  from UNIV_one show "finite (UNIV :: 'a set)"
immler@68072
   268
    by (auto intro!: card_ge_0_finite)
immler@68072
   269
qed
immler@68072
   270
immler@68072
   271
end
immler@68072
   272
immler@68072
   273
instance vec:: (order, finite) order
immler@68072
   274
  by standard (auto simp: less_eq_vec_def less_vec_def vec_eq_iff
immler@68072
   275
      intro: order.trans order.antisym order.strict_implies_order)
immler@68072
   276
immler@68072
   277
instance vec :: (linorder, cart_one) linorder
immler@68072
   278
proof
immler@68072
   279
  obtain a :: 'b where all: "\<And>P. (\<forall>i. P i) \<longleftrightarrow> P a"
immler@68072
   280
  proof -
immler@68072
   281
    have "card (UNIV :: 'b set) = Suc 0" by (rule UNIV_one)
immler@68072
   282
    then obtain b :: 'b where "UNIV = {b}" by (auto iff: card_Suc_eq)
immler@68072
   283
    then have "\<And>P. (\<forall>i\<in>UNIV. P i) \<longleftrightarrow> P b" by auto
immler@68072
   284
    then show thesis by (auto intro: that)
immler@68072
   285
  qed
immler@68072
   286
  fix x y :: "'a^'b::cart_one"
immler@68072
   287
  note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps
immler@68072
   288
  show "x \<le> y \<or> y \<le> x" by auto
immler@68072
   289
qed
immler@68072
   290
immler@68072
   291
text\<open>Constant Vectors\<close>
immler@68072
   292
immler@68072
   293
definition "vec x = (\<chi> i. x)"
immler@68072
   294
immler@68072
   295
text\<open>Also the scalar-vector multiplication.\<close>
immler@68072
   296
immler@68072
   297
definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
immler@68072
   298
  where "c *s x = (\<chi> i. c * (x$i))"
immler@68072
   299
immler@68072
   300
text \<open>scalar product\<close>
immler@68072
   301
immler@68072
   302
definition scalar_product :: "'a :: semiring_1 ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" where
immler@68072
   303
  "scalar_product v w = (\<Sum> i \<in> UNIV. v $ i * w $ i)"
immler@68072
   304
immler@68072
   305
wenzelm@60420
   306
subsection \<open>Real vector space\<close>
huffman@36591
   307
huffman@44136
   308
instantiation vec :: (real_vector, finite) real_vector
huffman@36591
   309
begin
huffman@36591
   310
huffman@44136
   311
definition "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
huffman@36591
   312
huffman@36591
   313
lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
huffman@44136
   314
  unfolding scaleR_vec_def by simp
huffman@36591
   315
huffman@36591
   316
instance
wenzelm@61169
   317
  by standard (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
huffman@36591
   318
huffman@36591
   319
end
huffman@36591
   320
huffman@36591
   321
wenzelm@60420
   322
subsection \<open>Topological space\<close>
huffman@36591
   323
huffman@44136
   324
instantiation vec :: (topological_space, finite) topological_space
huffman@36591
   325
begin
huffman@36591
   326
hoelzl@62101
   327
definition [code del]:
huffman@36591
   328
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@36591
   329
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@36591
   330
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@36591
   331
huffman@36591
   332
instance proof
huffman@36591
   333
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@44136
   334
    unfolding open_vec_def by auto
huffman@36591
   335
next
huffman@36591
   336
  fix S T :: "('a ^ 'b) set"
huffman@36591
   337
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@44136
   338
    unfolding open_vec_def
huffman@36591
   339
    apply clarify
huffman@36591
   340
    apply (drule (1) bspec)+
huffman@36591
   341
    apply (clarify, rename_tac Sa Ta)
huffman@36591
   342
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@36591
   343
    apply (simp add: open_Int)
huffman@36591
   344
    done
huffman@36591
   345
next
huffman@36591
   346
  fix K :: "('a ^ 'b) set set"
huffman@36591
   347
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@44136
   348
    unfolding open_vec_def
huffman@36591
   349
    apply clarify
huffman@36591
   350
    apply (drule (1) bspec)
huffman@36591
   351
    apply (drule (1) bspec)
huffman@36591
   352
    apply clarify
huffman@36591
   353
    apply (rule_tac x=A in exI)
huffman@36591
   354
    apply fast
huffman@36591
   355
    done
huffman@36591
   356
qed
huffman@36591
   357
huffman@36591
   358
end
huffman@36591
   359
huffman@36591
   360
lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   361
  unfolding open_vec_def by auto
huffman@36591
   362
huffman@44136
   363
lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@44136
   364
  unfolding open_vec_def
huffman@44136
   365
  apply clarify
huffman@44136
   366
  apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@44136
   367
  done
huffman@36591
   368
huffman@44136
   369
lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@44136
   370
  unfolding closed_open vimage_Compl [symmetric]
huffman@44136
   371
  by (rule open_vimage_vec_nth)
huffman@36591
   372
huffman@36591
   373
lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   374
proof -
huffman@36591
   375
  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
huffman@36591
   376
  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   377
    by (simp add: closed_INT closed_vimage_vec_nth)
huffman@36591
   378
qed
huffman@36591
   379
huffman@44136
   380
lemma tendsto_vec_nth [tendsto_intros]:
wenzelm@61973
   381
  assumes "((\<lambda>x. f x) \<longlongrightarrow> a) net"
wenzelm@61973
   382
  shows "((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
huffman@36591
   383
proof (rule topological_tendstoI)
huffman@36591
   384
  fix S assume "open S" "a $ i \<in> S"
huffman@36591
   385
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@44136
   386
    by (simp_all add: open_vimage_vec_nth)
huffman@36591
   387
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@36591
   388
    by (rule topological_tendstoD)
huffman@36591
   389
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@36591
   390
    by simp
huffman@36591
   391
qed
huffman@36591
   392
huffman@44631
   393
lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
huffman@44631
   394
  unfolding isCont_def by (rule tendsto_vec_nth)
huffman@44631
   395
huffman@44136
   396
lemma vec_tendstoI:
wenzelm@61973
   397
  assumes "\<And>i. ((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
wenzelm@61973
   398
  shows "((\<lambda>x. f x) \<longlongrightarrow> a) net"
huffman@36591
   399
proof (rule topological_tendstoI)
huffman@36591
   400
  fix S assume "open S" and "a \<in> S"
huffman@36591
   401
  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
huffman@36591
   402
    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
huffman@44136
   403
    unfolding open_vec_def by metis
huffman@36591
   404
  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
huffman@36591
   405
    using assms A by (rule topological_tendstoD)
huffman@36591
   406
  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
huffman@36591
   407
    by (rule eventually_all_finite)
huffman@36591
   408
  thus "eventually (\<lambda>x. f x \<in> S) net"
lp15@61810
   409
    by (rule eventually_mono, simp add: S)
huffman@36591
   410
qed
huffman@36591
   411
huffman@44136
   412
lemma tendsto_vec_lambda [tendsto_intros]:
wenzelm@61973
   413
  assumes "\<And>i. ((\<lambda>x. f x i) \<longlongrightarrow> a i) net"
wenzelm@61973
   414
  shows "((\<lambda>x. \<chi> i. f x i) \<longlongrightarrow> (\<chi> i. a i)) net"
huffman@44136
   415
  using assms by (simp add: vec_tendstoI)
huffman@36591
   416
huffman@44571
   417
lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
huffman@44571
   418
proof (rule openI)
huffman@44571
   419
  fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
huffman@44571
   420
  then obtain z where "a = z $ i" and "z \<in> S" ..
huffman@44571
   421
  then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
huffman@44571
   422
    and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
wenzelm@60420
   423
    using \<open>open S\<close> unfolding open_vec_def by auto
huffman@44571
   424
  hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   425
    by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
huffman@44571
   426
      simp_all)
huffman@44571
   427
  hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
wenzelm@60420
   428
    using A \<open>a = z $ i\<close> by simp
huffman@44571
   429
  then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
huffman@44571
   430
qed
huffman@36591
   431
huffman@44571
   432
instance vec :: (perfect_space, finite) perfect_space
huffman@44571
   433
proof
huffman@44571
   434
  fix x :: "'a ^ 'b" show "\<not> open {x}"
huffman@44571
   435
  proof
huffman@44571
   436
    assume "open {x}"
hoelzl@62102
   437
    hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)
huffman@44571
   438
    hence "\<forall>i. open {x $ i}" by simp
huffman@44571
   439
    thus "False" by (simp add: not_open_singleton)
huffman@44571
   440
  qed
huffman@44571
   441
qed
huffman@44571
   442
huffman@44571
   443
wenzelm@60420
   444
subsection \<open>Metric space\<close>
hoelzl@62101
   445
(* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
huffman@36591
   446
hoelzl@62101
   447
instantiation vec :: (metric_space, finite) dist
huffman@36591
   448
begin
huffman@36591
   449
huffman@44136
   450
definition
nipkow@67155
   451
  "dist x y = L2_set (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   452
hoelzl@62101
   453
instance ..
hoelzl@62101
   454
end
hoelzl@62101
   455
hoelzl@62101
   456
instantiation vec :: (metric_space, finite) uniformity_dist
hoelzl@62101
   457
begin
hoelzl@62101
   458
hoelzl@62101
   459
definition [code del]:
wenzelm@67731
   460
  "(uniformity :: (('a^'b::_) \<times> ('a^'b::_)) filter) =
hoelzl@62101
   461
    (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   462
hoelzl@62102
   463
instance
hoelzl@62101
   464
  by standard (rule uniformity_vec_def)
hoelzl@62101
   465
end
hoelzl@62101
   466
hoelzl@62102
   467
declare uniformity_Abort[where 'a="'a :: metric_space ^ 'b :: finite", code]
hoelzl@62102
   468
hoelzl@62101
   469
instantiation vec :: (metric_space, finite) metric_space
hoelzl@62101
   470
begin
hoelzl@62101
   471
huffman@44136
   472
lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
nipkow@67155
   473
  unfolding dist_vec_def by (rule member_le_L2_set) simp_all
huffman@36591
   474
huffman@36591
   475
instance proof
huffman@36591
   476
  fix x y :: "'a ^ 'b"
huffman@36591
   477
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44136
   478
    unfolding dist_vec_def
nipkow@67155
   479
    by (simp add: L2_set_eq_0_iff vec_eq_iff)
huffman@36591
   480
next
huffman@36591
   481
  fix x y z :: "'a ^ 'b"
huffman@36591
   482
  show "dist x y \<le> dist x z + dist y z"
huffman@44136
   483
    unfolding dist_vec_def
nipkow@67155
   484
    apply (rule order_trans [OF _ L2_set_triangle_ineq])
nipkow@67155
   485
    apply (simp add: L2_set_mono dist_triangle2)
huffman@36591
   486
    done
huffman@36591
   487
next
huffman@36591
   488
  fix S :: "('a ^ 'b) set"
hoelzl@62101
   489
  have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@44630
   490
  proof
huffman@44630
   491
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   492
    proof
huffman@44630
   493
      fix x assume "x \<in> S"
huffman@44630
   494
      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
huffman@44630
   495
        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
wenzelm@60420
   496
        using \<open>open S\<close> and \<open>x \<in> S\<close> unfolding open_vec_def by metis
huffman@44630
   497
      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
huffman@44630
   498
        using A unfolding open_dist by simp
huffman@44630
   499
      hence "\<exists>r. \<forall>i\<in>UNIV. 0 < r i \<and> (\<forall>y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i)"
huffman@44681
   500
        by (rule finite_set_choice [OF finite])
huffman@44630
   501
      then obtain r where r1: "\<forall>i. 0 < r i"
huffman@44630
   502
        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
huffman@44630
   503
      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
huffman@44630
   504
        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
huffman@44630
   505
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@44630
   506
    qed
huffman@44630
   507
  next
huffman@44630
   508
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44630
   509
    proof (unfold open_vec_def, rule)
huffman@44630
   510
      fix x assume "x \<in> S"
huffman@44630
   511
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   512
        using * by fast
wenzelm@63040
   513
      define r where [abs_def]: "r i = e / sqrt (of_nat CARD('b))" for i :: 'b
wenzelm@60420
   514
      from \<open>0 < e\<close> have r: "\<forall>i. 0 < r i"
nipkow@56541
   515
        unfolding r_def by simp_all
nipkow@67155
   516
      from \<open>0 < e\<close> have e: "e = L2_set r UNIV"
nipkow@67155
   517
        unfolding r_def by (simp add: L2_set_constant)
wenzelm@63040
   518
      define A where "A i = {y. dist (x $ i) y < r i}" for i
huffman@44630
   519
      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
huffman@44630
   520
        unfolding A_def by (simp add: open_ball r)
huffman@44630
   521
      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
nipkow@67155
   522
        by (simp add: A_def S dist_vec_def e L2_set_strict_mono dist_commute)
huffman@44630
   523
      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
huffman@44630
   524
        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
huffman@44630
   525
    qed
huffman@44630
   526
  qed
hoelzl@62101
   527
  show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
hoelzl@62101
   528
    unfolding * eventually_uniformity_metric
hoelzl@62101
   529
    by (simp del: split_paired_All add: dist_vec_def dist_commute)
huffman@36591
   530
qed
huffman@36591
   531
huffman@36591
   532
end
huffman@36591
   533
huffman@44136
   534
lemma Cauchy_vec_nth:
huffman@36591
   535
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@44136
   536
  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
huffman@36591
   537
huffman@44136
   538
lemma vec_CauchyI:
huffman@36591
   539
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   540
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   541
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   542
proof (rule metric_CauchyI)
huffman@36591
   543
  fix r :: real assume "0 < r"
nipkow@56541
   544
  hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp
wenzelm@63040
   545
  define N where "N i = (LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s)" for i
wenzelm@63040
   546
  define M where "M = Max (range N)"
huffman@36591
   547
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
wenzelm@60420
   548
    using X \<open>0 < ?s\<close> by (rule metric_CauchyD)
huffman@36591
   549
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   550
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   551
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   552
    unfolding M_def by simp
huffman@36591
   553
  {
huffman@36591
   554
    fix m n :: nat
huffman@36591
   555
    assume "M \<le> m" "M \<le> n"
nipkow@67155
   556
    have "dist (X m) (X n) = L2_set (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@44136
   557
      unfolding dist_vec_def ..
nipkow@64267
   558
    also have "\<dots> \<le> sum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
nipkow@67155
   559
      by (rule L2_set_le_sum [OF zero_le_dist])
nipkow@64267
   560
    also have "\<dots> < sum (\<lambda>i::'n. ?s) UNIV"
nipkow@64267
   561
      by (rule sum_strict_mono, simp_all add: M \<open>M \<le> m\<close> \<open>M \<le> n\<close>)
huffman@36591
   562
    also have "\<dots> = r"
huffman@36591
   563
      by simp
huffman@36591
   564
    finally have "dist (X m) (X n) < r" .
huffman@36591
   565
  }
huffman@36591
   566
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   567
    by simp
huffman@36591
   568
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   569
qed
huffman@36591
   570
huffman@44136
   571
instance vec :: (complete_space, finite) complete_space
huffman@36591
   572
proof
huffman@36591
   573
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
wenzelm@61969
   574
  have "\<And>i. (\<lambda>n. X n $ i) \<longlonglongrightarrow> lim (\<lambda>n. X n $ i)"
wenzelm@60420
   575
    using Cauchy_vec_nth [OF \<open>Cauchy X\<close>]
huffman@36591
   576
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   577
  hence "X \<longlonglongrightarrow> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@44136
   578
    by (simp add: vec_tendstoI)
huffman@36591
   579
  then show "convergent X"
huffman@36591
   580
    by (rule convergentI)
huffman@36591
   581
qed
huffman@36591
   582
huffman@36591
   583
wenzelm@60420
   584
subsection \<open>Normed vector space\<close>
huffman@36591
   585
huffman@44136
   586
instantiation vec :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   587
begin
huffman@36591
   588
nipkow@67155
   589
definition "norm x = L2_set (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   590
huffman@44141
   591
definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   592
huffman@36591
   593
instance proof
huffman@36591
   594
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   595
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   596
    unfolding norm_vec_def
nipkow@67155
   597
    by (simp add: L2_set_eq_0_iff vec_eq_iff)
huffman@36591
   598
  show "norm (x + y) \<le> norm x + norm y"
huffman@44136
   599
    unfolding norm_vec_def
nipkow@67155
   600
    apply (rule order_trans [OF _ L2_set_triangle_ineq])
nipkow@67155
   601
    apply (simp add: L2_set_mono norm_triangle_ineq)
huffman@36591
   602
    done
huffman@36591
   603
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@44136
   604
    unfolding norm_vec_def
nipkow@67155
   605
    by (simp add: L2_set_right_distrib)
huffman@36591
   606
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@44141
   607
    by (rule sgn_vec_def)
huffman@36591
   608
  show "dist x y = norm (x - y)"
huffman@44136
   609
    unfolding dist_vec_def norm_vec_def
huffman@36591
   610
    by (simp add: dist_norm)
huffman@36591
   611
qed
huffman@36591
   612
huffman@36591
   613
end
huffman@36591
   614
huffman@36591
   615
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@44136
   616
unfolding norm_vec_def
nipkow@67155
   617
by (rule member_le_L2_set) simp_all
huffman@36591
   618
immler@68072
   619
lemma norm_le_componentwise_cart:
immler@68072
   620
  fixes x :: "'a::real_normed_vector^'n"
immler@68072
   621
  assumes "\<And>i. norm(x$i) \<le> norm(y$i)"
immler@68072
   622
  shows "norm x \<le> norm y"
immler@68072
   623
  unfolding norm_vec_def
immler@68072
   624
  by (rule L2_set_mono) (auto simp: assms)
immler@68072
   625
immler@68072
   626
lemma component_le_norm_cart: "\<bar>x$i\<bar> \<le> norm x"
immler@68072
   627
  apply (simp add: norm_vec_def)
immler@68072
   628
  apply (rule member_le_L2_set, simp_all)
immler@68072
   629
  done
immler@68072
   630
immler@68072
   631
lemma norm_bound_component_le_cart: "norm x \<le> e ==> \<bar>x$i\<bar> \<le> e"
immler@68072
   632
  by (metis component_le_norm_cart order_trans)
immler@68072
   633
immler@68072
   634
lemma norm_bound_component_lt_cart: "norm x < e ==> \<bar>x$i\<bar> < e"
immler@68072
   635
  by (metis component_le_norm_cart le_less_trans)
immler@68072
   636
immler@68072
   637
lemma norm_le_l1_cart: "norm x \<le> sum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
immler@68072
   638
  by (simp add: norm_vec_def L2_set_le_sum)
immler@68072
   639
huffman@44282
   640
lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
wenzelm@61169
   641
apply standard
huffman@36591
   642
apply (rule vector_add_component)
huffman@36591
   643
apply (rule vector_scaleR_component)
huffman@36591
   644
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   645
done
huffman@36591
   646
huffman@44136
   647
instance vec :: (banach, finite) banach ..
huffman@36591
   648
huffman@36591
   649
wenzelm@60420
   650
subsection \<open>Inner product space\<close>
huffman@36591
   651
huffman@44136
   652
instantiation vec :: (real_inner, finite) real_inner
huffman@36591
   653
begin
huffman@36591
   654
nipkow@64267
   655
definition "inner x y = sum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   656
huffman@36591
   657
instance proof
huffman@36591
   658
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   659
  show "inner x y = inner y x"
huffman@44136
   660
    unfolding inner_vec_def
huffman@36591
   661
    by (simp add: inner_commute)
huffman@36591
   662
  show "inner (x + y) z = inner x z + inner y z"
huffman@44136
   663
    unfolding inner_vec_def
nipkow@64267
   664
    by (simp add: inner_add_left sum.distrib)
huffman@36591
   665
  show "inner (scaleR r x) y = r * inner x y"
huffman@44136
   666
    unfolding inner_vec_def
nipkow@64267
   667
    by (simp add: sum_distrib_left)
huffman@36591
   668
  show "0 \<le> inner x x"
huffman@44136
   669
    unfolding inner_vec_def
nipkow@64267
   670
    by (simp add: sum_nonneg)
huffman@36591
   671
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   672
    unfolding inner_vec_def
nipkow@64267
   673
    by (simp add: vec_eq_iff sum_nonneg_eq_0_iff)
huffman@36591
   674
  show "norm x = sqrt (inner x x)"
nipkow@67155
   675
    unfolding inner_vec_def norm_vec_def L2_set_def
huffman@36591
   676
    by (simp add: power2_norm_eq_inner)
huffman@36591
   677
qed
huffman@36591
   678
huffman@36591
   679
end
huffman@36591
   680
huffman@44166
   681
wenzelm@60420
   682
subsection \<open>Euclidean space\<close>
huffman@44135
   683
wenzelm@60420
   684
text \<open>Vectors pointing along a single axis.\<close>
huffman@44166
   685
huffman@44166
   686
definition "axis k x = (\<chi> i. if i = k then x else 0)"
huffman@44166
   687
huffman@44166
   688
lemma axis_nth [simp]: "axis i x $ i = x"
huffman@44166
   689
  unfolding axis_def by simp
huffman@44166
   690
huffman@44166
   691
lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
huffman@44166
   692
  unfolding axis_def vec_eq_iff by auto
huffman@44166
   693
huffman@44166
   694
lemma inner_axis_axis:
huffman@44166
   695
  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
huffman@44166
   696
  unfolding inner_vec_def
huffman@44166
   697
  apply (cases "i = j")
huffman@44166
   698
  apply clarsimp
nipkow@64267
   699
  apply (subst sum.remove [of _ j], simp_all)
nipkow@64267
   700
  apply (rule sum.neutral, simp add: axis_def)
nipkow@64267
   701
  apply (rule sum.neutral, simp add: axis_def)
huffman@44166
   702
  done
huffman@44166
   703
huffman@44166
   704
lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
lp15@67982
   705
  by (simp add: inner_vec_def axis_def sum.remove [where x=i])
huffman@44166
   706
lp15@67683
   707
lemma inner_axis': "inner(axis i y) x = inner y (x $ i)"
lp15@67683
   708
  by (simp add: inner_axis inner_commute)
lp15@67683
   709
huffman@44136
   710
instantiation vec :: (euclidean_space, finite) euclidean_space
huffman@44135
   711
begin
huffman@44135
   712
huffman@44166
   713
definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
huffman@44166
   714
huffman@44135
   715
instance proof
huffman@44166
   716
  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
huffman@44166
   717
    unfolding Basis_vec_def by simp
huffman@44166
   718
next
huffman@44166
   719
  show "finite (Basis :: ('a ^ 'b) set)"
huffman@44166
   720
    unfolding Basis_vec_def by simp
huffman@44135
   721
next
huffman@44166
   722
  fix u v :: "'a ^ 'b"
huffman@44166
   723
  assume "u \<in> Basis" and "v \<in> Basis"
huffman@44166
   724
  thus "inner u v = (if u = v then 1 else 0)"
huffman@44166
   725
    unfolding Basis_vec_def
huffman@44166
   726
    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
huffman@44135
   727
next
huffman@44166
   728
  fix x :: "'a ^ 'b"
huffman@44166
   729
  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
huffman@44166
   730
    unfolding Basis_vec_def
huffman@44166
   731
    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
hoelzl@50526
   732
qed
hoelzl@50526
   733
lp15@67982
   734
lemma DIM_cart [simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
lp15@67982
   735
proof -
lp15@67982
   736
  have "card (\<Union>i::'b. \<Union>u::'a\<in>Basis. {axis i u}) = (\<Sum>i::'b\<in>UNIV. card (\<Union>u::'a\<in>Basis. {axis i u}))"
lp15@67982
   737
    by (rule card_UN_disjoint) (auto simp: axis_eq_axis) 
lp15@67982
   738
  also have "... = CARD('b) * DIM('a)"
lp15@67982
   739
    by (subst card_UN_disjoint) (auto simp: axis_eq_axis)
lp15@67982
   740
  finally show ?thesis
lp15@67982
   741
    by (simp add: Basis_vec_def)
lp15@67982
   742
qed
huffman@44135
   743
huffman@36591
   744
end
huffman@44135
   745
immler@68072
   746
lemma norm_axis_1 [simp]: "norm (axis m (1::real)) = 1"
immler@68072
   747
  by (simp add: inner_axis' norm_eq_1)
immler@68072
   748
immler@68072
   749
lemma sum_norm_allsubsets_bound_cart:
immler@68072
   750
  fixes f:: "'a \<Rightarrow> real ^'n"
immler@68072
   751
  assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (sum f Q) \<le> e"
immler@68072
   752
  shows "sum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
immler@68072
   753
  using sum_norm_allsubsets_bound[OF assms]
immler@68072
   754
  by simp
immler@68072
   755
lp15@62397
   756
lemma cart_eq_inner_axis: "a $ i = inner a (axis i 1)"
lp15@62397
   757
  by (simp add: inner_axis)
lp15@62397
   758
lp15@67982
   759
lemma axis_eq_0_iff [simp]:
lp15@67982
   760
  shows "axis m x = 0 \<longleftrightarrow> x = 0"
lp15@67982
   761
  by (simp add: axis_def vec_eq_iff)
lp15@67982
   762
lp15@67982
   763
lemma axis_in_Basis_iff [simp]: "axis i a \<in> Basis \<longleftrightarrow> a \<in> Basis"
lp15@67982
   764
  by (auto simp: Basis_vec_def axis_eq_axis)
lp15@67982
   765
lp15@67982
   766
text\<open>Mapping each basis element to the corresponding finite index\<close>
lp15@67982
   767
definition axis_index :: "('a::comm_ring_1)^'n \<Rightarrow> 'n" where "axis_index v \<equiv> SOME i. v = axis i 1"
lp15@67982
   768
lp15@67982
   769
lemma axis_inverse:
lp15@67982
   770
  fixes v :: "real^'n"
lp15@67982
   771
  assumes "v \<in> Basis"
lp15@67982
   772
  shows "\<exists>i. v = axis i 1"
lp15@67982
   773
proof -
lp15@67982
   774
  have "v \<in> (\<Union>n. \<Union>r\<in>Basis. {axis n r})"
lp15@67982
   775
    using assms Basis_vec_def by blast
lp15@67982
   776
  then show ?thesis
lp15@67982
   777
    by (force simp add: vec_eq_iff)
lp15@67982
   778
qed
lp15@67982
   779
lp15@67982
   780
lemma axis_index:
lp15@67982
   781
  fixes v :: "real^'n"
lp15@67982
   782
  assumes "v \<in> Basis"
lp15@67982
   783
  shows "v = axis (axis_index v) 1"
lp15@67982
   784
  by (metis (mono_tags) assms axis_inverse axis_index_def someI_ex)
lp15@67982
   785
lp15@67982
   786
lemma axis_index_axis [simp]:
lp15@67982
   787
  fixes UU :: "real^'n"
lp15@67982
   788
  shows "(axis_index (axis u 1 :: real^'n)) = (u::'n)"
lp15@67982
   789
  by (simp add: axis_eq_axis axis_index_def)
lp15@62397
   790
immler@68072
   791
subsection \<open>A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space\<close>
immler@68072
   792
immler@68072
   793
lemma sum_cong_aux:
immler@68072
   794
  "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> sum f A = sum g A"
immler@68072
   795
  by (auto intro: sum.cong)
immler@68072
   796
immler@68072
   797
hide_fact (open) sum_cong_aux
immler@68072
   798
immler@68072
   799
method_setup vector = \<open>
immler@68072
   800
let
immler@68072
   801
  val ss1 =
immler@68072
   802
    simpset_of (put_simpset HOL_basic_ss @{context}
immler@68072
   803
      addsimps [@{thm sum.distrib} RS sym,
immler@68072
   804
      @{thm sum_subtractf} RS sym, @{thm sum_distrib_left},
immler@68072
   805
      @{thm sum_distrib_right}, @{thm sum_negf} RS sym])
immler@68072
   806
  val ss2 =
immler@68072
   807
    simpset_of (@{context} addsimps
immler@68072
   808
             [@{thm plus_vec_def}, @{thm times_vec_def},
immler@68072
   809
              @{thm minus_vec_def}, @{thm uminus_vec_def},
immler@68072
   810
              @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def},
immler@68072
   811
              @{thm scaleR_vec_def},
immler@68072
   812
              @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}])
immler@68072
   813
  fun vector_arith_tac ctxt ths =
immler@68072
   814
    simp_tac (put_simpset ss1 ctxt)
immler@68072
   815
    THEN' (fn i => resolve_tac ctxt @{thms Finite_Cartesian_Product.sum_cong_aux} i
immler@68072
   816
         ORELSE resolve_tac ctxt @{thms sum.neutral} i
immler@68072
   817
         ORELSE simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm vec_eq_iff}]) i)
immler@68072
   818
    (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
immler@68072
   819
    THEN' asm_full_simp_tac (put_simpset ss2 ctxt addsimps ths)
immler@68072
   820
in
immler@68072
   821
  Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (vector_arith_tac ctxt ths))
huffman@44135
   822
end
immler@68072
   823
\<close> "lift trivial vector statements to real arith statements"
immler@68072
   824
immler@68072
   825
lemma vec_0[simp]: "vec 0 = 0" by vector
immler@68072
   826
lemma vec_1[simp]: "vec 1 = 1" by vector
immler@68072
   827
immler@68072
   828
lemma vec_inj[simp]: "vec x = vec y \<longleftrightarrow> x = y" by vector
immler@68072
   829
immler@68072
   830
lemma vec_in_image_vec: "vec x \<in> (vec ` S) \<longleftrightarrow> x \<in> S" by auto
immler@68072
   831
immler@68072
   832
lemma vec_add: "vec(x + y) = vec x + vec y"  by vector
immler@68072
   833
lemma vec_sub: "vec(x - y) = vec x - vec y" by vector
immler@68072
   834
lemma vec_cmul: "vec(c * x) = c *s vec x " by vector
immler@68072
   835
lemma vec_neg: "vec(- x) = - vec x " by vector
immler@68072
   836
immler@68072
   837
lemma vec_scaleR: "vec(c * x) = c *\<^sub>R vec x"
immler@68072
   838
  by vector
immler@68072
   839
immler@68072
   840
lemma vec_sum:
immler@68072
   841
  assumes "finite S"
immler@68072
   842
  shows "vec(sum f S) = sum (vec \<circ> f) S"
immler@68072
   843
  using assms
immler@68072
   844
proof induct
immler@68072
   845
  case empty
immler@68072
   846
  then show ?case by simp
immler@68072
   847
next
immler@68072
   848
  case insert
immler@68072
   849
  then show ?case by (auto simp add: vec_add)
immler@68072
   850
qed
immler@68072
   851
immler@68072
   852
text\<open>Obvious "component-pushing".\<close>
immler@68072
   853
immler@68072
   854
lemma vec_component [simp]: "vec x $ i = x"
immler@68072
   855
  by vector
immler@68072
   856
immler@68072
   857
lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i"
immler@68072
   858
  by vector
immler@68072
   859
immler@68072
   860
lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)"
immler@68072
   861
  by vector
immler@68072
   862
immler@68072
   863
lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
immler@68072
   864
immler@68072
   865
lemmas vector_component =
immler@68072
   866
  vec_component vector_add_component vector_mult_component
immler@68072
   867
  vector_smult_component vector_minus_component vector_uminus_component
immler@68072
   868
  vector_scaleR_component cond_component
immler@68072
   869
immler@68072
   870
immler@68072
   871
subsection \<open>Some frequently useful arithmetic lemmas over vectors\<close>
immler@68072
   872
immler@68072
   873
instance vec :: (semigroup_mult, finite) semigroup_mult
immler@68072
   874
  by standard (vector mult.assoc)
immler@68072
   875
immler@68072
   876
instance vec :: (monoid_mult, finite) monoid_mult
immler@68072
   877
  by standard vector+
immler@68072
   878
immler@68072
   879
instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
immler@68072
   880
  by standard (vector mult.commute)
immler@68072
   881
immler@68072
   882
instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
immler@68072
   883
  by standard vector
immler@68072
   884
immler@68072
   885
instance vec :: (semiring, finite) semiring
immler@68072
   886
  by standard (vector field_simps)+
immler@68072
   887
immler@68072
   888
instance vec :: (semiring_0, finite) semiring_0
immler@68072
   889
  by standard (vector field_simps)+
immler@68072
   890
instance vec :: (semiring_1, finite) semiring_1
immler@68072
   891
  by standard vector
immler@68072
   892
instance vec :: (comm_semiring, finite) comm_semiring
immler@68072
   893
  by standard (vector field_simps)+
immler@68072
   894
immler@68072
   895
instance vec :: (comm_semiring_0, finite) comm_semiring_0 ..
immler@68072
   896
instance vec :: (semiring_0_cancel, finite) semiring_0_cancel ..
immler@68072
   897
instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel ..
immler@68072
   898
instance vec :: (ring, finite) ring ..
immler@68072
   899
instance vec :: (semiring_1_cancel, finite) semiring_1_cancel ..
immler@68072
   900
instance vec :: (comm_semiring_1, finite) comm_semiring_1 ..
immler@68072
   901
immler@68072
   902
instance vec :: (ring_1, finite) ring_1 ..
immler@68072
   903
immler@68072
   904
instance vec :: (real_algebra, finite) real_algebra
immler@68072
   905
  by standard (simp_all add: vec_eq_iff)
immler@68072
   906
immler@68072
   907
instance vec :: (real_algebra_1, finite) real_algebra_1 ..
immler@68072
   908
immler@68072
   909
lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
immler@68072
   910
proof (induct n)
immler@68072
   911
  case 0
immler@68072
   912
  then show ?case by vector
immler@68072
   913
next
immler@68072
   914
  case Suc
immler@68072
   915
  then show ?case by vector
immler@68072
   916
qed
immler@68072
   917
immler@68072
   918
lemma one_index [simp]: "(1 :: 'a :: one ^ 'n) $ i = 1"
immler@68072
   919
  by vector
immler@68072
   920
immler@68072
   921
lemma neg_one_index [simp]: "(- 1 :: 'a :: {one, uminus} ^ 'n) $ i = - 1"
immler@68072
   922
  by vector
immler@68072
   923
immler@68072
   924
instance vec :: (semiring_char_0, finite) semiring_char_0
immler@68072
   925
proof
immler@68072
   926
  fix m n :: nat
immler@68072
   927
  show "inj (of_nat :: nat \<Rightarrow> 'a ^ 'b)"
immler@68072
   928
    by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
immler@68072
   929
qed
immler@68072
   930
immler@68072
   931
instance vec :: (numeral, finite) numeral ..
immler@68072
   932
instance vec :: (semiring_numeral, finite) semiring_numeral ..
immler@68072
   933
immler@68072
   934
lemma numeral_index [simp]: "numeral w $ i = numeral w"
immler@68072
   935
  by (induct w) (simp_all only: numeral.simps vector_add_component one_index)
immler@68072
   936
immler@68072
   937
lemma neg_numeral_index [simp]: "- numeral w $ i = - numeral w"
immler@68072
   938
  by (simp only: vector_uminus_component numeral_index)
immler@68072
   939
immler@68072
   940
instance vec :: (comm_ring_1, finite) comm_ring_1 ..
immler@68072
   941
instance vec :: (ring_char_0, finite) ring_char_0 ..
immler@68072
   942
immler@68072
   943
lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
immler@68072
   944
  by (vector mult.assoc)
immler@68072
   945
lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
immler@68072
   946
  by (vector field_simps)
immler@68072
   947
lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
immler@68072
   948
  by (vector field_simps)
immler@68072
   949
lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
immler@68072
   950
lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
immler@68072
   951
lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
immler@68072
   952
  by (vector field_simps)
immler@68072
   953
lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
immler@68072
   954
lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
immler@68072
   955
lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
immler@68072
   956
lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
immler@68072
   957
lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
immler@68072
   958
  by (vector field_simps)
immler@68072
   959
immler@68072
   960
lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
immler@68072
   961
  by (simp add: vec_eq_iff)
immler@68072
   962
immler@68072
   963
lemma Vector_Spaces_linear_vec [simp]: "Vector_Spaces.linear ( * ) vector_scalar_mult vec"
immler@68072
   964
  by unfold_locales (vector algebra_simps)+
immler@68072
   965
immler@68072
   966
lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
immler@68072
   967
  by vector
immler@68072
   968
immler@68072
   969
lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::'a::field) \<or> x = y"
immler@68072
   970
  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
immler@68072
   971
immler@68072
   972
lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::'a::field) = b \<or> x = 0"
immler@68072
   973
  by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
immler@68072
   974
immler@68072
   975
lemma scalar_mult_eq_scaleR [abs_def]: "c *s x = c *\<^sub>R x"
immler@68072
   976
  unfolding scaleR_vec_def vector_scalar_mult_def by simp
immler@68072
   977
immler@68072
   978
lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
immler@68072
   979
  unfolding dist_norm scalar_mult_eq_scaleR
immler@68072
   980
  unfolding scaleR_right_diff_distrib[symmetric] by simp
immler@68072
   981
immler@68072
   982
lemma sum_component [simp]:
immler@68072
   983
  fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
immler@68072
   984
  shows "(sum f S)$i = sum (\<lambda>x. (f x)$i) S"
immler@68072
   985
proof (cases "finite S")
immler@68072
   986
  case True
immler@68072
   987
  then show ?thesis by induct simp_all
immler@68072
   988
next
immler@68072
   989
  case False
immler@68072
   990
  then show ?thesis by simp
immler@68072
   991
qed
immler@68072
   992
immler@68072
   993
lemma sum_eq: "sum f S = (\<chi> i. sum (\<lambda>x. (f x)$i ) S)"
immler@68072
   994
  by (simp add: vec_eq_iff)
immler@68072
   995
immler@68072
   996
lemma sum_cmul:
immler@68072
   997
  fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
immler@68072
   998
  shows "sum (\<lambda>x. c *s f x) S = c *s sum f S"
immler@68072
   999
  by (simp add: vec_eq_iff sum_distrib_left)
immler@68072
  1000
immler@68072
  1001
lemma linear_vec [simp]: "linear vec"
immler@68072
  1002
  using Vector_Spaces_linear_vec
immler@68072
  1003
  apply (auto )
immler@68072
  1004
  by unfold_locales (vector algebra_simps)+
immler@68072
  1005
immler@68072
  1006
subsection \<open>Matrix operations\<close>
immler@68072
  1007
immler@68072
  1008
text\<open>Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"}\<close>
immler@68072
  1009
immler@68072
  1010
definition map_matrix::"('a \<Rightarrow> 'b) \<Rightarrow> (('a, 'i::finite)vec, 'j::finite) vec \<Rightarrow> (('b, 'i)vec, 'j) vec" where
immler@68072
  1011
  "map_matrix f x = (\<chi> i j. f (x $ i $ j))"
immler@68072
  1012
immler@68072
  1013
lemma nth_map_matrix[simp]: "map_matrix f x $ i $ j = f (x $ i $ j)"
immler@68072
  1014
  by (simp add: map_matrix_def)
immler@68072
  1015
immler@68072
  1016
definition matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"
immler@68072
  1017
    (infixl "**" 70)
immler@68072
  1018
  where "m ** m' == (\<chi> i j. sum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
immler@68072
  1019
immler@68072
  1020
definition matrix_vector_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"
immler@68072
  1021
    (infixl "*v" 70)
immler@68072
  1022
  where "m *v x \<equiv> (\<chi> i. sum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
immler@68072
  1023
immler@68072
  1024
definition vector_matrix_mult :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "
immler@68072
  1025
    (infixl "v*" 70)
immler@68072
  1026
  where "v v* m == (\<chi> j. sum (\<lambda>i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n"
immler@68072
  1027
immler@68072
  1028
definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
immler@68072
  1029
definition transpose where
immler@68072
  1030
  "(transpose::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
immler@68072
  1031
definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
immler@68072
  1032
definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
immler@68072
  1033
definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
immler@68072
  1034
definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
immler@68072
  1035
immler@68073
  1036
lemma times0_left [simp]: "(0::'a::semiring_1^'n^'m) ** (A::'a ^'p^'n) = 0" 
immler@68073
  1037
  by (simp add: matrix_matrix_mult_def zero_vec_def)
immler@68073
  1038
immler@68073
  1039
lemma times0_right [simp]: "(A::'a::semiring_1^'n^'m) ** (0::'a ^'p^'n) = 0" 
immler@68073
  1040
  by (simp add: matrix_matrix_mult_def zero_vec_def)
immler@68073
  1041
immler@68072
  1042
lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
immler@68072
  1043
lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)"
immler@68072
  1044
  by (vector matrix_matrix_mult_def sum.distrib[symmetric] field_simps)
immler@68072
  1045
immler@68072
  1046
lemma matrix_mul_lid [simp]:
immler@68072
  1047
  fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
immler@68072
  1048
  shows "mat 1 ** A = A"
immler@68072
  1049
  apply (simp add: matrix_matrix_mult_def mat_def)
immler@68072
  1050
  apply vector
immler@68072
  1051
  apply (auto simp only: if_distrib if_distribR sum.delta'[OF finite]
immler@68072
  1052
    mult_1_left mult_zero_left if_True UNIV_I)
immler@68072
  1053
  done
immler@68072
  1054
immler@68072
  1055
lemma matrix_mul_rid [simp]:
immler@68072
  1056
  fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
immler@68072
  1057
  shows "A ** mat 1 = A"
immler@68072
  1058
  apply (simp add: matrix_matrix_mult_def mat_def)
immler@68072
  1059
  apply vector
immler@68072
  1060
  apply (auto simp only: if_distrib if_distribR sum.delta[OF finite]
immler@68072
  1061
    mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
immler@68072
  1062
  done
immler@68072
  1063
immler@68072
  1064
lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
immler@68072
  1065
  apply (vector matrix_matrix_mult_def sum_distrib_left sum_distrib_right mult.assoc)
immler@68072
  1066
  apply (subst sum.swap)
immler@68072
  1067
  apply simp
immler@68072
  1068
  done
immler@68072
  1069
immler@68072
  1070
lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
immler@68072
  1071
  apply (vector matrix_matrix_mult_def matrix_vector_mult_def
immler@68072
  1072
    sum_distrib_left sum_distrib_right mult.assoc)
immler@68072
  1073
  apply (subst sum.swap)
immler@68072
  1074
  apply simp
immler@68072
  1075
  done
immler@68072
  1076
immler@68073
  1077
lemma scalar_matrix_assoc:
immler@68073
  1078
  fixes A :: "('a::real_algebra_1)^'m^'n"
immler@68073
  1079
  shows "k *\<^sub>R (A ** B) = (k *\<^sub>R A) ** B"
immler@68073
  1080
  by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff scaleR_sum_right)
immler@68073
  1081
immler@68073
  1082
lemma matrix_scalar_ac:
immler@68073
  1083
  fixes A :: "('a::real_algebra_1)^'m^'n"
immler@68073
  1084
  shows "A ** (k *\<^sub>R B) = k *\<^sub>R A ** B"
immler@68073
  1085
  by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff)
immler@68073
  1086
immler@68072
  1087
lemma matrix_vector_mul_lid [simp]: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)"
immler@68072
  1088
  apply (vector matrix_vector_mult_def mat_def)
immler@68072
  1089
  apply (simp add: if_distrib if_distribR sum.delta' cong del: if_weak_cong)
immler@68072
  1090
  done
immler@68072
  1091
immler@68072
  1092
lemma matrix_transpose_mul:
immler@68072
  1093
    "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)"
immler@68072
  1094
  by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult.commute)
immler@68072
  1095
immler@68072
  1096
lemma matrix_eq:
immler@68072
  1097
  fixes A B :: "'a::semiring_1 ^ 'n ^ 'm"
immler@68072
  1098
  shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
immler@68072
  1099
  apply auto
immler@68072
  1100
  apply (subst vec_eq_iff)
immler@68072
  1101
  apply clarify
immler@68072
  1102
  apply (clarsimp simp add: matrix_vector_mult_def if_distrib if_distribR vec_eq_iff cong del: if_weak_cong)
immler@68072
  1103
  apply (erule_tac x="axis ia 1" in allE)
immler@68072
  1104
  apply (erule_tac x="i" in allE)
immler@68072
  1105
  apply (auto simp add: if_distrib if_distribR axis_def
immler@68072
  1106
    sum.delta[OF finite] cong del: if_weak_cong)
immler@68072
  1107
  done
immler@68072
  1108
immler@68073
  1109
lemma matrix_vector_mul_component: "(A *v x)$k = inner (A$k) x"
immler@68072
  1110
  by (simp add: matrix_vector_mult_def inner_vec_def)
immler@68072
  1111
immler@68072
  1112
lemma dot_lmul_matrix: "inner ((x::real ^_) v* A) y = inner x (A *v y)"
immler@68072
  1113
  apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def sum_distrib_right sum_distrib_left ac_simps)
immler@68072
  1114
  apply (subst sum.swap)
immler@68072
  1115
  apply simp
immler@68072
  1116
  done
immler@68072
  1117
immler@68072
  1118
lemma transpose_mat [simp]: "transpose (mat n) = mat n"
immler@68072
  1119
  by (vector transpose_def mat_def)
immler@68072
  1120
immler@68072
  1121
lemma transpose_transpose [simp]: "transpose(transpose A) = A"
immler@68072
  1122
  by (vector transpose_def)
immler@68072
  1123
immler@68072
  1124
lemma row_transpose [simp]: "row i (transpose A) = column i A"
immler@68072
  1125
  by (simp add: row_def column_def transpose_def vec_eq_iff)
immler@68072
  1126
immler@68072
  1127
lemma column_transpose [simp]: "column i (transpose A) = row i A"
immler@68072
  1128
  by (simp add: row_def column_def transpose_def vec_eq_iff)
immler@68072
  1129
immler@68072
  1130
lemma rows_transpose [simp]: "rows(transpose A) = columns A"
immler@68072
  1131
  by (auto simp add: rows_def columns_def intro: set_eqI)
immler@68072
  1132
immler@68072
  1133
lemma columns_transpose [simp]: "columns(transpose A) = rows A"
immler@68072
  1134
  by (metis transpose_transpose rows_transpose)
immler@68072
  1135
immler@68073
  1136
lemma transpose_scalar: "transpose (k *\<^sub>R A) = k *\<^sub>R transpose A"
immler@68073
  1137
  unfolding transpose_def
immler@68073
  1138
  by (simp add: vec_eq_iff)
immler@68073
  1139
immler@68073
  1140
lemma transpose_iff [iff]: "transpose A = transpose B \<longleftrightarrow> A = B"
immler@68073
  1141
  by (metis transpose_transpose)
immler@68073
  1142
immler@68072
  1143
lemma matrix_mult_sum:
immler@68072
  1144
  "(A::'a::comm_semiring_1^'n^'m) *v x = sum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
immler@68072
  1145
  by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult.commute)
immler@68072
  1146
immler@68072
  1147
lemma vector_componentwise:
immler@68072
  1148
  "(x::'a::ring_1^'n) = (\<chi> j. \<Sum>i\<in>UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)"
immler@68072
  1149
  by (simp add: axis_def if_distrib sum.If_cases vec_eq_iff)
immler@68072
  1150
immler@68072
  1151
lemma basis_expansion: "sum (\<lambda>i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)"
immler@68072
  1152
  by (auto simp add: axis_def vec_eq_iff if_distrib sum.If_cases cong del: if_weak_cong)
immler@68072
  1153
immler@68072
  1154
immler@68072
  1155
text\<open>Correspondence between matrices and linear operators.\<close>
immler@68072
  1156
immler@68072
  1157
definition matrix :: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
immler@68072
  1158
  where "matrix f = (\<chi> i j. (f(axis j 1))$i)"
immler@68072
  1159
immler@68072
  1160
lemma matrix_id_mat_1: "matrix id = mat 1"
immler@68072
  1161
  by (simp add: mat_def matrix_def axis_def)
immler@68072
  1162
immler@68072
  1163
lemma matrix_scaleR: "(matrix (( *\<^sub>R) r)) = mat r"
immler@68072
  1164
  by (simp add: mat_def matrix_def axis_def if_distrib cong: if_cong)
immler@68072
  1165
immler@68073
  1166
lemma matrix_vector_mul_linear[intro, simp]: "linear (\<lambda>x. A *v (x::'a::real_algebra_1 ^ _))"
immler@68073
  1167
  by (simp add: linear_iff matrix_vector_mult_def vec_eq_iff field_simps sum_distrib_left sum.distrib)
immler@68072
  1168
immler@68073
  1169
lemma vector_matrix_left_distrib [algebra_simps]:
immler@68073
  1170
  shows "(x + y) v* A = x v* A + y v* A"
immler@68073
  1171
  unfolding vector_matrix_mult_def
immler@68073
  1172
  by (simp add: algebra_simps sum.distrib vec_eq_iff)
immler@68073
  1173
immler@68073
  1174
lemma matrix_vector_right_distrib [algebra_simps]:
immler@68072
  1175
  "A *v (x + y) = A *v x + A *v y"
immler@68072
  1176
  by (vector matrix_vector_mult_def sum.distrib distrib_left)
immler@68072
  1177
immler@68072
  1178
lemma matrix_vector_mult_diff_distrib [algebra_simps]:
immler@68072
  1179
  fixes A :: "'a::ring_1^'n^'m"
immler@68072
  1180
  shows "A *v (x - y) = A *v x - A *v y"
immler@68072
  1181
  by (vector matrix_vector_mult_def sum_subtractf right_diff_distrib)
immler@68072
  1182
immler@68072
  1183
lemma matrix_vector_mult_scaleR[algebra_simps]:
immler@68072
  1184
  fixes A :: "real^'n^'m"
immler@68072
  1185
  shows "A *v (c *\<^sub>R x) = c *\<^sub>R (A *v x)"
immler@68072
  1186
  using linear_iff matrix_vector_mul_linear by blast
immler@68072
  1187
immler@68072
  1188
lemma matrix_vector_mult_0_right [simp]: "A *v 0 = 0"
immler@68072
  1189
  by (simp add: matrix_vector_mult_def vec_eq_iff)
immler@68072
  1190
immler@68072
  1191
lemma matrix_vector_mult_0 [simp]: "0 *v w = 0"
immler@68072
  1192
  by (simp add: matrix_vector_mult_def vec_eq_iff)
immler@68072
  1193
immler@68072
  1194
lemma matrix_vector_mult_add_rdistrib [algebra_simps]:
immler@68072
  1195
  "(A + B) *v x = (A *v x) + (B *v x)"
immler@68072
  1196
  by (vector matrix_vector_mult_def sum.distrib distrib_right)
immler@68072
  1197
immler@68072
  1198
lemma matrix_vector_mult_diff_rdistrib [algebra_simps]:
immler@68072
  1199
  fixes A :: "'a :: ring_1^'n^'m"
immler@68072
  1200
  shows "(A - B) *v x = (A *v x) - (B *v x)"
immler@68072
  1201
  by (vector matrix_vector_mult_def sum_subtractf left_diff_distrib)
immler@68072
  1202
immler@68072
  1203
lemma matrix_vector_column:
immler@68072
  1204
  "(A::'a::comm_semiring_1^'n^_) *v x = sum (\<lambda>i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)"
immler@68072
  1205
  by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult.commute)
immler@68072
  1206
immler@68072
  1207
subsection\<open>Inverse matrices  (not necessarily square)\<close>
immler@68072
  1208
immler@68072
  1209
definition
immler@68072
  1210
  "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
immler@68072
  1211
immler@68072
  1212
definition
immler@68072
  1213
  "matrix_inv(A:: 'a::semiring_1^'n^'m) =
immler@68072
  1214
    (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
immler@68072
  1215
immler@68072
  1216
lemma inj_matrix_vector_mult:
immler@68072
  1217
  fixes A::"'a::field^'n^'m"
immler@68072
  1218
  assumes "invertible A"
immler@68072
  1219
  shows "inj (( *v) A)"
immler@68072
  1220
  by (metis assms inj_on_inverseI invertible_def matrix_vector_mul_assoc matrix_vector_mul_lid)
immler@68072
  1221
immler@68073
  1222
lemma scalar_invertible:
immler@68073
  1223
  fixes A :: "('a::real_algebra_1)^'m^'n"
immler@68073
  1224
  assumes "k \<noteq> 0" and "invertible A"
immler@68073
  1225
  shows "invertible (k *\<^sub>R A)"
immler@68073
  1226
proof -
immler@68073
  1227
  obtain A' where "A ** A' = mat 1" and "A' ** A = mat 1"
immler@68073
  1228
    using assms unfolding invertible_def by auto
immler@68073
  1229
  with `k \<noteq> 0`
immler@68073
  1230
  have "(k *\<^sub>R A) ** ((1/k) *\<^sub>R A') = mat 1" "((1/k) *\<^sub>R A') ** (k *\<^sub>R A) = mat 1"
immler@68073
  1231
    by (simp_all add: assms matrix_scalar_ac)
immler@68073
  1232
  thus "invertible (k *\<^sub>R A)"
immler@68073
  1233
    unfolding invertible_def by auto
immler@68073
  1234
qed
immler@68073
  1235
immler@68073
  1236
lemma scalar_invertible_iff:
immler@68073
  1237
  fixes A :: "('a::real_algebra_1)^'m^'n"
immler@68073
  1238
  assumes "k \<noteq> 0" and "invertible A"
immler@68073
  1239
  shows "invertible (k *\<^sub>R A) \<longleftrightarrow> k \<noteq> 0 \<and> invertible A"
immler@68073
  1240
  by (simp add: assms scalar_invertible)
immler@68073
  1241
immler@68073
  1242
lemma vector_transpose_matrix [simp]: "x v* transpose A = A *v x"
immler@68073
  1243
  unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
immler@68073
  1244
  by simp
immler@68073
  1245
immler@68073
  1246
lemma transpose_matrix_vector [simp]: "transpose A *v x = x v* A"
immler@68073
  1247
  unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
immler@68073
  1248
  by simp
immler@68073
  1249
immler@68073
  1250
lemma vector_scalar_commute:
immler@68073
  1251
  fixes A :: "'a::{field}^'m^'n"
immler@68073
  1252
  shows "A *v (c *s x) = c *s (A *v x)"
immler@68073
  1253
  by (simp add: vector_scalar_mult_def matrix_vector_mult_def mult_ac sum_distrib_left)
immler@68073
  1254
immler@68073
  1255
lemma scalar_vector_matrix_assoc:
immler@68073
  1256
  fixes k :: "'a::{field}" and x :: "'a::{field}^'n" and A :: "'a^'m^'n"
immler@68073
  1257
  shows "(k *s x) v* A = k *s (x v* A)"
immler@68073
  1258
  by (metis transpose_matrix_vector vector_scalar_commute)
immler@68073
  1259
 
immler@68073
  1260
lemma vector_matrix_mult_0 [simp]: "0 v* A = 0"
immler@68073
  1261
  unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
immler@68073
  1262
immler@68073
  1263
lemma vector_matrix_mult_0_right [simp]: "x v* 0 = 0"
immler@68073
  1264
  unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
immler@68073
  1265
immler@68073
  1266
lemma vector_matrix_mul_rid [simp]:
immler@68073
  1267
  fixes v :: "('a::semiring_1)^'n"
immler@68073
  1268
  shows "v v* mat 1 = v"
immler@68073
  1269
  by (metis matrix_vector_mul_lid transpose_mat vector_transpose_matrix)
immler@68073
  1270
immler@68073
  1271
lemma scaleR_vector_matrix_assoc:
immler@68073
  1272
  fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
immler@68073
  1273
  shows "(k *\<^sub>R x) v* A = k *\<^sub>R (x v* A)"
immler@68073
  1274
  by (metis matrix_vector_mult_scaleR transpose_matrix_vector)
immler@68073
  1275
immler@68073
  1276
lemma vector_scaleR_matrix_ac:
immler@68073
  1277
  fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
immler@68073
  1278
  shows "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
immler@68073
  1279
proof -
immler@68073
  1280
  have "x v* (k *\<^sub>R A) = (k *\<^sub>R x) v* A"
immler@68073
  1281
    unfolding vector_matrix_mult_def
immler@68073
  1282
    by (simp add: algebra_simps)
immler@68073
  1283
  with scaleR_vector_matrix_assoc
immler@68073
  1284
  show "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
immler@68073
  1285
    by auto
immler@68073
  1286
qed
immler@68073
  1287
immler@68072
  1288
end