Prevent inserting zero values in SparseMatrix (#2836)

* Prevent inserting zero values in SparseMatrix

As discussed in #2830

* Formatting correction

* Lint correction
This commit is contained in:
Alexandre Alves 2022-11-07 10:21:59 +01:00 committed by GitHub
parent 19ac459fbe
commit 16cdf66385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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

View File

@ -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],