mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Simplify leaves -(5*x)

Open gwhitney opened this issue 3 weeks ago • 2 comments

In reviewing #3719, I noticed that currently simplify ends up with terms like -(5 * x). The question of why that multiplication isn't shown as implicit aside, this result seems like an unnecessary level of parentheses. Before acting on #3719, which introduces several examples of this sort of expression into the unit tests, it would be best to decide if we would prefer such terms to simplify to -5 x.

Describe the bug Simplify currently leaves behind terms like -(5*x) when -5 x is an apparently simpler alternative, one that would be much more commonly used in ordinary writing of polynomials and related expressions. (I don't think I've ever seen anyone write -(5 * x) - 12 instead of -5 x - 12.)

To Reproduce math.simplify('(3-8)x').toString() --> produces '-(5 * x)', where '-5 x' seems more natural/more simplified.

Discussion Of course, the "best" simplified form of an expression is to some extent a matter of taste, but in this case, the form simplify chooses has an additional layer of parentheses not needed in the "usual" way of writing such terms, which explicitly seems less simple. So we need to make a judgment call whether we would like simplify to prefer the -5 x form.

If so, one can add the following rule near the end of the simplify rules: -(c*n) -> -c*n and the only effects on the current unit tests are:

  1. former -(2 * (y + x)) result becomes -2 * (y+x), and
  2. former -(2 * (y - 2 * x)) result becomes -2 * (y - 2 * x).

These both seem like wins to me, so I am inclined to put in the rule, but it would be best to have at least another vote/perspective before proceeding. Thanks for any thoughts.

gwhitney avatar Nov 06 '26 20:11 gwhitney

Vote for -5 * x, with one caveat about which shapes the rule actually covers.

I measured it on mathjs 15.2.0 (math.simplify.rules has 54 entries; I appended -(c*n) -> -c*n at the end and compared). Appending the rule gives exactly the result you want:

input without rule with rule
(3-8)x -(5 * x) -5 x
-(2 * (y + x)) -(2 * (y + x)) -2 * (y + x)
-(2 * (y - 2 * x)) -(2 * (y - 2 * x)) -2 * (y - 2 * x)

Your two expected test changes are the two I see for those shapes, and the rule is stable: running simplify again on each output changes nothing, and none of the 12 shapes I tried oscillates with the other 53 rules.

Two things worth knowing before it goes in:

1. The rule also fires outside the shapes in your example. It rewrites every product with a constant factor, including multi-factor products and each term of a sum:

-(2*x*y)        ->  -2 * x * y        (was -(2 * x * y))
-(2*x) + -(3*y) ->  -2 * x - 3 * y    (was -(2 * x) - 3 * y)

Those are wins too, but they are different test shapes from the two you listed, so if any test asserts -(2 * x * y) or a sum of such terms, it will move as well.

2. Rational coefficients are untouched, so the rule does not establish the invariant. -(1/2 * x) still simplifies to x * -1 / 2 with the rule in place, because 1/2 is a Div node rather than a ConstantNode, so c does not match it. If the intent is "a unary minus never wraps a product", the rule as written delivers that only for integer constants. That is fine as a first step, but x * -1 / 2 is arguably the worse of the two outputs and it is the one that stays.

On your parenthetical about implicit multiplication: the -5 x vs -5 * x difference in my table is real and is not the rule's doing. (3-8)x parses as an implicit * and the OperatorNode keeps its implicit flag through simplification, so it prints without the *; -(5*x) parses as explicit and prints *. Useful to keep in mind for #3719, since it means the same simplified tree prints two ways depending on how it was built.

belentani7 avatar Nov 07 '26 15:11 belentani7

Three comments:

A) For rationals times a node containing variables, my preference is 3x/8 and -3x/8; I think those are arguably standard. If we decide to go forward with a change based on this issue, I agree we should also make the tweak(s) necessary to obtain those as opposed to (say) x*3/8 or x*-3/8 or 3/8 x or (-3/8)*x. The problem with 3/8 x is that although the mathjs implicit multiplication rules are clear that it is (3/8) x, it's also the case that y/z x parses as y / (z*x) and y/z*x parses as (y/z)*x, making this form a bit confusing/visually uncomfortable. 3x/8 does not suffer from any of these difficulties I also think that -x/8 should be preferred to x*-1/8 and -1*x/8 and -1/8 x for similar reasons. .

B) I am planning to wait until #3701 resolves to decide/address this, since I hope it will produce more powerful tools for assessing changes to simplify.

C) @josdejong if the consensus seems to be to go for a change to simplify that would result in -2 * (y + x) in place of a -(2 * (y + x)) currently recorded as the result of a simplify example that is in a unit test, does that qualify as a breaking change, meaning this would get deferred to v16? If the criterion is "any non-error unit test changes result" then of course that's a yes, but if one considers the current recorded result of -(2 * (y + x)) to be "a bug" as compared to the changed result of -2 * (y + x), then maybe changing this result is a "bugfix" as opposed to a breaking change, and doesn't bump the major version. I don't know if there's a major version policy that addresses the results of semi-subjective operations like simplify.

gwhitney avatar Nov 07 '26 20:11 gwhitney