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