|
| 1 | +import { AstToMathJs } from "../conversion/ast-to-mathjs"; |
| 2 | +import { LatexToAst } from "../conversion/latex-to-ast"; |
| 3 | +import { |
| 4 | + getUnknowns, |
| 5 | + getCoefficients, |
| 6 | + setXToOne, |
| 7 | + solveLinearEquation, |
| 8 | +} from "../symbolic/compare-equations"; |
| 9 | + |
| 10 | +const lta = new LatexToAst(); |
| 11 | +const atm = new AstToMathJs(); |
| 12 | + |
| 13 | +describe("getUnknowns", () => { |
| 14 | + it.each` |
| 15 | + expression | unknowns |
| 16 | + ${"x"} | ${["x"]} |
| 17 | + ${"x +1"} | ${["x"]} |
| 18 | + ${"((x^2 + x) / x) - 1"} | ${["x"]} |
| 19 | + ${"1+2"} | ${[]} |
| 20 | + ${"a +1+c"} | ${["a", "c"]} |
| 21 | + ${"((y^2 + z) / x) - 1"} | ${["x", "y", "z"]} |
| 22 | + ${"109h"} | ${["h"]} |
| 23 | + ${"m+n+10"} | ${["m", "n"]} |
| 24 | + `("$expression => $unknowns", ({ expression, unknowns }) => { |
| 25 | + const equation = atm.convert(lta.convert(expression)); |
| 26 | + const unknownsName = getUnknowns(equation); |
| 27 | + |
| 28 | + expect(unknownsName).toEqual(unknowns); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("getCoefficients", () => { |
| 33 | + it.each` |
| 34 | + expression | coefficients |
| 35 | + ${"x+0"} | ${[0, 1]} |
| 36 | + ${"x +1"} | ${[1, 1]} |
| 37 | + ${"((x^2 + x) / x) - 1"} | ${[0, 0, 1]} |
| 38 | + ${"1+2"} | ${[1, 0]} |
| 39 | + ${"a +1+c"} | ${[1, 0]} |
| 40 | + ${"y^2+5y - 1"} | ${[-1, 5, 1]} |
| 41 | + ${"2y^2+4y"} | ${[0, 4, 2]} |
| 42 | + ${"109h"} | ${[0, 109]} |
| 43 | + ${"m+n+10"} | ${[1, 0]} |
| 44 | + ${"x-x"} | ${[0, 0]} |
| 45 | + ${"x + 5 - 3 + x - 6 - x + 2"} | ${[-2, 1]} |
| 46 | + ${"2x-x"} | ${[0, 1]} |
| 47 | + ${"x - x - 2"} | ${[1, 0]} |
| 48 | + `("$expression => $coefficients", ({ expression, coefficients }) => { |
| 49 | + const equation = atm.convert(lta.convert(expression)); |
| 50 | + const coefficientsList = getCoefficients(equation); |
| 51 | + |
| 52 | + expect(coefficientsList).toEqual(coefficients); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("getCoefficients", () => { |
| 57 | + it('equation: "x = x" - has coefficients [0, 0]', () => { |
| 58 | + const equation = atm.convert(lta.convert("x-x")); |
| 59 | + const coefficientsList = getCoefficients(equation); |
| 60 | + |
| 61 | + expect(coefficientsList).toEqual([0, 0]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('equation: "1 = -2" - if equation has no coefficient for x it will return coefficients [1, 0]', () => { |
| 65 | + const equation = atm.convert(lta.convert("1+2")); |
| 66 | + const coefficientsList = getCoefficients(equation); |
| 67 | + |
| 68 | + expect(coefficientsList).toEqual([1, 0]); |
| 69 | + }); |
| 70 | + |
| 71 | + it('equation: "m + n = - 2" - if equation has more than one variable, will return coefficients [1, 0]', () => { |
| 72 | + const equation = atm.convert(lta.convert("m+n = - 2")); |
| 73 | + const coefficientsList = getCoefficients(equation); |
| 74 | + |
| 75 | + expect(coefficientsList).toEqual([1, 0]); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe("setXToOne", () => { |
| 80 | + it.each` |
| 81 | + expression | expressionNoX |
| 82 | + ${"x+0"} | ${"1+0"} |
| 83 | + ${"x +1"} | ${"1+1"} |
| 84 | + ${"((x^2 + x) / x) - 1"} | ${"((1^2 + 1) / 1) - 1"} |
| 85 | + ${"1+2"} | ${"1+2"} |
| 86 | + ${"a +1+c"} | ${"a+1+c"} |
| 87 | + ${"y^2+5x - 1"} | ${"y^2+5*1 - 1"} |
| 88 | + ${"109h"} | ${"109h"} |
| 89 | + ${"m+n+10"} | ${"m+n+10"} |
| 90 | + `("$expression => $expressionNoX", ({ expression, expressionNoX }) => { |
| 91 | + const equation = atm.convert(lta.convert(expression)); |
| 92 | + const equationNoX = setXToOne(equation, "x"); |
| 93 | + const result = atm.convert(lta.convert(expressionNoX)); |
| 94 | + |
| 95 | + expect(equationNoX.toTex()).toEqual(result.toTex()); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe("solveLinearEquation", () => { |
| 100 | + it.each` |
| 101 | + coefficients | xValue |
| 102 | + ${[0, 1]} | ${0} |
| 103 | + ${[1, 1]} | ${-1} |
| 104 | + ${[0, 0, 1]} | ${0} |
| 105 | + ${[]} | ${undefined} |
| 106 | + ${[-1, 5, 1]} | ${undefined} |
| 107 | + ${[0, 109]} | ${0} |
| 108 | + `("$coefficients => $xValue", ({ coefficients, xValue }) => { |
| 109 | + const result = solveLinearEquation(coefficients); |
| 110 | + |
| 111 | + expect(result).toEqual(xValue); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("solveLinearEquation", () => { |
| 116 | + it('equation: "x = x" - has infinite solutions', () => { |
| 117 | + const coefficients = [0, 0]; |
| 118 | + const result = solveLinearEquation(coefficients); |
| 119 | + |
| 120 | + expect(result).toEqual(Infinity); |
| 121 | + }); |
| 122 | + |
| 123 | + it('equation: "x + 5 - 3 + x = 6 + x - 2" - solution should be 2', () => { |
| 124 | + const coefficients = [-2, 1]; |
| 125 | + const result = solveLinearEquation(coefficients); |
| 126 | + |
| 127 | + expect(result).toEqual(2); |
| 128 | + }); |
| 129 | + |
| 130 | + it('equation: "2x = x" - solution should be 0', () => { |
| 131 | + const coefficients = [0, 1]; |
| 132 | + const result = solveLinearEquation(coefficients); |
| 133 | + |
| 134 | + expect(result).toEqual(0); |
| 135 | + }); |
| 136 | + |
| 137 | + it('equation: "x = x + 2" - if equation has no solution it will return - Infinity', () => { |
| 138 | + const coefficients = [2, 0]; |
| 139 | + const result = solveLinearEquation(coefficients); |
| 140 | + |
| 141 | + expect(result).toEqual(-Infinity); |
| 142 | + }); |
| 143 | + |
| 144 | + it('equation: "3x - 2x = x + 7 + 9" - if equation has no solution it will return - Infinity', () => { |
| 145 | + const coefficients = [16, 0]; |
| 146 | + const result = solveLinearEquation(coefficients); |
| 147 | + |
| 148 | + expect(result).toEqual(-Infinity); |
| 149 | + }); |
| 150 | + |
| 151 | + it('equation: "2x^2 = 2x" - has no solution', () => { |
| 152 | + const coefficients = [0, 4, 2]; |
| 153 | + const result = solveLinearEquation(coefficients); |
| 154 | + |
| 155 | + expect(result).toEqual(-2); |
| 156 | + }); |
| 157 | + |
| 158 | + it('equation: "y^2+5y - 1" - if equation is quadratic, result is undefined', () => { |
| 159 | + const coefficients = [-1, 5, 1]; |
| 160 | + const result = solveLinearEquation(coefficients); |
| 161 | + |
| 162 | + expect(result).toEqual(undefined); |
| 163 | + }); |
| 164 | + |
| 165 | + it('equation: "y^2+5y + 1" - if equation is quadratic, result is undefined', () => { |
| 166 | + const coefficients = [1, 5, 1]; |
| 167 | + const result = solveLinearEquation(coefficients); |
| 168 | + |
| 169 | + expect(result).toEqual(undefined); |
| 170 | + }); |
| 171 | +}); |
0 commit comments