src/HOL/Matrix/Matrix.thy
changeset 14593 90c88e7ef62d
child 14662 d2c6a0f030ab
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Matrix/Matrix.thy	Fri Apr 16 18:30:51 2004 +0200
     1.3 @@ -0,0 +1,283 @@
     1.4 +(*  Title:      HOL/Matrix/Matrix.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Steven Obua
     1.7 +    License:    2004 Technische Universität München
     1.8 +*)
     1.9 +
    1.10 +theory Matrix=MatrixGeneral:
    1.11 +
    1.12 +axclass almost_matrix_element < zero, plus, times
    1.13 +matrix_add_assoc: "(a+b)+c = a + (b+c)"
    1.14 +matrix_add_commute: "a+b = b+a"
    1.15 +
    1.16 +matrix_mult_assoc: "(a*b)*c = a*(b*c)"
    1.17 +matrix_mult_left_0[simp]: "0 * a = 0"
    1.18 +matrix_mult_right_0[simp]: "a * 0 = 0"
    1.19 +
    1.20 +matrix_left_distrib: "(a+b)*c = a*c+b*c"
    1.21 +matrix_right_distrib: "a*(b+c) = a*b+a*c"
    1.22 +
    1.23 +axclass matrix_element < almost_matrix_element
    1.24 +matrix_add_0[simp]: "0+0 = 0"
    1.25 +
    1.26 +instance matrix :: (plus) plus
    1.27 +by (intro_classes)
    1.28 +
    1.29 +instance matrix :: (times) times
    1.30 +by (intro_classes)
    1.31 +
    1.32 +defs (overloaded)
    1.33 +plus_matrix_def: "A + B == combine_matrix (op +) A B"
    1.34 +times_matrix_def: "A * B == mult_matrix (op *) (op +) A B"
    1.35 +
    1.36 +instance matrix :: (matrix_element) matrix_element
    1.37 +proof -
    1.38 +  note combine_matrix_assoc2 = combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec]
    1.39 +  {
    1.40 +    fix A::"('a::matrix_element) matrix"
    1.41 +    fix B
    1.42 +    fix C
    1.43 +    have "(A + B) + C = A + (B + C)"
    1.44 +      apply (simp add: plus_matrix_def)
    1.45 +      apply (rule combine_matrix_assoc2)
    1.46 +      by (simp_all add: matrix_add_assoc)
    1.47 +  }
    1.48 +  note plus_assoc = this
    1.49 +  {
    1.50 +    fix A::"('a::matrix_element) matrix"
    1.51 +    fix B
    1.52 +    fix C
    1.53 +    have "(A * B) * C = A * (B * C)"
    1.54 +      apply (simp add: times_matrix_def)
    1.55 +      apply (rule mult_matrix_assoc_simple)
    1.56 +      apply (simp_all add: associative_def commutative_def distributive_def l_distributive_def r_distributive_def)
    1.57 +      apply (auto)
    1.58 +      apply (simp_all add: matrix_add_assoc)
    1.59 +      apply (simp_all add: matrix_add_commute)
    1.60 +      apply (simp_all add: matrix_mult_assoc)
    1.61 +      by (simp_all add: matrix_left_distrib matrix_right_distrib)
    1.62 +  }
    1.63 +  note mult_assoc = this
    1.64 +  note combine_matrix_commute2 = combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec]
    1.65 +  {
    1.66 +    fix A::"('a::matrix_element) matrix"
    1.67 +    fix B
    1.68 +    have "A + B = B + A"
    1.69 +      apply (simp add: plus_matrix_def)
    1.70 +      apply (insert combine_matrix_commute2[of "op +"])
    1.71 +      apply (rule combine_matrix_commute2)
    1.72 +      by (simp add: matrix_add_commute)
    1.73 +  }
    1.74 +  note plus_commute = this
    1.75 +  have plus_zero: "(0::('a::matrix_element) matrix) + 0 = 0"
    1.76 +    apply (simp add: plus_matrix_def)
    1.77 +    apply (rule combine_matrix_zero)
    1.78 +    by (simp)
    1.79 +  have mult_left_zero: "!! A. (0::('a::matrix_element) matrix) * A = 0" by(simp add: times_matrix_def)
    1.80 +  have mult_right_zero: "!! A. A * (0::('a::matrix_element) matrix) = 0" by (simp add: times_matrix_def)
    1.81 +  note l_distributive_matrix2 = l_distributive_matrix[simplified l_distributive_def matrix_left_distrib, THEN spec, THEN spec, THEN spec]
    1.82 +  {
    1.83 +    fix A::"('a::matrix_element) matrix" 
    1.84 +    fix B 
    1.85 +    fix C
    1.86 +    have "(A + B) * C = A * C + B * C"
    1.87 +      apply (simp add: plus_matrix_def)
    1.88 +      apply (simp add: times_matrix_def)
    1.89 +      apply (rule l_distributive_matrix2)
    1.90 +      apply (simp_all add: associative_def commutative_def l_distributive_def)
    1.91 +      apply (auto)
    1.92 +      apply (simp_all add: matrix_add_assoc) 
    1.93 +      apply (simp_all add: matrix_add_commute)
    1.94 +      by (simp_all add: matrix_left_distrib)
    1.95 +  }
    1.96 +  note left_distrib = this
    1.97 +  note r_distributive_matrix2 = r_distributive_matrix[simplified r_distributive_def matrix_right_distrib, THEN spec, THEN spec, THEN spec]
    1.98 +  {
    1.99 +    fix A::"('a::matrix_element) matrix" 
   1.100 +    fix B 
   1.101 +    fix C
   1.102 +    have "C * (A + B) = C * A + C * B"
   1.103 +      apply (simp add: plus_matrix_def)
   1.104 +      apply (simp add: times_matrix_def)
   1.105 +      apply (rule r_distributive_matrix2)
   1.106 +      apply (simp_all add: associative_def commutative_def r_distributive_def)
   1.107 +      apply (auto)
   1.108 +      apply (simp_all add: matrix_add_assoc) 
   1.109 +      apply (simp_all add: matrix_add_commute)
   1.110 +      by (simp_all add: matrix_right_distrib)
   1.111 +  }
   1.112 +  note right_distrib = this
   1.113 +  show "OFCLASS('a matrix, matrix_element_class)"
   1.114 +    apply (intro_classes)
   1.115 +    apply (simp_all add: plus_assoc)
   1.116 +    apply (simp_all add: plus_commute)
   1.117 +    apply (simp_all add: plus_zero)
   1.118 +    apply (simp_all add: mult_assoc)
   1.119 +    apply (simp_all add: mult_left_zero mult_right_zero)
   1.120 +    by (simp_all add: left_distrib right_distrib)
   1.121 +qed
   1.122 +
   1.123 +axclass g_almost_semiring < almost_matrix_element
   1.124 +g_add_left_0[simp]: "0 + a = a"
   1.125 +
   1.126 +lemma g_add_right_0[simp]: "(a::'a::g_almost_semiring) + 0 = a"
   1.127 +by (simp add: matrix_add_commute)
   1.128 +
   1.129 +axclass g_semiring < g_almost_semiring
   1.130 +g_add_leftimp_eq: "a+b = a+c \<Longrightarrow> b = c"
   1.131 +
   1.132 +instance g_almost_semiring < matrix_element
   1.133 +by (intro_classes, simp)
   1.134 +
   1.135 +instance semiring < g_semiring
   1.136 +apply (intro_classes)
   1.137 +apply (simp_all add: add_ac)
   1.138 +by (simp_all add: mult_assoc left_distrib right_distrib)
   1.139 +
   1.140 +instance matrix :: (g_almost_semiring) g_almost_semiring
   1.141 +apply (intro_classes)
   1.142 +by (simp add: plus_matrix_def combine_matrix_def combine_infmatrix_def)
   1.143 +
   1.144 +lemma RepAbs_matrix_eq_left: " Rep_matrix(Abs_matrix f) = g \<Longrightarrow> \<exists>m. \<forall>j i. m \<le> j \<longrightarrow> f j i = 0 \<Longrightarrow> \<exists>n. \<forall>j i. n \<le> i \<longrightarrow> f j i = 0 \<Longrightarrow> f = g"
   1.145 +by (simp add: RepAbs_matrix)
   1.146 +
   1.147 +lemma RepAbs_matrix_eq_right: "g = Rep_matrix(Abs_matrix f) \<Longrightarrow> \<exists>m. \<forall>j i. m \<le> j \<longrightarrow> f j i = 0 \<Longrightarrow> \<exists>n. \<forall>j i. n \<le> i \<longrightarrow> f j i = 0 \<Longrightarrow> g = f"
   1.148 +by (simp add: RepAbs_matrix)
   1.149 +
   1.150 +instance matrix :: (g_semiring) g_semiring
   1.151 +apply (intro_classes)
   1.152 +apply (simp add: plus_matrix_def combine_matrix_def combine_infmatrix_def)
   1.153 +apply (subst Rep_matrix_inject[THEN sym])
   1.154 +apply (drule ssubst[OF Rep_matrix_inject, of "% x. x"])
   1.155 +apply (drule RepAbs_matrix_eq_left)
   1.156 +apply (auto)
   1.157 +apply (rule_tac x="max (nrows a) (nrows b)" in exI, simp add: nrows_le)
   1.158 +apply (rule_tac x="max (ncols a) (ncols b)" in exI, simp add: ncols_le)
   1.159 +apply (drule RepAbs_matrix_eq_right)
   1.160 +apply (rule_tac x="max (nrows a) (nrows c)" in exI, simp add: nrows_le)
   1.161 +apply (rule_tac x="max (ncols a) (ncols c)" in exI, simp add: ncols_le)
   1.162 +apply (rule ext)+
   1.163 +apply (drule_tac x="x" and y="x" in comb, simp)
   1.164 +apply (drule_tac x="xa" and y="xa" in comb, simp)
   1.165 +apply (drule g_add_leftimp_eq)
   1.166 +by simp
   1.167 +
   1.168 +axclass pordered_matrix_element < matrix_element, order, zero
   1.169 +pordered_add_right_mono: "a <= b \<Longrightarrow> a + c <= b + c"
   1.170 +pordered_mult_left: "0 <= c \<Longrightarrow> a <= b \<Longrightarrow> c*a <= c*b"
   1.171 +pordered_mult_right: "0 <= c \<Longrightarrow> a <= b \<Longrightarrow> a*c <= b*c"
   1.172 +
   1.173 +lemma pordered_add_left_mono: "(a::'a::pordered_matrix_element) <= b \<Longrightarrow> c + a <= c + b"
   1.174 +apply (insert pordered_add_right_mono[of a b c])
   1.175 +by (simp add: matrix_add_commute)
   1.176 +
   1.177 +lemma pordered_add: "(a::'a::pordered_matrix_element) <= b \<Longrightarrow> (c::'a::pordered_matrix_element) <= d \<Longrightarrow> a+c <= b+d"
   1.178 +proof -
   1.179 +  assume p1:"a <= b"
   1.180 +  assume p2:"c <= d"
   1.181 +  have "a+c <= b+c" by (rule pordered_add_right_mono) 
   1.182 +  also have "\<dots> <= b+d" by (rule pordered_add_left_mono)
   1.183 +  ultimately show "a+c <= b+d" by simp
   1.184 +qed
   1.185 +
   1.186 +instance matrix :: (pordered_matrix_element) pordered_matrix_element 
   1.187 +apply (intro_classes)
   1.188 +apply (simp_all add: plus_matrix_def times_matrix_def)
   1.189 +apply (rule le_combine_matrix)
   1.190 +apply (simp_all)
   1.191 +apply (simp_all add: pordered_add)
   1.192 +apply (rule le_left_mult)
   1.193 +apply (simp_all add: matrix_add_0 g_add_left_0 pordered_add pordered_mult_left matrix_mult_left_0 matrix_mult_right_0)
   1.194 +apply (rule le_right_mult)
   1.195 +by (simp_all add: pordered_add pordered_mult_right)
   1.196 +
   1.197 +axclass pordered_g_semiring < g_semiring, pordered_matrix_element
   1.198 +
   1.199 +instance almost_ordered_semiring < pordered_g_semiring
   1.200 +apply (intro_classes)
   1.201 +by (simp_all add: add_right_mono mult_right_mono mult_left_mono)
   1.202 +
   1.203 +instance matrix :: (pordered_g_semiring) pordered_g_semiring
   1.204 +by (intro_classes)
   1.205 +
   1.206 +lemma nrows_mult: "nrows ((A::('a::matrix_element) matrix) * B) <= nrows A"
   1.207 +by (simp add: times_matrix_def mult_nrows)
   1.208 +
   1.209 +lemma ncols_mult: "ncols ((A::('a::matrix_element) matrix) * B) <= ncols B"
   1.210 +by (simp add: times_matrix_def mult_ncols)
   1.211 +
   1.212 +constdefs
   1.213 +  one_matrix :: "nat \<Rightarrow> ('a::semiring) matrix"
   1.214 +  "one_matrix n == Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
   1.215 +
   1.216 +lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
   1.217 +apply (simp add: one_matrix_def)
   1.218 +apply (subst RepAbs_matrix)
   1.219 +apply (rule exI[of _ n], simp add: split_if)+
   1.220 +by (simp add: split_if, arith)
   1.221 +
   1.222 +lemma nrows_one_matrix[simp]: "nrows (one_matrix n) = n" (is "?r = _")
   1.223 +proof -
   1.224 +  have "?r <= n" by (simp add: nrows_le)
   1.225 +  moreover have "n <= ?r" by (simp add: le_nrows, arith)
   1.226 +  ultimately show "?r = n" by simp
   1.227 +qed
   1.228 +
   1.229 +lemma ncols_one_matrix[simp]: "ncols (one_matrix n) = n" (is "?r = _")
   1.230 +proof -
   1.231 +  have "?r <= n" by (simp add: ncols_le)
   1.232 +  moreover have "n <= ?r" by (simp add: le_ncols, arith)
   1.233 +  ultimately show "?r = n" by simp
   1.234 +qed
   1.235 +
   1.236 +lemma one_matrix_mult_right: "ncols A <= n \<Longrightarrow> A * (one_matrix n) = A"
   1.237 +apply (subst Rep_matrix_inject[THEN sym])
   1.238 +apply (rule ext)+
   1.239 +apply (simp add: times_matrix_def Rep_mult_matrix)
   1.240 +apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
   1.241 +apply (simp_all)
   1.242 +by (simp add: max_def ncols)
   1.243 +
   1.244 +lemma one_matrix_mult_left: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = A"
   1.245 +apply (subst Rep_matrix_inject[THEN sym])
   1.246 +apply (rule ext)+
   1.247 +apply (simp add: times_matrix_def Rep_mult_matrix)
   1.248 +apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
   1.249 +apply (simp_all)
   1.250 +by (simp add: max_def nrows)
   1.251 +
   1.252 +constdefs 
   1.253 +  right_inverse_matrix :: "('a::semiring) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   1.254 +  "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X)))" 
   1.255 +  inverse_matrix :: "('a::semiring) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   1.256 +  "inverse_matrix A X == (right_inverse_matrix A X) \<and> (right_inverse_matrix X A)"
   1.257 +
   1.258 +lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
   1.259 +apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
   1.260 +by (simp add: right_inverse_matrix_def)
   1.261 +
   1.262 +(* to be continued \<dots> *)
   1.263 +
   1.264 +end
   1.265 +
   1.266 +
   1.267 +
   1.268 +
   1.269 +
   1.270 +
   1.271 +
   1.272 +
   1.273 +
   1.274 +
   1.275 +  
   1.276 +  
   1.277 +
   1.278 +
   1.279 +
   1.280 +
   1.281 +
   1.282 +
   1.283 +
   1.284 +
   1.285 +
   1.286 +