From 16cdf663853bec31491fbdfc19c83a4220b637b3 Mon Sep 17 00:00:00 2001 From: Alexandre Alves <91118126+AlexandreAlvesDB@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:21:59 +0100 Subject: [PATCH] Prevent inserting zero values in SparseMatrix (#2836) * Prevent inserting zero values in SparseMatrix As discussed in #2830 * Formatting correction * Lint correction --- src/type/matrix/SparseMatrix.js | 6 ++++-- test/unit-tests/type/matrix/SparseMatrix.test.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/type/matrix/SparseMatrix.js b/src/type/matrix/SparseMatrix.js index f06ffdc8d..cc3717921 100644 --- a/src/type/matrix/SparseMatrix.js +++ b/src/type/matrix/SparseMatrix.js @@ -510,8 +510,10 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie _remove(k, j, this._values, this._index, this._ptr) } } else { - // insert value @ (i, j) - _insert(k, i, j, v, this._values, this._index, this._ptr) + if (!eq(v, zero)) { + // insert value @ (i, j) + _insert(k, i, j, v, this._values, this._index, this._ptr) + } } return this diff --git a/test/unit-tests/type/matrix/SparseMatrix.test.js b/test/unit-tests/type/matrix/SparseMatrix.test.js index cceb838dc..e365a343d 100644 --- a/test/unit-tests/type/matrix/SparseMatrix.test.js +++ b/test/unit-tests/type/matrix/SparseMatrix.test.js @@ -774,6 +774,17 @@ describe('SparseMatrix', function () { ]) }) + it('should not add matrix element (zero)', function () { + const m = new SparseMatrix([ + [0, 1], + [0, 0] + ]) + + m.set([0, 0], 0) + + assert.deepStrictEqual(m._values.length, 1) + }) + it('should update matrix element (non zero)', function () { const m = new SparseMatrix([ [10, 0, 0, 0, -2, 0],