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

add binary operation to matrix #92

Merged
merged 2 commits into from
Dec 27, 2016
Merged
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
97 changes: 47 additions & 50 deletions matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,23 @@
};

/**
* Static method. Adds two matrices `a` and `b` together.
* Static method. Perform binary operation on two matrices `a` and `b` together.
* @param {Matrix} a
* @param {Matrix} b
* @returns {Matrix} a new matrix containing the sum of `a` and `b`
* @param {function } op
* @returns {Matrix} a new matrix containing the results of binary operation of `a` and `b`
**/
Matrix.add = function (a, b) {
return new Matrix(a).add(b);
Matrix.binOp = function(a, b, op) {
return new Matrix(a).binOp(b, op);
};

/**
* Adds `matrix` to current matrix.
* @param {Matrix} a
* @param {Matrix} b
* @returns {Matrix} `this`
* Perform binary operation on `matrix` to the current matrix.
* @param {Matrix} matrix
* @param {function } op
* @returns {Matrix} this
**/
Matrix.prototype.add = function (matrix) {
Matrix.prototype.binOp = function(matrix, op) {
var r = this.shape[0], // rows in this matrix
c = this.shape[1], // columns in this matrix
d1 = this.data,
Expand All @@ -78,68 +79,49 @@

var i;
for (i = 0; i < r * c; i++)
d1[i] += d2[i];
d1[i] = op(d1[i], d2[i], i);

return this;
};

/**
* Static method. Subtracts the matrix `b` from matrix `a`.
* Static method. Adds two matrices `a` and `b` together.
* @param {Matrix} a
* @param {Matrix} b
* @returns {Matrix} a new matrix containing the difference between `a` and `b`
* @returns {Matrix} a new matrix containing the sum of `a` and `b`
**/
Matrix.subtract = function (a, b) {
return new Matrix(a).subtract(b);
Matrix.add = function (a, b) {
return new Matrix(a).add(b);
};

/**
* Subtracts `matrix` from current matrix.
* Adds `matrix` to current matrix.
* @param {Matrix} a
* @param {Matrix} b
* @returns {Matrix} `this`
**/
Matrix.prototype.subtract = function (matrix) {
var r = this.shape[0], // rows in this matrix
c = this.shape[1], // columns in this matrix
d1 = this.data,
d2 = matrix.data;

if (r !== matrix.shape[0] || c !== matrix.shape[1])
throw new Error('sizes do not match');

var i;
for (i = 0; i < r * c; i++)
d1[i] -= d2[i];

return this;
Matrix.prototype.add = function (matrix) {
return this.binOp(matrix, function(a, b) { return a + b });
};

/**
* Static method. Multiplies all elements of a matrix `a` with a specified `scalar`.
* Static method. Subtracts the matrix `b` from matrix `a`.
* @param {Matrix} a
* @param {Number} scalar
* @returns {Matrix} a new scaled matrix
* @param {Matrix} b
* @returns {Matrix} a new matrix containing the difference between `a` and `b`
**/
Matrix.scale = function (a, scalar) {
return new Matrix(a).scale(scalar);
Matrix.subtract = function (a, b) {
return new Matrix(a).subtract(b);
};

/**
* Multiplies all elements of current matrix with a specified `scalar`.
* @param {Number} scalar
* Subtracts `matrix` from current matrix.
* @param {Matrix} a
* @param {Matrix} b
* @returns {Matrix} `this`
**/
Matrix.prototype.scale = function (scalar) {
var r = this.shape[0], // rows in this matrix
c = this.shape[1], // columns in this matrix
d1 = this.data,
i;

for (i = 0; i < r * c; i++)
d1[i] *= scalar;

return this;
Matrix.prototype.subtract = function (matrix) {
return this.binOp(matrix, function(a, b) { return a - b });
};

/**
Expand All @@ -159,17 +141,32 @@
* @returns {Matrix} `this`
**/
Matrix.prototype.product = function (matrix) {
if (this.shape[0] !== matrix.shape[0] || this.shape[1] !== matrix.shape[1])
return new Error('invalid size');
return this.binOp(matrix, function(a, b) { return a * b });
};

/**
* Static method. Multiplies all elements of a matrix `a` with a specified `scalar`.
* @param {Matrix} a
* @param {Number} scalar
* @returns {Matrix} a new scaled matrix
**/
Matrix.scale = function (a, scalar) {
return new Matrix(a).scale(scalar);
};

/**
* Multiplies all elements of current matrix with a specified `scalar`.
* @param {Number} scalar
* @returns {Matrix} `this`
**/
Matrix.prototype.scale = function (scalar) {
var r = this.shape[0], // rows in this matrix
c = this.shape[1], // columns in this matrix
d1 = this.data,
d2 = matrix.data,
i;

for (i = 0; i < r * c; i++)
d1[i] *= d2[i];
d1[i] *= scalar;

return this;
};
Expand Down