Skip to content

Commit

Permalink
add lcm math operator (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp authored Oct 30, 2024
1 parent 0909ca2 commit 50dc57b
Show file tree
Hide file tree
Showing 10 changed files with 3,153 additions and 476 deletions.
3 changes: 0 additions & 3 deletions packages/doenetml-worker/src/components/BooleanList.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ export default class BooleanList extends CompositeComponent {
};

stateVariableDefinitions.booleans = {
shadowingInstructions: {
createComponentOfType: "boolean",
},
isArray: true,
entryPrefixes: ["boolean"],
stateVariablesDeterminingDependencies: ["childNameByComponent"],
Expand Down
3 changes: 0 additions & 3 deletions packages/doenetml-worker/src/components/MathList.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ export default class MathList extends CompositeComponent {
};

stateVariableDefinitions.maths = {
shadowingInstructions: {
createComponentOfType: "math",
},
isArray: true,
entryPrefixes: ["math"],
stateVariablesDeterminingDependencies: [
Expand Down
49 changes: 49 additions & 0 deletions packages/doenetml-worker/src/components/MathOperators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,45 @@ export class Gcd extends MathBaseOperator {
}
}

export class Lcm extends MathBaseOperator {
static componentType = "lcm";

static returnStateVariableDefinitions() {
let stateVariableDefinitions = super.returnStateVariableDefinitions();

stateVariableDefinitions.numericOperator = {
returnDependencies: () => ({}),
definition: () => ({
setValue: {
numericOperator: function (inputs) {
if (inputs.every(Number.isInteger)) {
return lcm(...inputs);
}
return NaN;
},
},
}),
};

stateVariableDefinitions.mathOperator = {
returnDependencies: () => ({}),
definition: () => ({
setValue: {
mathOperator: function (inputs) {
return me.fromAst([
"apply",
"lcm",
["tuple", ...inputs.map((x) => x.tree)],
]);
},
},
}),
};

return stateVariableDefinitions;
}
}

export class ExtractMath extends MathBaseOperatorOneInput {
static componentType = "extractMath";

Expand Down Expand Up @@ -1335,3 +1374,13 @@ function gcd(x, y, ...z) {
}
return gcd(y, x % y, ...z);
}

function lcm(...z) {
let prod = z.reduce((a, c) => a * c, 1);

let gcdArgs = z.map((v) => prod / v);

let g = gcd(...gcdArgs);

return prod / g;
}
7 changes: 0 additions & 7 deletions packages/doenetml-worker/src/components/NumberList.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ export default class NumberList extends CompositeComponent {
};

stateVariableDefinitions.mergeMathLists = {
public: true,
shadowingInstructions: {
createComponentOfType: "boolean",
},
returnDependencies: () => ({
mergeMathListsAttr: {
dependencyType: "attributeComponent",
Expand Down Expand Up @@ -253,9 +249,6 @@ export default class NumberList extends CompositeComponent {
};

stateVariableDefinitions.numbers = {
shadowingInstructions: {
createComponentOfType: "number",
},
isArray: true,
entryPrefixes: ["number"],
stateVariablesDeterminingDependencies: [
Expand Down
3 changes: 0 additions & 3 deletions packages/doenetml-worker/src/components/TextList.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ export default class TextList extends CompositeComponent {
};

stateVariableDefinitions.texts = {
shadowingInstructions: {
createComponentOfType: "text",
},
isArray: true,
entryPrefixes: ["text"],
stateVariablesDeterminingDependencies: ["childNameByComponent"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7569,23 +7569,43 @@ describe("Math operator tests", async () => {
it("gcd", async () => {
let core = await createTestCore({
doenetML: `
<gcd><number>135</number><number>81</number></gcd>
<gcd>135 81 63</gcd>
<gcd>x y z</gcd>
<gcd name="gcd1"><number>135</number><number>81</number></gcd>
<gcd name="gcd2">135 81 63</gcd>
<gcd name="gcd3">x y z</gcd>
`,
});

let stateVariables = await returnAllStateVariables(core);

expect(stateVariables["/_gcd1"].stateValues.value.tree).eq(27);
expect(stateVariables["/_gcd2"].stateValues.value.tree).eq(9);
expect(stateVariables["/_gcd3"].stateValues.value.tree).eqls([
expect(stateVariables["/gcd1"].stateValues.value.tree).eq(27);
expect(stateVariables["/gcd2"].stateValues.value.tree).eq(9);
expect(stateVariables["/gcd3"].stateValues.value.tree).eqls([
"apply",
"gcd",
["tuple", "x", "y", "z"],
]);
});

it("lcm", async () => {
let core = await createTestCore({
doenetML: `
<lcm name="lcm1"><number>135</number><number>81</number></lcm>
<lcm name="lcm2">135 81 63</lcm>
<lcm name="lcm3">x y z</lcm>
`,
});

let stateVariables = await returnAllStateVariables(core);

expect(stateVariables["/lcm1"].stateValues.value.tree).eq(405);
expect(stateVariables["/lcm2"].stateValues.value.tree).eq(2835);
expect(stateVariables["/lcm3"].stateValues.value.tree).eqls([
"apply",
"lcm",
["tuple", "x", "y", "z"],
]);
});

it("extract parts of math expression", async () => {
let core = await createTestCore({
doenetML: `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ describe("SelectRandomNumbers and SampleRandomNumbers tag tests", async () => {
numRepetitions: 80,
validValues: vals,
allowedMeanMid: mean,
allowedMeanSpread: 0.5,
allowedMeanSpread: 0.6,
allowedVarianceMid: variance,
allowedVarianceSpread: 1,
expectedMean: mean,
Expand Down
11 changes: 0 additions & 11 deletions packages/static-assets/scripts/get-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,6 @@ export function getSchema() {
}),
);
} else {
let foundMatch = false;

for (let prefix of arrayEntryPrefixesLongestToShortest) {
if (
aliasTargetName.substring(0, prefix.length) === prefix
Expand Down Expand Up @@ -338,18 +336,9 @@ export function getSchema() {
}),
);

foundMatch = true;
break;
}
}

if (!foundMatch) {
properties.push({
name: aliasName,
type: "unknown",
isArray: false,
});
}
}
}

Expand Down
Loading

0 comments on commit 50dc57b

Please sign in to comment.