src/HOL/Matrix/Matrix.thy
author haftmann
Fri Jul 20 14:28:01 2007 +0200 (2007-07-20 ago)
changeset 23879 4776af8be741
parent 23477 f4b83f03cac9
child 25303 0699e20feabd
permissions -rw-r--r--
split class abs from class minus
     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) abs
    26   abs_matrix_def: "abs A \<equiv> sup A (- A)" ..
    27 
    28 instance matrix :: (lordered_ab_group) lordered_ab_group_meet
    29 proof 
    30   fix A B C :: "('a::lordered_ab_group) 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]: "Rep_matrix ((a::('a::lordered_ab_group)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
    93 by (simp add: plus_matrix_def)
    94 
    95 lemma Rep_matrix_mult: "Rep_matrix ((a::('a::lordered_ring) matrix) * b) j i = 
    96   foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
    97 apply (simp add: times_matrix_def)
    98 apply (simp add: Rep_mult_matrix)
    99 done
   100 
   101 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)"
   102 apply (subst Rep_matrix_inject[symmetric])
   103 apply (rule ext)+
   104 apply (simp)
   105 done
   106 
   107 lemma singleton_matrix_add: "singleton_matrix j i ((a::_::lordered_ab_group)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
   108 apply (subst Rep_matrix_inject[symmetric])
   109 apply (rule ext)+
   110 apply (simp)
   111 done
   112 
   113 lemma nrows_mult: "nrows ((A::('a::lordered_ring) matrix) * B) <= nrows A"
   114 by (simp add: times_matrix_def mult_nrows)
   115 
   116 lemma ncols_mult: "ncols ((A::('a::lordered_ring) matrix) * B) <= ncols B"
   117 by (simp add: times_matrix_def mult_ncols)
   118 
   119 definition
   120   one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix" where
   121   "one_matrix n = Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
   122 
   123 lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
   124 apply (simp add: one_matrix_def)
   125 apply (simplesubst RepAbs_matrix)
   126 apply (rule exI[of _ n], simp add: split_if)+
   127 by (simp add: split_if)
   128 
   129 lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
   130 proof -
   131   have "?r <= n" by (simp add: nrows_le)
   132   moreover have "n <= ?r" by (simp add:le_nrows, arith)
   133   ultimately show "?r = n" by simp
   134 qed
   135 
   136 lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
   137 proof -
   138   have "?r <= n" by (simp add: ncols_le)
   139   moreover have "n <= ?r" by (simp add: le_ncols, arith)
   140   ultimately show "?r = n" by simp
   141 qed
   142 
   143 lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{lordered_ring,ring_1}) matrix) * (one_matrix n) = A"
   144 apply (subst Rep_matrix_inject[THEN sym])
   145 apply (rule ext)+
   146 apply (simp add: times_matrix_def Rep_mult_matrix)
   147 apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
   148 apply (simp_all)
   149 by (simp add: max_def ncols)
   150 
   151 lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::{lordered_ring, ring_1}) matrix)"
   152 apply (subst Rep_matrix_inject[THEN sym])
   153 apply (rule ext)+
   154 apply (simp add: times_matrix_def Rep_mult_matrix)
   155 apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
   156 apply (simp_all)
   157 by (simp add: max_def nrows)
   158 
   159 lemma transpose_matrix_mult: "transpose_matrix ((A::('a::{lordered_ring,comm_ring}) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
   160 apply (simp add: times_matrix_def)
   161 apply (subst transpose_mult_matrix)
   162 apply (simp_all add: mult_commute)
   163 done
   164 
   165 lemma transpose_matrix_add: "transpose_matrix ((A::('a::lordered_ab_group) matrix)+B) = transpose_matrix A + transpose_matrix B"
   166 by (simp add: plus_matrix_def transpose_combine_matrix)
   167 
   168 lemma transpose_matrix_diff: "transpose_matrix ((A::('a::lordered_ab_group) matrix)-B) = transpose_matrix A - transpose_matrix B"
   169 by (simp add: diff_matrix_def transpose_combine_matrix)
   170 
   171 lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::lordered_ring) matrix)) = - transpose_matrix (A::('a::lordered_ring) matrix)"
   172 by (simp add: minus_matrix_def transpose_apply_matrix)
   173 
   174 constdefs 
   175   right_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   176   "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
   177   left_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   178   "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
   179   inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   180   "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
   181 
   182 lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
   183 apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
   184 by (simp add: right_inverse_matrix_def)
   185 
   186 lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
   187 apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
   188 by (simp add: left_inverse_matrix_def)
   189 
   190 lemma left_right_inverse_matrix_unique: 
   191   assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
   192   shows "X = Y"
   193 proof -
   194   have "Y = Y * one_matrix (nrows A)" 
   195     apply (subst one_matrix_mult_right)
   196     apply (insert prems)
   197     by (simp_all add: left_inverse_matrix_def)
   198   also have "\<dots> = Y * (A * X)" 
   199     apply (insert prems)
   200     apply (frule right_inverse_matrix_dim)
   201     by (simp add: right_inverse_matrix_def)
   202   also have "\<dots> = (Y * A) * X" by (simp add: mult_assoc)
   203   also have "\<dots> = X" 
   204     apply (insert prems)
   205     apply (frule left_inverse_matrix_dim)
   206     apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
   207     done
   208   ultimately show "X = Y" by (simp)
   209 qed
   210 
   211 lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
   212   by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
   213 
   214 lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
   215   by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
   216 
   217 lemma zero_imp_mult_zero: "(a::'a::ring) = 0 | b = 0 \<Longrightarrow> a * b = 0"
   218 by auto
   219 
   220 lemma Rep_matrix_zero_imp_mult_zero:
   221   "! j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0  \<Longrightarrow> A * B = (0::('a::lordered_ring) matrix)"
   222 apply (subst Rep_matrix_inject[symmetric])
   223 apply (rule ext)+
   224 apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
   225 done
   226 
   227 lemma add_nrows: "nrows (A::('a::comm_monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
   228 apply (simp add: plus_matrix_def)
   229 apply (rule combine_nrows)
   230 apply (simp_all)
   231 done
   232 
   233 lemma move_matrix_row_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) j 0 = (move_matrix A j 0) * B"
   234 apply (subst Rep_matrix_inject[symmetric])
   235 apply (rule ext)+
   236 apply (auto simp add: Rep_matrix_mult foldseq_zero)
   237 apply (rule_tac foldseq_zerotail[symmetric])
   238 apply (auto simp add: nrows zero_imp_mult_zero max2)
   239 apply (rule order_trans)
   240 apply (rule ncols_move_matrix_le)
   241 apply (simp add: max1)
   242 done
   243 
   244 lemma move_matrix_col_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) 0 i = A * (move_matrix B 0 i)"
   245 apply (subst Rep_matrix_inject[symmetric])
   246 apply (rule ext)+
   247 apply (auto simp add: Rep_matrix_mult foldseq_zero)
   248 apply (rule_tac foldseq_zerotail[symmetric])
   249 apply (auto simp add: ncols zero_imp_mult_zero max1)
   250 apply (rule order_trans)
   251 apply (rule nrows_move_matrix_le)
   252 apply (simp add: max2)
   253 done
   254 
   255 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)" 
   256 apply (subst Rep_matrix_inject[symmetric])
   257 apply (rule ext)+
   258 apply (simp)
   259 done
   260 
   261 lemma move_matrix_mult: "move_matrix ((A::('a::lordered_ring) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)"
   262 by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
   263 
   264 constdefs
   265   scalar_mult :: "('a::lordered_ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix"
   266   "scalar_mult a m == apply_matrix (op * a) m"
   267 
   268 lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
   269 by (simp add: scalar_mult_def)
   270 
   271 lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
   272 by (simp add: scalar_mult_def apply_matrix_add ring_simps)
   273 
   274 lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
   275 by (simp add: scalar_mult_def)
   276 
   277 lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
   278 apply (subst Rep_matrix_inject[symmetric])
   279 apply (rule ext)+
   280 apply (auto)
   281 done
   282 
   283 lemma Rep_minus[simp]: "Rep_matrix (-(A::_::lordered_ab_group)) x y = - (Rep_matrix A x y)"
   284 by (simp add: minus_matrix_def)
   285 
   286 lemma Rep_abs[simp]: "Rep_matrix (abs (A::_::lordered_ring)) x y = abs (Rep_matrix A x y)"
   287 by (simp add: abs_lattice sup_matrix_def)
   288 
   289 end