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