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
101 changes: 101 additions & 0 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
solveQuadraticEquation,
expressionsCanBeCompared,
transformEqualityInExpression,
getSamplePoints,
areNumericallyEquivalent,
areEquationsNumericallyEquivalent,
} from "../symbolic/utils";

const lta = new LatexToAst();
Expand Down Expand Up @@ -250,3 +253,101 @@ describe("solveLinearEquation", () => {
expect(result).toEqual(undefined);
});
});

describe("getSamplePoints", () => {
it("gives each variable its own non-integer value, the same on every call", () => {
const points = getSamplePoints(["x", "y"]);

points.forEach((point) => {
expect(point.x).not.toEqual(point.y);
expect(Number.isInteger(point.x)).toEqual(false);
expect(Number.isInteger(point.y)).toEqual(false);
});

expect(getSamplePoints(["x", "y"])).toEqual(points);
});

it("includes negative and positive values for each variable", () => {
const points = getSamplePoints(["x", "y"]);

["x", "y"].forEach((v) => {
expect(points.some((point) => point[v] < 0)).toEqual(true);
expect(points.some((point) => point[v] > 0)).toEqual(true);
});
});
});

describe("areNumericallyEquivalent", () => {
it('"x^2" and "y^2" are not equivalent', () => {
const a = atm.convert(lta.convert("x^2"));
const b = atm.convert(lta.convert("y^2"));

expect(areNumericallyEquivalent(a, b, ["x", "y"])).toEqual(false);
});

it('"(x+y)^2" and "x^2+2xy+y^2" are equivalent', () => {
const a = atm.convert(lta.convert("(x+y)^2"));
const b = atm.convert(lta.convert("x^2+2xy+y^2"));

expect(areNumericallyEquivalent(a, b, ["x", "y"])).toEqual(true);
});
});

describe("areEquationsNumericallyEquivalent", () => {
const expression = (latex: string) =>
transformEqualityInExpression(atm.convert(lta.convert(latex)));

it('"x^2/16+y^2/9=1" and "9x^2+16y^2=144" are equivalent: one is a constant multiple of the other', () => {
const result = areEquationsNumericallyEquivalent(
expression("\\frac{x^2}{16}+\\frac{y^2}{9}=1"),
expression("9x^2+16y^2=144"),
["x", "y"]
);

expect(result).toEqual(true);
});

it('"x^2/16+y^2/9=1" and "x^2/17+y^2/9=1" are not equivalent', () => {
const result = areEquationsNumericallyEquivalent(
expression("\\frac{x^2}{16}+\\frac{y^2}{9}=1"),
expression("\\frac{x^2}{17}+\\frac{y^2}{9}=1"),
["x", "y"]
);

expect(result).toEqual(false);
});

it('"x+2y=7" and "x^2+2xy-7x=0" are equivalent only when a variable factor is allowed', () => {
const first = expression("x+2y=7");
const second = expression("x^2+2xy-7x=0");

expect(areEquationsNumericallyEquivalent(first, second, ["x", "y"])).toEqual(
false
);
expect(
areEquationsNumericallyEquivalent(first, second, ["x", "y"], {
allowVariableFactor: true,
})
).toEqual(true);
});

it('"(y-1)/(x-2)=3" and "y-1=3(x-2)" are equivalent: both sides were multiplied by the denominator', () => {
const result = areEquationsNumericallyEquivalent(
expression("\\frac{y-1}{x-2}=3"),
expression("y-1=3(x-2)"),
["x", "y"]
);

expect(result).toEqual(true);
});

it('"y=|x|" and "y=x" are not equivalent: they differ for negative x', () => {
const result = areEquationsNumericallyEquivalent(
expression("y=\\left|x\\right|"),
expression("y=x"),
["x", "y"]
);

expect(result).toEqual(false);
});
});
186 changes: 186 additions & 0 deletions src/fixtures/latex-equal/symbolic/PIE-443.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// two-variable equations with powers: changing a number or an exponent must make the answer incorrect
// targets are the correct answers of items E217532, E232686, E234938 and E234162; ne are the changes reported in PIE-443
export default {
mode: "symbolic",
tests: [
{
label: "E217532: $target",
target:
"\\frac{\\left(y+0\\right)^2}{16}-\\frac{\\left(x+2\\right)^2}{9}=1",
eq: [
"\\frac{y^2}{16}-\\frac{(x+2)^2}{9}=1",
"-\\frac{(x+2)^2}{9}+\\frac{y^2}{16}=1",
"1=\\frac{y^2}{16}-\\frac{(x+2)^2}{9}",
"9y^2-16(x+2)^2=144",
"9y^2-16x^2-64x-64=144",
"9y^2-16x^2-64x-208=0",
"\\frac{(x+2)^2}{9}-\\frac{y^2}{16}=-1",
],
ne: [
// change either or both of the exponents
"\\frac{\\left(y+0\\right)^3}{16}-\\frac{\\left(x+2\\right)^2}{9}=1",
"\\frac{\\left(y+0\\right)^2}{16}-\\frac{\\left(x+2\\right)^3}{9}=1",
"\\frac{\\left(y+0\\right)^3}{16}-\\frac{\\left(x+2\\right)^3}{9}=1",
// change y+0 to 2y+4
"\\frac{\\left(2y+4\\right)^2}{16}-\\frac{\\left(x+2\\right)^2}{9}=1",
// change 16 to 17
"\\frac{\\left(y+0\\right)^2}{17}-\\frac{\\left(x+2\\right)^2}{9}=1",
// change x+2 to 3x+7
"\\frac{\\left(y+0\\right)^2}{16}-\\frac{\\left(3x+7\\right)^2}{9}=1",
// change 9 to 11
"\\frac{\\left(y+0\\right)^2}{16}-\\frac{\\left(x+2\\right)^2}{11}=1",
// change 1 to 5
"\\frac{\\left(y+0\\right)^2}{16}-\\frac{\\left(x+2\\right)^2}{9}=5",
// change a variable
"\\frac{\\left(a+0\\right)^2}{16}-\\frac{\\left(x+2\\right)^2}{9}=1",
],
},
{
label: "E232686: $target",
target: "\\left(y-7\\right)^2=60\\left(x-15\\right)",
eq: [
"60(x-15)=(y-7)^2",
"y^2-14y+49=60x-900",
"y^2-14y-60x+949=0",
"\\frac{(y-7)^2}{60}=x-15",
"x=\\frac{(y-7)^2}{60}+15",
],
ne: [
// change the exponent 2 to 3
"\\left(y-7\\right)^3=60\\left(x-15\\right)",
// change y-7 to 2y-8
"\\left(2y-8\\right)^2=60\\left(x-15\\right)",
// change 60 to 40
"\\left(y-7\\right)^2=40\\left(x-15\\right)",
// change x-15 to 4x-14
"\\left(y-7\\right)^2=60\\left(4x-14\\right)",
// add an exponent 2 to (x-15)
"\\left(y-7\\right)^2=60\\left(x-15\\right)^2",
// change a variable
"\\left(a-7\\right)^2=60\\left(x-15\\right)",
],
},
{
label: "E234938: $target",
target: "\\frac{x^2}{64}+\\frac{y^2}{36}=1",
eq: [
// the item's alternate correct answer
"36x^2+64y^2=2,304",
"\\frac{y^2}{36}+\\frac{x^2}{64}=1",
"1=\\frac{x^2}{64}+\\frac{y^2}{36}",
"9x^2+16y^2=576",
"36x^2+64y^2-2304=0",
"\\frac{x^2}{64}=1-\\frac{y^2}{36}",
],
ne: [
// change x^2 to x^4
"\\frac{x^4}{64}+\\frac{y^2}{36}=1",
// change 64 to 6
"\\frac{x^2}{6}+\\frac{y^2}{36}=1",
// change y^2 to 3y^(-2)
"\\frac{x^2}{64}+\\frac{3y^{-2}}{36}=1",
// change 36 to 360
"\\frac{x^2}{64}+\\frac{y^2}{360}=1",
// change 1 to 3
"\\frac{x^2}{64}+\\frac{y^2}{36}=3",
// all of the above
"\\frac{x^4}{6}+\\frac{3y^{-2}}{360}=3",
// change a variable
"\\frac{x^2}{64}+\\frac{z^2}{36}=1",
],
},
{
label: "E234938 alternate: $target",
target: "36x^2+64y^2=2,304",
eq: ["\\frac{x^2}{64}+\\frac{y^2}{36}=1", "9x^2+16y^2=576"],
ne: [
"36x^4+64y^2=2,304",
"36x^2+64y^2=2,305",
"36x^2+64y^{-2}=2,304",
],
},
{
label: "E234162: $target",
target: "\\frac{\\left(x+10\\right)^2}{275}+\\frac{y^2}{900}=1",
eq: [
"\\frac{y^2}{900}+\\frac{(x+10)^2}{275}=1",
"1=\\frac{(x+10)^2}{275}+\\frac{y^2}{900}",
"900(x+10)^2+275y^2=247500",
"36(x+10)^2+11y^2=9900",
"36x^2+720x+3600+11y^2=9900",
],
ne: [
// change x+10 to 2x+11
"\\frac{\\left(2x+11\\right)^2}{275}+\\frac{y^2}{900}=1",
// change 275 to 27.5
"\\frac{\\left(x+10\\right)^2}{27.5}+\\frac{y^2}{900}=1",
// change y^2 to 3y^8
"\\frac{\\left(x+10\\right)^2}{275}+\\frac{3y^8}{900}=1",
// change 900 to 3
"\\frac{\\left(x+10\\right)^2}{275}+\\frac{y^2}{3}=1",
// change 1 to 5
"\\frac{\\left(x+10\\right)^2}{275}+\\frac{y^2}{900}=5",
// change a variable
"\\frac{\\left(x+10\\right)^2}{275}+\\frac{z^2}{900}=1",
],
},
{
// both sides multiplied by a denominator that contains a variable
label: "$target",
target: "\\frac{y-1}{x-2}=3",
eq: ["y-1=3(x-2)", "y-1=3x-6", "y=3x-5"],
ne: ["y-1=3(x+2)", "y-1=2(x-2)"],
},
{
label: "$target",
target: "y=\\frac{2x+1}{x-3}",
eq: ["y(x-3)=2x+1", "xy-3y=2x+1"],
ne: ["y(x-3)=2x-1", "y(x+3)=2x+1"],
},
{
label: "$target",
target: "y=\\frac{4}{x+3}",
eq: ["xy+3y=4", "y(x+3)=4"],
ne: ["xy+3y=5", "xy=4"],
},
{
// these only differ for negative values of x or y
label: "$target",
target: "x^3=8",
eq: ["2x^3=16"],
ne: [
"\\sqrt{x^6}=8",
"\\left|x\\right|^3=8",
"\\left|x^3\\right|=8",
],
},
{
label: "$target",
target: "y=x",
eq: ["2y=2x", "y-x=0"],
ne: ["y=\\left|x\\right|", "y=\\sqrt{x^2}", "\\left|y\\right|=x"],
},
{
// with the function in the target, the numeric check decides
label: "$target",
target: "y=\\left|x\\right|",
ne: ["y=x", "y=-x"],
},
{
label: "$target",
target: "\\left|x\\right|^3=8",
ne: ["x^3=8"],
},
{
label: "$target",
target: "y=\\sqrt{x^2}",
ne: ["y=x"],
},
{
// high powers skip the numerator comparison, which would otherwise take tens of seconds
label: "$target",
target: "\\frac{(x+1)^7}{(y-1)^7}=1",
ne: ["(x+1)^7=2(y-1)^7"],
},
],
};
31 changes: 29 additions & 2 deletions src/symbolic/compare-equations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
solveQuadraticEquation,
hasSymbolicExponent,
areNumericallyEquivalent,
areEquationsNumericallyEquivalent,
} from "./utils";

const m: any = mathjs;
Expand Down Expand Up @@ -142,9 +143,33 @@ export const compareEquations = (
equivalence =
solutionForFirstEquation === solutionForSecondEquation &&
solutionForFirstEquation !== undefined;

// equations that are neither linear nor quadratic can't be solved above; compare them numerically instead
if (
!isInequality &&
(solutionForFirstEquation === undefined ||
solutionForSecondEquation === undefined)
) {
return areEquationsNumericallyEquivalent(
firstExpression,
secondExpression,
firstEquationVariablesName
);
}
}

// equations in two variables describe the same curve when one is a multiple of the other;
// checking a single value of x is not enough, and fails for equations that are not linear in y (PIE-443)
if (firstEquationVariablesName.length === 2 && !isInequality) {
return areEquationsNumericallyEquivalent(
firstExpression,
secondExpression,
firstEquationVariablesName,
{ allowVariableFactor: true }
);
}

// if both equations are linear in two variabled then we give value "1" for both "x". Doing this we get a linear equation in one variable "y". Then we solve "y" for both. If y has the same value then equations are equivalent
// if both inequalities are linear in two variabled then we give value "1" for both "x". Doing this we get a linear inequality in one variable "y". Then we solve "y" for both. If y has the same value then inequalities are equivalent
if (firstEquationVariablesName.length === 2) {
let x = firstEquationVariablesName[0];

Expand All @@ -162,7 +187,9 @@ export const compareEquations = (
);

// if y has the same value, for the same x then the expressions should be equivalent
equivalence = yFromFirstExpression === yFromSecondExpression;
equivalence =
yFromFirstExpression === yFromSecondExpression &&
yFromFirstExpression !== undefined;
}

// determine equivalence between 2-way inequalities with 1 or 2 variables:
Expand Down
Loading
Loading