From 77514d20b7e542b1675bedf93ebe162500e4fefc Mon Sep 17 00:00:00 2001 From: Carla Costea Date: Tue, 26 Oct 2021 16:49:33 +0300 Subject: [PATCH] refactor code and format --- .../compare-compound-inequations.spec.ts | 32 +++++++++---------- src/__tests__/utils.spec.ts | 4 +-- src/conversion/latex-to-ast.ts | 2 -- src/symbolic/compare-compound-inequations.ts | 3 +- src/symbolic/compare-equations.ts | 7 ++-- src/symbolic/index.ts | 4 ++- src/symbolic/utils.ts | 11 ++----- 7 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/__tests__/compare-compound-inequations.spec.ts b/src/__tests__/compare-compound-inequations.spec.ts index 2cd2ece..4c551e4 100644 --- a/src/__tests__/compare-compound-inequations.spec.ts +++ b/src/__tests__/compare-compound-inequations.spec.ts @@ -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 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 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); } ); }); diff --git a/src/__tests__/utils.spec.ts b/src/__tests__/utils.spec.ts index 4c5d2ef..e067b58 100644 --- a/src/__tests__/utils.spec.ts +++ b/src/__tests__/utils.spec.ts @@ -8,7 +8,7 @@ import { setXToOne, solveLinearEquation, expressionsCanBeCompared, - transformEqualityInExpression + transformEqualityInExpression, } from "../symbolic/utils"; const lta = new LatexToAst(); @@ -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"} | ${[]} diff --git a/src/conversion/latex-to-ast.ts b/src/conversion/latex-to-ast.ts index 5ad05d0..50702a0 100644 --- a/src/conversion/latex-to-ast.ts +++ b/src/conversion/latex-to-ast.ts @@ -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"], @@ -739,7 +738,6 @@ export class LatexToAst { case "ge": return "largerEq"; case "NE": - return "unequal"; case "ne": return "unequal"; } diff --git a/src/symbolic/compare-compound-inequations.ts b/src/symbolic/compare-compound-inequations.ts index f92c437..0a9ea2b 100644 --- a/src/symbolic/compare-compound-inequations.ts +++ b/src/symbolic/compare-compound-inequations.ts @@ -1,5 +1,5 @@ import { mathjs } from "../mathjs"; -import { MathNode, unequal } from "mathjs"; +import { MathNode } from "mathjs"; import { expressionsCanBeCompared, equationsHaveTheSameVariables, @@ -75,7 +75,6 @@ export const compareCompoundInequations = ( secondInequation: any ) => { - if (!expressionsCanBeCompared(firstInequation, secondInequation)) { return false; } diff --git a/src/symbolic/compare-equations.ts b/src/symbolic/compare-equations.ts index ea36ece..447a325 100644 --- a/src/symbolic/compare-equations.ts +++ b/src/symbolic/compare-equations.ts @@ -83,7 +83,7 @@ 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]) && @@ -91,9 +91,8 @@ export const compareEquations = ( (m.isNegative(firstEquationCoefficients[0]) && m.isPositive(firstEquationCoefficients[1]) && m.isPositive(secondEquationCoefficients[0]) && - m.isNegative(secondEquationCoefficients[1])); - - return !signShouldBeChanged; + m.isNegative(secondEquationCoefficients[1])) + ); } } diff --git a/src/symbolic/index.ts b/src/symbolic/index.ts index 0a0c140..96ff068 100644 --- a/src/symbolic/index.ts +++ b/src/symbolic/index.ts @@ -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", diff --git a/src/symbolic/utils.ts b/src/symbolic/utils.ts index 1f439bc..d63eb0f 100644 --- a/src/symbolic/utils.ts +++ b/src/symbolic/utils.ts @@ -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) => { @@ -53,9 +50,7 @@ export const getVariables = (equation: MathNode) => { } }); - variableNames.sort(); - - return variableNames; + return variableNames.sort(); }; export const getCoefficients = (equation: MathNode) => {