-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from pie-framework/beta
feat: linear-equation-equivalence
- Loading branch information
Showing
12 changed files
with
516 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/fixtures/latex-equal/symbolic/linear-equations-in-one-variable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}, | ||
], | ||
}; |
51 changes: 51 additions & 0 deletions
51
src/fixtures/latex-equal/symbolic/linear-equations-in-two-variables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.