sparse-example.k 505 B

12345678910111213141516171819202122232425262728293031
  1. \d sm
  2. \l sparse.k
  3. \d .
  4. smm:sm.mm / sparse matrix multiply
  5. sp:sm.m2sm / sparsify matrix (SCR format)
  6. sh:-1_#'*:\
  7. `prng@0
  8. mk:{(x#0)@[;;:;1]'1?/:#/x}
  9. / random pair of sparse matrices
  10. (ml;mr):mk' \ds:0N 2#(5+3?15)0 1 1 2
  11. / total number of elements in full matrix
  12. */+ds
  13. / sparse matrix multiply
  14. (w;v):smm.(sp'(ml;mr))
  15. / convert results into a sparse matrix
  16. sm.spf[v;w;1+*|/w]
  17. / full multiplication
  18. :mm:+/'ml*\:mr
  19. / shape of result matrix
  20. :d:(sh'(ml;mr))@'!2
  21. / regression test
  22. mm~d#@[&*/d;d/'w;:;v]