Skip to content

Commit b66ee9f

Browse files
committed
feat: add method with one argument template
Methods with one argument work with scalar and matrix input
1 parent 9d10e93 commit b66ee9f

File tree

2 files changed

+84
-13
lines changed

2 files changed

+84
-13
lines changed

src/matrix.js

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,37 @@ var staticMethodWithArgs = `
14471447
})
14481448
`;
14491449

1450+
var inplaceMethodWithOneArgScalar = `
1451+
(function %name%S(%args%) {
1452+
for (var i = 0; i < this.rows; i++) {
1453+
for (var j = 0; j < this.columns; j++) {
1454+
this[i][j] = %method%(this[i][j], %args%);
1455+
}
1456+
}
1457+
return this;
1458+
})
1459+
`;
1460+
var inplaceMethodWithOneArgMatrix = `
1461+
(function %name%M(%args%) {
1462+
checkDimensions(this, %args%);
1463+
for (var i = 0; i < this.rows; i++) {
1464+
for (var j = 0; j < this.columns; j++) {
1465+
this[i][j] = %method%(this[i][j], %args%[i][j]);
1466+
}
1467+
}
1468+
return this;
1469+
})
1470+
`;
1471+
1472+
var inplaceMethodWithOneArg = `
1473+
(function %name%(value) {
1474+
if (typeof value === 'number') return this.%name%S(value);
1475+
return this.%name%M(value);
1476+
})
1477+
`;
1478+
1479+
var staticMethodWithOneArg = staticMethodWithArgs;
1480+
14501481
var operators = [
14511482
// Arithmetic operators
14521483
['+', 'add'],
@@ -1506,15 +1537,35 @@ for (var methodWithArg of methodsWithArgs) {
15061537
for (var i = 1; i < methodWithArg[1]; i++) {
15071538
args += `, arg${i}`;
15081539
}
1509-
var inplaceMethWithArgs = eval(fillTemplateFunction(inplaceMethodWithArgs, {
1510-
name: methodWithArg[2],
1511-
method: methodWithArg[0],
1512-
args: args
1513-
}));
1514-
var staticMethWithArgs = eval(fillTemplateFunction(staticMethodWithArgs, {name: methodWithArg[2], args: args}));
1515-
for (var i = 2; i < methodWithArg.length; i++) {
1516-
Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs;
1517-
Matrix[methodWithArg[i]] = staticMethWithArgs;
1540+
if (methodWithArg[1] !== 1) {
1541+
var inplaceMethWithArgs = eval(fillTemplateFunction(inplaceMethodWithArgs, {
1542+
name: methodWithArg[2],
1543+
method: methodWithArg[0],
1544+
args: args
1545+
}));
1546+
var staticMethWithArgs = eval(fillTemplateFunction(staticMethodWithArgs, {name: methodWithArg[2], args: args}));
1547+
for (var i = 2; i < methodWithArg.length; i++) {
1548+
Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs;
1549+
Matrix[methodWithArg[i]] = staticMethWithArgs;
1550+
}
1551+
} else {
1552+
var tmplVar = {
1553+
name: methodWithArg[2],
1554+
args: args,
1555+
method: methodWithArg[0]
1556+
};
1557+
console.log(tmplVar);
1558+
let inplaceMethod = eval(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar));
1559+
let inplaceMethodS = eval(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar));
1560+
let inplaceMethodM = eval(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar));
1561+
console.log(fillTemplateFunction(staticMethodWithOneArg, tmplVar));
1562+
let staticMethod = eval(fillTemplateFunction(staticMethodWithOneArg, tmplVar));
1563+
for (var i = 2; i < methodWithArg.length; i++) {
1564+
Matrix.prototype[methodWithArg[i]] = inplaceMethod;
1565+
Matrix.prototype[methodWithArg[i] + 'M'] = inplaceMethodM;
1566+
Matrix.prototype[methodWithArg[i] + 'S'] = inplaceMethodS;
1567+
Matrix[methodWithArg[i]] = staticMethod;
1568+
}
15181569
}
15191570
}
15201571

test/matrix/dynamicMethods.js

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

70-
describe('with args', function () {
71-
it('inplace MathPow', function () {
70+
describe('with one arg', function () {
71+
it('inplace MathPow with scalar', function () {
7272
matrix = matrix.subMatrix(0, 2, 0, 2);
73-
matrix.pow(2);
73+
var retMatrix = matrix.pow(2);
7474
matrix.to2DArray().should.eql([
7575
[0, 1, 4],
7676
[9, 16, 25],
7777
[36, 49, 64]
7878
]);
79+
retMatrix.should.equal(matrix);
7980
});
8081

81-
it('static MathPow', function () {
82+
it('static MathPow with scalar', function () {
8283
matrix = matrix.subMatrix(0, 2, 0, 2);
8384
var newMatrix = Matrix.pow(matrix, 2);
8485
newMatrix.should.not.eql(matrix);
@@ -88,6 +89,25 @@ describe('Dynamic methods on matrices', function () {
8889
[36, 49, 64]
8990
]);
9091
});
92+
93+
it('inplace MathPow with matrix', function () {
94+
matrix = matrix.subMatrix(0, 1, 0, 1);
95+
var retMatrix = matrix.pow([[1, 10], [2, 0]]);
96+
matrix.to2DArray().should.eql([
97+
[0, 1],
98+
[9, 1]
99+
]);
100+
retMatrix.should.equal(matrix);
101+
});
102+
103+
it('static MathPow with matrix', function () {
104+
matrix = matrix.subMatrix(0, 1, 0, 1);
105+
var newMatrix = Matrix.pow(matrix, [[1, 10], [2, 0]]);
106+
newMatrix.to2DArray().should.eql([
107+
[0, 1],
108+
[9, 1]
109+
]);
110+
});
91111
});
92112

93113
});

0 commit comments

Comments
 (0)