304 definition "sparsify xs = [i \<leftarrow> zip [0..<length xs] xs. snd i \<noteq> 0]" |
304 definition "sparsify xs = [i \<leftarrow> zip [0..<length xs] xs. snd i \<noteq> 0]" |
305 (* |
305 (* |
306 lemma sparsify_length: "(i, x) \<in> set (sparsify xs) \<Longrightarrow> i < length xs" |
306 lemma sparsify_length: "(i, x) \<in> set (sparsify xs) \<Longrightarrow> i < length xs" |
307 by (auto simp: sparsify_def set_zip) |
307 by (auto simp: sparsify_def set_zip) |
308 |
308 |
309 lemma listsum_sparsify[simp]: |
309 lemma sum_list_sparsify[simp]: |
310 fixes v :: "('a :: semiring_0) list" |
310 fixes v :: "('a :: semiring_0) list" |
311 assumes "length w = length v" |
311 assumes "length w = length v" |
312 shows "(\<Sum>x\<leftarrow>sparsify w. (\<lambda>(i, x). v ! i) x * snd x) = scalar_product v w" |
312 shows "(\<Sum>x\<leftarrow>sparsify w. (\<lambda>(i, x). v ! i) x * snd x) = scalar_product v w" |
313 (is "(\<Sum>x\<leftarrow>_. ?f x) = _") |
313 (is "(\<Sum>x\<leftarrow>_. ?f x) = _") |
314 unfolding sparsify_def scalar_product_def |
314 unfolding sparsify_def scalar_product_def |
315 using assms listsum_map_filter[where f="?f" and P="\<lambda> i. snd i \<noteq> (0::'a)"] |
315 using assms sum_list_map_filter[where f="?f" and P="\<lambda> i. snd i \<noteq> (0::'a)"] |
316 by (simp add: listsum_setsum) |
316 by (simp add: sum_list_setsum) |
317 *) |
317 *) |
318 definition [simp]: "unzip w = (map fst w, map snd w)" |
318 definition [simp]: "unzip w = (map fst w, map snd w)" |
319 |
319 |
320 primrec insert :: "('a \<Rightarrow> 'b :: linorder) => 'a \<Rightarrow> 'a list => 'a list" where |
320 primrec insert :: "('a \<Rightarrow> 'b :: linorder) => 'a \<Rightarrow> 'a list => 'a list" where |
321 "insert f x [] = [x]" | |
321 "insert f x [] = [x]" | |
336 |
336 |
337 definition |
337 definition |
338 "jad = apsnd transpose o length_permutate o map sparsify" |
338 "jad = apsnd transpose o length_permutate o map sparsify" |
339 |
339 |
340 definition |
340 definition |
341 "jad_mv v = inflate o case_prod zip o apsnd (map listsum o transpose o map (map (\<lambda> (i, x). v ! i * x)))" |
341 "jad_mv v = inflate o case_prod zip o apsnd (map sum_list o transpose o map (map (\<lambda> (i, x). v ! i * x)))" |
342 |
342 |
343 lemma "matrix (M::int list list) rs cs \<Longrightarrow> False" |
343 lemma "matrix (M::int list list) rs cs \<Longrightarrow> False" |
344 quickcheck[tester = smart_exhaustive, size = 6] |
344 quickcheck[tester = smart_exhaustive, size = 6] |
345 oops |
345 oops |
346 |
346 |