| author | nipkow | 
| Fri, 18 Feb 2005 15:20:27 +0100 | |
| changeset 15537 | 5538d3244b4d | 
| parent 15481 | fc075ae929e4 | 
| child 15580 | 900291ee0af8 | 
| permissions | -rw-r--r-- | 
| 15009 | 1 | theory SparseMatrix = Matrix: | 
| 2 | ||
| 3 | types | |
| 4 | 'a spvec = "(nat * 'a) list" | |
| 5 |   'a spmat = "('a spvec) spvec"
 | |
| 6 | ||
| 7 | consts | |
| 8 |   sparse_row_vector :: "('a::lordered_ring) spvec \<Rightarrow> 'a matrix"
 | |
| 9 |   sparse_row_matrix :: "('a::lordered_ring) spmat \<Rightarrow> 'a matrix"
 | |
| 10 | ||
| 11 | defs | |
| 12 | sparse_row_vector_def : "sparse_row_vector arr == foldl (% m x. m + (singleton_matrix 0 (fst x) (snd x))) 0 arr" | |
| 13 | sparse_row_matrix_def : "sparse_row_matrix arr == foldl (% m r. m + (move_matrix (sparse_row_vector (snd r)) (int (fst r)) 0)) 0 arr" | |
| 14 | ||
| 15 | lemma sparse_row_vector_empty[simp]: "sparse_row_vector [] = 0" | |
| 16 | by (simp add: sparse_row_vector_def) | |
| 17 | ||
| 18 | lemma sparse_row_matrix_empty[simp]: "sparse_row_matrix [] = 0" | |
| 19 | by (simp add: sparse_row_matrix_def) | |
| 20 | ||
| 21 | lemma foldl_distrstart[rule_format]: "! a x y. (f (g x y) a = g x (f y a)) \<Longrightarrow> ! x y. (foldl f (g x y) l = g x (foldl f y l))" | |
| 22 | by (induct l, auto) | |
| 23 | ||
| 24 | lemma sparse_row_vector_cons[simp]: "sparse_row_vector (a#arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)" | |
| 25 | apply (induct arr) | |
| 26 | apply (auto simp add: sparse_row_vector_def) | |
| 27 | apply (simp add: foldl_distrstart[of "\<lambda>m x. m + singleton_matrix 0 (fst x) (snd x)" "\<lambda>x m. singleton_matrix 0 (fst x) (snd x) + m"]) | |
| 28 | done | |
| 29 | ||
| 30 | lemma sparse_row_vector_append[simp]: "sparse_row_vector (a @ b) = (sparse_row_vector a) + (sparse_row_vector b)" | |
| 31 | by (induct a, auto) | |
| 32 | ||
| 33 | lemma nrows_spvec[simp]: "nrows (sparse_row_vector x) <= (Suc 0)" | |
| 34 | apply (induct x) | |
| 35 | apply (simp_all add: add_nrows) | |
| 36 | done | |
| 37 | ||
| 38 | lemma sparse_row_matrix_cons: "sparse_row_matrix (a#arr) = ((move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0)) + sparse_row_matrix arr" | |
| 39 | apply (induct arr) | |
| 40 | apply (auto simp add: sparse_row_matrix_def) | |
| 41 | apply (simp add: foldl_distrstart[of "\<lambda>m x. m + (move_matrix (sparse_row_vector (snd x)) (int (fst x)) 0)" | |
| 42 | "% a m. (move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0) + m"]) | |
| 43 | done | |
| 44 | ||
| 45 | lemma sparse_row_matrix_append: "sparse_row_matrix (arr@brr) = (sparse_row_matrix arr) + (sparse_row_matrix brr)" | |
| 46 | apply (induct arr) | |
| 47 | apply (auto simp add: sparse_row_matrix_cons) | |
| 48 | done | |
| 49 | ||
| 50 | consts | |
| 51 | sorted_spvec :: "'a spvec \<Rightarrow> bool" | |
| 52 | sorted_spmat :: "'a spmat \<Rightarrow> bool" | |
| 53 | ||
| 54 | primrec | |
| 55 | "sorted_spmat [] = True" | |
| 56 | "sorted_spmat (a#as) = ((sorted_spvec (snd a)) & (sorted_spmat as))" | |
| 57 | ||
| 58 | primrec | |
| 59 | "sorted_spvec [] = True" | |
| 60 | sorted_spvec_step: "sorted_spvec (a#as) = (case as of [] \<Rightarrow> True | b#bs \<Rightarrow> ((fst a < fst b) & (sorted_spvec as)))" | |
| 61 | ||
| 62 | declare sorted_spvec.simps [simp del] | |
| 63 | ||
| 64 | lemma sorted_spvec_empty[simp]: "sorted_spvec [] = True" | |
| 65 | by (simp add: sorted_spvec.simps) | |
| 66 | ||
| 67 | lemma sorted_spvec_cons1: "sorted_spvec (a#as) \<Longrightarrow> sorted_spvec as" | |
| 68 | apply (induct as) | |
| 69 | apply (auto simp add: sorted_spvec.simps) | |
| 70 | done | |
| 71 | ||
| 72 | lemma sorted_spvec_cons2: "sorted_spvec (a#b#t) \<Longrightarrow> sorted_spvec (a#t)" | |
| 73 | apply (induct t) | |
| 74 | apply (auto simp add: sorted_spvec.simps) | |
| 75 | done | |
| 76 | ||
| 77 | lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b" | |
| 78 | apply (auto simp add: sorted_spvec.simps) | |
| 79 | done | |
| 80 | ||
| 81 | lemma sorted_sparse_row_vector_zero[rule_format]: "m <= n \<longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_vector arr) j m = 0" | |
| 82 | apply (induct arr) | |
| 83 | apply (auto) | |
| 84 | apply (frule sorted_spvec_cons2,simp)+ | |
| 85 | apply (frule sorted_spvec_cons3, simp) | |
| 86 | done | |
| 87 | ||
| 88 | lemma sorted_sparse_row_matrix_zero[rule_format]: "m <= n \<longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_matrix arr) m j = 0" | |
| 89 | apply (induct arr) | |
| 90 | apply (auto) | |
| 91 | apply (frule sorted_spvec_cons2, simp) | |
| 92 | apply (frule sorted_spvec_cons3, simp) | |
| 93 | apply (simp add: sparse_row_matrix_cons neg_def) | |
| 94 | done | |
| 95 | ||
| 96 | consts | |
| 15178 | 97 |   abs_spvec :: "('a::lordered_ring) spvec \<Rightarrow> 'a spvec"
 | 
| 98 |   minus_spvec ::  "('a::lordered_ring) spvec \<Rightarrow> 'a spvec"
 | |
| 15009 | 99 |   smult_spvec :: "('a::lordered_ring) \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec" 
 | 
| 100 |   addmult_spvec :: "('a::lordered_ring) * 'a spvec * 'a spvec \<Rightarrow> 'a spvec"
 | |
| 101 | ||
| 15178 | 102 | primrec | 
| 103 | "minus_spvec [] = []" | |
| 104 | "minus_spvec (a#as) = (fst a, -(snd a))#(minus_spvec as)" | |
| 105 | ||
| 106 | primrec | |
| 107 | "abs_spvec [] = []" | |
| 108 | "abs_spvec (a#as) = (fst a, abs (snd a))#(abs_spvec as)" | |
| 109 | ||
| 110 | lemma sparse_row_vector_minus: | |
| 111 | "sparse_row_vector (minus_spvec v) = - (sparse_row_vector v)" | |
| 112 | apply (induct v) | |
| 113 | apply (simp_all add: sparse_row_vector_cons) | |
| 114 | apply (simp add: Rep_matrix_inject[symmetric]) | |
| 115 | apply (rule ext)+ | |
| 116 | apply simp | |
| 117 | done | |
| 118 | ||
| 119 | lemma sparse_row_vector_abs: | |
| 120 | "sorted_spvec v \<Longrightarrow> sparse_row_vector (abs_spvec v) = abs (sparse_row_vector v)" | |
| 121 | apply (induct v) | |
| 122 | apply (simp_all add: sparse_row_vector_cons) | |
| 123 | apply (frule_tac sorted_spvec_cons1, simp) | |
| 124 | apply (simp only: Rep_matrix_inject[symmetric]) | |
| 125 | apply (rule ext)+ | |
| 126 | apply auto | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 127 | apply (subgoal_tac "Rep_matrix (sparse_row_vector v) 0 a = 0") | 
| 15178 | 128 | apply (simp) | 
| 129 | apply (rule sorted_sparse_row_vector_zero) | |
| 130 | apply auto | |
| 131 | done | |
| 132 | ||
| 133 | lemma sorted_spvec_minus_spvec: | |
| 134 | "sorted_spvec v \<Longrightarrow> sorted_spvec (minus_spvec v)" | |
| 135 | apply (induct v) | |
| 136 | apply (simp) | |
| 137 | apply (frule sorted_spvec_cons1, simp) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 138 | apply (simp add: sorted_spvec.simps split:list.split_asm) | 
| 15178 | 139 | done | 
| 140 | ||
| 141 | lemma sorted_spvec_abs_spvec: | |
| 142 | "sorted_spvec v \<Longrightarrow> sorted_spvec (abs_spvec v)" | |
| 143 | apply (induct v) | |
| 144 | apply (simp) | |
| 145 | apply (frule sorted_spvec_cons1, simp) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 146 | apply (simp add: sorted_spvec.simps split:list.split_asm) | 
| 15178 | 147 | done | 
| 148 | ||
| 15009 | 149 | defs | 
| 150 | smult_spvec_def: "smult_spvec y arr == map (% a. (fst a, y * snd a)) arr" | |
| 151 | ||
| 152 | lemma smult_spvec_empty[simp]: "smult_spvec y [] = []" | |
| 153 | by (simp add: smult_spvec_def) | |
| 154 | ||
| 155 | lemma smult_spvec_cons: "smult_spvec y (a#arr) = (fst a, y * (snd a)) # (smult_spvec y arr)" | |
| 156 | by (simp add: smult_spvec_def) | |
| 157 | ||
| 158 | recdef addmult_spvec "measure (% (y, a, b). length a + (length b))" | |
| 159 | "addmult_spvec (y, arr, []) = arr" | |
| 160 | "addmult_spvec (y, [], brr) = smult_spvec y brr" | |
| 161 | "addmult_spvec (y, a#arr, b#brr) = ( | |
| 162 | if (fst a) < (fst b) then (a#(addmult_spvec (y, arr, b#brr))) | |
| 163 | else (if (fst b < fst a) then ((fst b, y * (snd b))#(addmult_spvec (y, a#arr, brr))) | |
| 164 | else ((fst a, (snd a)+ y*(snd b))#(addmult_spvec (y, arr,brr)))))" | |
| 165 | ||
| 166 | lemma addmult_spvec_empty1[simp]: "addmult_spvec (y, [], a) = smult_spvec y a" | |
| 167 | by (induct a, auto) | |
| 168 | ||
| 169 | lemma addmult_spvec_empty2[simp]: "addmult_spvec (y, a, []) = a" | |
| 170 | by (induct a, auto) | |
| 171 | ||
| 172 | lemma sparse_row_vector_map: "(! x y. f (x+y) = (f x) + (f y)) \<Longrightarrow> (f::'a\<Rightarrow>('a::lordered_ring)) 0 = 0 \<Longrightarrow> 
 | |
| 173 | sparse_row_vector (map (% x. (fst x, f (snd x))) a) = apply_matrix f (sparse_row_vector a)" | |
| 174 | apply (induct a) | |
| 175 | apply (simp_all add: apply_matrix_add) | |
| 176 | done | |
| 177 | ||
| 178 | lemma sparse_row_vector_smult: "sparse_row_vector (smult_spvec y a) = scalar_mult y (sparse_row_vector a)" | |
| 179 | apply (induct a) | |
| 180 | apply (simp_all add: smult_spvec_cons scalar_mult_add) | |
| 181 | done | |
| 182 | ||
| 183 | lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring, a, b)) = | |
| 184 | (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))" | |
| 185 | apply (rule addmult_spvec.induct[of _ y]) | |
| 186 | apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+ | |
| 187 | done | |
| 188 | ||
| 189 | lemma sorted_smult_spvec[rule_format]: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)" | |
| 190 | apply (auto simp add: smult_spvec_def) | |
| 191 | apply (induct a) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 192 | apply (auto simp add: sorted_spvec.simps split:list.split_asm) | 
| 15009 | 193 | done | 
| 194 | ||
| 195 | lemma sorted_spvec_addmult_spvec_helper: "\<lbrakk>sorted_spvec (addmult_spvec (y, (a, b) # arr, brr)); aa < a; sorted_spvec ((a, b) # arr); | |
| 196 | sorted_spvec ((aa, ba) # brr)\<rbrakk> \<Longrightarrow> sorted_spvec ((aa, y * ba) # addmult_spvec (y, (a, b) # arr, brr))" | |
| 197 | apply (induct brr) | |
| 198 | apply (auto simp add: sorted_spvec.simps) | |
| 199 | apply (simp split: list.split) | |
| 200 | apply (auto) | |
| 201 | apply (simp split: list.split) | |
| 202 | apply (auto) | |
| 203 | done | |
| 204 | ||
| 205 | lemma sorted_spvec_addmult_spvec_helper2: | |
| 206 | "\<lbrakk>sorted_spvec (addmult_spvec (y, arr, (aa, ba) # brr)); a < aa; sorted_spvec ((a, b) # arr); sorted_spvec ((aa, ba) # brr)\<rbrakk> | |
| 207 | \<Longrightarrow> sorted_spvec ((a, b) # addmult_spvec (y, arr, (aa, ba) # brr))" | |
| 208 | apply (induct arr) | |
| 209 | apply (auto simp add: smult_spvec_def sorted_spvec.simps) | |
| 210 | apply (simp split: list.split) | |
| 211 | apply (auto) | |
| 212 | done | |
| 213 | ||
| 214 | lemma sorted_spvec_addmult_spvec_helper3[rule_format]: | |
| 215 | "sorted_spvec (addmult_spvec (y, arr, brr)) \<longrightarrow> sorted_spvec ((aa, b) # arr) \<longrightarrow> sorted_spvec ((aa, ba) # brr) | |
| 216 | \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec (y, arr, brr)))" | |
| 217 | apply (rule addmult_spvec.induct[of _ y arr brr]) | |
| 218 | apply (simp_all add: sorted_spvec.simps smult_spvec_def) | |
| 219 | done | |
| 220 | ||
| 221 | lemma sorted_addmult_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (addmult_spvec (y, a, b))" | |
| 222 | apply (rule addmult_spvec.induct[of _ y a b]) | |
| 223 | apply (simp_all add: sorted_smult_spvec) | |
| 224 | apply (rule conjI, intro strip) | |
| 225 | apply (case_tac "~(a < aa)") | |
| 226 | apply (simp_all) | |
| 227 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 228 | apply (simp add: sorted_spvec_addmult_spvec_helper) | |
| 229 | apply (intro strip | rule conjI)+ | |
| 230 | apply (frule_tac as=arr in sorted_spvec_cons1) | |
| 231 | apply (simp add: sorted_spvec_addmult_spvec_helper2) | |
| 232 | apply (intro strip) | |
| 233 | apply (frule_tac as=arr in sorted_spvec_cons1) | |
| 234 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 235 | apply (simp) | |
| 236 | apply (case_tac "a=aa") | |
| 237 | apply (simp_all add: sorted_spvec_addmult_spvec_helper3) | |
| 238 | done | |
| 239 | ||
| 240 | consts | |
| 241 |   mult_spvec_spmat :: "('a::lordered_ring) spvec * 'a spvec * 'a spmat  \<Rightarrow> 'a spvec"
 | |
| 242 | ||
| 243 | recdef mult_spvec_spmat "measure (% (c, arr, brr). (length arr) + (length brr))" | |
| 244 | "mult_spvec_spmat (c, [], brr) = c" | |
| 245 | "mult_spvec_spmat (c, arr, []) = c" | |
| 246 | "mult_spvec_spmat (c, a#arr, b#brr) = ( | |
| 247 | if ((fst a) < (fst b)) then (mult_spvec_spmat (c, arr, b#brr)) | |
| 248 | else (if ((fst b) < (fst a)) then (mult_spvec_spmat (c, a#arr, brr)) | |
| 249 | else (mult_spvec_spmat (addmult_spvec (snd a, c, snd b), arr, brr))))" | |
| 250 | ||
| 251 | lemma sparse_row_mult_spvec_spmat[rule_format]: "sorted_spvec (a::('a::lordered_ring) spvec) \<longrightarrow> sorted_spvec B \<longrightarrow> 
 | |
| 252 | sparse_row_vector (mult_spvec_spmat (c, a, B)) = (sparse_row_vector c) + (sparse_row_vector a) * (sparse_row_matrix B)" | |
| 253 | proof - | |
| 254 | have comp_1: "!! a b. a < b \<Longrightarrow> Suc 0 <= nat ((int b)-(int a))" by arith | |
| 255 | have not_iff: "!! a b. a = b \<Longrightarrow> (~ a) = (~ b)" by simp | |
| 256 | have max_helper: "!! a b. ~ (a <= max (Suc a) b) \<Longrightarrow> False" | |
| 257 | by arith | |
| 258 |   {
 | |
| 259 | fix a | |
| 260 | fix v | |
| 261 | assume a:"a < nrows(sparse_row_vector v)" | |
| 262 | have b:"nrows(sparse_row_vector v) <= 1" by simp | |
| 263 | note dummy = less_le_trans[of a "nrows (sparse_row_vector v)" 1, OF a b] | |
| 264 | then have "a = 0" by simp | |
| 265 | } | |
| 266 | note nrows_helper = this | |
| 267 | show ?thesis | |
| 268 | apply (rule mult_spvec_spmat.induct) | |
| 269 | apply simp+ | |
| 270 | apply (rule conjI) | |
| 271 | apply (intro strip) | |
| 272 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 273 | apply (simp add: ring_eq_simps sparse_row_matrix_cons) | |
| 15481 | 274 | apply (simplesubst Rep_matrix_zero_imp_mult_zero) | 
| 15009 | 275 | apply (simp) | 
| 276 | apply (intro strip) | |
| 277 | apply (rule disjI2) | |
| 278 | apply (intro strip) | |
| 279 | apply (subst nrows) | |
| 280 | apply (rule order_trans[of _ 1]) | |
| 281 | apply (simp add: comp_1)+ | |
| 282 | apply (subst Rep_matrix_zero_imp_mult_zero) | |
| 283 | apply (intro strip) | |
| 284 | apply (case_tac "k <= aa") | |
| 285 | apply (rule_tac m1 = k and n1 = a and a1 = b in ssubst[OF sorted_sparse_row_vector_zero]) | |
| 286 | apply (simp_all) | |
| 287 | apply (rule impI) | |
| 288 | apply (rule disjI2) | |
| 289 | apply (rule nrows) | |
| 290 | apply (rule order_trans[of _ 1]) | |
| 291 | apply (simp_all add: comp_1) | |
| 292 | ||
| 293 | apply (intro strip | rule conjI)+ | |
| 294 | apply (frule_tac as=arr in sorted_spvec_cons1) | |
| 295 | apply (simp add: ring_eq_simps) | |
| 296 | apply (subst Rep_matrix_zero_imp_mult_zero) | |
| 297 | apply (simp) | |
| 298 | apply (rule disjI2) | |
| 299 | apply (intro strip) | |
| 300 | apply (simp add: sparse_row_matrix_cons neg_def) | |
| 301 | apply (case_tac "a <= aa") | |
| 302 | apply (erule sorted_sparse_row_matrix_zero) | |
| 303 | apply (simp_all) | |
| 304 | apply (intro strip) | |
| 305 | apply (case_tac "a=aa") | |
| 306 | apply (simp_all) | |
| 307 | apply (frule_tac as=arr in sorted_spvec_cons1) | |
| 308 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 309 | apply (simp add: sparse_row_matrix_cons ring_eq_simps sparse_row_vector_addmult_spvec) | |
| 310 | apply (rule_tac B1 = "sparse_row_matrix brr" in ssubst[OF Rep_matrix_zero_imp_mult_zero]) | |
| 311 | apply (auto) | |
| 312 | apply (rule sorted_sparse_row_matrix_zero) | |
| 313 | apply (simp_all) | |
| 314 | apply (rule_tac A1 = "sparse_row_vector arr" in ssubst[OF Rep_matrix_zero_imp_mult_zero]) | |
| 315 | apply (auto) | |
| 316 | apply (rule_tac m=k and n = aa and a = b and arr=arr in sorted_sparse_row_vector_zero) | |
| 317 | apply (simp_all) | |
| 318 | apply (simp add: neg_def) | |
| 319 | apply (drule nrows_notzero) | |
| 320 | apply (drule nrows_helper) | |
| 321 | apply (arith) | |
| 322 | ||
| 323 | apply (subst Rep_matrix_inject[symmetric]) | |
| 324 | apply (rule ext)+ | |
| 325 | apply (simp) | |
| 326 | apply (subst Rep_matrix_mult) | |
| 327 | apply (rule_tac j1=aa in ssubst[OF foldseq_almostzero]) | |
| 328 | apply (simp_all) | |
| 329 | apply (intro strip, rule conjI) | |
| 330 | apply (intro strip) | |
| 331 | apply (drule_tac max_helper) | |
| 332 | apply (simp) | |
| 333 | apply (auto) | |
| 334 | apply (rule zero_imp_mult_zero) | |
| 335 | apply (rule disjI2) | |
| 336 | apply (rule nrows) | |
| 337 | apply (rule order_trans[of _ 1]) | |
| 338 | apply (simp) | |
| 339 | apply (simp) | |
| 340 | done | |
| 341 | qed | |
| 342 | ||
| 343 | lemma sorted_mult_spvec_spmat[rule_format]: | |
| 344 |   "sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat (c, a, B))"
 | |
| 345 | apply (rule mult_spvec_spmat.induct[of _ c a B]) | |
| 346 | apply (simp_all add: sorted_addmult_spvec) | |
| 347 | done | |
| 348 | ||
| 349 | consts | |
| 350 |   mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
 | |
| 351 | ||
| 352 | primrec | |
| 353 | "mult_spmat [] A = []" | |
| 354 | "mult_spmat (a#as) A = (fst a, mult_spvec_spmat ([], snd a, A))#(mult_spmat as A)" | |
| 355 | ||
| 356 | lemma sparse_row_mult_spmat[rule_format]: | |
| 357 | "sorted_spmat A \<longrightarrow> sorted_spvec B \<longrightarrow> sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)" | |
| 358 | apply (induct A) | |
| 359 | apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat ring_eq_simps move_matrix_mult) | |
| 360 | done | |
| 361 | ||
| 362 | lemma sorted_spvec_mult_spmat[rule_format]: | |
| 363 |   "sorted_spvec (A::('a::lordered_ring) spmat) \<longrightarrow> sorted_spvec (mult_spmat A B)"
 | |
| 364 | apply (induct A) | |
| 365 | apply (auto) | |
| 366 | apply (drule sorted_spvec_cons1, simp) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 367 | apply (case_tac A) | 
| 15009 | 368 | apply (auto simp add: sorted_spvec.simps) | 
| 369 | done | |
| 370 | ||
| 371 | lemma sorted_spmat_mult_spmat[rule_format]: | |
| 372 |   "sorted_spmat (B::('a::lordered_ring) spmat) \<longrightarrow> sorted_spmat (mult_spmat A B)"
 | |
| 373 | apply (induct A) | |
| 374 | apply (auto simp add: sorted_mult_spvec_spmat) | |
| 375 | done | |
| 376 | ||
| 377 | consts | |
| 378 |   add_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> 'a spvec"
 | |
| 379 |   add_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> 'a spmat"
 | |
| 380 | ||
| 381 | recdef add_spvec "measure (% (a, b). length a + (length b))" | |
| 382 | "add_spvec (arr, []) = arr" | |
| 383 | "add_spvec ([], brr) = brr" | |
| 384 | "add_spvec (a#arr, b#brr) = ( | |
| 385 | if (fst a) < (fst b) then (a#(add_spvec (arr, b#brr))) | |
| 386 | else (if (fst b < fst a) then (b#(add_spvec (a#arr, brr))) | |
| 387 | else ((fst a, (snd a)+(snd b))#(add_spvec (arr,brr)))))" | |
| 388 | ||
| 389 | lemma add_spvec_empty1[simp]: "add_spvec ([], a) = a" | |
| 390 | by (induct a, auto) | |
| 391 | ||
| 392 | lemma add_spvec_empty2[simp]: "add_spvec (a, []) = a" | |
| 393 | by (induct a, auto) | |
| 394 | ||
| 395 | lemma sparse_row_vector_add: "sparse_row_vector (add_spvec (a,b)) = (sparse_row_vector a) + (sparse_row_vector b)" | |
| 396 | apply (rule add_spvec.induct[of _ a b]) | |
| 397 | apply (simp_all add: singleton_matrix_add) | |
| 398 | done | |
| 399 | ||
| 400 | recdef add_spmat "measure (% (A,B). (length A)+(length B))" | |
| 401 | "add_spmat ([], bs) = bs" | |
| 402 | "add_spmat (as, []) = as" | |
| 403 | "add_spmat (a#as, b#bs) = ( | |
| 404 | if fst a < fst b then | |
| 405 | (a#(add_spmat (as, b#bs))) | |
| 406 | else (if fst b < fst a then | |
| 407 | (b#(add_spmat (a#as, bs))) | |
| 408 | else | |
| 409 | ((fst a, add_spvec (snd a, snd b))#(add_spmat (as, bs)))))" | |
| 410 | ||
| 411 | lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat (A, B)) = (sparse_row_matrix A) + (sparse_row_matrix B)" | |
| 412 | apply (rule add_spmat.induct) | |
| 413 | apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add) | |
| 414 | done | |
| 415 | ||
| 416 | lemma sorted_add_spvec_helper1[rule_format]: "add_spvec ((a,b)#arr, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))" | |
| 417 | proof - | |
| 418 | have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec (x, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" | |
| 419 | by (rule add_spvec.induct[of _ _ brr], auto) | |
| 420 | then show ?thesis | |
| 421 | by (case_tac brr, auto) | |
| 422 | qed | |
| 423 | ||
| 424 | lemma sorted_add_spmat_helper1[rule_format]: "add_spmat ((a,b)#arr, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))" | |
| 425 | proof - | |
| 426 | have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat (x, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" | |
| 427 | by (rule add_spmat.induct[of _ _ brr], auto) | |
| 428 | then show ?thesis | |
| 429 | by (case_tac brr, auto) | |
| 430 | qed | |
| 431 | ||
| 432 | lemma sorted_add_spvec_helper[rule_format]: "add_spvec (arr, brr) = (ab, bb) # list \<longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))" | |
| 433 | apply (rule add_spvec.induct[of _ arr brr]) | |
| 434 | apply (auto) | |
| 435 | done | |
| 436 | ||
| 437 | lemma sorted_add_spmat_helper[rule_format]: "add_spmat (arr, brr) = (ab, bb) # list \<longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))" | |
| 438 | apply (rule add_spmat.induct[of _ arr brr]) | |
| 439 | apply (auto) | |
| 440 | done | |
| 441 | ||
| 442 | lemma add_spvec_commute: "add_spvec (a, b) = add_spvec (b, a)" | |
| 443 | by (rule add_spvec.induct[of _ a b], auto) | |
| 444 | ||
| 445 | lemma add_spmat_commute: "add_spmat (a, b) = add_spmat (b, a)" | |
| 446 | apply (rule add_spmat.induct[of _ a b]) | |
| 447 | apply (simp_all add: add_spvec_commute) | |
| 448 | done | |
| 449 | ||
| 450 | lemma sorted_add_spvec_helper2: "add_spvec ((a,b)#arr, brr) = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab" | |
| 451 | apply (drule sorted_add_spvec_helper1) | |
| 452 | apply (auto) | |
| 453 | apply (case_tac brr) | |
| 454 | apply (simp_all) | |
| 455 | apply (drule_tac sorted_spvec_cons3) | |
| 456 | apply (simp) | |
| 457 | done | |
| 458 | ||
| 459 | lemma sorted_add_spmat_helper2: "add_spmat ((a,b)#arr, brr) = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab" | |
| 460 | apply (drule sorted_add_spmat_helper1) | |
| 461 | apply (auto) | |
| 462 | apply (case_tac brr) | |
| 463 | apply (simp_all) | |
| 464 | apply (drule_tac sorted_spvec_cons3) | |
| 465 | apply (simp) | |
| 466 | done | |
| 467 | ||
| 468 | lemma sorted_spvec_add_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (add_spvec (a, b))" | |
| 469 | apply (rule add_spvec.induct[of _ a b]) | |
| 470 | apply (simp_all) | |
| 471 | apply (rule conjI) | |
| 472 | apply (intro strip) | |
| 473 | apply (simp) | |
| 474 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 475 | apply (simp) | |
| 476 | apply (subst sorted_spvec_step) | |
| 477 | apply (simp split: list.split) | |
| 478 | apply (clarify, simp) | |
| 479 | apply (simp add: sorted_add_spvec_helper2) | |
| 480 | apply (clarify) | |
| 481 | apply (rule conjI) | |
| 482 | apply (case_tac "a=aa") | |
| 483 | apply (simp) | |
| 484 | apply (clarify) | |
| 485 | apply (frule_tac as=arr in sorted_spvec_cons1, simp) | |
| 486 | apply (subst sorted_spvec_step) | |
| 487 | apply (simp split: list.split) | |
| 488 | apply (clarify, simp) | |
| 489 | apply (simp add: sorted_add_spvec_helper2 add_spvec_commute) | |
| 490 | apply (case_tac "a=aa") | |
| 491 | apply (simp_all) | |
| 492 | apply (clarify) | |
| 493 | apply (frule_tac as=arr in sorted_spvec_cons1) | |
| 494 | apply (frule_tac as=brr in sorted_spvec_cons1) | |
| 495 | apply (simp) | |
| 496 | apply (subst sorted_spvec_step) | |
| 497 | apply (simp split: list.split) | |
| 498 | apply (clarify, simp) | |
| 499 | apply (drule_tac sorted_add_spvec_helper) | |
| 500 | apply (auto) | |
| 501 | apply (case_tac arr) | |
| 502 | apply (simp_all) | |
| 503 | apply (drule sorted_spvec_cons3) | |
| 504 | apply (simp) | |
| 505 | apply (case_tac brr) | |
| 506 | apply (simp_all) | |
| 507 | apply (drule sorted_spvec_cons3) | |
| 508 | apply (simp) | |
| 509 | done | |
| 510 | ||
| 511 | lemma sorted_spvec_add_spmat[rule_format]: "sorted_spvec A \<longrightarrow> sorted_spvec B \<longrightarrow> sorted_spvec (add_spmat (A, B))" | |
| 512 | apply (rule add_spmat.induct[of _ A B]) | |
| 513 | apply (simp_all) | |
| 514 | apply (rule conjI) | |
| 515 | apply (intro strip) | |
| 516 | apply (simp) | |
| 517 | apply (frule_tac as=bs in sorted_spvec_cons1) | |
| 518 | apply (simp) | |
| 519 | apply (subst sorted_spvec_step) | |
| 520 | apply (simp split: list.split) | |
| 521 | apply (clarify, simp) | |
| 522 | apply (simp add: sorted_add_spmat_helper2) | |
| 523 | apply (clarify) | |
| 524 | apply (rule conjI) | |
| 525 | apply (case_tac "a=aa") | |
| 526 | apply (simp) | |
| 527 | apply (clarify) | |
| 528 | apply (frule_tac as=as in sorted_spvec_cons1, simp) | |
| 529 | apply (subst sorted_spvec_step) | |
| 530 | apply (simp split: list.split) | |
| 531 | apply (clarify, simp) | |
| 532 | apply (simp add: sorted_add_spmat_helper2 add_spmat_commute) | |
| 533 | apply (case_tac "a=aa") | |
| 534 | apply (simp_all) | |
| 535 | apply (clarify) | |
| 536 | apply (frule_tac as=as in sorted_spvec_cons1) | |
| 537 | apply (frule_tac as=bs in sorted_spvec_cons1) | |
| 538 | apply (simp) | |
| 539 | apply (subst sorted_spvec_step) | |
| 540 | apply (simp split: list.split) | |
| 541 | apply (clarify, simp) | |
| 542 | apply (drule_tac sorted_add_spmat_helper) | |
| 543 | apply (auto) | |
| 544 | apply (case_tac as) | |
| 545 | apply (simp_all) | |
| 546 | apply (drule sorted_spvec_cons3) | |
| 547 | apply (simp) | |
| 548 | apply (case_tac bs) | |
| 549 | apply (simp_all) | |
| 550 | apply (drule sorted_spvec_cons3) | |
| 551 | apply (simp) | |
| 552 | done | |
| 553 | ||
| 554 | lemma sorted_spmat_add_spmat[rule_format]: "sorted_spmat A \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spmat (add_spmat (A, B))" | |
| 555 | apply (rule add_spmat.induct[of _ A B]) | |
| 556 | apply (simp_all add: sorted_spvec_add_spvec) | |
| 557 | done | |
| 558 | ||
| 559 | consts | |
| 560 |   le_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> bool" 
 | |
| 561 |   le_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> bool" 
 | |
| 562 | ||
| 563 | recdef le_spvec "measure (% (a,b). (length a) + (length b))" | |
| 564 | "le_spvec ([], []) = True" | |
| 565 | "le_spvec (a#as, []) = ((snd a <= 0) & (le_spvec (as, [])))" | |
| 566 | "le_spvec ([], b#bs) = ((0 <= snd b) & (le_spvec ([], bs)))" | |
| 567 | "le_spvec (a#as, b#bs) = ( | |
| 568 | if (fst a < fst b) then | |
| 569 | ((snd a <= 0) & (le_spvec (as, b#bs))) | |
| 570 | else (if (fst b < fst a) then | |
| 571 | ((0 <= snd b) & (le_spvec (a#as, bs))) | |
| 572 | else | |
| 573 | ((snd a <= snd b) & (le_spvec (as, bs)))))" | |
| 574 | ||
| 575 | recdef le_spmat "measure (% (a,b). (length a) + (length b))" | |
| 576 | "le_spmat ([], []) = True" | |
| 577 | "le_spmat (a#as, []) = (le_spvec (snd a, []) & (le_spmat (as, [])))" | |
| 578 | "le_spmat ([], b#bs) = (le_spvec ([], snd b) & (le_spmat ([], bs)))" | |
| 579 | "le_spmat (a#as, b#bs) = ( | |
| 580 | if fst a < fst b then | |
| 581 | (le_spvec(snd a,[]) & le_spmat(as, b#bs)) | |
| 582 | else (if (fst b < fst a) then | |
| 583 | (le_spvec([], snd b) & le_spmat(a#as, bs)) | |
| 584 | else | |
| 585 | (le_spvec(snd a, snd b) & le_spmat (as, bs))))" | |
| 586 | ||
| 587 | constdefs | |
| 588 |   disj_matrices :: "('a::zero) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
 | |
| 589 | "disj_matrices A B == (! j i. (Rep_matrix A j i \<noteq> 0) \<longrightarrow> (Rep_matrix B j i = 0)) & (! j i. (Rep_matrix B j i \<noteq> 0) \<longrightarrow> (Rep_matrix A j i = 0))" | |
| 590 | ||
| 591 | ML {* simp_depth_limit := 2 *}
 | |
| 592 | ||
| 593 | lemma disj_matrices_add: "disj_matrices A B \<Longrightarrow> disj_matrices C D \<Longrightarrow> disj_matrices A D \<Longrightarrow> disj_matrices B C \<Longrightarrow> | |
| 594 |   (A + B <= C + D) = (A <= C & B <= (D::('a::lordered_ab_group) matrix))"
 | |
| 595 | apply (auto) | |
| 596 | apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def) | |
| 597 | apply (intro strip) | |
| 598 | apply (erule conjE)+ | |
| 599 | apply (drule_tac j=j and i=i in spec2)+ | |
| 600 | apply (case_tac "Rep_matrix B j i = 0") | |
| 601 | apply (case_tac "Rep_matrix D j i = 0") | |
| 602 | apply (simp_all) | |
| 603 | apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def) | |
| 604 | apply (intro strip) | |
| 605 | apply (erule conjE)+ | |
| 606 | apply (drule_tac j=j and i=i in spec2)+ | |
| 607 | apply (case_tac "Rep_matrix A j i = 0") | |
| 608 | apply (case_tac "Rep_matrix C j i = 0") | |
| 609 | apply (simp_all) | |
| 610 | apply (erule add_mono) | |
| 611 | apply (assumption) | |
| 612 | done | |
| 613 | ||
| 614 | lemma disj_matrices_zero1[simp]: "disj_matrices 0 B" | |
| 615 | by (simp add: disj_matrices_def) | |
| 616 | ||
| 617 | lemma disj_matrices_zero2[simp]: "disj_matrices A 0" | |
| 618 | by (simp add: disj_matrices_def) | |
| 619 | ||
| 620 | lemma disj_matrices_commute: "disj_matrices A B = disj_matrices B A" | |
| 621 | by (auto simp add: disj_matrices_def) | |
| 622 | ||
| 623 | lemma disj_matrices_add_le_zero: "disj_matrices A B \<Longrightarrow> | |
| 624 |   (A + B <= 0) = (A <= 0 & (B::('a::lordered_ab_group) matrix) <= 0)"
 | |
| 625 | by (rule disj_matrices_add[of A B 0 0, simplified]) | |
| 626 | ||
| 627 | lemma disj_matrices_add_zero_le: "disj_matrices A B \<Longrightarrow> | |
| 628 |   (0 <= A + B) = (0 <= A & 0 <= (B::('a::lordered_ab_group) matrix))"
 | |
| 629 | by (rule disj_matrices_add[of 0 0 A B, simplified]) | |
| 630 | ||
| 631 | lemma disj_matrices_add_x_le: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> | |
| 632 |   (A <= B + C) = (A <= C & 0 <= (B::('a::lordered_ab_group) matrix))"
 | |
| 633 | by (auto simp add: disj_matrices_add[of 0 A B C, simplified]) | |
| 634 | ||
| 635 | lemma disj_matrices_add_le_x: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> | |
| 636 |   (B + A <= C) = (A <= C &  (B::('a::lordered_ab_group) matrix) <= 0)"
 | |
| 637 | by (auto simp add: disj_matrices_add[of B A 0 C,simplified] disj_matrices_commute) | |
| 638 | ||
| 639 | lemma disj_sparse_row_singleton: "i <= j \<Longrightarrow> sorted_spvec((j,y)#v) \<Longrightarrow> disj_matrices (sparse_row_vector v) (singleton_matrix 0 i x)" | |
| 640 | apply (simp add: disj_matrices_def) | |
| 641 | apply (rule conjI) | |
| 642 | apply (rule neg_imp) | |
| 643 | apply (simp) | |
| 644 | apply (intro strip) | |
| 645 | apply (rule sorted_sparse_row_vector_zero) | |
| 646 | apply (simp_all) | |
| 647 | apply (intro strip) | |
| 648 | apply (rule sorted_sparse_row_vector_zero) | |
| 649 | apply (simp_all) | |
| 650 | done | |
| 651 | ||
| 652 | lemma disj_matrices_x_add: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (A::('a::lordered_ab_group) matrix) (B+C)"
 | |
| 653 | apply (simp add: disj_matrices_def) | |
| 654 | apply (auto) | |
| 655 | apply (drule_tac j=j and i=i in spec2)+ | |
| 656 | apply (case_tac "Rep_matrix B j i = 0") | |
| 657 | apply (case_tac "Rep_matrix C j i = 0") | |
| 658 | apply (simp_all) | |
| 659 | done | |
| 660 | ||
| 661 | lemma disj_matrices_add_x: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (B+C) (A::('a::lordered_ab_group) matrix)" 
 | |
| 662 | by (simp add: disj_matrices_x_add disj_matrices_commute) | |
| 663 | ||
| 664 | lemma disj_singleton_matrices[simp]: "disj_matrices (singleton_matrix j i x) (singleton_matrix u v y) = (j \<noteq> u | i \<noteq> v | x = 0 | y = 0)" | |
| 665 | by (auto simp add: disj_matrices_def) | |
| 666 | ||
| 667 | lemma disj_move_sparse_vec_mat[simplified disj_matrices_commute]: | |
| 668 | "j <= a \<Longrightarrow> sorted_spvec((a,c)#as) \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector b) (int j) i) (sparse_row_matrix as)" | |
| 669 | apply (auto simp add: neg_def disj_matrices_def) | |
| 670 | apply (drule nrows_notzero) | |
| 671 | apply (drule less_le_trans[OF _ nrows_spvec]) | |
| 672 | apply (subgoal_tac "ja = j") | |
| 673 | apply (simp add: sorted_sparse_row_matrix_zero) | |
| 674 | apply (arith) | |
| 675 | apply (rule nrows) | |
| 676 | apply (rule order_trans[of _ 1 _]) | |
| 677 | apply (simp) | |
| 678 | apply (case_tac "nat (int ja - int j) = 0") | |
| 679 | apply (case_tac "ja = j") | |
| 680 | apply (simp add: sorted_sparse_row_matrix_zero) | |
| 681 | apply arith+ | |
| 682 | done | |
| 683 | ||
| 684 | lemma disj_move_sparse_row_vector_twice: | |
| 685 | "j \<noteq> u \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector a) j i) (move_matrix (sparse_row_vector b) u v)" | |
| 686 | apply (auto simp add: neg_def disj_matrices_def) | |
| 687 | apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+ | |
| 688 | done | |
| 689 | ||
| 15178 | 690 | lemma le_spvec_iff_sparse_row_le[rule_format]: "(sorted_spvec a) \<longrightarrow> (sorted_spvec b) \<longrightarrow> (le_spvec (a,b)) = (sparse_row_vector a <= sparse_row_vector b)" | 
| 691 | apply (rule le_spvec.induct) | |
| 692 | apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le | |
| 693 | disj_sparse_row_singleton[OF order_refl] disj_matrices_commute) | |
| 694 | apply (rule conjI, intro strip) | |
| 695 | apply (simp add: sorted_spvec_cons1) | |
| 696 | apply (subst disj_matrices_add_x_le) | |
| 697 | apply (simp add: disj_sparse_row_singleton[OF less_imp_le] disj_matrices_x_add disj_matrices_commute) | |
| 698 | apply (simp add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute) | |
| 699 | apply (simp, blast) | |
| 700 | apply (intro strip, rule conjI, intro strip) | |
| 701 | apply (simp add: sorted_spvec_cons1) | |
| 702 | apply (subst disj_matrices_add_le_x) | |
| 703 | apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_sparse_row_singleton[OF less_imp_le] disj_matrices_commute disj_matrices_x_add) | |
| 704 | apply (blast) | |
| 705 | apply (intro strip) | |
| 706 | apply (simp add: sorted_spvec_cons1) | |
| 707 | apply (case_tac "a=aa", simp_all) | |
| 708 | apply (subst disj_matrices_add) | |
| 709 | apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute) | |
| 15009 | 710 | done | 
| 711 | ||
| 712 | lemma le_spvec_empty2_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec (b,[]) = (sparse_row_vector b <= 0))" | |
| 713 | apply (induct b) | |
| 714 | apply (simp_all add: sorted_spvec_cons1) | |
| 715 | apply (intro strip) | |
| 716 | apply (subst disj_matrices_add_le_zero) | |
| 717 | apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1) | |
| 718 | apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl]) | |
| 719 | apply (simp_all) | |
| 720 | done | |
| 721 | ||
| 722 | lemma le_spvec_empty1_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec ([],b) = (0 <= sparse_row_vector b))" | |
| 723 | apply (induct b) | |
| 724 | apply (simp_all add: sorted_spvec_cons1) | |
| 725 | apply (intro strip) | |
| 726 | apply (subst disj_matrices_add_zero_le) | |
| 727 | apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1) | |
| 728 | apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl]) | |
| 729 | apply (simp_all) | |
| 730 | done | |
| 731 | ||
| 732 | lemma le_spmat_iff_sparse_row_le[rule_format]: "(sorted_spvec A) \<longrightarrow> (sorted_spmat A) \<longrightarrow> (sorted_spvec B) \<longrightarrow> (sorted_spmat B) \<longrightarrow> | |
| 733 | le_spmat(A, B) = (sparse_row_matrix A <= sparse_row_matrix B)" | |
| 734 | apply (rule le_spmat.induct) | |
| 735 | apply (simp add: sparse_row_matrix_cons disj_matrices_add_le_zero disj_matrices_add_zero_le disj_move_sparse_vec_mat[OF order_refl] | |
| 736 | disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ | |
| 737 | apply (rule conjI, intro strip) | |
| 738 | apply (simp add: sorted_spvec_cons1) | |
| 739 | apply (subst disj_matrices_add_x_le) | |
| 740 | apply (rule disj_matrices_add_x) | |
| 741 | apply (simp add: disj_move_sparse_row_vector_twice) | |
| 742 | apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute) | |
| 743 | apply (simp add: disj_move_sparse_vec_mat[OF order_refl] disj_matrices_commute) | |
| 744 | apply (simp, blast) | |
| 745 | apply (intro strip, rule conjI, intro strip) | |
| 746 | apply (simp add: sorted_spvec_cons1) | |
| 747 | apply (subst disj_matrices_add_le_x) | |
| 748 | apply (simp add: disj_move_sparse_vec_mat[OF order_refl]) | |
| 749 | apply (rule disj_matrices_x_add) | |
| 750 | apply (simp add: disj_move_sparse_row_vector_twice) | |
| 751 | apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute) | |
| 752 | apply (simp, blast) | |
| 753 | apply (intro strip) | |
| 754 | apply (case_tac "a=aa") | |
| 755 | apply (simp_all) | |
| 756 | apply (subst disj_matrices_add) | |
| 757 | apply (simp_all add: disj_matrices_commute disj_move_sparse_vec_mat[OF order_refl]) | |
| 758 | apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le) | |
| 759 | done | |
| 760 | ||
| 15178 | 761 | ML {* simp_depth_limit := 999 *}
 | 
| 762 | ||
| 763 | consts | |
| 764 |    abs_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
 | |
| 765 |    minus_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
 | |
| 766 | ||
| 767 | primrec | |
| 768 | "abs_spmat [] = []" | |
| 769 | "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)" | |
| 770 | ||
| 771 | primrec | |
| 772 | "minus_spmat [] = []" | |
| 773 | "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)" | |
| 774 | ||
| 775 | lemma sparse_row_matrix_minus: | |
| 776 | "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)" | |
| 777 | apply (induct A) | |
| 778 | apply (simp_all add: sparse_row_vector_minus sparse_row_matrix_cons) | |
| 779 | apply (subst Rep_matrix_inject[symmetric]) | |
| 780 | apply (rule ext)+ | |
| 781 | apply simp | |
| 782 | done | |
| 15009 | 783 | |
| 15178 | 784 | lemma Rep_sparse_row_vector_zero: "x \<noteq> 0 \<Longrightarrow> Rep_matrix (sparse_row_vector v) x y = 0" | 
| 785 | proof - | |
| 786 | assume x:"x \<noteq> 0" | |
| 787 | have r:"nrows (sparse_row_vector v) <= Suc 0" by (rule nrows_spvec) | |
| 788 | show ?thesis | |
| 789 | apply (rule nrows) | |
| 790 | apply (subgoal_tac "Suc 0 <= x") | |
| 791 | apply (insert r) | |
| 792 | apply (simp only:) | |
| 793 | apply (insert x) | |
| 794 | apply arith | |
| 795 | done | |
| 796 | qed | |
| 797 | ||
| 798 | lemma sparse_row_matrix_abs: | |
| 799 | "sorted_spvec A \<Longrightarrow> sorted_spmat A \<Longrightarrow> sparse_row_matrix (abs_spmat A) = abs (sparse_row_matrix A)" | |
| 800 | apply (induct A) | |
| 801 | apply (simp_all add: sparse_row_vector_abs sparse_row_matrix_cons) | |
| 802 | apply (frule_tac sorted_spvec_cons1, simp) | |
| 803 | apply (subst Rep_matrix_inject[symmetric]) | |
| 804 | apply (rule ext)+ | |
| 805 | apply auto | |
| 806 | apply (case_tac "x=a") | |
| 807 | apply (simp) | |
| 15481 | 808 | apply (simplesubst sorted_sparse_row_matrix_zero) | 
| 15178 | 809 | apply auto | 
| 15481 | 810 | apply (simplesubst Rep_sparse_row_vector_zero) | 
| 15178 | 811 | apply (simp_all add: neg_def) | 
| 812 | done | |
| 813 | ||
| 814 | lemma sorted_spvec_minus_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (minus_spmat A)" | |
| 815 | apply (induct A) | |
| 816 | apply (simp) | |
| 817 | apply (frule sorted_spvec_cons1, simp) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 818 | apply (simp add: sorted_spvec.simps split:list.split_asm) | 
| 15178 | 819 | done | 
| 820 | ||
| 821 | lemma sorted_spvec_abs_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (abs_spmat A)" | |
| 822 | apply (induct A) | |
| 823 | apply (simp) | |
| 824 | apply (frule sorted_spvec_cons1, simp) | |
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15197diff
changeset | 825 | apply (simp add: sorted_spvec.simps split:list.split_asm) | 
| 15178 | 826 | done | 
| 827 | ||
| 828 | lemma sorted_spmat_minus_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (minus_spmat A)" | |
| 829 | apply (induct A) | |
| 830 | apply (simp_all add: sorted_spvec_minus_spvec) | |
| 831 | done | |
| 832 | ||
| 833 | lemma sorted_spmat_abs_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (abs_spmat A)" | |
| 834 | apply (induct A) | |
| 835 | apply (simp_all add: sorted_spvec_abs_spvec) | |
| 836 | done | |
| 15009 | 837 | |
| 15178 | 838 | constdefs | 
| 839 |   diff_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
 | |
| 840 | "diff_spmat A B == add_spmat (A, minus_spmat B)" | |
| 841 | ||
| 842 | lemma sorted_spmat_diff_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat B \<Longrightarrow> sorted_spmat (diff_spmat A B)" | |
| 843 | by (simp add: diff_spmat_def sorted_spmat_minus_spmat sorted_spmat_add_spmat) | |
| 844 | ||
| 845 | lemma sorted_spvec_diff_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec B \<Longrightarrow> sorted_spvec (diff_spmat A B)" | |
| 846 | by (simp add: diff_spmat_def sorted_spvec_minus_spmat sorted_spvec_add_spmat) | |
| 847 | ||
| 848 | lemma sparse_row_diff_spmat: "sparse_row_matrix (diff_spmat A B ) = (sparse_row_matrix A) - (sparse_row_matrix B)" | |
| 849 | by (simp add: diff_spmat_def sparse_row_add_spmat sparse_row_matrix_minus) | |
| 850 | ||
| 851 | constdefs | |
| 852 | sorted_sparse_matrix :: "'a spmat \<Rightarrow> bool" | |
| 853 | "sorted_sparse_matrix A == (sorted_spvec A) & (sorted_spmat A)" | |
| 854 | ||
| 855 | lemma sorted_sparse_matrix_imp_spvec: "sorted_sparse_matrix A \<Longrightarrow> sorted_spvec A" | |
| 856 | by (simp add: sorted_sparse_matrix_def) | |
| 857 | ||
| 858 | lemma sorted_sparse_matrix_imp_spmat: "sorted_sparse_matrix A \<Longrightarrow> sorted_spmat A" | |
| 859 | by (simp add: sorted_sparse_matrix_def) | |
| 860 | ||
| 861 | lemmas sparse_row_matrix_op_simps = | |
| 862 | sorted_sparse_matrix_imp_spmat sorted_sparse_matrix_imp_spvec | |
| 863 | sparse_row_add_spmat sorted_spvec_add_spmat sorted_spmat_add_spmat | |
| 864 | sparse_row_diff_spmat sorted_spvec_diff_spmat sorted_spmat_diff_spmat | |
| 865 | sparse_row_matrix_minus sorted_spvec_minus_spmat sorted_spmat_minus_spmat | |
| 866 | sparse_row_mult_spmat sorted_spvec_mult_spmat sorted_spmat_mult_spmat | |
| 867 | sparse_row_matrix_abs sorted_spvec_abs_spmat sorted_spmat_abs_spmat | |
| 868 | le_spmat_iff_sparse_row_le | |
| 869 | ||
| 870 | lemma zero_eq_Numeral0: "(0::_::number_ring) = Numeral0" by simp | |
| 15009 | 871 | |
| 15178 | 872 | lemmas sparse_row_matrix_arith_simps[simplified zero_eq_Numeral0] = | 
| 873 | mult_spmat.simps mult_spvec_spmat.simps | |
| 874 | addmult_spvec.simps | |
| 875 | smult_spvec_empty smult_spvec_cons | |
| 876 | add_spmat.simps add_spvec.simps | |
| 877 | minus_spmat.simps minus_spvec.simps | |
| 878 | abs_spmat.simps abs_spvec.simps | |
| 879 | diff_spmat_def | |
| 880 | le_spmat.simps le_spvec.simps | |
| 881 | ||
| 882 | lemmas sorted_sp_simps = | |
| 883 | sorted_spvec.simps | |
| 884 | sorted_spmat.simps | |
| 885 | sorted_sparse_matrix_def | |
| 886 | ||
| 887 | lemma bool1: "(\<not> True) = False" by blast | |
| 888 | lemma bool2: "(\<not> False) = True" by blast | |
| 889 | lemma bool3: "((P\<Colon>bool) \<and> True) = P" by blast | |
| 890 | lemma bool4: "(True \<and> (P\<Colon>bool)) = P" by blast | |
| 891 | lemma bool5: "((P\<Colon>bool) \<and> False) = False" by blast | |
| 892 | lemma bool6: "(False \<and> (P\<Colon>bool)) = False" by blast | |
| 893 | lemma bool7: "((P\<Colon>bool) \<or> True) = True" by blast | |
| 894 | lemma bool8: "(True \<or> (P\<Colon>bool)) = True" by blast | |
| 895 | lemma bool9: "((P\<Colon>bool) \<or> False) = P" by blast | |
| 896 | lemma bool10: "(False \<or> (P\<Colon>bool)) = P" by blast | |
| 897 | lemmas boolarith = bool1 bool2 bool3 bool4 bool5 bool6 bool7 bool8 bool9 bool10 | |
| 898 | ||
| 899 | lemma if_case_eq: "(if b then x else y) = (case b of True => x | False => y)" by simp | |
| 900 | ||
| 901 | lemma spm_linprog_dual_estimate_1: | |
| 902 | assumes | |
| 903 | "sorted_sparse_matrix A1" | |
| 904 | "sorted_sparse_matrix A2" | |
| 905 | "sorted_sparse_matrix c1" | |
| 906 | "sorted_sparse_matrix c2" | |
| 907 | "sorted_sparse_matrix y" | |
| 908 | "sorted_spvec b" | |
| 909 | "sorted_spvec r" | |
| 910 | "le_spmat ([], y)" | |
| 911 |   "A * x \<le> sparse_row_matrix (b::('a::lordered_ring) spmat)"
 | |
| 912 | "sparse_row_matrix A1 <= A" | |
| 913 | "A <= sparse_row_matrix A2" | |
| 914 | "sparse_row_matrix c1 <= c" | |
| 915 | "c <= sparse_row_matrix c2" | |
| 916 | "abs x \<le> sparse_row_matrix r" | |
| 917 | shows | |
| 918 | "c * x \<le> sparse_row_matrix (add_spmat (mult_spmat y b, mult_spmat (add_spmat (add_spmat (mult_spmat y (diff_spmat A2 A1), | |
| 919 | abs_spmat (diff_spmat (mult_spmat y A1) c1)), diff_spmat c2 c1)) r))" | |
| 920 | by (insert prems, simp add: sparse_row_matrix_op_simps linprog_dual_estimate_1[where A=A]) | |
| 15009 | 921 | |
| 922 | end |