Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active June 9, 2016 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swivelgames/a26c84f8652c024dde88e17e582b34a2 to your computer and use it in GitHub Desktop.
Save Swivelgames/a26c84f8652c024dde88e17e582b34a2 to your computer and use it in GitHub Desktop.
Multiply Matrices
var Matrix; Matrix = (function(){
var Constructor = function(inp) {
if(typeof inp !== "object" || !inp.length) {
throw new TypeError("Matrix required");
}
this.matrix = inp;
this.length = {
y: this.matrix.length,
x: this.matrix[0].length
};
if(!Matrix.test(this.matrix)) {
throw new Error("Invalid matrix");
}
this.initCoord();
this.initColPointer();
this.initRowPointer();
};
Constructor.prototype = {
initCoord: function(){
this.curCoord = { x: -1, y: 0 };
},
initColPointer: function() {
this.curCol = -1;
},
initRowPointer: function() {
this.curRow = -1;
},
getNextCoord: function() {
var curCoord = this.curCoord;
curCoord.x++;
if(curCoord.x >= this.length.x) {
if( (curCoord.y + 1) >= this.length.y) {
return void 0;
} else {
curCoord.y++;
}
curCoord.x = 0;
}
return curCoord;
},
getNextValue: function() {
var curCoord = this.getNextCoord();
if(!curCoord) return void 0;
return [
this.matrix[curCoord.y][curCoord.x],
curCoord.y,
curCoord.x
];
},
multiply: function(val) {
if(!val instanceof Matrix) {
throw new TypeError("Value must be of type Matrix");
return void 0;
}
var product = [[]];
var lastY = 0;
this.initCoord();
var coord, col, row;
while(coord = this.getNextCoord()) {
if(coord.x===0) {
val.initColPointer();
}
col = val.getNextCol();
row = this.getRow(coord.y);
if(!product[coord.y]) {
product[coord.y] = [0];
}
product[coord.y][coord.x] = 0;
row.forEach( (v,i) => {
product[coord.y][coord.x] += row[i] * col[i]
});
}
return new Matrix(product);
},
getNextCol() {
this.curCol++;
if(this.curCol >= this.length.x) {
this.initColPointer();
return void 0;
}
return this.getCol(this.curCol);
},
getNextRow() {
this.curRow++;
if(this.curRow >= this.length.y) {
this.initRowPointer();
return void 0;
}
return this.getRow(this.curRow);
},
getCol(col) {
var ret = [],
mx = this.matrix;
for(var i=0;i<this.length.y;i++) {
ret.push(mx[i][col]);
}
return ret;
},
getRow(row) {
return this.matrix[row].concat();
}
};
Constructor.test = function(mx) {
var lens = {
y: mx.length,
x: mx[0].length
};
for(var i=0;i<mx.length;mx++) {
if(mx[i].length !== lens.x) {
return false;
}
}
return true;
};
return Constructor;
})();
module.exports = Matrix;
const util = require('util');
const Matrix = require('./matrix.js');
var Mx1 = new Matrix([
[1,2,3],
[4,5,6],
[7,8,9]
]),
Mx2 = new Matrix([
[2,2,2],
[10,10,10],
[20,20,20]
]);
var Product = Mx1.multiply(Mx2);
console.log(
util.inspect(Mx1, false, null)
);
console.log(
util.inspect(Mx2, false, null)
);
console.log(
util.inspect(Product, false, null)
);
@Swivelgames
Copy link
Author

Example Output:

$ node test.js
{ matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
  length: { y: 3, x: 3 },
  curCoord: { x: 3, y: 2 },
  curCol: -1,
  curRow: -1 }
{ matrix: [ [ 2, 2, 2 ], [ 10, 10, 10 ], [ 20, 20, 20 ] ],
  length: { y: 3, x: 3 },
  curCoord: { x: -1, y: 0 },
  curCol: 2,
  curRow: -1 }
{ matrix: [ [ 82, 82, 82 ], [ 178, 178, 178 ], [ 274, 274, 274 ] ],
  length: { y: 3, x: 3 },
  curCoord: { x: -1, y: 0 },
  curCol: -1,
  curRow: -1 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment