Skip to content
Merged
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
157 changes: 157 additions & 0 deletions scripts/makeMathMethods.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* eslint-disable no-console */

const inplaceOperator = `
/**
* @param {number|SparseMatrix} value
* @returns {this}
*/
%name%(value) {
if (typeof value === 'number') return this.%name%S(value);
return this.%name%M(value);
}`;

Check warning on line 11 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L3-L11

Added lines #L3 - L11 were not covered by tests

const inplaceOperatorScalar = `
/**
* @param {number} value
* @returns {this}
*/
%name%(value) {
this.forEachNonZero((i, j, v) => v %op% value);
return this;
}`;

Check warning on line 21 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L13-L21

Added lines #L13 - L21 were not covered by tests

const inplaceOperatorMatrix = `
/**
* @param {SparseMatrix} matrix
* @returns {this}
*/
%name%(matrix) {
matrix.forEachNonZero((i, j, v) => {
this.set(i, j, this.get(i, j) %op% v);
return v;
});
return this;
}`;

Check warning on line 34 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L23-L34

Added lines #L23 - L34 were not covered by tests

const staticOperator = `
/**
* @param {SparseMatrix} matrix
* @param {number} value
* @returns {SparseMatrix}
*/
static %name%(matrix, value) {
return new SparseMatrix(matrix).%name%S(value);
}`;

Check warning on line 44 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L36-L44

Added lines #L36 - L44 were not covered by tests

const inplaceMethod = `
/**
* @returns {this}
*/
%name%() {
this.forEachNonZero((i, j, v) => %method%(v));
return this;
}`;

Check warning on line 53 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L46-L53

Added lines #L46 - L53 were not covered by tests

const staticMethod = `
/**
* @param {SparseMatrix} matrix
* @returns {SparseMatrix}
*/
static %name%(matrix) {
return new SparseMatrix(matrix).%name%();
}`;

Check warning on line 62 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L55-L62

Added lines #L55 - L62 were not covered by tests

const operators = [
// Arithmetic operators
['+', 'add'],
['-', 'sub', 'subtract'],
['*', 'mul', 'multiply'],
['/', 'div', 'divide'],
['%', 'mod', 'modulus'],
// Bitwise operators
['&', 'and'],
['|', 'or'],
['^', 'xor'],
['<<', 'leftShift'],
['>>', 'signPropagatingRightShift'],
['>>>', 'rightShift', 'zeroFillRightShift'],
];

Check warning on line 78 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L64-L78

Added lines #L64 - L78 were not covered by tests

for (const operator of operators) {
for (let i = 1; i < operator.length; i++) {
console.log(
fillTemplateFunction(inplaceOperator, {
name: operator[i],
op: operator[0],
}),
);
console.log(
fillTemplateFunction(inplaceOperatorScalar, {
name: `${operator[i]}S`,
op: operator[0],
}),
);
console.log(
fillTemplateFunction(inplaceOperatorMatrix, {
name: `${operator[i]}M`,
op: operator[0],
}),
);

Check warning on line 99 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L80-L99

Added lines #L80 - L99 were not covered by tests

console.log(fillTemplateFunction(staticOperator, { name: operator[i] }));
}
}

Check warning on line 103 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L101-L103

Added lines #L101 - L103 were not covered by tests

let methods = [['~', 'not']];

Check warning on line 105 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L105

Added line #L105 was not covered by tests

for (const mathMethod of [
'abs',
'acos',
'acosh',
'asin',
'asinh',
'atan',
'atanh',
'cbrt',
'ceil',
'clz32',
'cos',
'cosh',
'exp',
'expm1',
'floor',
'fround',
'log',
'log1p',
'log10',
'log2',
'round',
'sign',
'sin',
'sinh',
'sqrt',
'tan',
'tanh',
'trunc',
]) {
methods.push([`Math.${mathMethod}`, mathMethod]);
}

Check warning on line 138 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L107-L138

Added lines #L107 - L138 were not covered by tests

for (const method of methods) {
for (let i = 1; i < method.length; i++) {
console.log(
fillTemplateFunction(inplaceMethod, {
name: method[i],
method: method[0],
}),
);
console.log(fillTemplateFunction(staticMethod, { name: method[i] }));
}
}

Check warning on line 150 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L140-L150

Added lines #L140 - L150 were not covered by tests

function fillTemplateFunction(template, values) {
for (const i in values) {
template = template.replaceAll(new RegExp(`%${i}%`, 'g'), values[i]);
}
return template;
}

Check warning on line 157 in scripts/makeMathMethods.mjs

View check run for this annotation

Codecov / codecov/patch

scripts/makeMathMethods.mjs#L152-L157

Added lines #L152 - L157 were not covered by tests
Loading