Last active
June 9, 2016 20:43
-
-
Save Swivelgames/a26c84f8652c024dde88e17e582b34a2 to your computer and use it in GitHub Desktop.
Multiply Matrices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Output: