sparse.k 687 B

123456789101112131415
  1. spf:{(,/'(x;.y[1]@d)),,+\0,@[&z;!d;:;]@#'x:x@d:=*y:+y} / sparsify (CSR)
  2. nnz:{(((,/x)@&,/;+&:)@\:~~x),,#x} / non-zero entries and row count
  3. m2sm:{spf.(nnz@x)} / matrix to sparse matrix
  4. rws:{@[i;(-1_x[2])_i:!#x[0];:;!-1+#x[2]]} / rows
  5. ci:{p@\:{x^x^y}/!'p:='(x;y)} / common indices
  6. rc:{(rws;@[;1])@\:x} / rows/columns
  7. cx:+,/,/,/:\:'/ / match elements
  8. enc:{b:1+|/'x;(b;b/x)} / encode
  9. mm:{(l;r):`row`col!/:rc'(x;y)
  10. j:*|e:enc@cx(l`row;r`col)@'w:ci[l`col;r`row]
  11. c:*/cx((x;y)@\:0)@'w
  12. (e[0]\'w;v@w:&~~:v:@[;;+;]/[&1+|/j;j;c])}