diff --git a/lib/types/matrix.js b/lib/types/matrix.js index 093bb1d..8ec546f 100644 --- a/lib/types/matrix.js +++ b/lib/types/matrix.js @@ -1,16 +1,36 @@ var matrix = require('../numbers/matrix'); var Matrix = function (data) { - if (!data) { - throw new Error('must provide valid data'); + if (data) { + this.data = data; + + //cache row and colCount so .length + //isn't constantly recalculatd. + this.colCount = this.data[0].length; + this.rowCount = this.data.length; } +} - this.data = data; +/** + * Fill a matrix with an array of arrays. + * + * @param {Array} Array of array of numbers. + * @return {Matrix} Updated/Filled matrix. + */ +Matrix.prototype.fill = function(aoa) { + for (var i = 0, rowCount = aoa.length, colCount = aoa[0].length; i < rowCount; i++) { + if (aoa[i].length != colCount) { + throw new IllegalArgumentException("All rows must have the same length."); + } + for (var j = 0; j < colCount; j++) { + this.data[i][j] = aoa[i][j]; + } + } - //cache row and colCount so .length - //isn't constantly recalculatd. - this.colCount = this.data[0].length; - this.rowCount = this.data.length; + this.colCount = colCount; + this.rowCount = rowCount; + + return this; } /**