| 16 | * @return The number of nonzero elements in the matrix |
| 17 | */ |
| 18 | export function csFkeep (a, callback, other) { |
| 19 | // a arrays |
| 20 | const avalues = a._values |
| 21 | const aindex = a._index |
| 22 | const aptr = a._ptr |
| 23 | const asize = a._size |
| 24 | // columns |
| 25 | const n = asize[1] |
| 26 | // nonzero items |
| 27 | let nz = 0 |
| 28 | // loop columns |
| 29 | for (let j = 0; j < n; j++) { |
| 30 | // get current location of col j |
| 31 | let p = aptr[j] |
| 32 | // record new location of col j |
| 33 | aptr[j] = nz |
| 34 | for (; p < aptr[j + 1]; p++) { |
| 35 | // check we need to keep this item |
| 36 | if (callback(aindex[p], j, avalues ? avalues[p] : 1, other)) { |
| 37 | // keep A(i,j) |
| 38 | aindex[nz] = aindex[p] |
| 39 | // check we need to process values (pattern only) |
| 40 | if (avalues) { avalues[nz] = avalues[p] } |
| 41 | // increment nonzero items |
| 42 | nz++ |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | // finalize A |
| 47 | aptr[n] = nz |
| 48 | // trim arrays |
| 49 | aindex.splice(nz, aindex.length - nz) |
| 50 | // check we need to process values (pattern only) |
| 51 | if (avalues) { avalues.splice(nz, avalues.length - nz) } |
| 52 | // return number of nonzero items |
| 53 | return nz |
| 54 | } |