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

122 lines (111 loc) • 3.72 kB
import { factory } from '../../../utils/factory.js'; import { createMatAlgo11xS0s } from './matAlgo11xS0s.js'; var name = 'matAlgo12xSfs'; var dependencies = ['typed', 'DenseMatrix']; export var createMatAlgo12xSfs = /* #__PURE__ */factory(name, dependencies, _ref => { var typed = _ref.typed, DenseMatrix = _ref.DenseMatrix; var matAlgo11xS0s = createMatAlgo11xS0s({ typed }); /** * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). * The value f(0, b) for the zero items of S is calculated once. When that value is zero, * the result is sparse and the operation is handed over to matAlgo11xS0s. A boolean result * is always dense, since a SparseMatrix would turn false into 0. * Callback function invoked NZ + 1 times (number of nonzero items in S). * * * ┌ f(Sij, b) ; S(i,j) !== 0 * C(i,j) = ┤ * └ f(0, b) ; otherwise * * * @param {Matrix} s The SparseMatrix instance (S) * @param {Scalar} b The Scalar value * @param {Function} callback The f(Aij,b) operation to invoke * @param {boolean} flip A true value indicates callback should be invoked f(b,Sij) * * @return {Matrix} DenseMatrix (C), or SparseMatrix (C) when f(0, b) is zero * * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 */ return function matAlgo12xSfs(s, b, callback, flip) { // sparse matrix arrays var avalues = s._values; var aindex = s._index; var aptr = s._ptr; var asize = s._size; var adt = s._datatype; // sparse matrix cannot be a Pattern matrix if (!avalues) { throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); } // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string') { // datatype dt = adt; // callback if (flip) { cf = typed.resolve(callback, [b, s.zero]).implementation; } else { cf = typed.resolve(callback, [s.zero, b]).implementation; } } // the value for all zero items of S var fzero = flip ? cf(b, s.zero) : cf(s.zero, b); // the zero items stay zero, so the result is sparse. A boolean result // stays dense, since false would become 0 in a sparse matrix if (typeof fzero !== 'boolean' && s.isZero(fzero)) { return matAlgo11xS0s(s, b, callback, flip); } // result arrays var cdata = []; // workspaces var x = []; // marks indicating we have a value in x for a given column var w = []; // loop columns for (var j = 0; j < columns; j++) { // columns mark var mark = j + 1; // values in j for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row var r = aindex[k]; // update workspace x[r] = avalues[k]; w[r] = mark; } // loop rows for (var i = 0; i < rows; i++) { // initialize C on first column if (j === 0) { // create row array cdata[i] = []; } // check sparse matrix has a value @ i,j if (w[i] === mark) { // invoke callback, update C cdata[i][j] = flip ? cf(b, x[i]) : cf(x[i], b); } else { // dense matrix value @ i, j cdata[i][j] = fzero; } } } // return dense matrix return new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); }; });