Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
89ae164
fix(symbolic): sample distinct non-integer values per variable in num…
CarlaCostea Oct 28, 2026
8a7c141
fix(symbolic): stop scoring unsolvable two-variable equations as equi…
CarlaCostea Oct 28, 2026
fc4a2ce
test(symbolic): cover numeric sampling and equation comparison (PIE-443)
CarlaCostea Oct 28, 2026
aa7defb
test(symbolic): add fixtures for two-variable equations with powers (…
CarlaCostea Oct 29, 2026
be10199
fix(symbolic): compare two-variable inequalities by region instead of…
CarlaCostea Nov 3, 2026
18bcb27
Merge remote-tracking branch 'origin/develop' into fix/PIE-443
CarlaCostea Nov 3, 2026
5d17d47
fix(symbolic): sample negative values and compare numerators in numer…
CarlaCostea Nov 3, 2026
1cdaa6e
test(symbolic): add fixtures for variable denominators and negative v…
CarlaCostea Nov 3, 2026
181821d
test(symbolic): cover targets with absolute values and square roots (…
CarlaCostea Nov 3, 2026
a789b02
Merge branch 'fix/PIE-443' into fix/PIE-1419
CarlaCostea Nov 3, 2026
9fbeec1
refactor(symbolic): remove setXToOne, no longer used (PIE-1419)
CarlaCostea Nov 3, 2026
260d09f
test(symbolic): cover positiveOnly and inequalities that differ for n…
CarlaCostea Nov 3, 2026
a89d44d
refactor(symbolic): remove the unused setXToOne helper (PIE-1419)
CarlaCostea Nov 3, 2026
7f6fd77
fix(symbolic): accept inequalities multiplied by an even power of a v…
CarlaCostea Nov 3, 2026
a7ca8e1
fix(symbolic): skip the numerator comparison for high powers and long…
CarlaCostea Nov 3, 2026
5b01946
Merge branch 'fix/PIE-443' into fix/PIE-1419
CarlaCostea Nov 3, 2026
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
168 changes: 147 additions & 21 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { simplify } from "../symbolic";
import {
getVariables,
getCoefficients,
setXToOne,
solveLinearEquation,
solveQuadraticEquation,
expressionsCanBeCompared,
transformEqualityInExpression,
getSamplePoints,
areNumericallyEquivalent,
areEquationsNumericallyEquivalent,
} from "../symbolic/utils";

const lta = new LatexToAst();
Expand Down Expand Up @@ -137,26 +139,6 @@ describe("getCoefficients", () => {
});
});

describe("setXToOne", () => {
it.each`
expression | expressionNoX
${"x+0"} | ${"1+0"}
${"x +1"} | ${"1+1"}
${"((x^2 + x) / x) - 1"} | ${"((1^2 + 1) / 1) - 1"}
${"1+2"} | ${"1+2"}
${"a +1+c"} | ${"a+1+c"}
${"y^2+5x - 1"} | ${"y^2+5*1 - 1"}
${"109h"} | ${"109h"}
${"m+n+10"} | ${"m+n+10"}
`("$expression => $expressionNoX", ({ expression, expressionNoX }) => {
const equation = atm.convert(lta.convert(expression));
const equationNoX = setXToOne(equation, "x");
const result = atm.convert(lta.convert(expressionNoX));

expect(equationNoX.toTex()).toEqual(result.toTex());
});
});

describe("solveLinearEquation", () => {
it.each`
coefficients | xValue
Expand Down Expand Up @@ -250,3 +232,147 @@ 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>2x+1" and "2y>4x+2" are the same inequality: one is a positive multiple of the other', () => {
const result = areEquationsNumericallyEquivalent(
expression("y>2x+1"),
expression("2y>4x+2"),
["x", "y"],
{ positiveOnly: true }
);

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

it('"y>2x+1" and "-y>-2x-1" are different inequalities: the multiple is negative', () => {
const first = expression("y>2x+1");
const second = expression("-y>-2x-1");

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

it('inequalities allow multiplying by an even power of a variable, but not an odd one', () => {
const options = { allowVariableFactor: true, positiveOnly: true };

expect(
areEquationsNumericallyEquivalent(
expression("y>x"),
expression("x^2y>x^3"),
["x", "y"],
options
)
).toEqual(true);
expect(
areEquationsNumericallyEquivalent(
expression("y>x"),
expression("xy>x^2"),
["x", "y"],
options
)
).toEqual(false);
});

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);
});
});
81 changes: 81 additions & 0 deletions src/fixtures/latex-equal/symbolic/PIE-1419.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// two-variable inequalities must describe the same region, not just agree at x = 1
export default {
mode: "symbolic",
tests: [
{
label: "$target",
target: "y>2x+1",
eq: [
"y-2x>1",
"y-2x-1>0",
"2y>4x+2",
"2x+1<y",
"-y<-2x-1",
"-y+2x+1<0",
],
ne: [
// same boundary value at x = 1, different slope
"y>3x",
"y>x+2",
// same line, opposite side
"y<2x+1",
"y\\ge2x+1",
"-y>-2x-1",
],
},
{
label: "$target",
target: "3x-2y\\ge6",
eq: ["3x-6\\ge2y", "-3x+2y\\le-6", "6x-4y\\ge12", "2y\\le3x-6"],
ne: ["3x-2y\\ge5", "2x-3y\\ge6", "3x-2y\\le6", "3x+2y\\ge6"],
},
{
// not linear in y, so the x = 1 check could not solve it
label: "$target",
target: "x^2+y^2\\le4",
eq: ["4\\ge x^2+y^2", "2x^2+2y^2\\le8", "x^2+y^2-4\\le0"],
ne: ["x^2+y^2\\le5", "x^2+2y^2\\le4", "x^2+y^2\\ge4"],
},
{
label: "$target",
target: "y^2>x",
eq: ["2y^2>2x", "x<y^2", "y^2-x>0"],
ne: ["y^2>2x", "y^3>x", "y^2<x"],
},
{
// these only describe the same region where x or y is positive
label: "$target",
target: "y>x",
eq: ["y-x>0", "2y>2x", "x<y"],
ne: [
"\\frac{y}{x}>1",
"\\frac{1}{y}<\\frac{1}{x}",
"y\\cdot y>x\\cdot y",
"(y-x)x>0",
],
},
{
label: "$target",
target: "xy>1",
ne: ["y>\\frac{1}{x}"],
},
{
// both sides multiplied or divided by an even power of a variable, which can't reverse the sign
label: "$target",
target: "y>x^2",
eq: ["\\frac{y}{x^2}>1", "x^2y>x^4"],
ne: ["\\frac{y}{x}>x", "xy>x^3"],
},
{
label: "$target",
target: "y>2x+1",
eq: ["x^2y>x^2(2x+1)"],
},
{
// with the function in the target, the numeric check decides
label: "$target",
target: "y>\\left|x\\right|",
ne: ["y>x", "y>-x"],
},
],
};
Loading
Loading