Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve vector by uniformizing the interface #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var Matrix = function Matrix (rows, cols, isIdentity) {
this.m[r][c] = 0;
}
}

this.isVector = false;

this.size = {rows: this.rows, cols: this.cols};
Expand Down Expand Up @@ -46,14 +46,14 @@ Matrix.prototype = {
},

dot: function (B) {
if (B.isVector ? B.v.length !== this.cols : B.rows !== this.cols) {
if (B.isVector ? B.v.length < this.cols : B.rows !== this.cols) {
return new Error('number of cols of A must equal number of rows of B');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change !== to <? Isn't it also an issue if B.v.length > this.cols?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to allow a 2x2 matrix operate on a 2D vector. Because this pull request treats 2D vectors as 3D vectors with a z-component 0, therefor B.v.length is 3.

So the 'simplest' solution to that problem was to allow matrices of any size act upon vectors just as long as there is a vector-element for each matrix-row, i.e. B.v.length >= this.cols.

Having said that, I agree that it is not intuitive. Because this is a consequence of treating 2D vectors as disguised 3D vectors, you can wonder if that is a good choice to do. There are alternatives.

}

var C;
if (B.isVector) {
var arr = Array(B.v.length);
for (var i = 0; i < B.v.length; ++i) {
for (var i = 0; i < this.rows; ++i) {
arr[i] = 0;
for (var j = 0; j < this.rows; ++j) {
arr[i] += parseFloat((this.get(i,j) * B.v[j]).toFixed(6));
Expand Down
49 changes: 28 additions & 21 deletions lib/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
// -----------------------------------------------------------
var Vector = function Vector (x, y, z) {
if (typeof y === 'undefined' && typeof z === 'undefined') {
this.x = typeof x.x !== 'undefined' ? x.x : x[0];
this.y = typeof x.y !== 'undefined' ? x.y : x[1];
this.z = typeof x.z !== 'undefined' ? x.z : x[2];
if (typeof x.x != 'undefined') {
this.x = x.x;
this.y = x.y;
this.z = definedValueOrZero(x.z);
} else {
this.x = x[0];
this.y = x[1];
this.z = definedValueOrZero(x[2]);
}
} else {
this.x = x;
this.y = y;
this.z = z;
this.z = definedValueOrZero(z);
}

this.v = typeof this.z !== 'undefined' ? [this.x, this.y, this.z] : [this.x, this.y];

this.v = [this.x, this.y, this.z];
};

var definedValueOrZero = function(value) {
return typeof value !== 'undefined' ? value : 0;
}

Vector.prototype = {
add: function (b) {
return new Vector(this.x + b.x, this.y + b.y, this.z + b.z);
Expand All @@ -28,9 +37,10 @@ Vector.prototype = {
isVector: true,

moveTo: function(pt) {
this.x = typeof pt.x !== 'undefined' ? pt.x : pt[0];
this.y = typeof pt.y !== 'undefined' ? pt.y : pt[1];
this.z = typeof pt.z !== 'undefined' ? pt.z : pt[2];
var v = new Vector(pt);
this.x = v.x;
this.y = v.y;
this.z = v.z;
},
// cross: function (b) {
// var i = y * b.z - z * b.y;
Expand All @@ -43,22 +53,19 @@ Vector.prototype = {
// },

distanceFrom: function (b) {
var sumOfSquares = 0;
for (var i = 0; i < this.v.length; ++i) {
sumOfSquares += (this.v[i] - b.v[i]) * (this.v[i] - b.v[i]);
}

return Math.sqrt(sumOfSquares);

return length([this.x - b.x, this.y - b.y, this.z - b.z]);
},

length: function () {
var sumOfSquares = 0;
for (var i = 0; i < this.v.length; ++i) {
sumOfSquares += this.v[i] * this.v[i];
}
return Math.sqrt(sumOfSquares)
return length(this.v);
}
};

var length = function length(elements) {
var sumOfSquares = 0;
for (var index = 0; index < elements.length; index++) {
sumOfSquares += elements[index] * elements[index]
}
return Math.sqrt(sumOfSquares);
}
exports = module.exports = Vector;
4 changes: 2 additions & 2 deletions tests/matrix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Creating matrices: ', function () {
it('should multiply a vector by a matrix', function (done) {
var v = new Vector(2,3);
var C = A.dot(v);
C.v.should.eql([8,18]);
C.v.should.eql([8,18,0]);
done();
});

Expand Down Expand Up @@ -139,4 +139,4 @@ describe('Creating matrices: ', function () {
done();
});
});
});
});
14 changes: 7 additions & 7 deletions tests/vector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ describe('Creating vectors: ', function () {

it('Should pass with 2 arguments', function (done) {
var a = new v(1,0);
a.v.length.should.eql(2);
a.v.should.eql([1,0]);
a.v.length.should.eql(3);
a.v.should.eql([1,0,0]);
done();
});

it('should pass with 1 argument as an object', function (done) {
var a = new v({x: 1, y: 0});
a.v.length.should.eql(2);
a.v.should.eql([1,0]);
a.v.length.should.eql(3);
a.v.should.eql([1,0,0]);
done();
});

it('should pass with 1 argument as a vector', function (done) {
var a = new v([1,0]);
a.v.length.should.eql(2);
a.v.should.eql([1,0]);
a.v.length.should.eql(3);
a.v.should.eql([1,0,0]);
done();
});
});
Expand Down Expand Up @@ -67,4 +67,4 @@ describe('Between two vectors: ', function () {
lenB.should.eql(Math.sqrt(2));
done();
})
});
});