From a2fb68fd98252e7cda1ab10e11ff1e5df0355964 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 25 Sep 2013 11:59:40 +0200 Subject: [PATCH] Fix for creating square matrices and added identity property to the Matrix function The Matrix constructor used the (undefined) cols variable when creating the appropriate array instead of this.cols. There was a isIdentity parameter in the Matrix constructor but it wasn't used. I added the Matrix.identity(size). --- lib/matrix.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/matrix.js b/lib/matrix.js index c343277..397d470 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -3,25 +3,33 @@ var v = require('./vector'); // ----------------------------------------------------------- // Matrices // ----------------------------------------------------------- -var Matrix = function Matrix (rows, cols, isIdentity) { +var Matrix = function Matrix (rows, cols) { // initialize the matrix this.rows = rows; this.cols = cols ? cols : rows; // make it a square matrix if only one argument - this.m = new Array(rows); - for (var r = 0; r < rows; ++r) { - this.m[r] = new Array(cols); - for (var c = 0; c < cols; ++c) { + this.m = new Array(this.rows); + for (var r = 0; r < this.rows; ++r) { + this.m[r] = new Array(this.cols); + for (var c = 0; c < this.cols; ++c) { this.m[r][c] = 0; } } - + this.isVector = false; this.size = {rows: this.rows, cols: this.cols}; }; +Matrix.identity = function(size) { + var I = new Matrix(size); + for (var i = 0; i < size; ++i) { + I.set(i, i, 1); + } + return I; +} + Matrix.prototype = { set : function (i, j, val) { this.m[i][j] = parseFloat(val.toFixed(6));