UNPKG

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