Skip to content
Open
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
102 changes: 69 additions & 33 deletions packages/doenetml-worker-javascript/src/components/MathOperators.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Sum extends MathBaseOperator {
definition: () => ({
setValue: {
numericOperator: function (inputs) {
return inputs.reduce((a, c) => a + c);
return inputs.reduce((a, c) => a + c, 0);
},
},
}),
Expand All @@ -28,7 +28,11 @@ export class Sum extends MathBaseOperator {
definition: () => ({
setValue: {
mathOperator: function (inputs) {
return inputs.reduce((a, c) => a.add(c));
if(inputs.length === 0) {
return me.fromAst(0);
} else {
return inputs.reduce((a, c) => a.add(c));
}
},
},
}),
Expand All @@ -52,7 +56,7 @@ export class Product extends MathBaseOperator {
definition: () => ({
setValue: {
numericOperator: function (inputs) {
return inputs.reduce((a, c) => a * c);
return inputs.reduce((a, c) => a * c, 1);
},
},
}),
Expand All @@ -63,7 +67,11 @@ export class Product extends MathBaseOperator {
definition: () => ({
setValue: {
mathOperator: function (values) {
return values.reduce((a, c) => a.multiply(c));
if(values.length === 0) {
return me.fromAst(1);
} else {
return values.reduce((a, c) => a.multiply(c));
}
},
},
}),
Expand Down Expand Up @@ -679,9 +687,13 @@ export class Mean extends MathBaseOperator {
definition: () => ({
setValue: {
numericOperator: function (inputs) {
let mean = inputs.reduce((a, c) => a + c);
mean /= inputs.length;
return mean;
if(inputs.length === 0) {
return NaN;
} else {
let mean = inputs.reduce((a, c) => a + c);
mean /= inputs.length;
return mean;
}
},
},
}),
Expand All @@ -692,9 +704,13 @@ export class Mean extends MathBaseOperator {
definition: () => ({
setValue: {
mathOperator: function (inputs) {
return inputs
.reduce((a, c) => a.add(c))
.divide(inputs.length);
if(inputs.length === 0) {
return me.fromAst("\uff3f");
} else {
return inputs
.reduce((a, c) => a.add(c))
.divide(inputs.length);
}
},
},
}),
Expand Down Expand Up @@ -723,7 +739,11 @@ export class Median extends MathBaseOperator {
definition: () => ({
setValue: {
numericOperator: function (inputs) {
return me.math.median(inputs);
if(inputs.length === 0) {
return NaN;
} else {
return me.math.median(inputs);
}
},
},
}),
Expand Down Expand Up @@ -766,10 +786,14 @@ export class Variance extends MathBaseOperator {
definition: ({ dependencyValues }) => ({
setValue: {
numericOperator: function (inputs) {
return calculateNumericVariance(
inputs,
dependencyValues.population,
);
if(inputs.length === 0) {
return NaN;
} else {
return calculateNumericVariance(
inputs,
dependencyValues.population,
);
}
},
},
}),
Expand All @@ -785,10 +809,14 @@ export class Variance extends MathBaseOperator {
definition: ({ dependencyValues }) => ({
setValue: {
mathOperator: function (inputs) {
return calculateSymbolicVariance(
inputs,
dependencyValues.population,
);
if(inputs.length === 0) {
return me.fromAst("\uff3f");
} else {
return calculateSymbolicVariance(
inputs,
dependencyValues.population,
);
}
},
},
}),
Expand Down Expand Up @@ -853,12 +881,16 @@ export class StandardDeviation extends Variance {
}) => ({
setValue: {
numericOperator: function (inputs) {
return Math.sqrt(
calculateNumericVariance(
inputs,
dependencyValues.population,
),
);
if(inputs.length === 0) {
return NaN;
} else {
return Math.sqrt(
calculateNumericVariance(
inputs,
dependencyValues.population,
),
);
}
},
},
});
Expand All @@ -868,14 +900,18 @@ export class StandardDeviation extends Variance {
}) => ({
setValue: {
mathOperator: function (inputs) {
return me.fromAst([
"apply",
"sqrt",
calculateSymbolicVariance(
inputs,
dependencyValues.population,
).tree,
]);
if(inputs.length === 0) {
return me.fromAst("\uff3f");
} else {
return me.fromAst([
"apply",
"sqrt",
calculateSymbolicVariance(
inputs,
dependencyValues.population,
).tree,
]);
}
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ export default class MathOperator extends MathComponent {
},
}),
definition: function ({ dependencyValues, componentInfoObjects }) {
if (dependencyValues.mathNumberChildren.length === 0) {
return {
setValue: { unnormalizedValue: me.fromAst("\uff3f") },
};
} else if (dependencyValues.isNumericOperator) {
if (dependencyValues.isNumericOperator) {
let inputs = [];
for (let child of dependencyValues.mathNumberChildren) {
if (
Expand Down
Loading