Skip to content

Commit f2e2935

Browse files
committed
feat: update benchmark parameters and add comprehensive tests for SparseMatrix operations
1 parent 7ac8a4c commit f2e2935

File tree

4 files changed

+160
-88
lines changed

4 files changed

+160
-88
lines changed

benchmark/benchmark.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ function printWinner(label1, avg1, label2, avg2) {
5454

5555
function runBenchmarks() {
5656
const m = 128;
57-
const n = 64;
57+
const n = 128;
5858
const p = 128;
59-
const densityA = 0.03;
60-
const densityB = 0.03;
59+
const densityA = 0.01;
60+
const densityB = 0.01;
61+
62+
const nbIterations = 3;
6163
const A = randomSparseMatrix(m, n, densityA);
6264
const B = randomSparseMatrix(n, p, densityB);
6365

@@ -66,10 +68,11 @@ function runBenchmarks() {
6668

6769
const AOld = new SparseMatrixOld(denseA);
6870
const BOld = new SparseMatrixOld(denseB);
69-
A.cardinality;
71+
7072
denseA = new Matrix(denseA);
7173
denseB = new Matrix(denseB);
7274

75+
denseA.mmul(denseB);
7376
// 1. add vs addNew
7477
// const addAvg = benchmark(() => {
7578
// const a = AOld.clone();
@@ -90,23 +93,23 @@ function runBenchmarks() {
9093
A.mmul(B);
9194
},
9295
'mmulNew',
93-
3,
96+
nbIterations,
9497
);
9598

9699
const mmulAvg = benchmark(
97100
() => {
98101
AOld.mmul(BOld);
99102
},
100103
'mmul',
101-
3,
104+
nbIterations,
102105
);
103106

104107
const denseAvg = benchmark(
105108
() => {
106109
denseA.mmul(denseB);
107110
},
108111
'denseMatrix',
109-
3,
112+
nbIterations,
110113
);
111114

112115
printWinner('mmul', mmulAvg, 'mmulNew', mmulNewAvg);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"compile": "rollup -c",
1313
"eslint": "eslint src",
1414
"eslint-fix": "npm run eslint -- --fix",
15-
"prepack": "npm run compile",
15+
"prepack": "npm run compile",
1616
"prettier": "prettier --check src",
1717
"prettier-write": "prettier --write src",
1818
"test": "npm run test-only && npm run eslint && npm run prettier",
@@ -37,6 +37,7 @@
3737
"eslint": "^8.10.0",
3838
"eslint-config-cheminfo": "^7.2.2",
3939
"jest": "^27.5.1",
40+
"ml-matrix": "^6.12.1",
4041
"prettier": "^2.5.1",
4142
"rollup": "^2.69.0"
4243
}

src/__tests__/test.js renamed to src/__tests__/matrix.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ describe('Sparse Matrix', () => {
4040
[0, 2],
4141
[0, 0],
4242
]);
43+
44+
// Compare with dense multiplication
45+
const denseM1 = m1.to2DArray();
46+
const denseM2 = m2.to2DArray();
47+
const expectedDense = denseMatrixMultiply(denseM1, denseM2);
48+
expect(m3.to2DArray()).toStrictEqual(expectedDense);
49+
});
50+
51+
it('mmul', () => {
52+
const size = 32;
53+
const density = 0.1;
54+
const m1 = randomSparseMatrix(size, size, density);
55+
const m2 = randomSparseMatrix(size, size, density);
56+
let m3 = m1.mmul(m2);
57+
58+
const denseM1 = m1.to2DArray();
59+
const denseM2 = m2.to2DArray();
60+
61+
const newSparse = new SparseMatrix(denseM1);
62+
expect(newSparse.to2DArray()).toStrictEqual(denseM1);
63+
const expectedDense = denseMatrixMultiply(denseM1, denseM2);
64+
65+
expect(m3.to2DArray()).toStrictEqual(expectedDense);
4366
});
4467

4568
it('kronecker', () => {
@@ -142,3 +165,32 @@ describe('Banded matrices', () => {
142165
expect(matrix4.isBanded(1)).toBe(true);
143166
});
144167
});
168+
169+
function denseMatrixMultiply(A, B) {
170+
const rowsA = A.length;
171+
const colsA = A[0].length;
172+
const colsB = B[0].length;
173+
const result = Array.from({ length: rowsA }, () => Array(colsB).fill(0));
174+
for (let i = 0; i < rowsA; i++) {
175+
for (let j = 0; j < colsB; j++) {
176+
for (let k = 0; k < colsA; k++) {
177+
result[i][j] += A[i][k] * B[k][j];
178+
}
179+
}
180+
}
181+
return result;
182+
}
183+
184+
function randomSparseMatrix(rows, cols, density = 0.01) {
185+
const matrix = [];
186+
for (let i = 0; i < rows; i++) {
187+
const row = new Float64Array(cols);
188+
for (let j = 0; j < cols; j++) {
189+
if (Math.random() < density) {
190+
row[j] = Math.random() * 10;
191+
}
192+
}
193+
matrix.push(row);
194+
}
195+
return new SparseMatrix(matrix);
196+
}

0 commit comments

Comments
 (0)