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
feat: add fast multiplication algorithm (strassen)
* add an implementation of matrix product in the benchmark (strassen's algorithm)
* add the function mmul_strassen to the class abstractMatrix.
* Modification of the benchmark of mmul : use integer instead of float between 0 and 1.
* Add a test of mmul_strassen
Copy file name to clipboardExpand all lines: src/abstractMatrix.js
+128-1Lines changed: 128 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -122,6 +122,25 @@ function abstractMatrix(superCtor) {
122
122
returnmatrix;
123
123
}
124
124
125
+
/**
126
+
* Creates a matrix with the given dimensions. Values will be randomly set.
127
+
* @param {number} rows - Number of rows
128
+
* @param {number} columns - Number of columns
129
+
* @param {function} [rng] - Random number generator (default: Math.random)
130
+
* @returns {Matrix} The new matrix
131
+
*/
132
+
staticrandInt(rows,columns,rng){
133
+
if(rng===undefined)rng=Math.random;
134
+
varmatrix=this.empty(rows,columns);
135
+
for(vari=0;i<rows;i++){
136
+
for(varj=0;j<columns;j++){
137
+
varvalue=parseInt(rng()*1000);
138
+
matrix.set(i,j,value);
139
+
}
140
+
}
141
+
returnmatrix;
142
+
}
143
+
125
144
/**
126
145
* Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0.
127
146
* @param {number} rows - Number of rows
@@ -958,6 +977,114 @@ function abstractMatrix(superCtor) {
958
977
returnresult;
959
978
}
960
979
980
+
/**
981
+
* Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000.
982
+
* @param {Matrix} x
983
+
* @param {Matrix} y
984
+
* @returns {Matrix}
985
+
*/
986
+
mmul_strassen(y){
987
+
varx=this.clone();
988
+
varr1=x.rows;
989
+
varc1=x.columns;
990
+
varr2=y.rows;
991
+
varc2=y.columns;
992
+
if(c1!=r2){
993
+
console.log(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`)
994
+
}
995
+
996
+
// Put a matrix into the top left of a matrix of zeros.
997
+
// `rows` and `cols` are the dimensions of the output matrix.
998
+
functionembed(mat,rows,cols){
999
+
varr=mat.rows;
1000
+
varc=mat.columns;
1001
+
if((r==rows)&&(c==cols)){
1002
+
returnmat;
1003
+
}
1004
+
else{
1005
+
varresultat=Matrix.zeros(rows,cols);
1006
+
resultat=resultat.setSubMatrix(mat,0,0);
1007
+
returnresultat;
1008
+
}
1009
+
}
1010
+
1011
+
1012
+
// Make sure both matrices are the same size.
1013
+
// This is exclusively for simplicity:
1014
+
// this algorithm can be implemented with matrices of different sizes.
1015
+
1016
+
varr=Math.max(r1,r2);
1017
+
varc=Math.max(c1,c2);
1018
+
varx=embed(x,r,c);
1019
+
vary=embed(y,r,c);
1020
+
1021
+
// Our recursive multiplication function.
1022
+
functionblock_mult(a,b,rows,cols){
1023
+
// For small matrices, resort to naive multiplication.
0 commit comments