mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
118 lines (108 loc) • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createMatAlgo02xDS0 = void 0;
var _factory = require("../../../utils/factory.js");
var _DimensionError = require("../../../error/DimensionError.js");
const name = 'matAlgo02xDS0';
const dependencies = ['typed'];
const createMatAlgo02xDS0 = exports.createMatAlgo02xDS0 = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
let typed = _ref.typed;
/**
* Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij).
* Callback function invoked NNZ times (number of nonzero items in SparseMatrix).
*
*
* ┌ f(Dij, Sij) ; S(i,j) !== 0
* C(i,j) = ┤
* └ 0 ; otherwise
*
*
* @param {Matrix} denseMatrix The DenseMatrix instance (D)
* @param {Matrix} sparseMatrix The SparseMatrix instance (S)
* @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j)
* @param {boolean} flip A true value indicates callback should be invoked f(Sij,Dij)
*
* @return {Matrix} SparseMatrix (C)
*
* see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571
*/
return function matAlgo02xDS0(denseMatrix, sparseMatrix, callback, flip) {
// dense matrix arrays
const adata = denseMatrix._data;
const asize = denseMatrix._size;
const adt = denseMatrix._datatype || denseMatrix.getDataType();
// sparse matrix arrays
const bvalues = sparseMatrix._values;
const bindex = sparseMatrix._index;
const bptr = sparseMatrix._ptr;
const bsize = sparseMatrix._size;
const bdt = sparseMatrix._datatype || sparseMatrix._data === undefined ? sparseMatrix._datatype : sparseMatrix.getDataType();
// validate dimensions
if (asize.length !== bsize.length) {
throw new _DimensionError.DimensionError(asize.length, bsize.length);
}
// check rows & columns
if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) {
throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')');
}
// sparse matrix cannot be a Pattern matrix
if (!bvalues) {
throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix');
}
// rows & columns
const rows = asize[0];
const columns = asize[1];
// datatype
let dt;
// isZero signature to use
let isZ = sparseMatrix.isZero_Full;
// callback signature to use
let cf = callback;
// process data types
if (typeof adt === 'string' && adt === bdt && adt !== 'mixed') {
dt = adt;
}
// find a callback signature if possible
if (typeof adt === 'string' && adt !== 'mixed' && typeof bdt === 'string' && bdt !== 'mixed') {
if (flip) cf = typed.find(callback, [bdt, adt]);else cf = typed.find(callback, [adt, bdt]);
}
// find an isZero signature if possible
if (dt && dt === sparseMatrix._datatype) {
isZ = sparseMatrix.isZero;
}
// result (SparseMatrix)
const cvalues = [];
const cindex = [];
const cptr = [];
// loop columns in b
for (let j = 0; j < columns; j++) {
// update cptr
cptr[j] = cindex.length;
// values in column j
for (let k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) {
// row
const i = bindex[k];
// update C(i,j)
const cij = flip ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]);
// check for nonzero
if (!isZ(cij)) {
// push i & v
cindex.push(i);
cvalues.push(cij);
}
}
}
// update cptr
cptr[columns] = cindex.length;
// return sparse matrix
return sparseMatrix.createSparseMatrix({
values: cvalues,
index: cindex,
ptr: cptr,
size: [rows, columns],
datatype: adt === denseMatrix._datatype && bdt === sparseMatrix._datatype ? dt : undefined
});
};
});