Skip to content

Commit

Permalink
refactor code and format
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlaCostea committed Oct 26, 2021
1 parent 109ff0b commit 77514d2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 35 deletions.
32 changes: 16 additions & 16 deletions src/__tests__/compare-compound-inequations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ const atm = new AstToMathJs();

describe("splitInequality", () => {
it.each`
compoundInequality | leftPart | rightPart
${"20>x>7"} | ${"20>x"} | ${"x>7"}
${" 5 ≥ -4 + x > -1 - 1"} | ${" 5 ≥ -4 + x"} | ${"-4 + x > -1 - 1"}
${"x/x<x+y≤1.5*2"} | ${"x/x<x+y"} | ${"x+y≤1.5*2"}
${ "2 < 4x > 20"} | ${"2<4x"} | ${"4x>20"}
${ "-3 < 2x+5 < 17"} | ${"-3<2x+5"} | ${"2x+5 < 17"}
${ "-3 = 6x = -3"} | ${"-3 = 6x"} | ${"6x = -3"}
${ "a≥b≥c "} | ${"a≥b"} | ${"b≥c"}
${ "a+2≥b-10≥c-100 "} | ${"a+2≥b-10"} | ${"b-10≥c-100"}
${ "a≠b≠c "} | ${"a≠b"} | ${"b≠c"}
compoundInequality | leftPart | rightPart
${"20>x>7"} | ${"20>x"} | ${"x>7"}
${" 5 ≥ -4 + x > -1 - 1"} | ${" 5 ≥ -4 + x"} | ${"-4 + x > -1 - 1"}
${"x/x<x+y≤1.5*2"} | ${"x/x<x+y"} | ${"x+y≤1.5*2"}
${"2 < 4x > 20"} | ${"2<4x"} | ${"4x>20"}
${"-3 < 2x+5 < 17"} | ${"-3<2x+5"} | ${"2x+5 < 17"}
${"-3 = 6x = -3"} | ${"-3 = 6x"} | ${"6x = -3"}
${"a≥b≥c "} | ${"a≥b"} | ${"b≥c"}
${"a+2≥b-10≥c-100 "} | ${"a+2≥b-10"} | ${"b-10≥c-100"}
${"a≠b≠c "} | ${"a≠b"} | ${"b≠c"}
`(
"$compoundInequality => $leftPart, $rightPart",
({ compoundInequality, leftPart, rightPart }) => {
const equation = atm.convert(lta.convert(compoundInequality));
const broken = splitInequality(equation);
const inequality = atm.convert(lta.convert(compoundInequality));
const broken = splitInequality(inequality);

const leftPartExpression = atm.convert(lta.convert(leftPart));
const rightPartExpression = atm.convert(lta.convert(rightPart));
const leftSide = atm.convert(lta.convert(leftPart));
const rightSide = atm.convert(lta.convert(rightPart));

expect(broken.left).toEqual(leftPartExpression);
expect(broken.right).toEqual(rightPartExpression);
expect(broken.left).toEqual(leftSide);
expect(broken.right).toEqual(rightSide);
}
);
});
4 changes: 2 additions & 2 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
setXToOne,
solveLinearEquation,
expressionsCanBeCompared,
transformEqualityInExpression
transformEqualityInExpression,
} from "../symbolic/utils";

const lta = new LatexToAst();
Expand Down Expand Up @@ -92,7 +92,7 @@ describe("getCoefficients", () => {
it.each`
expression | coefficients
${"x+0"} | ${[0, 1]}
${"2x^2 = 2x"} | ${[1, 0]}
${"2x^2 = 2x"} | ${[1, 0]}
${"x +1"} | ${[1, 1]}
${"((x^2 + x) / x) - 1"} | ${[0, 0, 1]}
${"1+2"} | ${[]}
Expand Down
2 changes: 0 additions & 2 deletions src/conversion/latex-to-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ export const latex_rules = [
["\\\\neq(?![a-zA-Z])", "NE"],
["\\\\ne(?![a-zA-Z])", "NE"],
["\\\\not\\s*=", "NE"],

["\\\\leq(?![a-zA-Z])", "LE"],
["\\\\le(?![a-zA-Z])", "LE"],
["\\\\geq(?![a-zA-Z])", "GE"],
Expand Down Expand Up @@ -739,7 +738,6 @@ export class LatexToAst {
case "ge":
return "largerEq";
case "NE":
return "unequal";
case "ne":
return "unequal";
}
Expand Down
3 changes: 1 addition & 2 deletions src/symbolic/compare-compound-inequations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mathjs } from "../mathjs";
import { MathNode, unequal } from "mathjs";
import { MathNode } from "mathjs";
import {
expressionsCanBeCompared,
equationsHaveTheSameVariables,
Expand Down Expand Up @@ -75,7 +75,6 @@ export const compareCompoundInequations = (
secondInequation: any
) => {


if (!expressionsCanBeCompared(firstInequation, secondInequation)) {
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions src/symbolic/compare-equations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@ export const compareEquations = (
// we have one distinct case, when multiplying both parts of an inequality with a negative number, the sign must change direction
if (equivalence && isInequality) {
// check if direction should be changed
let signShouldBeChanged =
return !(
(m.isPositive(firstEquationCoefficients[0]) &&
m.isNegative(firstEquationCoefficients[1]) &&
m.isNegative(secondEquationCoefficients[0]) &&
m.isPositive(secondEquationCoefficients[1])) ||
(m.isNegative(firstEquationCoefficients[0]) &&
m.isPositive(firstEquationCoefficients[1]) &&
m.isPositive(secondEquationCoefficients[0]) &&
m.isNegative(secondEquationCoefficients[1]));

return !signShouldBeChanged;
m.isNegative(secondEquationCoefficients[1]))
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/symbolic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ export const isMathEqual = (a: any, b: any) => {
//@ts-ignore
as?.conditionals?.length === bs?.conditionals?.length &&
//@ts-ignore
as?.conditionals?.length === 2 && as?.conditionals?.toString() === bs?.conditionals?.toString()
as?.conditionals?.length === 2 &&
//@ts-ignore
as?.conditionals?.toString() === bs?.conditionals?.toString()
) {
const params = [
"smaller",
Expand Down
11 changes: 3 additions & 8 deletions src/symbolic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ export const expressionsCanBeCompared = (
};

// move the terms of the equations to the left hand side
export const transformEqualityInExpression = (equality: MathNode) => {
const expression = new m.OperatorNode("-", "subtract", equality.args);

export const transformEqualityInExpression = (equality: MathNode) =>
// remove added/subtracted numbers/variables from both sides of the equation
return customSimplify(expression);
};
customSimplify(new m.OperatorNode("-", "subtract", equality.args));

// check if equation is valid and find out the number of variables and their name
export const getVariables = (equation: MathNode) => {
Expand All @@ -53,9 +50,7 @@ export const getVariables = (equation: MathNode) => {
}
});

variableNames.sort();

return variableNames;
return variableNames.sort();
};

export const getCoefficients = (equation: MathNode) => {
Expand Down

0 comments on commit 77514d2

Please sign in to comment.