Skip to content

Commit

Permalink
feat: add support for Math.pow
Browse files Browse the repository at this point in the history
Closes: #21
  • Loading branch information
targos committed Jul 7, 2016
1 parent ca80c0f commit 2524b73
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,24 @@ var staticMethod = `
})
`;

var inplaceMethodWithArgs = `
(function %name%(...args) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.columns; j++) {
this[i][j] = %method%(this[i][j], ...args);
}
}
return this;
})
`;

var staticMethodWithArgs = `
(function %name%(matrix, ...args) {
var newMatrix = new Matrix(matrix);
return newMatrix.%name%(...args);
})
`;

var operators = [
// Arithmetic operators
['+', 'add'],
Expand All @@ -1356,12 +1374,15 @@ var operators = [
];

for (var operator of operators) {
var inplaceOp = eval(fillTemplateFunction(inplaceOperator, {name: operator[1], op: operator[0]}));
var inplaceOpS = eval(fillTemplateFunction(inplaceOperatorScalar, {name: operator[1] + 'S', op: operator[0]}));
var inplaceOpM = eval(fillTemplateFunction(inplaceOperatorMatrix, {name: operator[1] + 'M', op: operator[0]}));
var staticOp = eval(fillTemplateFunction(staticOperator, {name: operator[1]}));
for (var i = 1; i < operator.length; i++) {
Matrix.prototype[operator[i]] = eval(fillTemplateFunction(inplaceOperator, {name: operator[i], op: operator[0]}));
Matrix.prototype[operator[i] + 'S'] = eval(fillTemplateFunction(inplaceOperatorScalar, {name: operator[i] + 'S', op: operator[0]}));
Matrix.prototype[operator[i] + 'M'] = eval(fillTemplateFunction(inplaceOperatorMatrix, {name: operator[i] + 'M', op: operator[0]}));

Matrix[operator[i]] = eval(fillTemplateFunction(staticOperator, {name: operator[i]}));
Matrix.prototype[operator[i]] = inplaceOp;
Matrix.prototype[operator[i] + 'S'] = inplaceOpS;
Matrix.prototype[operator[i] + 'M'] = inplaceOpM;
Matrix[operator[i]] = staticOp;
}
}

Expand All @@ -1378,9 +1399,24 @@ var methods = [
});

for (var method of methods) {
var inplaceMeth = eval(fillTemplateFunction(inplaceMethod, {name: method[1], method: method[0]}));
var staticMeth = eval(fillTemplateFunction(staticMethod, {name: method[1]}));
for (var i = 1; i < method.length; i++) {
Matrix.prototype[method[i]] = eval(fillTemplateFunction(inplaceMethod, {name: method[i], method: method[0]}));
Matrix[method[i]] = eval(fillTemplateFunction(staticMethod, {name: method[i]}));
Matrix.prototype[method[i]] = inplaceMeth;
Matrix[method[i]] = staticMeth;
}
}

var methodsWithArgs = [
['Math.pow', 'pow']
];

for (var methodWithArg of methodsWithArgs) {
var inplaceMethWithArgs = eval(fillTemplateFunction(inplaceMethodWithArgs, {name: methodWithArg[1], method: methodWithArg[0]}));
var staticMethWithArgs = eval(fillTemplateFunction(staticMethodWithArgs, {name: methodWithArg[1]}));
for (var i = 1; i < methodWithArg.length; i++) {
Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs;
Matrix[methodWithArg[i]] = staticMethWithArgs;
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/matrix/dynamicMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,27 @@ describe('Dynamic methods on matrices', function () {
});
});

describe('with args', function () {
it('inplace MathPow', function () {
matrix = matrix.subMatrix(0, 2, 0, 2);
matrix.pow(2);
matrix.to2DArray().should.eql([
[0, 1, 4],
[9, 16, 25],
[36, 49, 64]
]);
});

it('static MathPow', function () {
matrix = matrix.subMatrix(0, 2, 0, 2);
var newMatrix = Matrix.pow(matrix, 2);
newMatrix.should.not.eql(matrix);
newMatrix.to2DArray().should.eql([
[0, 1, 4],
[9, 16, 25],
[36, 49, 64]
]);
});
});

});

0 comments on commit 2524b73

Please sign in to comment.