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

Handling of Non-Associative Operations with more than two operands #171

Closed lpettenella closed 2 months ago

lpettenella commented 2 months ago

Hello,

I've been using Cortex.js for a project that I'm working on and I've noticed an issue with the handling of non-associative operations, specifically subtraction and division.

When I pass an array like ["Subtract", 24, 10, 3, 3] or ["Divide", 24, 4, 3] using new ComputeEngine().box(expr).evaluate().latex, Cortex.js seems to only consider the first two operands and ignores the rest. For example, ["Subtract", 24, 10, 3, 3] gives as a result 24 - 10, ignoring the 3 and 3.

However, my intention with these expressions is to perform the operations sequentially from left to right, i.e., ((24 - 10) - 3) - 3 for subtraction and (24 / 4) / 3 for division.

I understand that subtraction and division are not associative operations, but it would be helpful if Cortex.js could interpret these expressions in the way I described, at least giving an option to perform this calculation without needing to format a simple operation like ["Divide", 16, 2, 2] to ["Divide", ["Divide", 16, 2], 2]. This behavior is especially confusing because the "Add" operation does handle multiple operands as expected.

Could you please look into this issue? I believe it would make Cortex.js more intuitive and flexible for users performing complex calculations.

Thank you.