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