25 lemma sparse_row_matrix_empty [simp]: "sparse_row_matrix [] = 0" |
25 lemma sparse_row_matrix_empty [simp]: "sparse_row_matrix [] = 0" |
26 by (simp add: sparse_row_matrix_def) |
26 by (simp add: sparse_row_matrix_def) |
27 |
27 |
28 lemmas [code] = sparse_row_vector_empty [symmetric] |
28 lemmas [code] = sparse_row_vector_empty [symmetric] |
29 |
29 |
30 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))" |
30 lemma foldl_distrstart: "! a x y. (f (g x y) a = g x (f y a)) \<Longrightarrow> (foldl f (g x y) l = g x (foldl f y l))" |
31 by (induct l, auto) |
31 by (induct l arbitrary: x y, auto) |
32 |
32 |
33 lemma sparse_row_vector_cons[simp]: |
33 lemma sparse_row_vector_cons[simp]: |
34 "sparse_row_vector (a # arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)" |
34 "sparse_row_vector (a # arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)" |
35 apply (induct arr) |
35 apply (induct arr) |
36 apply (auto simp add: sparse_row_vector_def) |
36 apply (auto simp add: sparse_row_vector_def) |
83 |
83 |
84 lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b" |
84 lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b" |
85 apply (auto simp add: sorted_spvec.simps) |
85 apply (auto simp add: sorted_spvec.simps) |
86 done |
86 done |
87 |
87 |
88 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" |
88 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" |
89 apply (induct arr) |
89 apply (induct arr) |
90 apply (auto) |
90 apply (auto) |
91 apply (frule sorted_spvec_cons2,simp)+ |
91 apply (frule sorted_spvec_cons2,simp)+ |
92 apply (frule sorted_spvec_cons3, simp) |
92 apply (frule sorted_spvec_cons3, simp) |
93 done |
93 done |
94 |
94 |
95 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" |
95 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" |
96 apply (induct arr) |
96 apply (induct arr) |
97 apply (auto) |
97 apply (auto) |
98 apply (frule sorted_spvec_cons2, simp) |
98 apply (frule sorted_spvec_cons2, simp) |
99 apply (frule sorted_spvec_cons3, simp) |
99 apply (frule sorted_spvec_cons3, simp) |
100 apply (simp add: sparse_row_matrix_cons neg_def) |
100 apply (simp add: sparse_row_matrix_cons neg_def) |
186 apply (simp_all add: smult_spvec_cons scalar_mult_add) |
186 apply (simp_all add: smult_spvec_cons scalar_mult_add) |
187 done |
187 done |
188 |
188 |
189 lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring) a b) = |
189 lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring) a b) = |
190 (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))" |
190 (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))" |
191 apply (rule addmult_spvec.induct[of _ y]) |
191 apply (induct y a b rule: addmult_spvec.induct) |
192 apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+ |
192 apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+ |
193 done |
193 done |
194 |
194 |
195 lemma sorted_smult_spvec[rule_format]: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)" |
195 lemma sorted_smult_spvec: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)" |
196 apply (auto simp add: smult_spvec_def) |
196 apply (auto simp add: smult_spvec_def) |
197 apply (induct a) |
197 apply (induct a) |
198 apply (auto simp add: sorted_spvec.simps split:list.split_asm) |
198 apply (auto simp add: sorted_spvec.simps split:list.split_asm) |
199 done |
199 done |
200 |
200 |
216 \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec y arr brr))" |
216 \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec y arr brr))" |
217 apply (induct y arr brr rule: addmult_spvec.induct) |
217 apply (induct y arr brr rule: addmult_spvec.induct) |
218 apply (simp_all add: sorted_spvec.simps smult_spvec_def split:list.split) |
218 apply (simp_all add: sorted_spvec.simps smult_spvec_def split:list.split) |
219 done |
219 done |
220 |
220 |
221 lemma sorted_addmult_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (addmult_spvec y a b)" |
221 lemma sorted_addmult_spvec: "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]) |
222 apply (induct y a b rule: addmult_spvec.induct) |
223 apply (simp_all add: sorted_smult_spvec) |
223 apply (simp_all add: sorted_smult_spvec) |
224 apply (rule conjI, intro strip) |
224 apply (rule conjI, intro strip) |
225 apply (case_tac "~(i < j)") |
225 apply (case_tac "~(i < j)") |
226 apply (simp_all) |
226 apply (simp_all) |
227 apply (frule_tac as=brr in sorted_spvec_cons1) |
227 apply (frule_tac as=brr in sorted_spvec_cons1) |
337 done |
337 done |
338 qed |
338 qed |
339 |
339 |
340 lemma sorted_mult_spvec_spmat[rule_format]: |
340 lemma sorted_mult_spvec_spmat[rule_format]: |
341 "sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat c a B)" |
341 "sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat c a B)" |
342 apply (rule mult_spvec_spmat.induct[of _ c a B]) |
342 apply (induct c a B rule: mult_spvec_spmat.induct) |
343 apply (simp_all add: sorted_addmult_spvec) |
343 apply (simp_all add: sorted_addmult_spvec) |
344 done |
344 done |
345 |
345 |
346 consts |
346 consts |
347 mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat" |
347 mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat" |
348 |
348 |
349 primrec |
349 primrec |
350 "mult_spmat [] A = []" |
350 "mult_spmat [] A = []" |
351 "mult_spmat (a#as) A = (fst a, mult_spvec_spmat [] (snd a) A)#(mult_spmat as A)" |
351 "mult_spmat (a#as) A = (fst a, mult_spvec_spmat [] (snd a) A)#(mult_spmat as A)" |
352 |
352 |
353 lemma sparse_row_mult_spmat[rule_format]: |
353 lemma sparse_row_mult_spmat: |
354 "sorted_spmat A \<longrightarrow> sorted_spvec B \<longrightarrow> sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)" |
354 "sorted_spmat A \<Longrightarrow> sorted_spvec B \<Longrightarrow> |
|
355 sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)" |
355 apply (induct A) |
356 apply (induct A) |
356 apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat algebra_simps move_matrix_mult) |
357 apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat algebra_simps move_matrix_mult) |
357 done |
358 done |
358 |
359 |
359 lemma sorted_spvec_mult_spmat[rule_format]: |
360 lemma sorted_spvec_mult_spmat[rule_format]: |
403 |
404 |
404 lemma add_spmat_Nil2[simp]: "add_spmat as [] = as" |
405 lemma add_spmat_Nil2[simp]: "add_spmat as [] = as" |
405 by(cases as) auto |
406 by(cases as) auto |
406 |
407 |
407 lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat A B) = (sparse_row_matrix A) + (sparse_row_matrix B)" |
408 lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat A B) = (sparse_row_matrix A) + (sparse_row_matrix B)" |
408 apply (rule add_spmat.induct) |
409 apply (induct A B rule: add_spmat.induct) |
409 apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add) |
410 apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add) |
410 done |
411 done |
411 |
412 |
412 lemmas [code] = sparse_row_add_spmat [symmetric] |
413 lemmas [code] = sparse_row_add_spmat [symmetric] |
413 lemmas [code] = sparse_row_vector_add [symmetric] |
414 lemmas [code] = sparse_row_vector_add [symmetric] |
414 |
415 |
415 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)))" |
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)))" |
416 proof - |
417 proof - |
417 have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" |
418 have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" |
418 by (rule add_spvec.induct[of _ _ brr]) (auto split:if_splits) |
419 by (induct brr rule: add_spvec.induct) (auto split:if_splits) |
419 then show ?thesis |
420 then show ?thesis |
420 by (case_tac brr, auto) |
421 by (case_tac brr, auto) |
421 qed |
422 qed |
422 |
423 |
423 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)))" |
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)))" |
424 proof - |
425 proof - |
425 have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" |
426 have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))" |
426 by (rule add_spmat.induct[of _ _ brr], auto split:if_splits) |
427 by (rule add_spmat.induct) (auto split:if_splits) |
427 then show ?thesis |
428 then show ?thesis |
428 by (case_tac brr, auto) |
429 by (case_tac brr, auto) |
429 qed |
430 qed |
430 |
431 |
431 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)))" |
432 lemma sorted_add_spvec_helper: "add_spvec arr brr = (ab, bb) # list \<Longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))" |
432 apply (rule add_spvec.induct[of _ arr brr]) |
433 apply (induct arr brr rule: add_spvec.induct) |
433 apply (auto) |
434 apply (auto split:if_splits) |
434 done |
435 done |
435 |
436 |
436 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)))" |
437 lemma sorted_add_spmat_helper: "add_spmat arr brr = (ab, bb) # list \<Longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))" |
437 apply (rule add_spmat.induct[of _ arr brr]) |
438 apply (induct arr brr rule: add_spmat.induct) |
438 apply (auto) |
439 apply (auto split:if_splits) |
439 done |
440 done |
440 |
441 |
441 lemma add_spvec_commute: "add_spvec a b = add_spvec b a" |
442 lemma add_spvec_commute: "add_spvec a b = add_spvec b a" |
442 by (rule add_spvec.induct[of _ a b], auto) |
443 by (induct a b rule: add_spvec.induct) auto |
443 |
444 |
444 lemma add_spmat_commute: "add_spmat a b = add_spmat b a" |
445 lemma add_spmat_commute: "add_spmat a b = add_spmat b a" |
445 apply (rule add_spmat.induct[of _ a b]) |
446 apply (induct a b rule: add_spmat.induct) |
446 apply (simp_all add: add_spvec_commute) |
447 apply (simp_all add: add_spvec_commute) |
447 done |
448 done |
448 |
449 |
449 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" |
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" |
450 apply (drule sorted_add_spvec_helper1) |
451 apply (drule sorted_add_spvec_helper1) |
661 apply (auto simp add: neg_def disj_matrices_def) |
662 apply (auto simp add: neg_def disj_matrices_def) |
662 apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+ |
663 apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+ |
663 done |
664 done |
664 |
665 |
665 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)" |
666 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)" |
666 apply (rule le_spvec.induct) |
667 apply (induct a b rule: le_spvec.induct) |
667 apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le |
668 apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le |
668 disj_sparse_row_singleton[OF order_refl] disj_matrices_commute) |
669 disj_sparse_row_singleton[OF order_refl] disj_matrices_commute) |
669 apply (rule conjI, intro strip) |
670 apply (rule conjI, intro strip) |
670 apply (simp add: sorted_spvec_cons1) |
671 apply (simp add: sorted_spvec_cons1) |
671 apply (subst disj_matrices_add_x_le) |
672 apply (subst disj_matrices_add_x_le) |
700 apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1) |
701 apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1) |
701 done |
702 done |
702 |
703 |
703 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> |
704 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> |
704 le_spmat A B = (sparse_row_matrix A <= sparse_row_matrix B)" |
705 le_spmat A B = (sparse_row_matrix A <= sparse_row_matrix B)" |
705 apply (rule le_spmat.induct) |
706 apply (induct A B rule: le_spmat.induct) |
706 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] |
707 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] |
707 disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ |
708 disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ |
708 apply (rule conjI, intro strip) |
709 apply (rule conjI, intro strip) |
709 apply (simp add: sorted_spvec_cons1) |
710 apply (simp add: sorted_spvec_cons1) |
710 apply (subst disj_matrices_add_x_le) |
711 apply (subst disj_matrices_add_x_le) |
729 apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le) |
730 apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le) |
730 done |
731 done |
731 |
732 |
732 declare [[simp_depth_limit = 999]] |
733 declare [[simp_depth_limit = 999]] |
733 |
734 |
734 consts |
735 primrec abs_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat" where |
735 abs_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat" |
736 "abs_spmat [] = []" | |
736 minus_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat" |
|
737 |
|
738 primrec |
|
739 "abs_spmat [] = []" |
|
740 "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)" |
737 "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)" |
741 |
738 |
742 primrec |
739 primrec minus_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat" where |
743 "minus_spmat [] = []" |
740 "minus_spmat [] = []" | |
744 "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)" |
741 "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)" |
745 |
742 |
746 lemma sparse_row_matrix_minus: |
743 lemma sparse_row_matrix_minus: |
747 "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)" |
744 "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)" |
748 apply (induct A) |
745 apply (induct A) |