src/HOL/Matrix/Matrix.thy
author haftmann
Mon Aug 14 13:46:06 2006 +0200 (2006-08-14)
changeset 20380 14f9f2a1caa6
parent 17915 e38947f9ba5e
child 20633 e98f59806244
permissions -rw-r--r--
simplified code generator setup
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
wenzelm@17915
    10
instance matrix :: (minus) minus ..
obua@14593
    11
wenzelm@17915
    12
instance matrix :: (plus) plus ..
wenzelm@17915
    13
wenzelm@17915
    14
instance matrix :: ("{plus,times}") times ..
obua@14940
    15
obua@14940
    16
defs (overloaded)
obua@14940
    17
  plus_matrix_def: "A + B == combine_matrix (op +) A B"
obua@14940
    18
  diff_matrix_def: "A - B == combine_matrix (op -) A B"
obua@14940
    19
  minus_matrix_def: "- A == apply_matrix uminus A"
obua@14940
    20
  times_matrix_def: "A * B == mult_matrix (op *) (op +) A B"
obua@14940
    21
obua@14940
    22
lemma is_meet_combine_matrix_meet: "is_meet (combine_matrix meet)"
obua@15178
    23
  by (simp_all add: is_meet_def le_matrix_def meet_left_le meet_right_le meet_imp_le)
obua@15178
    24
obua@15178
    25
lemma is_join_combine_matrix_join: "is_join (combine_matrix join)"
obua@15178
    26
  by (simp_all add: is_join_def le_matrix_def join_left_le join_right_le join_imp_le)
obua@14593
    27
obua@14940
    28
instance matrix :: (lordered_ab_group) lordered_ab_group_meet
obua@14940
    29
proof 
obua@14940
    30
  fix A B C :: "('a::lordered_ab_group) 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
  show "\<exists>m\<Colon>'a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix. is_meet m"
obua@14940
    51
    by (auto intro: is_meet_combine_matrix_meet)
obua@14940
    52
  assume "A <= B"
obua@14940
    53
  then show "C + A <= C + B"
obua@14940
    54
    apply (simp add: plus_matrix_def)
obua@14940
    55
    apply (rule le_left_combine_matrix)
obua@14940
    56
    apply (simp_all)
obua@14940
    57
    done
obua@14940
    58
qed
obua@14593
    59
obua@14593
    60
defs (overloaded)
obua@14940
    61
  abs_matrix_def: "abs (A::('a::lordered_ab_group) matrix) == join A (- A)"
obua@14593
    62
obua@14940
    63
instance matrix :: (lordered_ring) lordered_ring
obua@14940
    64
proof
obua@14940
    65
  fix A B C :: "('a :: lordered_ring) matrix"
obua@14940
    66
  show "A * B * C = A * (B * C)"
obua@14940
    67
    apply (simp add: times_matrix_def)
obua@14940
    68
    apply (rule mult_matrix_assoc)
obua@14940
    69
    apply (simp_all add: associative_def ring_eq_simps)
obua@14940
    70
    done
obua@14940
    71
  show "(A + B) * C = A * C + B * C"
obua@14940
    72
    apply (simp add: times_matrix_def plus_matrix_def)
obua@14940
    73
    apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec])
obua@14940
    74
    apply (simp_all add: associative_def commutative_def ring_eq_simps)
obua@14940
    75
    done
obua@14940
    76
  show "A * (B + C) = A * B + A * C"
obua@14940
    77
    apply (simp add: times_matrix_def plus_matrix_def)
obua@14940
    78
    apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec])
obua@14940
    79
    apply (simp_all add: associative_def commutative_def ring_eq_simps)
obua@14940
    80
    done  
obua@14940
    81
  show "abs A = join A (-A)" 
obua@14940
    82
    by (simp add: abs_matrix_def)
obua@14940
    83
  assume a: "A \<le> B"
obua@14940
    84
  assume b: "0 \<le> C"
obua@14940
    85
  from a b show "C * A \<le> C * B"
obua@14940
    86
    apply (simp add: times_matrix_def)
obua@14940
    87
    apply (rule le_left_mult)
obua@14940
    88
    apply (simp_all add: add_mono mult_left_mono)
obua@14940
    89
    done
obua@14940
    90
  from a b show "A * C \<le> B * C"
obua@14940
    91
    apply (simp add: times_matrix_def)
obua@14940
    92
    apply (rule le_right_mult)
obua@14940
    93
    apply (simp_all add: add_mono mult_right_mono)
obua@14940
    94
    done
obua@14593
    95
qed
obua@14593
    96
obua@14940
    97
lemma Rep_matrix_add[simp]: "Rep_matrix ((a::('a::lordered_ab_group)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
obua@14940
    98
by (simp add: plus_matrix_def)
obua@14593
    99
obua@14940
   100
lemma Rep_matrix_mult: "Rep_matrix ((a::('a::lordered_ring) matrix) * b) j i = 
obua@14940
   101
  foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
obua@14940
   102
apply (simp add: times_matrix_def)
obua@14940
   103
apply (simp add: Rep_mult_matrix)
obua@14940
   104
done
obua@14940
   105
 
obua@14593
   106
obua@14940
   107
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) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)"
obua@14940
   108
apply (subst Rep_matrix_inject[symmetric])
obua@14593
   109
apply (rule ext)+
obua@14940
   110
apply (simp)
obua@14940
   111
done
obua@14593
   112
obua@14940
   113
lemma singleton_matrix_add: "singleton_matrix j i ((a::_::lordered_ab_group)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
obua@14940
   114
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   115
apply (rule ext)+
obua@14940
   116
apply (simp)
obua@14940
   117
done
obua@14593
   118
obua@14940
   119
lemma nrows_mult: "nrows ((A::('a::lordered_ring) matrix) * B) <= nrows A"
obua@14593
   120
by (simp add: times_matrix_def mult_nrows)
obua@14593
   121
obua@14940
   122
lemma ncols_mult: "ncols ((A::('a::lordered_ring) matrix) * B) <= ncols B"
obua@14593
   123
by (simp add: times_matrix_def mult_ncols)
obua@14593
   124
obua@14593
   125
constdefs
obua@14940
   126
  one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix"
obua@14593
   127
  "one_matrix n == Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
obua@14593
   128
obua@14593
   129
lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
obua@14593
   130
apply (simp add: one_matrix_def)
paulson@15481
   131
apply (simplesubst RepAbs_matrix)
obua@14593
   132
apply (rule exI[of _ n], simp add: split_if)+
nipkow@16733
   133
by (simp add: split_if)
obua@14593
   134
obua@14940
   135
lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::axclass_0_neq_1)matrix) = n" (is "?r = _")
obua@14593
   136
proof -
obua@14593
   137
  have "?r <= n" by (simp add: nrows_le)
obua@14940
   138
  moreover have "n <= ?r" by (simp add:le_nrows, arith)
obua@14593
   139
  ultimately show "?r = n" by simp
obua@14593
   140
qed
obua@14593
   141
obua@14940
   142
lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::axclass_0_neq_1)matrix) = n" (is "?r = _")
obua@14593
   143
proof -
obua@14593
   144
  have "?r <= n" by (simp add: ncols_le)
obua@14593
   145
  moreover have "n <= ?r" by (simp add: le_ncols, arith)
obua@14593
   146
  ultimately show "?r = n" by simp
obua@14593
   147
qed
obua@14593
   148
obua@14940
   149
lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{lordered_ring,ring_1}) matrix) * (one_matrix n) = A"
obua@14593
   150
apply (subst Rep_matrix_inject[THEN sym])
obua@14593
   151
apply (rule ext)+
obua@14593
   152
apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593
   153
apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
obua@14593
   154
apply (simp_all)
obua@14593
   155
by (simp add: max_def ncols)
obua@14593
   156
obua@14940
   157
lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::{lordered_ring, ring_1}) matrix)"
obua@14593
   158
apply (subst Rep_matrix_inject[THEN sym])
obua@14593
   159
apply (rule ext)+
obua@14593
   160
apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593
   161
apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
obua@14593
   162
apply (simp_all)
obua@14593
   163
by (simp add: max_def nrows)
obua@14593
   164
obua@14940
   165
lemma transpose_matrix_mult: "transpose_matrix ((A::('a::{lordered_ring,comm_ring}) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
obua@14940
   166
apply (simp add: times_matrix_def)
obua@14940
   167
apply (subst transpose_mult_matrix)
obua@14940
   168
apply (simp_all add: mult_commute)
obua@14940
   169
done
obua@14940
   170
obua@14940
   171
lemma transpose_matrix_add: "transpose_matrix ((A::('a::lordered_ab_group) matrix)+B) = transpose_matrix A + transpose_matrix B"
obua@14940
   172
by (simp add: plus_matrix_def transpose_combine_matrix)
obua@14940
   173
obua@14940
   174
lemma transpose_matrix_diff: "transpose_matrix ((A::('a::lordered_ab_group) matrix)-B) = transpose_matrix A - transpose_matrix B"
obua@14940
   175
by (simp add: diff_matrix_def transpose_combine_matrix)
obua@14940
   176
obua@14940
   177
lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::lordered_ring) matrix)) = - transpose_matrix (A::('a::lordered_ring) matrix)"
obua@14940
   178
by (simp add: minus_matrix_def transpose_apply_matrix)
obua@14940
   179
obua@14940
   180
constdefs 
obua@14940
   181
  right_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   182
  "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
obua@14940
   183
  left_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   184
  "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
obua@14940
   185
  inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940
   186
  "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
obua@14593
   187
obua@14593
   188
lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
obua@14593
   189
apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
obua@14593
   190
by (simp add: right_inverse_matrix_def)
obua@14593
   191
obua@14940
   192
lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
obua@14940
   193
apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
obua@14940
   194
by (simp add: left_inverse_matrix_def)
obua@14940
   195
obua@14940
   196
lemma left_right_inverse_matrix_unique: 
obua@14940
   197
  assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
obua@14940
   198
  shows "X = Y"
obua@14940
   199
proof -
obua@14940
   200
  have "Y = Y * one_matrix (nrows A)" 
obua@14940
   201
    apply (subst one_matrix_mult_right)
obua@14940
   202
    apply (insert prems)
obua@14940
   203
    by (simp_all add: left_inverse_matrix_def)
obua@14940
   204
  also have "\<dots> = Y * (A * X)" 
obua@14940
   205
    apply (insert prems)
obua@14940
   206
    apply (frule right_inverse_matrix_dim)
obua@14940
   207
    by (simp add: right_inverse_matrix_def)
obua@14940
   208
  also have "\<dots> = (Y * A) * X" by (simp add: mult_assoc)
obua@14940
   209
  also have "\<dots> = X" 
obua@14940
   210
    apply (insert prems)
obua@14940
   211
    apply (frule left_inverse_matrix_dim)
obua@14940
   212
    apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
obua@14940
   213
    done
obua@14940
   214
  ultimately show "X = Y" by (simp)
obua@14940
   215
qed
obua@14940
   216
obua@14940
   217
lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
obua@14940
   218
  by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
obua@14940
   219
obua@14940
   220
lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
obua@14940
   221
  by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
obua@14940
   222
obua@14940
   223
lemma zero_imp_mult_zero: "(a::'a::ring) = 0 | b = 0 \<Longrightarrow> a * b = 0"
obua@14940
   224
by auto
obua@14940
   225
obua@14940
   226
lemma Rep_matrix_zero_imp_mult_zero:
obua@14940
   227
  "! 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
   228
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   229
apply (rule ext)+
obua@14940
   230
apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
obua@14940
   231
done
obua@14940
   232
obua@14940
   233
lemma add_nrows: "nrows (A::('a::comm_monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
obua@14940
   234
apply (simp add: plus_matrix_def)
obua@14940
   235
apply (rule combine_nrows)
obua@14940
   236
apply (simp_all)
obua@14940
   237
done
obua@14940
   238
obua@14940
   239
lemma move_matrix_row_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) j 0 = (move_matrix A j 0) * B"
obua@14940
   240
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   241
apply (rule ext)+
obua@14940
   242
apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940
   243
apply (rule_tac foldseq_zerotail[symmetric])
obua@14940
   244
apply (auto simp add: nrows zero_imp_mult_zero max2)
obua@14940
   245
apply (rule order_trans)
obua@14940
   246
apply (rule ncols_move_matrix_le)
obua@14940
   247
apply (simp add: max1)
obua@14940
   248
done
obua@14940
   249
obua@14940
   250
lemma move_matrix_col_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) 0 i = A * (move_matrix B 0 i)"
obua@14940
   251
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   252
apply (rule ext)+
obua@14940
   253
apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940
   254
apply (rule_tac foldseq_zerotail[symmetric])
obua@14940
   255
apply (auto simp add: ncols zero_imp_mult_zero max1)
obua@14940
   256
apply (rule order_trans)
obua@14940
   257
apply (rule nrows_move_matrix_le)
obua@14940
   258
apply (simp add: max2)
obua@14940
   259
done
obua@14940
   260
obua@14940
   261
lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::lordered_ab_group) matrix)) = (move_matrix A j i) + (move_matrix B j i)" 
obua@14940
   262
apply (subst Rep_matrix_inject[symmetric])
obua@14940
   263
apply (rule ext)+
obua@14940
   264
apply (simp)
obua@14940
   265
done
obua@14940
   266
obua@14940
   267
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
   268
by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
obua@14940
   269
obua@14940
   270
constdefs
obua@14940
   271
  scalar_mult :: "('a::lordered_ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix"
obua@14940
   272
  "scalar_mult a m == apply_matrix (op * a) m"
obua@14940
   273
obua@14940
   274
lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
obua@14940
   275
  by (simp add: scalar_mult_def)
obua@14940
   276
obua@14940
   277
lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
obua@14940
   278
  by (simp add: scalar_mult_def apply_matrix_add ring_eq_simps)
obua@14940
   279
obua@14940
   280
lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
obua@14940
   281
  by (simp add: scalar_mult_def)
obua@14940
   282
obua@14940
   283
lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
obua@14940
   284
  apply (subst Rep_matrix_inject[symmetric])
obua@14940
   285
  apply (rule ext)+
obua@14940
   286
  apply (auto)
obua@14940
   287
  done
obua@14940
   288
obua@15178
   289
lemma Rep_minus[simp]: "Rep_matrix (-(A::_::lordered_ab_group)) x y = - (Rep_matrix A x y)"
obua@15178
   290
  by (simp add: minus_matrix_def)
obua@14940
   291
obua@15178
   292
lemma join_matrix: "join (A::('a::lordered_ring) matrix) B = combine_matrix join A B"  
obua@15178
   293
  apply (insert join_unique[of "(combine_matrix join)::('a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix)", simplified is_join_combine_matrix_join])
obua@15178
   294
  apply (simp)
obua@15178
   295
  done
obua@14940
   296
obua@15178
   297
lemma meet_matrix: "meet (A::('a::lordered_ring) matrix) B = combine_matrix meet A B"
obua@15178
   298
  apply (insert meet_unique[of "(combine_matrix meet)::('a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix)", simplified is_meet_combine_matrix_meet])
obua@15178
   299
  apply (simp)
obua@15178
   300
  done
obua@15178
   301
obua@15178
   302
lemma Rep_abs[simp]: "Rep_matrix (abs (A::_::lordered_ring)) x y = abs (Rep_matrix A x y)"
obua@15178
   303
  by (simp add: abs_lattice join_matrix)
obua@14940
   304
obua@14593
   305
end