UNPKG

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

150 lines (140 loc) • 4.34 kB
import { factory } from '../../../utils/factory.js'; import { DimensionError } from '../../../error/DimensionError.js'; var name = 'matAlgo09xS0Sf'; var dependencies = ['typed']; export var createMatAlgo09xS0Sf = /* #__PURE__ */factory(name, dependencies, _ref => { var typed = _ref.typed; /** * Iterates over SparseMatrix A and invokes the callback function f(Aij, Bij). * Callback function invoked NZA times, number of nonzero elements in A. * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ return function matAlgo09xS0Sf(a, b, callback) { // sparse matrix arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype || a._data === undefined ? a._datatype : a.getDataType(); // sparse matrix arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype || b._data === undefined ? b._datatype : b.getDataType(); // validate dimensions if (asize.length !== bsize.length) { throw new 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 + ')'); } // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // isZero signature to use var isZ = a.isZero_Full; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt && adt !== 'mixed') { dt = adt; // OK, need a zero of that type if (adt === a._datatype) { zero = a.zero; } else if (adt === b._datatype) { zero = b.zero; } else { zero = typed.convert(0, adt); } } // try to find an isZero signature: if (dt === a._datatype) { isZ = a.isZero; } else if (dt === b._datatype) { isZ = b.isZero; } // try to pin down a callback signature: if (typeof adt === 'string' && adt !== 'mixed' && typeof bdt === 'string' && bdt !== 'mixed') { cf = typed.find(callback, [adt, bdt]); } // result arrays var cvalues = avalues && bvalues ? [] : undefined; var cindex = []; var cptr = []; // workspaces var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column var w = []; // vars var i, j, k, k0, k1; // loop columns for (j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // column mark var mark = j + 1; // check we need to process values if (x) { // loop B(:,j) for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row i = bindex[k]; // update workspace w[i] = mark; x[i] = bvalues[k]; } } // loop A(:,j) for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // check we need to process values if (x) { // b value @ i,j var vb = w[i] === mark ? x[i] : zero; // invoke f var vc = cf(avalues[k], vb); // check zero value if (!isZ(vc)) { // push index cindex.push(i); // push value cvalues.push(vc); } } else { // push index cindex.push(i); } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return a.createSparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: adt === a._datatype && bdt === b._datatype ? dt : undefined }); }; });