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