|
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
|
|
|
97 |
smult_spvec :: "('a::lordered_ring) \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec"
|
|
|
98 |
addmult_spvec :: "('a::lordered_ring) * 'a spvec * 'a spvec \<Rightarrow> 'a spvec"
|
|
|
99 |
|
|
|
100 |
defs
|
|
|
101 |
smult_spvec_def: "smult_spvec y arr == map (% a. (fst a, y * snd a)) arr"
|
|
|
102 |
|
|
|
103 |
lemma smult_spvec_empty[simp]: "smult_spvec y [] = []"
|
|
|
104 |
by (simp add: smult_spvec_def)
|
|
|
105 |
|
|
|
106 |
lemma smult_spvec_cons: "smult_spvec y (a#arr) = (fst a, y * (snd a)) # (smult_spvec y arr)"
|
|
|
107 |
by (simp add: smult_spvec_def)
|
|
|
108 |
|
|
|
109 |
recdef addmult_spvec "measure (% (y, a, b). length a + (length b))"
|
|
|
110 |
"addmult_spvec (y, arr, []) = arr"
|
|
|
111 |
"addmult_spvec (y, [], brr) = smult_spvec y brr"
|
|
|
112 |
"addmult_spvec (y, a#arr, b#brr) = (
|
|
|
113 |
if (fst a) < (fst b) then (a#(addmult_spvec (y, arr, b#brr)))
|
|
|
114 |
else (if (fst b < fst a) then ((fst b, y * (snd b))#(addmult_spvec (y, a#arr, brr)))
|
|
|
115 |
else ((fst a, (snd a)+ y*(snd b))#(addmult_spvec (y, arr,brr)))))"
|
|
|
116 |
|
|
|
117 |
lemma addmult_spvec_empty1[simp]: "addmult_spvec (y, [], a) = smult_spvec y a"
|
|
|
118 |
by (induct a, auto)
|
|
|
119 |
|
|
|
120 |
lemma addmult_spvec_empty2[simp]: "addmult_spvec (y, a, []) = a"
|
|
|
121 |
by (induct a, auto)
|
|
|
122 |
|
|
|
123 |
lemma sparse_row_vector_map: "(! x y. f (x+y) = (f x) + (f y)) \<Longrightarrow> (f::'a\<Rightarrow>('a::lordered_ring)) 0 = 0 \<Longrightarrow>
|
|
|
124 |
sparse_row_vector (map (% x. (fst x, f (snd x))) a) = apply_matrix f (sparse_row_vector a)"
|
|
|
125 |
apply (induct a)
|
|
|
126 |
apply (simp_all add: apply_matrix_add)
|
|
|
127 |
done
|
|
|
128 |
|
|
|
129 |
lemma sparse_row_vector_smult: "sparse_row_vector (smult_spvec y a) = scalar_mult y (sparse_row_vector a)"
|
|
|
130 |
apply (induct a)
|
|
|
131 |
apply (simp_all add: smult_spvec_cons scalar_mult_add)
|
|
|
132 |
done
|
|
|
133 |
|
|
|
134 |
lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring, a, b)) =
|
|
|
135 |
(sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))"
|
|
|
136 |
apply (rule addmult_spvec.induct[of _ y])
|
|
|
137 |
apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+
|
|
|
138 |
apply (case_tac "a=aa")
|
|
|
139 |
apply (auto)
|
|
|
140 |
done
|
|
|
141 |
|
|
|
142 |
lemma sorted_smult_spvec[rule_format]: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)"
|
|
|
143 |
apply (auto simp add: smult_spvec_def)
|
|
|
144 |
apply (induct a)
|
|
|
145 |
apply (auto simp add: sorted_spvec.simps)
|
|
|
146 |
apply (case_tac list)
|
|
|
147 |
apply (auto)
|
|
|
148 |
done
|
|
|
149 |
|
|
|
150 |
lemma sorted_spvec_addmult_spvec_helper: "\<lbrakk>sorted_spvec (addmult_spvec (y, (a, b) # arr, brr)); aa < a; sorted_spvec ((a, b) # arr);
|
|
|
151 |
sorted_spvec ((aa, ba) # brr)\<rbrakk> \<Longrightarrow> sorted_spvec ((aa, y * ba) # addmult_spvec (y, (a, b) # arr, brr))"
|
|
|
152 |
apply (induct brr)
|
|
|
153 |
apply (auto simp add: sorted_spvec.simps)
|
|
|
154 |
apply (simp split: list.split)
|
|
|
155 |
apply (auto)
|
|
|
156 |
apply (simp split: list.split)
|
|
|
157 |
apply (auto)
|
|
|
158 |
done
|
|
|
159 |
|
|
|
160 |
lemma sorted_spvec_addmult_spvec_helper2:
|
|
|
161 |
"\<lbrakk>sorted_spvec (addmult_spvec (y, arr, (aa, ba) # brr)); a < aa; sorted_spvec ((a, b) # arr); sorted_spvec ((aa, ba) # brr)\<rbrakk>
|
|
|
162 |
\<Longrightarrow> sorted_spvec ((a, b) # addmult_spvec (y, arr, (aa, ba) # brr))"
|
|
|
163 |
apply (induct arr)
|
|
|
164 |
apply (auto simp add: smult_spvec_def sorted_spvec.simps)
|
|
|
165 |
apply (simp split: list.split)
|
|
|
166 |
apply (auto)
|
|
|
167 |
done
|
|
|
168 |
|
|
|
169 |
lemma sorted_spvec_addmult_spvec_helper3[rule_format]:
|
|
|
170 |
"sorted_spvec (addmult_spvec (y, arr, brr)) \<longrightarrow> sorted_spvec ((aa, b) # arr) \<longrightarrow> sorted_spvec ((aa, ba) # brr)
|
|
|
171 |
\<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec (y, arr, brr)))"
|
|
|
172 |
apply (rule addmult_spvec.induct[of _ y arr brr])
|
|
|
173 |
apply (simp_all add: sorted_spvec.simps smult_spvec_def)
|
|
|
174 |
done
|
|
|
175 |
|
|
|
176 |
lemma sorted_addmult_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (addmult_spvec (y, a, b))"
|
|
|
177 |
apply (rule addmult_spvec.induct[of _ y a b])
|
|
|
178 |
apply (simp_all add: sorted_smult_spvec)
|
|
|
179 |
apply (rule conjI, intro strip)
|
|
|
180 |
apply (case_tac "~(a < aa)")
|
|
|
181 |
apply (simp_all)
|
|
|
182 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
183 |
apply (simp add: sorted_spvec_addmult_spvec_helper)
|
|
|
184 |
apply (intro strip | rule conjI)+
|
|
|
185 |
apply (frule_tac as=arr in sorted_spvec_cons1)
|
|
|
186 |
apply (simp add: sorted_spvec_addmult_spvec_helper2)
|
|
|
187 |
apply (intro strip)
|
|
|
188 |
apply (frule_tac as=arr in sorted_spvec_cons1)
|
|
|
189 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
190 |
apply (simp)
|
|
|
191 |
apply (case_tac "a=aa")
|
|
|
192 |
apply (simp_all add: sorted_spvec_addmult_spvec_helper3)
|
|
|
193 |
done
|
|
|
194 |
|
|
|
195 |
consts
|
|
|
196 |
mult_spvec_spmat :: "('a::lordered_ring) spvec * 'a spvec * 'a spmat \<Rightarrow> 'a spvec"
|
|
|
197 |
|
|
|
198 |
recdef mult_spvec_spmat "measure (% (c, arr, brr). (length arr) + (length brr))"
|
|
|
199 |
"mult_spvec_spmat (c, [], brr) = c"
|
|
|
200 |
"mult_spvec_spmat (c, arr, []) = c"
|
|
|
201 |
"mult_spvec_spmat (c, a#arr, b#brr) = (
|
|
|
202 |
if ((fst a) < (fst b)) then (mult_spvec_spmat (c, arr, b#brr))
|
|
|
203 |
else (if ((fst b) < (fst a)) then (mult_spvec_spmat (c, a#arr, brr))
|
|
|
204 |
else (mult_spvec_spmat (addmult_spvec (snd a, c, snd b), arr, brr))))"
|
|
|
205 |
|
|
|
206 |
lemma sparse_row_mult_spvec_spmat[rule_format]: "sorted_spvec (a::('a::lordered_ring) spvec) \<longrightarrow> sorted_spvec B \<longrightarrow>
|
|
|
207 |
sparse_row_vector (mult_spvec_spmat (c, a, B)) = (sparse_row_vector c) + (sparse_row_vector a) * (sparse_row_matrix B)"
|
|
|
208 |
proof -
|
|
|
209 |
have comp_1: "!! a b. a < b \<Longrightarrow> Suc 0 <= nat ((int b)-(int a))" by arith
|
|
|
210 |
have not_iff: "!! a b. a = b \<Longrightarrow> (~ a) = (~ b)" by simp
|
|
|
211 |
have max_helper: "!! a b. ~ (a <= max (Suc a) b) \<Longrightarrow> False"
|
|
|
212 |
by arith
|
|
|
213 |
{
|
|
|
214 |
fix a
|
|
|
215 |
fix v
|
|
|
216 |
assume a:"a < nrows(sparse_row_vector v)"
|
|
|
217 |
have b:"nrows(sparse_row_vector v) <= 1" by simp
|
|
|
218 |
note dummy = less_le_trans[of a "nrows (sparse_row_vector v)" 1, OF a b]
|
|
|
219 |
then have "a = 0" by simp
|
|
|
220 |
}
|
|
|
221 |
note nrows_helper = this
|
|
|
222 |
show ?thesis
|
|
|
223 |
apply (rule mult_spvec_spmat.induct)
|
|
|
224 |
apply simp+
|
|
|
225 |
apply (rule conjI)
|
|
|
226 |
apply (intro strip)
|
|
|
227 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
228 |
apply (simp add: ring_eq_simps sparse_row_matrix_cons)
|
|
|
229 |
apply (subst Rep_matrix_zero_imp_mult_zero)
|
|
|
230 |
apply (simp)
|
|
|
231 |
apply (intro strip)
|
|
|
232 |
apply (rule disjI2)
|
|
|
233 |
apply (intro strip)
|
|
|
234 |
apply (subst nrows)
|
|
|
235 |
apply (rule order_trans[of _ 1])
|
|
|
236 |
apply (simp add: comp_1)+
|
|
|
237 |
apply (subst Rep_matrix_zero_imp_mult_zero)
|
|
|
238 |
apply (intro strip)
|
|
|
239 |
apply (case_tac "k <= aa")
|
|
|
240 |
apply (rule_tac m1 = k and n1 = a and a1 = b in ssubst[OF sorted_sparse_row_vector_zero])
|
|
|
241 |
apply (simp_all)
|
|
|
242 |
apply (rule impI)
|
|
|
243 |
apply (rule disjI2)
|
|
|
244 |
apply (rule nrows)
|
|
|
245 |
apply (rule order_trans[of _ 1])
|
|
|
246 |
apply (simp_all add: comp_1)
|
|
|
247 |
|
|
|
248 |
apply (intro strip | rule conjI)+
|
|
|
249 |
apply (frule_tac as=arr in sorted_spvec_cons1)
|
|
|
250 |
apply (simp add: ring_eq_simps)
|
|
|
251 |
apply (subst Rep_matrix_zero_imp_mult_zero)
|
|
|
252 |
apply (simp)
|
|
|
253 |
apply (rule disjI2)
|
|
|
254 |
apply (intro strip)
|
|
|
255 |
apply (simp add: sparse_row_matrix_cons neg_def)
|
|
|
256 |
apply (case_tac "a <= aa")
|
|
|
257 |
apply (erule sorted_sparse_row_matrix_zero)
|
|
|
258 |
apply (simp_all)
|
|
|
259 |
apply (intro strip)
|
|
|
260 |
apply (case_tac "a=aa")
|
|
|
261 |
apply (simp_all)
|
|
|
262 |
apply (frule_tac as=arr in sorted_spvec_cons1)
|
|
|
263 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
264 |
apply (simp add: sparse_row_matrix_cons ring_eq_simps sparse_row_vector_addmult_spvec)
|
|
|
265 |
apply (rule_tac B1 = "sparse_row_matrix brr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
|
|
|
266 |
apply (auto)
|
|
|
267 |
apply (rule sorted_sparse_row_matrix_zero)
|
|
|
268 |
apply (simp_all)
|
|
|
269 |
apply (rule_tac A1 = "sparse_row_vector arr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
|
|
|
270 |
apply (auto)
|
|
|
271 |
apply (rule_tac m=k and n = aa and a = b and arr=arr in sorted_sparse_row_vector_zero)
|
|
|
272 |
apply (simp_all)
|
|
|
273 |
apply (simp add: neg_def)
|
|
|
274 |
apply (drule nrows_notzero)
|
|
|
275 |
apply (drule nrows_helper)
|
|
|
276 |
apply (arith)
|
|
|
277 |
|
|
|
278 |
apply (subst Rep_matrix_inject[symmetric])
|
|
|
279 |
apply (rule ext)+
|
|
|
280 |
apply (simp)
|
|
|
281 |
apply (subst Rep_matrix_mult)
|
|
|
282 |
apply (rule_tac j1=aa in ssubst[OF foldseq_almostzero])
|
|
|
283 |
apply (simp_all)
|
|
|
284 |
apply (intro strip, rule conjI)
|
|
|
285 |
apply (intro strip)
|
|
|
286 |
apply (drule_tac max_helper)
|
|
|
287 |
apply (simp)
|
|
|
288 |
apply (auto)
|
|
|
289 |
apply (rule zero_imp_mult_zero)
|
|
|
290 |
apply (rule disjI2)
|
|
|
291 |
apply (rule nrows)
|
|
|
292 |
apply (rule order_trans[of _ 1])
|
|
|
293 |
apply (simp)
|
|
|
294 |
apply (simp)
|
|
|
295 |
done
|
|
|
296 |
qed
|
|
|
297 |
|
|
|
298 |
lemma sorted_mult_spvec_spmat[rule_format]:
|
|
|
299 |
"sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat (c, a, B))"
|
|
|
300 |
apply (rule mult_spvec_spmat.induct[of _ c a B])
|
|
|
301 |
apply (simp_all add: sorted_addmult_spvec)
|
|
|
302 |
done
|
|
|
303 |
|
|
|
304 |
consts
|
|
|
305 |
mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
|
|
|
306 |
|
|
|
307 |
primrec
|
|
|
308 |
"mult_spmat [] A = []"
|
|
|
309 |
"mult_spmat (a#as) A = (fst a, mult_spvec_spmat ([], snd a, A))#(mult_spmat as A)"
|
|
|
310 |
|
|
|
311 |
lemma sparse_row_mult_spmat[rule_format]:
|
|
|
312 |
"sorted_spmat A \<longrightarrow> sorted_spvec B \<longrightarrow> sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)"
|
|
|
313 |
apply (induct A)
|
|
|
314 |
apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat ring_eq_simps move_matrix_mult)
|
|
|
315 |
done
|
|
|
316 |
|
|
|
317 |
lemma sorted_spvec_mult_spmat[rule_format]:
|
|
|
318 |
"sorted_spvec (A::('a::lordered_ring) spmat) \<longrightarrow> sorted_spvec (mult_spmat A B)"
|
|
|
319 |
apply (induct A)
|
|
|
320 |
apply (auto)
|
|
|
321 |
apply (drule sorted_spvec_cons1, simp)
|
|
|
322 |
apply (case_tac list)
|
|
|
323 |
apply (auto simp add: sorted_spvec.simps)
|
|
|
324 |
done
|
|
|
325 |
|
|
|
326 |
lemma sorted_spmat_mult_spmat[rule_format]:
|
|
|
327 |
"sorted_spmat (B::('a::lordered_ring) spmat) \<longrightarrow> sorted_spmat (mult_spmat A B)"
|
|
|
328 |
apply (induct A)
|
|
|
329 |
apply (auto simp add: sorted_mult_spvec_spmat)
|
|
|
330 |
done
|
|
|
331 |
|
|
|
332 |
consts
|
|
|
333 |
add_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> 'a spvec"
|
|
|
334 |
add_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> 'a spmat"
|
|
|
335 |
|
|
|
336 |
recdef add_spvec "measure (% (a, b). length a + (length b))"
|
|
|
337 |
"add_spvec (arr, []) = arr"
|
|
|
338 |
"add_spvec ([], brr) = brr"
|
|
|
339 |
"add_spvec (a#arr, b#brr) = (
|
|
|
340 |
if (fst a) < (fst b) then (a#(add_spvec (arr, b#brr)))
|
|
|
341 |
else (if (fst b < fst a) then (b#(add_spvec (a#arr, brr)))
|
|
|
342 |
else ((fst a, (snd a)+(snd b))#(add_spvec (arr,brr)))))"
|
|
|
343 |
|
|
|
344 |
lemma add_spvec_empty1[simp]: "add_spvec ([], a) = a"
|
|
|
345 |
by (induct a, auto)
|
|
|
346 |
|
|
|
347 |
lemma add_spvec_empty2[simp]: "add_spvec (a, []) = a"
|
|
|
348 |
by (induct a, auto)
|
|
|
349 |
|
|
|
350 |
lemma sparse_row_vector_add: "sparse_row_vector (add_spvec (a,b)) = (sparse_row_vector a) + (sparse_row_vector b)"
|
|
|
351 |
apply (rule add_spvec.induct[of _ a b])
|
|
|
352 |
apply (simp_all add: singleton_matrix_add)
|
|
|
353 |
apply (case_tac "a = aa")
|
|
|
354 |
apply (simp_all)
|
|
|
355 |
done
|
|
|
356 |
|
|
|
357 |
recdef add_spmat "measure (% (A,B). (length A)+(length B))"
|
|
|
358 |
"add_spmat ([], bs) = bs"
|
|
|
359 |
"add_spmat (as, []) = as"
|
|
|
360 |
"add_spmat (a#as, b#bs) = (
|
|
|
361 |
if fst a < fst b then
|
|
|
362 |
(a#(add_spmat (as, b#bs)))
|
|
|
363 |
else (if fst b < fst a then
|
|
|
364 |
(b#(add_spmat (a#as, bs)))
|
|
|
365 |
else
|
|
|
366 |
((fst a, add_spvec (snd a, snd b))#(add_spmat (as, bs)))))"
|
|
|
367 |
|
|
|
368 |
lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat (A, B)) = (sparse_row_matrix A) + (sparse_row_matrix B)"
|
|
|
369 |
apply (rule add_spmat.induct)
|
|
|
370 |
apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add)
|
|
|
371 |
apply (case_tac "a=aa", simp, simp)+
|
|
|
372 |
done
|
|
|
373 |
|
|
|
374 |
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)))"
|
|
|
375 |
proof -
|
|
|
376 |
have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec (x, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
|
|
|
377 |
by (rule add_spvec.induct[of _ _ brr], auto)
|
|
|
378 |
then show ?thesis
|
|
|
379 |
by (case_tac brr, auto)
|
|
|
380 |
qed
|
|
|
381 |
|
|
|
382 |
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)))"
|
|
|
383 |
proof -
|
|
|
384 |
have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat (x, brr) = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
|
|
|
385 |
by (rule add_spmat.induct[of _ _ brr], auto)
|
|
|
386 |
then show ?thesis
|
|
|
387 |
by (case_tac brr, auto)
|
|
|
388 |
qed
|
|
|
389 |
|
|
|
390 |
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)))"
|
|
|
391 |
apply (rule add_spvec.induct[of _ arr brr])
|
|
|
392 |
apply (auto)
|
|
|
393 |
done
|
|
|
394 |
|
|
|
395 |
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)))"
|
|
|
396 |
apply (rule add_spmat.induct[of _ arr brr])
|
|
|
397 |
apply (auto)
|
|
|
398 |
done
|
|
|
399 |
|
|
|
400 |
lemma add_spvec_commute: "add_spvec (a, b) = add_spvec (b, a)"
|
|
|
401 |
by (rule add_spvec.induct[of _ a b], auto)
|
|
|
402 |
|
|
|
403 |
lemma add_spmat_commute: "add_spmat (a, b) = add_spmat (b, a)"
|
|
|
404 |
apply (rule add_spmat.induct[of _ a b])
|
|
|
405 |
apply (simp_all add: add_spvec_commute)
|
|
|
406 |
done
|
|
|
407 |
|
|
|
408 |
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"
|
|
|
409 |
apply (drule sorted_add_spvec_helper1)
|
|
|
410 |
apply (auto)
|
|
|
411 |
apply (case_tac brr)
|
|
|
412 |
apply (simp_all)
|
|
|
413 |
apply (drule_tac sorted_spvec_cons3)
|
|
|
414 |
apply (simp)
|
|
|
415 |
done
|
|
|
416 |
|
|
|
417 |
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"
|
|
|
418 |
apply (drule sorted_add_spmat_helper1)
|
|
|
419 |
apply (auto)
|
|
|
420 |
apply (case_tac brr)
|
|
|
421 |
apply (simp_all)
|
|
|
422 |
apply (drule_tac sorted_spvec_cons3)
|
|
|
423 |
apply (simp)
|
|
|
424 |
done
|
|
|
425 |
|
|
|
426 |
lemma sorted_spvec_add_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (add_spvec (a, b))"
|
|
|
427 |
apply (rule add_spvec.induct[of _ a b])
|
|
|
428 |
apply (simp_all)
|
|
|
429 |
apply (rule conjI)
|
|
|
430 |
apply (intro strip)
|
|
|
431 |
apply (simp)
|
|
|
432 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
433 |
apply (simp)
|
|
|
434 |
apply (subst sorted_spvec_step)
|
|
|
435 |
apply (simp split: list.split)
|
|
|
436 |
apply (clarify, simp)
|
|
|
437 |
apply (simp add: sorted_add_spvec_helper2)
|
|
|
438 |
apply (clarify)
|
|
|
439 |
apply (rule conjI)
|
|
|
440 |
apply (case_tac "a=aa")
|
|
|
441 |
apply (simp)
|
|
|
442 |
apply (clarify)
|
|
|
443 |
apply (frule_tac as=arr in sorted_spvec_cons1, simp)
|
|
|
444 |
apply (subst sorted_spvec_step)
|
|
|
445 |
apply (simp split: list.split)
|
|
|
446 |
apply (clarify, simp)
|
|
|
447 |
apply (simp add: sorted_add_spvec_helper2 add_spvec_commute)
|
|
|
448 |
apply (case_tac "a=aa")
|
|
|
449 |
apply (simp_all)
|
|
|
450 |
apply (clarify)
|
|
|
451 |
apply (frule_tac as=arr in sorted_spvec_cons1)
|
|
|
452 |
apply (frule_tac as=brr in sorted_spvec_cons1)
|
|
|
453 |
apply (simp)
|
|
|
454 |
apply (subst sorted_spvec_step)
|
|
|
455 |
apply (simp split: list.split)
|
|
|
456 |
apply (clarify, simp)
|
|
|
457 |
apply (drule_tac sorted_add_spvec_helper)
|
|
|
458 |
apply (auto)
|
|
|
459 |
apply (case_tac arr)
|
|
|
460 |
apply (simp_all)
|
|
|
461 |
apply (drule sorted_spvec_cons3)
|
|
|
462 |
apply (simp)
|
|
|
463 |
apply (case_tac brr)
|
|
|
464 |
apply (simp_all)
|
|
|
465 |
apply (drule sorted_spvec_cons3)
|
|
|
466 |
apply (simp)
|
|
|
467 |
done
|
|
|
468 |
|
|
|
469 |
lemma sorted_spvec_add_spmat[rule_format]: "sorted_spvec A \<longrightarrow> sorted_spvec B \<longrightarrow> sorted_spvec (add_spmat (A, B))"
|
|
|
470 |
apply (rule add_spmat.induct[of _ A B])
|
|
|
471 |
apply (simp_all)
|
|
|
472 |
apply (rule conjI)
|
|
|
473 |
apply (intro strip)
|
|
|
474 |
apply (simp)
|
|
|
475 |
apply (frule_tac as=bs in sorted_spvec_cons1)
|
|
|
476 |
apply (simp)
|
|
|
477 |
apply (subst sorted_spvec_step)
|
|
|
478 |
apply (simp split: list.split)
|
|
|
479 |
apply (clarify, simp)
|
|
|
480 |
apply (simp add: sorted_add_spmat_helper2)
|
|
|
481 |
apply (clarify)
|
|
|
482 |
apply (rule conjI)
|
|
|
483 |
apply (case_tac "a=aa")
|
|
|
484 |
apply (simp)
|
|
|
485 |
apply (clarify)
|
|
|
486 |
apply (frule_tac as=as in sorted_spvec_cons1, simp)
|
|
|
487 |
apply (subst sorted_spvec_step)
|
|
|
488 |
apply (simp split: list.split)
|
|
|
489 |
apply (clarify, simp)
|
|
|
490 |
apply (simp add: sorted_add_spmat_helper2 add_spmat_commute)
|
|
|
491 |
apply (case_tac "a=aa")
|
|
|
492 |
apply (simp_all)
|
|
|
493 |
apply (clarify)
|
|
|
494 |
apply (frule_tac as=as in sorted_spvec_cons1)
|
|
|
495 |
apply (frule_tac as=bs in sorted_spvec_cons1)
|
|
|
496 |
apply (simp)
|
|
|
497 |
apply (subst sorted_spvec_step)
|
|
|
498 |
apply (simp split: list.split)
|
|
|
499 |
apply (clarify, simp)
|
|
|
500 |
apply (drule_tac sorted_add_spmat_helper)
|
|
|
501 |
apply (auto)
|
|
|
502 |
apply (case_tac as)
|
|
|
503 |
apply (simp_all)
|
|
|
504 |
apply (drule sorted_spvec_cons3)
|
|
|
505 |
apply (simp)
|
|
|
506 |
apply (case_tac bs)
|
|
|
507 |
apply (simp_all)
|
|
|
508 |
apply (drule sorted_spvec_cons3)
|
|
|
509 |
apply (simp)
|
|
|
510 |
done
|
|
|
511 |
|
|
|
512 |
lemma sorted_spmat_add_spmat[rule_format]: "sorted_spmat A \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spmat (add_spmat (A, B))"
|
|
|
513 |
apply (rule add_spmat.induct[of _ A B])
|
|
|
514 |
apply (simp_all add: sorted_spvec_add_spvec)
|
|
|
515 |
done
|
|
|
516 |
|
|
|
517 |
consts
|
|
|
518 |
le_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> bool"
|
|
|
519 |
le_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> bool"
|
|
|
520 |
|
|
|
521 |
recdef le_spvec "measure (% (a,b). (length a) + (length b))"
|
|
|
522 |
"le_spvec ([], []) = True"
|
|
|
523 |
"le_spvec (a#as, []) = ((snd a <= 0) & (le_spvec (as, [])))"
|
|
|
524 |
"le_spvec ([], b#bs) = ((0 <= snd b) & (le_spvec ([], bs)))"
|
|
|
525 |
"le_spvec (a#as, b#bs) = (
|
|
|
526 |
if (fst a < fst b) then
|
|
|
527 |
((snd a <= 0) & (le_spvec (as, b#bs)))
|
|
|
528 |
else (if (fst b < fst a) then
|
|
|
529 |
((0 <= snd b) & (le_spvec (a#as, bs)))
|
|
|
530 |
else
|
|
|
531 |
((snd a <= snd b) & (le_spvec (as, bs)))))"
|
|
|
532 |
|
|
|
533 |
recdef le_spmat "measure (% (a,b). (length a) + (length b))"
|
|
|
534 |
"le_spmat ([], []) = True"
|
|
|
535 |
"le_spmat (a#as, []) = (le_spvec (snd a, []) & (le_spmat (as, [])))"
|
|
|
536 |
"le_spmat ([], b#bs) = (le_spvec ([], snd b) & (le_spmat ([], bs)))"
|
|
|
537 |
"le_spmat (a#as, b#bs) = (
|
|
|
538 |
if fst a < fst b then
|
|
|
539 |
(le_spvec(snd a,[]) & le_spmat(as, b#bs))
|
|
|
540 |
else (if (fst b < fst a) then
|
|
|
541 |
(le_spvec([], snd b) & le_spmat(a#as, bs))
|
|
|
542 |
else
|
|
|
543 |
(le_spvec(snd a, snd b) & le_spmat (as, bs))))"
|
|
|
544 |
|
|
|
545 |
lemma spec2: "! j i. P j i \<Longrightarrow> P j i" by blast
|
|
|
546 |
lemma neg_imp: "(\<not> Q \<longrightarrow> \<not> P) \<Longrightarrow> P \<longrightarrow> Q" by blast
|
|
|
547 |
|
|
|
548 |
constdefs
|
|
|
549 |
disj_matrices :: "('a::zero) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
|
|
|
550 |
"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))"
|
|
|
551 |
|
|
|
552 |
ML {* simp_depth_limit := 2 *}
|
|
|
553 |
|
|
|
554 |
lemma disj_matrices_add: "disj_matrices A B \<Longrightarrow> disj_matrices C D \<Longrightarrow> disj_matrices A D \<Longrightarrow> disj_matrices B C \<Longrightarrow>
|
|
|
555 |
(A + B <= C + D) = (A <= C & B <= (D::('a::lordered_ab_group) matrix))"
|
|
|
556 |
apply (auto)
|
|
|
557 |
apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
|
|
|
558 |
apply (intro strip)
|
|
|
559 |
apply (erule conjE)+
|
|
|
560 |
apply (drule_tac j=j and i=i in spec2)+
|
|
|
561 |
apply (case_tac "Rep_matrix B j i = 0")
|
|
|
562 |
apply (case_tac "Rep_matrix D j i = 0")
|
|
|
563 |
apply (simp_all)
|
|
|
564 |
apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
|
|
|
565 |
apply (intro strip)
|
|
|
566 |
apply (erule conjE)+
|
|
|
567 |
apply (drule_tac j=j and i=i in spec2)+
|
|
|
568 |
apply (case_tac "Rep_matrix A j i = 0")
|
|
|
569 |
apply (case_tac "Rep_matrix C j i = 0")
|
|
|
570 |
apply (simp_all)
|
|
|
571 |
apply (erule add_mono)
|
|
|
572 |
apply (assumption)
|
|
|
573 |
done
|
|
|
574 |
|
|
|
575 |
lemma disj_matrices_zero1[simp]: "disj_matrices 0 B"
|
|
|
576 |
by (simp add: disj_matrices_def)
|
|
|
577 |
|
|
|
578 |
lemma disj_matrices_zero2[simp]: "disj_matrices A 0"
|
|
|
579 |
by (simp add: disj_matrices_def)
|
|
|
580 |
|
|
|
581 |
lemma disj_matrices_commute: "disj_matrices A B = disj_matrices B A"
|
|
|
582 |
by (auto simp add: disj_matrices_def)
|
|
|
583 |
|
|
|
584 |
lemma disj_matrices_add_le_zero: "disj_matrices A B \<Longrightarrow>
|
|
|
585 |
(A + B <= 0) = (A <= 0 & (B::('a::lordered_ab_group) matrix) <= 0)"
|
|
|
586 |
by (rule disj_matrices_add[of A B 0 0, simplified])
|
|
|
587 |
|
|
|
588 |
lemma disj_matrices_add_zero_le: "disj_matrices A B \<Longrightarrow>
|
|
|
589 |
(0 <= A + B) = (0 <= A & 0 <= (B::('a::lordered_ab_group) matrix))"
|
|
|
590 |
by (rule disj_matrices_add[of 0 0 A B, simplified])
|
|
|
591 |
|
|
|
592 |
lemma disj_matrices_add_x_le: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow>
|
|
|
593 |
(A <= B + C) = (A <= C & 0 <= (B::('a::lordered_ab_group) matrix))"
|
|
|
594 |
by (auto simp add: disj_matrices_add[of 0 A B C, simplified])
|
|
|
595 |
|
|
|
596 |
lemma disj_matrices_add_le_x: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow>
|
|
|
597 |
(B + A <= C) = (A <= C & (B::('a::lordered_ab_group) matrix) <= 0)"
|
|
|
598 |
by (auto simp add: disj_matrices_add[of B A 0 C,simplified] disj_matrices_commute)
|
|
|
599 |
|
|
|
600 |
lemma singleton_le_zero[simp]: "(singleton_matrix j i x <= 0) = (x <= (0::'a::{order,zero}))"
|
|
|
601 |
apply (auto)
|
|
|
602 |
apply (simp add: le_matrix_def)
|
|
|
603 |
apply (drule_tac j=j and i=i in spec2)
|
|
|
604 |
apply (simp)
|
|
|
605 |
apply (simp add: le_matrix_def)
|
|
|
606 |
done
|
|
|
607 |
|
|
|
608 |
lemma singleton_ge_zero[simp]: "(0 <= singleton_matrix j i x) = ((0::'a::{order,zero}) <= x)"
|
|
|
609 |
apply (auto)
|
|
|
610 |
apply (simp add: le_matrix_def)
|
|
|
611 |
apply (drule_tac j=j and i=i in spec2)
|
|
|
612 |
apply (simp)
|
|
|
613 |
apply (simp add: le_matrix_def)
|
|
|
614 |
done
|
|
|
615 |
|
|
|
616 |
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)"
|
|
|
617 |
apply (simp add: disj_matrices_def)
|
|
|
618 |
apply (rule conjI)
|
|
|
619 |
apply (rule neg_imp)
|
|
|
620 |
apply (simp)
|
|
|
621 |
apply (intro strip)
|
|
|
622 |
apply (rule sorted_sparse_row_vector_zero)
|
|
|
623 |
apply (simp_all)
|
|
|
624 |
apply (intro strip)
|
|
|
625 |
apply (rule sorted_sparse_row_vector_zero)
|
|
|
626 |
apply (simp_all)
|
|
|
627 |
done
|
|
|
628 |
|
|
|
629 |
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)"
|
|
|
630 |
apply (simp add: disj_matrices_def)
|
|
|
631 |
apply (auto)
|
|
|
632 |
apply (drule_tac j=j and i=i in spec2)+
|
|
|
633 |
apply (case_tac "Rep_matrix B j i = 0")
|
|
|
634 |
apply (case_tac "Rep_matrix C j i = 0")
|
|
|
635 |
apply (simp_all)
|
|
|
636 |
done
|
|
|
637 |
|
|
|
638 |
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)"
|
|
|
639 |
by (simp add: disj_matrices_x_add disj_matrices_commute)
|
|
|
640 |
|
|
|
641 |
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)"
|
|
|
642 |
by (auto simp add: disj_matrices_def)
|
|
|
643 |
|
|
|
644 |
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)"
|
|
|
645 |
apply (rule le_spvec.induct)
|
|
|
646 |
apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
|
|
|
647 |
apply (rule conjI, intro strip)
|
|
|
648 |
apply (simp add: sorted_spvec_cons1)
|
|
|
649 |
apply (subst disj_matrices_add_x_le)
|
|
|
650 |
apply (simp add: disj_sparse_row_singleton[OF less_imp_le] disj_matrices_x_add disj_matrices_commute)
|
|
|
651 |
apply (simp add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
|
|
|
652 |
apply (simp, blast)
|
|
|
653 |
apply (intro strip, rule conjI, intro strip)
|
|
|
654 |
apply (simp add: sorted_spvec_cons1)
|
|
|
655 |
apply (subst disj_matrices_add_le_x)
|
|
|
656 |
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)
|
|
|
657 |
apply (blast)
|
|
|
658 |
apply (intro strip)
|
|
|
659 |
apply (simp add: sorted_spvec_cons1)
|
|
|
660 |
apply (case_tac "a=aa", simp_all)
|
|
|
661 |
apply (subst disj_matrices_add)
|
|
|
662 |
apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
|
|
|
663 |
done
|
|
|
664 |
|
|
|
665 |
lemma disj_move_sparse_vec_mat[simplified disj_matrices_commute]:
|
|
|
666 |
"j <= a \<Longrightarrow> sorted_spvec((a,c)#as) \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector b) (int j) i) (sparse_row_matrix as)"
|
|
|
667 |
apply (auto simp add: neg_def disj_matrices_def)
|
|
|
668 |
apply (drule nrows_notzero)
|
|
|
669 |
apply (drule less_le_trans[OF _ nrows_spvec])
|
|
|
670 |
apply (subgoal_tac "ja = j")
|
|
|
671 |
apply (simp add: sorted_sparse_row_matrix_zero)
|
|
|
672 |
apply (arith)
|
|
|
673 |
apply (rule nrows)
|
|
|
674 |
apply (rule order_trans[of _ 1 _])
|
|
|
675 |
apply (simp)
|
|
|
676 |
apply (case_tac "nat (int ja - int j) = 0")
|
|
|
677 |
apply (case_tac "ja = j")
|
|
|
678 |
apply (simp add: sorted_sparse_row_matrix_zero)
|
|
|
679 |
apply arith+
|
|
|
680 |
done
|
|
|
681 |
|
|
|
682 |
lemma disj_move_sparse_row_vector_twice:
|
|
|
683 |
"j \<noteq> u \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector a) j i) (move_matrix (sparse_row_vector b) u v)"
|
|
|
684 |
apply (auto simp add: neg_def disj_matrices_def)
|
|
|
685 |
apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+
|
|
|
686 |
done
|
|
|
687 |
|
|
|
688 |
lemma move_matrix_le_zero[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= 0) = (A <= (0::('a::{order,zero}) matrix))"
|
|
|
689 |
apply (auto simp add: le_matrix_def neg_def)
|
|
|
690 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
|
|
|
691 |
apply (auto)
|
|
|
692 |
done
|
|
|
693 |
|
|
|
694 |
lemma move_matrix_zero_le[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (0 <= move_matrix A j i) = ((0::('a::{order,zero}) matrix) <= A)"
|
|
|
695 |
apply (auto simp add: le_matrix_def neg_def)
|
|
|
696 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
|
|
|
697 |
apply (auto)
|
|
|
698 |
done
|
|
|
699 |
|
|
|
700 |
lemma move_matrix_le_move_matrix_iff[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= move_matrix B j i) = (A <= (B::('a::{order,zero}) matrix))"
|
|
|
701 |
apply (auto simp add: le_matrix_def neg_def)
|
|
|
702 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
|
|
|
703 |
apply (auto)
|
|
|
704 |
done
|
|
|
705 |
|
|
|
706 |
lemma le_spvec_empty2_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec (b,[]) = (sparse_row_vector b <= 0))"
|
|
|
707 |
apply (induct b)
|
|
|
708 |
apply (simp_all add: sorted_spvec_cons1)
|
|
|
709 |
apply (intro strip)
|
|
|
710 |
apply (subst disj_matrices_add_le_zero)
|
|
|
711 |
apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1)
|
|
|
712 |
apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl])
|
|
|
713 |
apply (simp_all)
|
|
|
714 |
done
|
|
|
715 |
|
|
|
716 |
lemma le_spvec_empty1_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec ([],b) = (0 <= sparse_row_vector b))"
|
|
|
717 |
apply (induct b)
|
|
|
718 |
apply (simp_all add: sorted_spvec_cons1)
|
|
|
719 |
apply (intro strip)
|
|
|
720 |
apply (subst disj_matrices_add_zero_le)
|
|
|
721 |
apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1)
|
|
|
722 |
apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl])
|
|
|
723 |
apply (simp_all)
|
|
|
724 |
done
|
|
|
725 |
|
|
|
726 |
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>
|
|
|
727 |
le_spmat(A, B) = (sparse_row_matrix A <= sparse_row_matrix B)"
|
|
|
728 |
apply (rule le_spmat.induct)
|
|
|
729 |
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]
|
|
|
730 |
disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+
|
|
|
731 |
apply (rule conjI, intro strip)
|
|
|
732 |
apply (simp add: sorted_spvec_cons1)
|
|
|
733 |
apply (subst disj_matrices_add_x_le)
|
|
|
734 |
apply (rule disj_matrices_add_x)
|
|
|
735 |
apply (simp add: disj_move_sparse_row_vector_twice)
|
|
|
736 |
apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
|
|
|
737 |
apply (simp add: disj_move_sparse_vec_mat[OF order_refl] disj_matrices_commute)
|
|
|
738 |
apply (simp, blast)
|
|
|
739 |
apply (intro strip, rule conjI, intro strip)
|
|
|
740 |
apply (simp add: sorted_spvec_cons1)
|
|
|
741 |
apply (subst disj_matrices_add_le_x)
|
|
|
742 |
apply (simp add: disj_move_sparse_vec_mat[OF order_refl])
|
|
|
743 |
apply (rule disj_matrices_x_add)
|
|
|
744 |
apply (simp add: disj_move_sparse_row_vector_twice)
|
|
|
745 |
apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
|
|
|
746 |
apply (simp, blast)
|
|
|
747 |
apply (intro strip)
|
|
|
748 |
apply (case_tac "a=aa")
|
|
|
749 |
apply (simp_all)
|
|
|
750 |
apply (subst disj_matrices_add)
|
|
|
751 |
apply (simp_all add: disj_matrices_commute disj_move_sparse_vec_mat[OF order_refl])
|
|
|
752 |
apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le)
|
|
|
753 |
done
|
|
|
754 |
|
|
|
755 |
term smult_spvec
|
|
|
756 |
term addmult_spvec
|
|
|
757 |
term add_spvec
|
|
|
758 |
term mult_spvec_spmat
|
|
|
759 |
term mult_spmat
|
|
|
760 |
term add_spmat
|
|
|
761 |
term le_spvec
|
|
|
762 |
term le_spmat
|
|
|
763 |
term sorted_spvec
|
|
|
764 |
term sorted_spmat
|
|
|
765 |
|
|
|
766 |
thm sparse_row_mult_spmat
|
|
|
767 |
thm sparse_row_add_spmat
|
|
|
768 |
thm le_spmat_iff_sparse_row_le
|
|
|
769 |
|
|
|
770 |
thm sorted_spvec_mult_spmat
|
|
|
771 |
thm sorted_spmat_mult_spmat
|
|
|
772 |
thm sorted_spvec_add_spmat
|
|
|
773 |
thm sorted_spmat_add_spmat
|
|
|
774 |
|
|
|
775 |
thm smult_spvec_empty
|
|
|
776 |
thm smult_spvec_cons
|
|
|
777 |
thm addmult_spvec.simps
|
|
|
778 |
thm add_spvec.simps
|
|
|
779 |
thm add_spmat.simps
|
|
|
780 |
thm mult_spvec_spmat.simps
|
|
|
781 |
thm mult_spmat.simps
|
|
|
782 |
thm le_spvec.simps
|
|
|
783 |
thm le_spmat.simps
|
|
|
784 |
thm sorted_spvec.simps
|
|
|
785 |
thm sorted_spmat.simps
|
|
|
786 |
|
|
|
787 |
end
|
|
|
788 |
|
|
|
789 |
|
|
|
790 |
|