Skip to content

Commit 2524b73

Browse files
committed
feat: add support for Math.pow
Closes: #21
1 parent ca80c0f commit 2524b73

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/matrix.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,24 @@ var staticMethod = `
13391339
})
13401340
`;
13411341

1342+
var inplaceMethodWithArgs = `
1343+
(function %name%(...args) {
1344+
for (var i = 0; i < this.rows; i++) {
1345+
for (var j = 0; j < this.columns; j++) {
1346+
this[i][j] = %method%(this[i][j], ...args);
1347+
}
1348+
}
1349+
return this;
1350+
})
1351+
`;
1352+
1353+
var staticMethodWithArgs = `
1354+
(function %name%(matrix, ...args) {
1355+
var newMatrix = new Matrix(matrix);
1356+
return newMatrix.%name%(...args);
1357+
})
1358+
`;
1359+
13421360
var operators = [
13431361
// Arithmetic operators
13441362
['+', 'add'],
@@ -1356,12 +1374,15 @@ var operators = [
13561374
];
13571375

13581376
for (var operator of operators) {
1377+
var inplaceOp = eval(fillTemplateFunction(inplaceOperator, {name: operator[1], op: operator[0]}));
1378+
var inplaceOpS = eval(fillTemplateFunction(inplaceOperatorScalar, {name: operator[1] + 'S', op: operator[0]}));
1379+
var inplaceOpM = eval(fillTemplateFunction(inplaceOperatorMatrix, {name: operator[1] + 'M', op: operator[0]}));
1380+
var staticOp = eval(fillTemplateFunction(staticOperator, {name: operator[1]}));
13591381
for (var i = 1; i < operator.length; i++) {
1360-
Matrix.prototype[operator[i]] = eval(fillTemplateFunction(inplaceOperator, {name: operator[i], op: operator[0]}));
1361-
Matrix.prototype[operator[i] + 'S'] = eval(fillTemplateFunction(inplaceOperatorScalar, {name: operator[i] + 'S', op: operator[0]}));
1362-
Matrix.prototype[operator[i] + 'M'] = eval(fillTemplateFunction(inplaceOperatorMatrix, {name: operator[i] + 'M', op: operator[0]}));
1363-
1364-
Matrix[operator[i]] = eval(fillTemplateFunction(staticOperator, {name: operator[i]}));
1382+
Matrix.prototype[operator[i]] = inplaceOp;
1383+
Matrix.prototype[operator[i] + 'S'] = inplaceOpS;
1384+
Matrix.prototype[operator[i] + 'M'] = inplaceOpM;
1385+
Matrix[operator[i]] = staticOp;
13651386
}
13661387
}
13671388

@@ -1378,9 +1399,24 @@ var methods = [
13781399
});
13791400

13801401
for (var method of methods) {
1402+
var inplaceMeth = eval(fillTemplateFunction(inplaceMethod, {name: method[1], method: method[0]}));
1403+
var staticMeth = eval(fillTemplateFunction(staticMethod, {name: method[1]}));
13811404
for (var i = 1; i < method.length; i++) {
1382-
Matrix.prototype[method[i]] = eval(fillTemplateFunction(inplaceMethod, {name: method[i], method: method[0]}));
1383-
Matrix[method[i]] = eval(fillTemplateFunction(staticMethod, {name: method[i]}));
1405+
Matrix.prototype[method[i]] = inplaceMeth;
1406+
Matrix[method[i]] = staticMeth;
1407+
}
1408+
}
1409+
1410+
var methodsWithArgs = [
1411+
['Math.pow', 'pow']
1412+
];
1413+
1414+
for (var methodWithArg of methodsWithArgs) {
1415+
var inplaceMethWithArgs = eval(fillTemplateFunction(inplaceMethodWithArgs, {name: methodWithArg[1], method: methodWithArg[0]}));
1416+
var staticMethWithArgs = eval(fillTemplateFunction(staticMethodWithArgs, {name: methodWithArg[1]}));
1417+
for (var i = 1; i < methodWithArg.length; i++) {
1418+
Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs;
1419+
Matrix[methodWithArg[i]] = staticMethWithArgs;
13841420
}
13851421
}
13861422

test/matrix/dynamicMethods.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,27 @@ describe('Dynamic methods on matrices', function () {
6767
});
6868
});
6969

70+
describe('with args', function () {
71+
it('inplace MathPow', function () {
72+
matrix = matrix.subMatrix(0, 2, 0, 2);
73+
matrix.pow(2);
74+
matrix.to2DArray().should.eql([
75+
[0, 1, 4],
76+
[9, 16, 25],
77+
[36, 49, 64]
78+
]);
79+
});
80+
81+
it('static MathPow', function () {
82+
matrix = matrix.subMatrix(0, 2, 0, 2);
83+
var newMatrix = Matrix.pow(matrix, 2);
84+
newMatrix.should.not.eql(matrix);
85+
newMatrix.to2DArray().should.eql([
86+
[0, 1, 4],
87+
[9, 16, 25],
88+
[36, 49, 64]
89+
]);
90+
});
91+
});
92+
7093
});

0 commit comments

Comments
 (0)