src/HOL/Matrix/Matrix.thy
author wenzelm
Sat May 01 22:01:57 2004 +0200 (2004-05-01 ago)
changeset 14691 e1eedc8cad37
parent 14662 d2c6a0f030ab
child 14724 021264410f87
permissions -rw-r--r--
tuned instance statements;
     1 (*  Title:      HOL/Matrix/Matrix.thy
     2     ID:         $Id$
     3     Author:     Steven Obua
     4     License:    2004 Technische Universität München
     5 *)
     6 
     7 theory Matrix = MatrixGeneral:
     8 
     9 axclass almost_matrix_element < zero, plus, times
    10 matrix_add_assoc: "(a+b)+c = a + (b+c)"
    11 matrix_add_commute: "a+b = b+a"
    12 
    13 matrix_mult_assoc: "(a*b)*c = a*(b*c)"
    14 matrix_mult_left_0[simp]: "0 * a = 0"
    15 matrix_mult_right_0[simp]: "a * 0 = 0"
    16 
    17 matrix_left_distrib: "(a+b)*c = a*c+b*c"
    18 matrix_right_distrib: "a*(b+c) = a*b+a*c"
    19 
    20 axclass matrix_element < almost_matrix_element
    21 matrix_add_0[simp]: "0+0 = 0"
    22 
    23 instance matrix :: (plus) plus ..
    24 instance matrix :: (times) times ..
    25 
    26 defs (overloaded)
    27 plus_matrix_def: "A + B == combine_matrix (op +) A B"
    28 times_matrix_def: "A * B == mult_matrix (op *) (op +) A B"
    29 
    30 instance matrix :: (matrix_element) matrix_element
    31 proof -
    32   note combine_matrix_assoc2 = combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec]
    33   {
    34     fix A::"('a::matrix_element) matrix"
    35     fix B
    36     fix C
    37     have "(A + B) + C = A + (B + C)"
    38       apply (simp add: plus_matrix_def)
    39       apply (rule combine_matrix_assoc2)
    40       by (simp_all add: matrix_add_assoc)
    41   }
    42   note plus_assoc = this
    43   {
    44     fix A::"('a::matrix_element) matrix"
    45     fix B
    46     fix C
    47     have "(A * B) * C = A * (B * C)"
    48       apply (simp add: times_matrix_def)
    49       apply (rule mult_matrix_assoc_simple)
    50       apply (simp_all add: associative_def commutative_def distributive_def l_distributive_def r_distributive_def)
    51       apply (auto)
    52       apply (simp_all add: matrix_add_assoc)
    53       apply (simp_all add: matrix_add_commute)
    54       apply (simp_all add: matrix_mult_assoc)
    55       by (simp_all add: matrix_left_distrib matrix_right_distrib)
    56   }
    57   note mult_assoc = this
    58   note combine_matrix_commute2 = combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec]
    59   {
    60     fix A::"('a::matrix_element) matrix"
    61     fix B
    62     have "A + B = B + A"
    63       apply (simp add: plus_matrix_def)
    64       apply (insert combine_matrix_commute2[of "op +"])
    65       apply (rule combine_matrix_commute2)
    66       by (simp add: matrix_add_commute)
    67   }
    68   note plus_commute = this
    69   have plus_zero: "(0::('a::matrix_element) matrix) + 0 = 0"
    70     apply (simp add: plus_matrix_def)
    71     apply (rule combine_matrix_zero)
    72     by (simp)
    73   have mult_left_zero: "!! A. (0::('a::matrix_element) matrix) * A = 0" by(simp add: times_matrix_def)
    74   have mult_right_zero: "!! A. A * (0::('a::matrix_element) matrix) = 0" by (simp add: times_matrix_def)
    75   note l_distributive_matrix2 = l_distributive_matrix[simplified l_distributive_def matrix_left_distrib, THEN spec, THEN spec, THEN spec]
    76   {
    77     fix A::"('a::matrix_element) matrix"
    78     fix B
    79     fix C
    80     have "(A + B) * C = A * C + B * C"
    81       apply (simp add: plus_matrix_def)
    82       apply (simp add: times_matrix_def)
    83       apply (rule l_distributive_matrix2)
    84       apply (simp_all add: associative_def commutative_def l_distributive_def)
    85       apply (auto)
    86       apply (simp_all add: matrix_add_assoc)
    87       apply (simp_all add: matrix_add_commute)
    88       by (simp_all add: matrix_left_distrib)
    89   }
    90   note left_distrib = this
    91   note r_distributive_matrix2 = r_distributive_matrix[simplified r_distributive_def matrix_right_distrib, THEN spec, THEN spec, THEN spec]
    92   {
    93     fix A::"('a::matrix_element) matrix"
    94     fix B
    95     fix C
    96     have "C * (A + B) = C * A + C * B"
    97       apply (simp add: plus_matrix_def)
    98       apply (simp add: times_matrix_def)
    99       apply (rule r_distributive_matrix2)
   100       apply (simp_all add: associative_def commutative_def r_distributive_def)
   101       apply (auto)
   102       apply (simp_all add: matrix_add_assoc)
   103       apply (simp_all add: matrix_add_commute)
   104       by (simp_all add: matrix_right_distrib)
   105   }
   106   note right_distrib = this
   107   show "OFCLASS('a matrix, matrix_element_class)"
   108     apply (intro_classes)
   109     apply (simp_all add: plus_assoc)
   110     apply (simp_all add: plus_commute)
   111     apply (simp_all add: plus_zero)
   112     apply (simp_all add: mult_assoc)
   113     apply (simp_all add: mult_left_zero mult_right_zero)
   114     by (simp_all add: left_distrib right_distrib)
   115 qed
   116 
   117 axclass g_almost_semiring < almost_matrix_element
   118 g_add_left_0[simp]: "0 + a = a"
   119 
   120 lemma g_add_right_0[simp]: "(a::'a::g_almost_semiring) + 0 = a"
   121 by (simp add: matrix_add_commute)
   122 
   123 axclass g_semiring < g_almost_semiring
   124 g_add_leftimp_eq: "a+b = a+c \<Longrightarrow> b = c"
   125 
   126 instance g_almost_semiring < matrix_element
   127   by intro_classes simp
   128 
   129 instance semiring < g_semiring
   130 apply (intro_classes)
   131 apply (simp_all add: add_ac)
   132 by (simp_all add: mult_assoc left_distrib right_distrib)
   133 
   134 instance matrix :: (g_almost_semiring) g_almost_semiring
   135 apply (intro_classes)
   136 by (simp add: plus_matrix_def combine_matrix_def combine_infmatrix_def)
   137 
   138 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"
   139 by (simp add: RepAbs_matrix)
   140 
   141 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"
   142 by (simp add: RepAbs_matrix)
   143 
   144 instance matrix :: (g_semiring) g_semiring
   145 apply (intro_classes)
   146 apply (simp add: plus_matrix_def combine_matrix_def combine_infmatrix_def)
   147 apply (subst Rep_matrix_inject[THEN sym])
   148 apply (drule ssubst[OF Rep_matrix_inject, of "% x. x"])
   149 apply (drule RepAbs_matrix_eq_left)
   150 apply (auto)
   151 apply (rule_tac x="max (nrows a) (nrows b)" in exI, simp add: nrows_le)
   152 apply (rule_tac x="max (ncols a) (ncols b)" in exI, simp add: ncols_le)
   153 apply (drule RepAbs_matrix_eq_right)
   154 apply (rule_tac x="max (nrows a) (nrows c)" in exI, simp add: nrows_le)
   155 apply (rule_tac x="max (ncols a) (ncols c)" in exI, simp add: ncols_le)
   156 apply (rule ext)+
   157 apply (drule_tac x="x" and y="x" in comb, simp)
   158 apply (drule_tac x="xa" and y="xa" in comb, simp)
   159 apply (drule g_add_leftimp_eq)
   160 by simp
   161 
   162 axclass pordered_matrix_element < matrix_element, order, zero
   163 pordered_add_right_mono: "a <= b \<Longrightarrow> a + c <= b + c"
   164 pordered_mult_left: "0 <= c \<Longrightarrow> a <= b \<Longrightarrow> c*a <= c*b"
   165 pordered_mult_right: "0 <= c \<Longrightarrow> a <= b \<Longrightarrow> a*c <= b*c"
   166 
   167 lemma pordered_add_left_mono: "(a::'a::pordered_matrix_element) <= b \<Longrightarrow> c + a <= c + b"
   168 apply (insert pordered_add_right_mono[of a b c])
   169 by (simp add: matrix_add_commute)
   170 
   171 lemma pordered_add: "(a::'a::pordered_matrix_element) <= b \<Longrightarrow> (c::'a::pordered_matrix_element) <= d \<Longrightarrow> a+c <= b+d"
   172 proof -
   173   assume p1:"a <= b"
   174   assume p2:"c <= d"
   175   have "a+c <= b+c" by (rule pordered_add_right_mono)
   176   also have "\<dots> <= b+d" by (rule pordered_add_left_mono)
   177   ultimately show "a+c <= b+d" by simp
   178 qed
   179 
   180 instance matrix :: (pordered_matrix_element) pordered_matrix_element
   181 apply (intro_classes)
   182 apply (simp_all add: plus_matrix_def times_matrix_def)
   183 apply (rule le_combine_matrix)
   184 apply (simp_all)
   185 apply (simp_all add: pordered_add)
   186 apply (rule le_left_mult)
   187 apply (simp_all add: matrix_add_0 g_add_left_0 pordered_add pordered_mult_left matrix_mult_left_0 matrix_mult_right_0)
   188 apply (rule le_right_mult)
   189 by (simp_all add: pordered_add pordered_mult_right)
   190 
   191 axclass pordered_g_semiring < g_semiring, pordered_matrix_element
   192 
   193 instance almost_ordered_semiring < pordered_g_semiring
   194 apply (intro_classes)
   195 by (simp_all add: add_right_mono mult_right_mono mult_left_mono)
   196 
   197 instance matrix :: (pordered_g_semiring) pordered_g_semiring ..
   198 
   199 lemma nrows_mult: "nrows ((A::('a::matrix_element) matrix) * B) <= nrows A"
   200 by (simp add: times_matrix_def mult_nrows)
   201 
   202 lemma ncols_mult: "ncols ((A::('a::matrix_element) matrix) * B) <= ncols B"
   203 by (simp add: times_matrix_def mult_ncols)
   204 
   205 constdefs
   206   one_matrix :: "nat \<Rightarrow> ('a::semiring) matrix"
   207   "one_matrix n == Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
   208 
   209 lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
   210 apply (simp add: one_matrix_def)
   211 apply (subst RepAbs_matrix)
   212 apply (rule exI[of _ n], simp add: split_if)+
   213 by (simp add: split_if, arith)
   214 
   215 lemma nrows_one_matrix[simp]: "nrows (one_matrix n) = n" (is "?r = _")
   216 proof -
   217   have "?r <= n" by (simp add: nrows_le)
   218   moreover have "n <= ?r" by (simp add: le_nrows, arith)
   219   ultimately show "?r = n" by simp
   220 qed
   221 
   222 lemma ncols_one_matrix[simp]: "ncols (one_matrix n) = n" (is "?r = _")
   223 proof -
   224   have "?r <= n" by (simp add: ncols_le)
   225   moreover have "n <= ?r" by (simp add: le_ncols, arith)
   226   ultimately show "?r = n" by simp
   227 qed
   228 
   229 lemma one_matrix_mult_right: "ncols A <= n \<Longrightarrow> A * (one_matrix n) = A"
   230 apply (subst Rep_matrix_inject[THEN sym])
   231 apply (rule ext)+
   232 apply (simp add: times_matrix_def Rep_mult_matrix)
   233 apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
   234 apply (simp_all)
   235 by (simp add: max_def ncols)
   236 
   237 lemma one_matrix_mult_left: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = A"
   238 apply (subst Rep_matrix_inject[THEN sym])
   239 apply (rule ext)+
   240 apply (simp add: times_matrix_def Rep_mult_matrix)
   241 apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
   242 apply (simp_all)
   243 by (simp add: max_def nrows)
   244 
   245 constdefs
   246   right_inverse_matrix :: "('a::semiring) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   247   "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X)))"
   248   inverse_matrix :: "('a::semiring) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
   249   "inverse_matrix A X == (right_inverse_matrix A X) \<and> (right_inverse_matrix X A)"
   250 
   251 lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
   252 apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
   253 by (simp add: right_inverse_matrix_def)
   254 
   255 text {* to be continued \dots *}
   256 
   257 end