mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
function factory(type, config, load, typed) {
|
|
|
|
/**
|
|
* Copies a sparse vector into a dense one.
|
|
*
|
|
* @param {Matrix} a The source sparse matrix
|
|
* @param {Number} j The columns in the sparse matrix to copy the values from
|
|
* @param {Array} w Array with marks indicating the column j has been copied for a given row
|
|
* @param {Array} x The dense vector of values
|
|
* @param {Number} mark The value column j must have in w array
|
|
* @param {Matrix} c The target sparce matrix
|
|
*
|
|
* @return {Number} The nonzero elements in matrix c
|
|
*/
|
|
var sparseScatter = function (a, j, w, x, mark, c) {
|
|
// a arrays
|
|
var avalues = a._values;
|
|
var aindex = a._index;
|
|
var aptr = a._ptr;
|
|
// c arrays
|
|
var cindex = c._index;
|
|
|
|
// values in j
|
|
for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
|
|
// index
|
|
var i = aindex[k];
|
|
// check value exists in current j
|
|
if (!w[i] || w[i] < mark) {
|
|
// i is new entry in j
|
|
w[i] = mark;
|
|
// add i to pattern of C
|
|
cindex.push(i);
|
|
// x(i) = A
|
|
if (x)
|
|
x[i] = avalues[k];
|
|
}
|
|
else if (x) {
|
|
// i exists in C already
|
|
x[i] = add(x[i], avalues[k]);
|
|
}
|
|
}
|
|
// number of nonzero elements in C
|
|
return cindex.length;
|
|
};
|
|
|
|
return sparseScatter;
|
|
}
|
|
|
|
exports.name = 'sparseScatter';
|
|
exports.factory = factory;
|