csFkeep.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.csFkeep = csFkeep;
  6. /**
  7. * Keeps entries in the matrix when the callback function returns true, removes the entry otherwise
  8. *
  9. * @param {Matrix} a The sparse matrix
  10. * @param {function} callback The callback function, function will be invoked with the following args:
  11. * - The entry row
  12. * - The entry column
  13. * - The entry value
  14. * - The state parameter
  15. * @param {any} other The state
  16. *
  17. * @return The number of nonzero elements in the matrix
  18. *
  19. * Reference: http://faculty.cse.tamu.edu/davis/publications.html
  20. */
  21. function csFkeep(a, callback, other) {
  22. // a arrays
  23. var avalues = a._values;
  24. var aindex = a._index;
  25. var aptr = a._ptr;
  26. var asize = a._size;
  27. // columns
  28. var n = asize[1];
  29. // nonzero items
  30. var nz = 0;
  31. // loop columns
  32. for (var j = 0; j < n; j++) {
  33. // get current location of col j
  34. var p = aptr[j];
  35. // record new location of col j
  36. aptr[j] = nz;
  37. for (; p < aptr[j + 1]; p++) {
  38. // check we need to keep this item
  39. if (callback(aindex[p], j, avalues ? avalues[p] : 1, other)) {
  40. // keep A(i,j)
  41. aindex[nz] = aindex[p];
  42. // check we need to process values (pattern only)
  43. if (avalues) {
  44. avalues[nz] = avalues[p];
  45. }
  46. // increment nonzero items
  47. nz++;
  48. }
  49. }
  50. }
  51. // finalize A
  52. aptr[n] = nz;
  53. // trim arrays
  54. aindex.splice(nz, aindex.length - nz);
  55. // check we need to process values (pattern only)
  56. if (avalues) {
  57. avalues.splice(nz, avalues.length - nz);
  58. }
  59. // return number of nonzero items
  60. return nz;
  61. }