cortex-js / compute-engine

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON
https://cortexjs.io
MIT License
356 stars 42 forks source link

expression.solve failed basic math #160

Closed Seancheey closed 4 months ago

Seancheey commented 4 months ago

Include code fragments or CodePen.io links to illustrate the issue. Do not include screenshots of code.

ce is failing the following unit tests after I upgrade from 12.3 to latest version.

test("solveEquation", async () => {
    // failed
    expect(solveEquation("3x^2 = 15")).toContain("\\sqrt{5}");
    expect(solveEquation("3x^2-10 = 5")).toEqual("\\sqrt{5}");
});
export function solveEquation(equationExpression: string): string[] {
    const expression = computeEngine.parse(equationExpression);
    const symbols = expression.symbols;
    if (symbols.length === 0) return [];
    if (symbols.length > 1) throw new Error("Can only solve for one variable at a time");
    return expression.solve(symbols[0])?.map((x) => x.latex) || [];
}

Expected Behavior

The above equation should return \\sqrt{5} but instead returned \frac{\sqrt{15}}{3}

arnog commented 4 months ago

There was a typo in the rule for this form of equation.