src/HOL/Matrix/Matrix.thy
author haftmann
Thu Nov 29 17:08:26 2007 +0100 (2007-11-29 ago)
changeset 25502 9200b36280c0
parent 25303 0699e20feabd
child 25764 878c37886eed
permissions -rw-r--r--
instance command as rudimentary class target
obua@14593
     1
(*  Title:      HOL/Matrix/Matrix.thy
obua@14593
     2
    ID:         $Id$
obua@14593
     3
    Author:     Steven Obua
obua@14593
     4
*)
obua@14593
     5
wenzelm@17915
     6
theory Matrix
wenzelm@17915
     7
imports MatrixGeneral
wenzelm@17915
     8
begin
obua@14940
     9
haftmann@22452
    10
instance matrix :: ("{zero, lattice}") lattice
haftmann@22452
    11
  "inf \<equiv> combine_matrix inf"
haftmann@22452
    12
  "sup \<equiv> combine_matrix sup"
haftmann@22452
    13
  by default (auto simp add: inf_le1 inf_le2 le_infI le_matrix_def inf_matrix_def sup_matrix_def)
haftmann@22452
    14
haftmann@22422
    15
instance matrix :: ("{plus, zero}") plus
haftmann@22422
    16
  plus_matrix_def: "A + B \<equiv> combine_matrix (op +) A B" ..
obua@14593
    17
haftmann@22422
    18
instance matrix :: ("{minus, zero}") minus
haftmann@22422
    19
  minus_matrix_def: "- A \<equiv> apply_matrix uminus A"
haftmann@22422
    20
  diff_matrix_def: "A - B \<equiv> combine_matrix (op -) A B" ..
obua@14940
    21
haftmann@22422
    22
instance matrix :: ("{plus, times, zero}") times
haftmann@22422
    23
  times_matrix_def: "A * B \<equiv> mult_matrix (op *) (op +) A B" ..
obua@14940
    24
haftmann@25303
    25
instance matrix :: (lordered_ab_group_add) abs
haftmann@25502
    26
  abs_matrix_def: "abs (A \<Colon> 'a matrix) \<equiv> sup A (- A)" ..
haftmann@23879
    27
haftmann@25303
    28
instance matrix :: (lordered_ab_group_add) lordered_ab_group_add_meet
obua@14940
    29
proof 
haftmann@25303
    30
  fix A B C :: "('a::lordered_ab_group_add) matrix"
obua@14940
    31
  show "A + B + C = A + (B + C)"    
obua@14940
    32
    apply (simp add: plus_matrix_def)
obua@14940
    33
    apply (rule combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec])
obua@14940
    34
    apply (simp_all add: add_assoc)
obua@14940
    35
    done
obua@14940
    36
  show "A + B = B + A"
obua@14940
    37
    apply (simp add: plus_matrix_def)
obua@14940
    38
    apply (rule combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec])
obua@14940
    39
    apply (simp_all add: add_commute)
obua@14940
    40
    done
obua@14940
    41
  show "0 + A = A"
obua@14940
    42
    apply (simp add: plus_matrix_def)
obua@14940
    43
    apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
obua@14940
    44
    apply (simp)
obua@14940
    45
    done
obua@14940
    46
  show "- A + A = 0" 
obua@14940
    47
    by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
obua@14940
    48
  show "A - B = A + - B" 
obua@14940
    49
    by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
obua@14940
    50
  assume "A <= B"
obua@14940
    51
  then show "C + A <= C + B"
obua@14940
    52
    apply (simp add: plus_matrix_def)
obua@14940
    53
    apply (rule le_left_combine_matrix)
obua@14940
    54
    apply (simp_all)
obua@14940
    55
    done
obua@14940
    56
qed
obua@14593
    57
obua@14940
    58
instance matrix :: (lordered_ring) lordered_ring
obua@14940
    59
proof
obua@14940
    60
  fix A B C :: "('a :: lordered_ring) matrix"
obua@14940
    61
  show "A * B * C = A * (B * C)"
obua@14940
    62
    apply (simp add: times_matrix_def)
obua@14940
    63
    apply (rule mult_matrix_assoc)
nipkow@23477
    64
    apply (simp_all add: associative_def ring_simps)
obua@14940
    65
    done
obua@14940
    66
  show "(A + B) * C = A * C + B * C"
obua@14940
    67
    apply (simp add: times_matrix_def plus_matrix_def)
obua@14940
    68
    apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec])
nipkow@23477
    69
    apply (simp_all add: associative_def commutative_def ring_simps)
obua@14940
    70
    done
obua@14940
    71
  show "A * (B + C) = A * B + A * C"
obua@14940
    72
    apply (simp add: times_matrix_def plus_matrix_def)
obua@14940
    73
    apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec])
nipkow@23477
    74
    apply (simp_all add: associative_def commutative_def ring_simps)
obua@14940
    75
    done  
haftmann@22422
    76
  show "abs A = sup A (-A)" 
obua@14940
    77
    by (simp add: abs_matrix_def)
obua@14940
    78
  assume a: "A \<le> B"
obua@14940
    79
  assume b: "0 \<le> C"
obua@14940
    80
  from a b show "C * A \<le> C * B"
obua@14940
    81
    apply (simp add: times_matrix_def)
obua@14940
    82
    apply (rule le_left_mult)
obua@14940
    83
    apply (simp_all add: add_mono mult_left_mono)
obua@14940
    84
    done
obua@14940
    85
  from a b show "A * C \<le> B * C"
obua@14940
    86
    apply (simp add: times_matrix_def)
obua@14940
    87
    apply (rule le_right_mult)
obua@14940
    88
    apply (simp_all add: add_mono mult_right_mono)
obua@14940
    89
    done
haftmann@22452
    90
qed 
obua@14593
    91
haftmann@25303
    92
lemma Rep_matrix_add[simp]:
haftmann@25303
    93
  "Rep_matrix ((a::('a::lordered_ab_group_add)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
obua@14940
    94
by (simp add: plus_matrix_def)
obua@14593
    95
obua@14940
    96
lemma Rep_matrix_mult: "Rep_matrix ((a::('a::lordered_ring) matrix) * b) j i = 
obua@14940
    97
  foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
obua@14940
    98
apply (simp add: times_matrix_def)
obua@14940
    99
apply (simp add: Rep_mult_matrix)
obua@14940
   100
done
obua@14593
   101
haftmann@25303
   102
lemma apply_matrix_add: "! x y. f (x+y) = (f x) + (f y) \<Longrightarrow> f 0 = (0::'a) \<Longrightarrow> apply_matrix f ((a::('a::lordered_ab_group_add) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)"
obua@14940
   103
apply (subst Rep_matrix_inject[symmetric])
obua@14593
   104
apply (rule ext)+
obua@14940
   105
apply (simp)
obua@14940
   106
done
obua@14593
   107
haftmann@25303
   108
lemma singleton_matrix_add: "singleton_matrix j i ((a::_::lordered_ab_group_add)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
obua@14940
   109
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   110
apply (rule ext)+
obua@14940
   111
apply (simp)
obua@14940
   112
done
obua@14593
   113
obua@14940
   114
lemma nrows_mult: "nrows ((A::('a::lordered_ring) matrix) * B) <= nrows A"
obua@14593
   115
by (simp add: times_matrix_def mult_nrows)
obua@14593
   116
obua@14940
   117
lemma ncols_mult: "ncols ((A::('a::lordered_ring) matrix) * B) <= ncols B"
obua@14593
   118
by (simp add: times_matrix_def mult_ncols)
obua@14593
   119
haftmann@22422
   120
definition
haftmann@22422
   121
  one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix" where
haftmann@22422
   122
  "one_matrix n = Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
obua@14593
   123
obua@14593
   124
lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
obua@14593
   125
apply (simp add: one_matrix_def)
paulson@15481
   126
apply (simplesubst RepAbs_matrix)
obua@14593
   127
apply (rule exI[of _ n], simp add: split_if)+
nipkow@16733
   128
by (simp add: split_if)
obua@14593
   129
wenzelm@20633
   130
lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
obua@14593
   131
proof -
obua@14593
   132
  have "?r <= n" by (simp add: nrows_le)
obua@14940
   133
  moreover have "n <= ?r" by (simp add:le_nrows, arith)
obua@14593
   134
  ultimately show "?r = n" by simp
obua@14593
   135
qed
obua@14593
   136
wenzelm@20633
   137
lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
obua@14593
   138
proof -
obua@14593
   139
  have "?r <= n" by (simp add: ncols_le)
obua@14593
   140
  moreover have "n <= ?r" by (simp add: le_ncols, arith)
obua@14593
   141
  ultimately show "?r = n" by simp
obua@14593
   142
qed
obua@14593
   143
obua@14940
   144
lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{lordered_ring,ring_1}) matrix) * (one_matrix n) = A"
obua@14593
   145
apply (subst Rep_matrix_inject[THEN sym])
obua@14593
   146
apply (rule ext)+
obua@14593
   147
apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593
   148
apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
obua@14593
   149
apply (simp_all)
obua@14593
   150
by (simp add: max_def ncols)
obua@14593
   151
obua@14940
   152
lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::{lordered_ring, ring_1}) matrix)"
obua@14593
   153
apply (subst Rep_matrix_inject[THEN sym])
obua@14593
   154
apply (rule ext)+
obua@14593
   155
apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593
   156
apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
obua@14593
   157
apply (simp_all)
obua@14593
   158
by (simp add: max_def nrows)
obua@14593
   159
obua@14940
   160
lemma transpose_matrix_mult: "transpose_matrix ((A::('a::{lordered_ring,comm_ring}) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
obua@14940
   161
apply (simp add: times_matrix_def)
obua@14940
   162
apply (subst transpose_mult_matrix)
obua@14940
   163
apply (simp_all add: mult_commute)
obua@14940
   164
done
obua@14940
   165
haftmann@25303
   166
lemma transpose_matrix_add: "transpose_matrix ((A::('a::lordered_ab_group_add) matrix)+B) = transpose_matrix A + transpose_matrix B"
obua@14940
   167
by (simp add: plus_matrix_def transpose_combine_matrix)
obua@14940
   168
haftmann@25303
   169
lemma transpose_matrix_diff: "transpose_matrix ((A::('a::lordered_ab_group_add) matrix)-B) = transpose_matrix A - transpose_matrix B"
obua@14940
   170
by (simp add: diff_matrix_def transpose_combine_matrix)
obua@14940
   171
obua@14940
   172
lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::lordered_ring) matrix)) = - transpose_matrix (A::('a::lordered_ring) matrix)"
obua@14940
   173
by (simp add: minus_matrix_def transpose_apply_matrix)
obua@14940
   174
obua@14940
   175
constdefs 
obua@14940
   176
  right_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   177
  "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
obua@14940
   178
  left_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   179
  "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
obua@14940
   180
  inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   181
  "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
obua@14593
   182
obua@14593
   183
lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
obua@14593
   184
apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
obua@14593
   185
by (simp add: right_inverse_matrix_def)
obua@14593
   186
obua@14940
   187
lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
obua@14940
   188
apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
obua@14940
   189
by (simp add: left_inverse_matrix_def)
obua@14940
   190
obua@14940
   191
lemma left_right_inverse_matrix_unique: 
obua@14940
   192
  assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
obua@14940
   193
  shows "X = Y"
obua@14940
   194
proof -
obua@14940
   195
  have "Y = Y * one_matrix (nrows A)" 
obua@14940
   196
    apply (subst one_matrix_mult_right)
obua@14940
   197
    apply (insert prems)
obua@14940
   198
    by (simp_all add: left_inverse_matrix_def)
obua@14940
   199
  also have "\<dots> = Y * (A * X)" 
obua@14940
   200
    apply (insert prems)
obua@14940
   201
    apply (frule right_inverse_matrix_dim)
obua@14940
   202
    by (simp add: right_inverse_matrix_def)
obua@14940
   203
  also have "\<dots> = (Y * A) * X" by (simp add: mult_assoc)
obua@14940
   204
  also have "\<dots> = X" 
obua@14940
   205
    apply (insert prems)
obua@14940
   206
    apply (frule left_inverse_matrix_dim)
obua@14940
   207
    apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
obua@14940
   208
    done
obua@14940
   209
  ultimately show "X = Y" by (simp)
obua@14940
   210
qed
obua@14940
   211
obua@14940
   212
lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
obua@14940
   213
  by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
obua@14940
   214
obua@14940
   215
lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
obua@14940
   216
  by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
obua@14940
   217
obua@14940
   218
lemma zero_imp_mult_zero: "(a::'a::ring) = 0 | b = 0 \<Longrightarrow> a * b = 0"
obua@14940
   219
by auto
obua@14940
   220
obua@14940
   221
lemma Rep_matrix_zero_imp_mult_zero:
obua@14940
   222
  "! j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0  \<Longrightarrow> A * B = (0::('a::lordered_ring) matrix)"
obua@14940
   223
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   224
apply (rule ext)+
obua@14940
   225
apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
obua@14940
   226
done
obua@14940
   227
obua@14940
   228
lemma add_nrows: "nrows (A::('a::comm_monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
obua@14940
   229
apply (simp add: plus_matrix_def)
obua@14940
   230
apply (rule combine_nrows)
obua@14940
   231
apply (simp_all)
obua@14940
   232
done
obua@14940
   233
obua@14940
   234
lemma move_matrix_row_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) j 0 = (move_matrix A j 0) * B"
obua@14940
   235
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   236
apply (rule ext)+
obua@14940
   237
apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940
   238
apply (rule_tac foldseq_zerotail[symmetric])
obua@14940
   239
apply (auto simp add: nrows zero_imp_mult_zero max2)
obua@14940
   240
apply (rule order_trans)
obua@14940
   241
apply (rule ncols_move_matrix_le)
obua@14940
   242
apply (simp add: max1)
obua@14940
   243
done
obua@14940
   244
obua@14940
   245
lemma move_matrix_col_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) 0 i = A * (move_matrix B 0 i)"
obua@14940
   246
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   247
apply (rule ext)+
obua@14940
   248
apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940
   249
apply (rule_tac foldseq_zerotail[symmetric])
obua@14940
   250
apply (auto simp add: ncols zero_imp_mult_zero max1)
obua@14940
   251
apply (rule order_trans)
obua@14940
   252
apply (rule nrows_move_matrix_le)
obua@14940
   253
apply (simp add: max2)
obua@14940
   254
done
obua@14940
   255
haftmann@25303
   256
lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::lordered_ab_group_add) matrix)) = (move_matrix A j i) + (move_matrix B j i)" 
obua@14940
   257
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   258
apply (rule ext)+
obua@14940
   259
apply (simp)
obua@14940
   260
done
obua@14940
   261
obua@14940
   262
lemma move_matrix_mult: "move_matrix ((A::('a::lordered_ring) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)"
obua@14940
   263
by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
obua@14940
   264
obua@14940
   265
constdefs
obua@14940
   266
  scalar_mult :: "('a::lordered_ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix"
obua@14940
   267
  "scalar_mult a m == apply_matrix (op * a) m"
obua@14940
   268
obua@14940
   269
lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
nipkow@23477
   270
by (simp add: scalar_mult_def)
obua@14940
   271
obua@14940
   272
lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
nipkow@23477
   273
by (simp add: scalar_mult_def apply_matrix_add ring_simps)
obua@14940
   274
obua@14940
   275
lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
nipkow@23477
   276
by (simp add: scalar_mult_def)
obua@14940
   277
obua@14940
   278
lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
nipkow@23477
   279
apply (subst Rep_matrix_inject[symmetric])
nipkow@23477
   280
apply (rule ext)+
nipkow@23477
   281
apply (auto)
nipkow@23477
   282
done
obua@14940
   283
haftmann@25303
   284
lemma Rep_minus[simp]: "Rep_matrix (-(A::_::lordered_ab_group_add)) x y = - (Rep_matrix A x y)"
nipkow@23477
   285
by (simp add: minus_matrix_def)
obua@14940
   286
obua@15178
   287
lemma Rep_abs[simp]: "Rep_matrix (abs (A::_::lordered_ring)) x y = abs (Rep_matrix A x y)"
nipkow@23477
   288
by (simp add: abs_lattice sup_matrix_def)
obua@14940
   289
obua@14593
   290
end