You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Matrix multiplication, does not modify the current instance.
160
162
* @param {SparseMatrix} other
161
-
* @returns {SparseMatrix}
163
+
* @returns {SparseMatrix} returns a new matrix instance.
164
+
* @throws {Error} If the number of columns of this matrix does not match the number of rows of the other matrix.
162
165
*/
163
166
mmul(other){
164
167
if(this.columns!==other.rows){
165
-
// eslint-disable-next-line no-console
166
-
console.warn(
168
+
thrownewRangeError(
167
169
'Number of columns of left matrix are not equal to number of rows of right matrix.',
168
170
);
169
171
}
@@ -177,6 +179,13 @@ export class SparseMatrix {
177
179
returnthis._mmulMediumDensity(other);
178
180
}
179
181
182
+
/**
183
+
* Matrix multiplication optimized for very small matrices (both cardinalities < 42).
184
+
*
185
+
* @private
186
+
* @param {SparseMatrix} other - The right-hand side matrix to multiply with.
187
+
* @returns {SparseMatrix} - The resulting matrix after multiplication.
188
+
*/
180
189
_mmulSmall(other){
181
190
constm=this.rows;
182
191
constp=other.columns;
@@ -199,6 +208,13 @@ export class SparseMatrix {
199
208
returnresult;
200
209
}
201
210
211
+
/**
212
+
* Matrix multiplication optimized for low-density right-hand side matrices (other.rows > 100 and other.cardinality < 100).
213
+
*
214
+
* @private
215
+
* @param {SparseMatrix} other - The right-hand side matrix to multiply with.
216
+
* @returns {SparseMatrix} - The resulting matrix after multiplication.
217
+
*/
202
218
_mmulLowDensity(other){
203
219
constm=this.rows;
204
220
constp=other.columns;
@@ -230,20 +246,27 @@ export class SparseMatrix {
230
246
returnresult;
231
247
}
232
248
249
+
/**
250
+
* Matrix multiplication for medium-density matrices using CSR format for the right-hand side.
251
+
*
252
+
* @private
253
+
* @param {SparseMatrix} other - The right-hand side matrix to multiply with.
254
+
* @returns {SparseMatrix} - The resulting matrix after multiplication.
255
+
*/
233
256
_mmulMediumDensity(other){
234
-
constm=this.rows;
235
-
constp=other.columns;
236
-
const{
237
-
columns: otherCols,
238
-
rows: otherRows,
239
-
values: otherValues,
240
-
}=other.getNonZeros({format: 'csr'});
241
257
const{
242
258
columns: thisCols,
243
259
rows: thisRows,
244
260
values: thisValues,
245
261
}=this.getNonZeros();
262
+
const{
263
+
columns: otherCols,
264
+
rows: otherRows,
265
+
values: otherValues,
266
+
}=other.getNonZeros({csr: true});
246
267
268
+
constm=this.rows;
269
+
constp=other.columns;
247
270
constresult=newSparseMatrix(m,p);
248
271
constnbThisActive=thisCols.length;
249
272
for(lett=0;t<nbThisActive;t++){
@@ -358,32 +381,41 @@ export class SparseMatrix {
358
381
}
359
382
360
383
/**
361
-
* Returns the non-zero elements of the matrix in coordinate (COO), CSR, or CSC format.
384
+
* Returns the non-zero elements of the matrix in coordinates (COO) or CSR format.
385
+
*
386
+
* **COO (Coordinate) format:**
387
+
* Stores the non-zero elements as three arrays: `rows`, `columns`, and `values`, where each index corresponds to a non-zero entry at (row, column) with the given value.
388
+
*
389
+
* **CSR (Compressed Sparse Row) format:**
390
+
* Stores the matrix using three arrays:
391
+
* - `rows`: Row pointer array of length `numRows + 1`, where each entry indicates the start of a row in the `columns` and `values` arrays.
392
+
* - `columns`: Column indices of non-zero elements.
393
+
* - `values`: Non-zero values.
394
+
* This format is efficient for row slicing and matrix-vector multiplication.
362
395
*
363
396
* @param {Object} [options={}] - Options for output format and sorting.
364
-
* @param {boolean} [options.format] - If specified, returns the result in CSR or CSC format. Otherwise, returns COO format.
397
+
* @param {boolean} [options.csr] - If true, returns the result in CSR format. Otherwise, returns COO format.
365
398
* @param {boolean} [options.sort] - If true, sorts the non-zero elements by their indices.
366
-
* @returns {Object} If no format is specified, returns an object with Float64Array `rows`, `columns`, and `values` (COO format).
367
-
* If format is true, returns { rows, columns, values } in CSR format.
368
-
* @throws {Error} If an unsupported format is specified.
399
+
* @returns {Object} If `csr` is not specified, returns an object with Float64Array `rows`, `columns`, and `values` (COO format).
400
+
* If `csr` is true, returns `{ rows, columns, values }` in CSR format.
0 commit comments