Skip to content

Commit c050090

Browse files
committed
refactor: remove mul method from SparseMatrix class and update mulM method
1 parent 77eff3a commit c050090

File tree

4 files changed

+14
-219
lines changed

4 files changed

+14
-219
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"devDependencies": {
3939
"@types/node": "^22.15.21",
4040
"@vitest/coverage-v8": "^3.1.4",
41+
"benchmark": "^2.1.4",
4142
"eslint": "^9.27.0",
4243
"eslint-config-cheminfo-typescript": "^18.0.1",
4344
"ml-matrix": "^6.12.1",

src/__tests__/getNonZeros.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { SparseMatrix } from '../index';
1+
import { describe, it, expect } from 'vitest';
2+
import { SparseMatrix } from '../index.js';
23

34
describe('Sparse Matrix', () => {
45
it('getNonZeros', () => {

src/__tests__/matrix.test.js

Lines changed: 0 additions & 198 deletions
This file was deleted.

src/index.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,6 @@ export class SparseMatrix {
156156
return this;
157157
}
158158

159-
mul(other) {
160-
if (typeof other !== 'number') {
161-
throw new RangeError('the argument should be a number');
162-
}
163-
164-
if (other === 0) {
165-
return new SparseMatrix(this.rows, this.columns);
166-
}
167-
168-
this.withEachNonZero((i, j, v) => {
169-
this.set(i, j, v * other);
170-
});
171-
172-
return this;
173-
}
174-
175159
mmul(other) {
176160
if (this.columns !== other.rows) {
177161
// eslint-disable-next-line no-console
@@ -510,10 +494,18 @@ export class SparseMatrix {
510494
* @returns {this}
511495
*/
512496
mulM(matrix) {
513-
matrix.forEachNonZero((i, j, v) => {
514-
this.set(i, j, this.get(i, j) * v);
515-
return v;
497+
if (typeof matrix !== 'number') {
498+
throw new RangeError('the argument should be a number');
499+
}
500+
501+
if (matrix === 0) {
502+
this.elements = new HashTable();
503+
}
504+
505+
this.withEachNonZero((i, j, v) => {
506+
this.set(i, j, v * matrix);
516507
});
508+
517509
return this;
518510
}
519511

@@ -1503,7 +1495,6 @@ function csrToCsc(csrMatrix, numCols) {
15031495

15041496
function cooToCsr(cooMatrix, nbRows = 9) {
15051497
const { values, columns, rows } = cooMatrix;
1506-
//could not be the same length
15071498
const csrRowPtr = new Float64Array(nbRows + 1);
15081499
const length = values.length;
15091500
let currentRow = rows[0];

0 commit comments

Comments
 (0)