Skip to content

Commit

Permalink
Merge pull request #44 from pie-framework/beta
Browse files Browse the repository at this point in the history
feat: linear-equation-equivalence
  • Loading branch information
CarlaCostea authored Oct 4, 2021
2 parents 73b69a3 + 6311ece commit 3e4350d
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 64 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@semantic-release/github": "^7.2.3",
"@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3",
"prettier": "^2.3.2",
"@types/fs-extra": "^9.0.1",
"@types/glob": "^7.1.3",
"@types/jest": "^26.0.14",
Expand All @@ -53,6 +52,7 @@
"latex-utensils": "^2.0.3",
"latex.js": "^0.12.4",
"minimist": "^1.2.5",
"prettier": "^2.3.2",
"rollup": "^2.50.2",
"rollup-plugin-cleanup": "^3.2.1",
"semantic-release": "^17.4.7",
Expand Down
171 changes: 171 additions & 0 deletions src/__tests__/compare-equations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { AstToMathJs } from "../conversion/ast-to-mathjs";
import { LatexToAst } from "../conversion/latex-to-ast";
import {
getUnknowns,
getCoefficients,
setXToOne,
solveLinearEquation,
} from "../symbolic/compare-equations";

const lta = new LatexToAst();
const atm = new AstToMathJs();

describe("getUnknowns", () => {
it.each`
expression | unknowns
${"x"} | ${["x"]}
${"x +1"} | ${["x"]}
${"((x^2 + x) / x) - 1"} | ${["x"]}
${"1+2"} | ${[]}
${"a +1+c"} | ${["a", "c"]}
${"((y^2 + z) / x) - 1"} | ${["x", "y", "z"]}
${"109h"} | ${["h"]}
${"m+n+10"} | ${["m", "n"]}
`("$expression => $unknowns", ({ expression, unknowns }) => {
const equation = atm.convert(lta.convert(expression));
const unknownsName = getUnknowns(equation);

expect(unknownsName).toEqual(unknowns);
});
});

describe("getCoefficients", () => {
it.each`
expression | coefficients
${"x+0"} | ${[0, 1]}
${"x +1"} | ${[1, 1]}
${"((x^2 + x) / x) - 1"} | ${[0, 0, 1]}
${"1+2"} | ${[1, 0]}
${"a +1+c"} | ${[1, 0]}
${"y^2+5y - 1"} | ${[-1, 5, 1]}
${"2y^2+4y"} | ${[0, 4, 2]}
${"109h"} | ${[0, 109]}
${"m+n+10"} | ${[1, 0]}
${"x-x"} | ${[0, 0]}
${"x + 5 - 3 + x - 6 - x + 2"} | ${[-2, 1]}
${"2x-x"} | ${[0, 1]}
${"x - x - 2"} | ${[1, 0]}
`("$expression => $coefficients", ({ expression, coefficients }) => {
const equation = atm.convert(lta.convert(expression));
const coefficientsList = getCoefficients(equation);

expect(coefficientsList).toEqual(coefficients);
});
});

describe("getCoefficients", () => {
it('equation: "x = x" - has coefficients [0, 0]', () => {
const equation = atm.convert(lta.convert("x-x"));
const coefficientsList = getCoefficients(equation);

expect(coefficientsList).toEqual([0, 0]);
});

it('equation: "1 = -2" - if equation has no coefficient for x it will return coefficients [1, 0]', () => {
const equation = atm.convert(lta.convert("1+2"));
const coefficientsList = getCoefficients(equation);

expect(coefficientsList).toEqual([1, 0]);
});

it('equation: "m + n = - 2" - if equation has more than one variable, will return coefficients [1, 0]', () => {
const equation = atm.convert(lta.convert("m+n = - 2"));
const coefficientsList = getCoefficients(equation);

expect(coefficientsList).toEqual([1, 0]);
});
});

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
${[0, 1]} | ${0}
${[1, 1]} | ${-1}
${[0, 0, 1]} | ${0}
${[]} | ${undefined}
${[-1, 5, 1]} | ${undefined}
${[0, 109]} | ${0}
`("$coefficients => $xValue", ({ coefficients, xValue }) => {
const result = solveLinearEquation(coefficients);

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

describe("solveLinearEquation", () => {
it('equation: "x = x" - has infinite solutions', () => {
const coefficients = [0, 0];
const result = solveLinearEquation(coefficients);

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

it('equation: "x + 5 - 3 + x = 6 + x - 2" - solution should be 2', () => {
const coefficients = [-2, 1];
const result = solveLinearEquation(coefficients);

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

it('equation: "2x = x" - solution should be 0', () => {
const coefficients = [0, 1];
const result = solveLinearEquation(coefficients);

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

it('equation: "x = x + 2" - if equation has no solution it will return - Infinity', () => {
const coefficients = [2, 0];
const result = solveLinearEquation(coefficients);

expect(result).toEqual(-Infinity);
});

it('equation: "3x - 2x = x + 7 + 9" - if equation has no solution it will return - Infinity', () => {
const coefficients = [16, 0];
const result = solveLinearEquation(coefficients);

expect(result).toEqual(-Infinity);
});

it('equation: "2x^2 = 2x" - has no solution', () => {
const coefficients = [0, 4, 2];
const result = solveLinearEquation(coefficients);

expect(result).toEqual(-2);
});

it('equation: "y^2+5y - 1" - if equation is quadratic, result is undefined', () => {
const coefficients = [-1, 5, 1];
const result = solveLinearEquation(coefficients);

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

it('equation: "y^2+5y + 1" - if equation is quadratic, result is undefined', () => {
const coefficients = [1, 5, 1];
const result = solveLinearEquation(coefficients);

expect(result).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
{
target: "4=4",
eq: ["4=4", "2+2=4"],
// ne: ["1", "3=3", "9=8", "a=b", "0=0", "\\true=\\true"],
ne: ["1", "3=3", "9=8", "a=b", "0=0", "\\true=\\true"],
},
{
target: "4=4=2+2",
Expand Down
38 changes: 18 additions & 20 deletions src/fixtures/latex-equal/symbolic/equiv-symbolic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default {
{
// equation with variable
target: "y= \\frac{1}{2}x +5",

eq: [
// flip sides of equation
"\\frac{1}{2}x +5 = y",
Expand All @@ -21,19 +20,19 @@ export default {
"y = 0.5x+5 ",
// Use division sign rather than fraction form
"y =x÷2 +5",
// // Multiply both sides of the equation by a non-zero number
// "-2y =-x -10",
// Multiply both sides of the equation by a non-zero number
"-2y =-x -10",
// Add a number to both sides of the equation
"y-5 =\\frac{x}{2}",
"-5 =\\frac{1}{2}x-y",
// Subtract one of the variables from both sides of the equation
"0=x/2-y+5",
// // Add a new variable to both sides of the equation
// "z=x/2-y+5 +z",
// // Divide both sides of the equation by y
// "1=\\frac{x}{2y}+\\frac{5}{y}",
// // Combine several of the above
// "x-2y+z=z-10",
// Add a new variable to both sides of the equation
"z=x/2-y+5 +z",
// Divide both sides of the equation by y TO DO- equation is transformed in a quadratic eequation
"1=\\frac{x}{2y}+\\frac{5}{y}",
// Combine several of the above
"x-2y+z=z-10",
],
ne: [
"\\frac{1}{2}x +5 = x",
Expand Down Expand Up @@ -61,17 +60,16 @@ export default {
// target: "2x<4",
// eq: ["4>2x", "x+x < 2*2", "x<2"],
// },
// {
// // 3-way inequality with variable(s)
// target: "1<2x ≤ 3",
// eq: ["3 ≥ 2x>1", "x/x<x+x≤1.5*2", "1/2<x ≤1.5"], //
// },
// {
// // inequality with no variable(s)
// target: "1<2",
// eq: ["0<1", "x+1>x"],
// },
// expression (no equal or inequality sign)
{
// 3-way inequality with variable(s)
target: "1<2x ≤ 3",
eq: [
"3 ≥ 2x>1",
"x/x<x+x≤1.5*2",
// "1/2<x ≤1.5"
],
},
// expressions (no equal or inequality sign)
{
target: "\\frac{1}{3}*(8c)-5",
eq: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export default {
mode: "symbolic",
tests: [
{
target: "x=9",
eq: ["30-2x=12", "10+10-3x+x+10=12", "\\frac{1}{x}=\\frac{1}{9}"],
ne: ["31-2x=12"],
},
{
target: "a=9",
eq: ["30-2a=12"],
ne: ["31-2x=12", "b=9"],
},
{
// infinite solutions equations
target: "a=a",
eq: ["2a=2a"],
ne: ["12=12", "a=0"],
},
{
// infinite solutions equations
target: "5c+5c=5c+5c",
eq: ["10c=10c", "c=c"],
ne: ["12=12"],
},
{
// if equations have no solutions they will not be equivalent
target: "x=x+2",
ne: ["2x=2x+4"],
},
{
target: "((x^2 + x) / x) - 1=0",
eq: ["3x=0", "109x=0"],
},
{
target: "q=9",
eq: ["15-q-6=0"],
ne: ["31-2q=12", "qa=9", "q=q"],
},
{
target: "2x+3=8",
eq: [
"4x+6=16",
"2x=5",
"x=2.5",
"x=\\frac{5}{2}",
"2.500000=x",
"2x+3-8=0",
"2x+3-8-m=0-m",
],
ne: ["5x+6=16", "2x+4-8=0", "x+3=4"],
},
{
target: "4x+2=6x-4",
eq: ["-2x+6=0"],
},
{
target: "y^2+5y - 1=0",
ne: ["y^2+5y +1=0"],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export default {
mode: "symbolic",
tests: [
{
target: "y= \\frac{1}{2}x +5",
eq: [
// Multiply both sides of the equation by a non-zero number
"-2y =-x -10",
// Divide both sides of the equation by y
"1=\\frac{x}{2y}+\\frac{5}{y}",
// Combine several of the above
"x-2y+z=z-10",
// Multiply both sides of the equation by a variable
"y^2=\\frac{xy}{2}+5y",
],
},
{
// linear equations with 2 unknowns that are not x or y
target: "a=\\frac{1}{2}b +5",
eq: [
"-2a =-b -10",
"1=\\frac{b}{2a}+\\frac{5}{a}",
"b-2a+c=c-10",
],
},
{
// linear equations with 2 unknowns that are not x or y
target: "a=\\frac{1}{2}b +5",
eq: [
"-2a =-b -10",
"1=\\frac{b}{2a}+\\frac{5}{a}",
"b-2a+c=c-10",
],
},
{
// Multiply both sides of the equation by a variable
target: "x+2y=7",
eq: ["x*x+2yx-7x=0"],
},
{
// Multiply both sides of the equation by a variable
target: "x+2y=7",
eq: ["x^2+2yx-7x=0"],
},
{
// Divide both sides of the equation by a variable
target: "x+2y=7",
eq: ["\\frac{x}{y}+2-7/y=0", "\\frac{x}{x}+\\frac{2y}{x}=\\frac{7}{x}"],
},
],
};
12 changes: 12 additions & 0 deletions src/fixtures/latex-equal/symbolic/quadratic-equations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
mode: "symbolic",

tests: [
{
// // quadratic equation
//only: true,
// target: "x^2-7x+10=0",
// eq: ["2x^2-14x+20=0"],
},
],
};
2 changes: 1 addition & 1 deletion src/latex-equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export const latexEqual = (a: Latex, b: Latex, opts: Opts) => {
if (opts.mode === "literal") {
return isLiteralEqual(amo, bmo, opts.literal);
} else {
return isSymbolicEqual(amo, bmo, opts.symbolic);
return isSymbolicEqual(amo, bmo);
}
};
Loading

0 comments on commit 3e4350d

Please sign in to comment.