11
$\begingroup$

As I understand it, Mathematica often uses alphabetical order when simplifying expressions. For example, b-a simplifies to -a+b.

Similarly, Sin[a-b] simplifies to Sin[a-b], but Sin[b-a] simplifies to -Sin[a-b], which, in my opinion, introduces unnecessary complexity to the output. Is there some way to prevent this behavior?

Intuitively, it made much more sense to me if an uneven function like sine simplified like -f(a-b) -> f(b-a).

$\endgroup$
22
$\begingroup$

This is not simplification. It is canonicalization. Bringing expressions to canonical form is very useful because then two expressions can be compared for equality by simply checking that they have the same structure. Checking equality is a very common operation during symbolic manipulations.

a+b and b+a are structurally different, but mathematically equivalent. Sorting brings them to the same (canonical) form: a+b. Now they can be trivially compared.

Some limited canonicalization is done automatically by Mathematica. This is what you see here.

Canonicalization does not optimize expressions for readability by humans. It optimizes them for computation. For readability, use TraditionalForm. It will display -a+b as b-a (even though the underlying structure is still (-1*a)+b).

TraditionalForm is not smart enough to handle the example you show, i.e. -Sin[a-b]. I am not aware of any existing formatter that handles this the way you want. You can attempt writing one using replacement rules, but I think covering all similar cases is almost hopeless and not worth the effort ... there are too many possibilities.

You can prevent any evaluation/canonicalization using HoldForm[Sin[b-a]], but this just maintains the expression in its original form. It is not a means to convert a computation result back to the form you ask for.

$\endgroup$
  • $\begingroup$ Thank for elaborating. Are you aware of a way to tell Mathematica how to "canonicalize" a given set of variables. For example, if I have variables a, b and c, and I want to order them as "c+b+a" instead of "a+b+c". $\endgroup$ – Muk Jun 11 at 9:10
  • 1
    $\begingroup$ @Muk This cannot be configured. It is a fundamental part of the system. If this could be changed, everything would break. What is theoretically possible is to post-process a certain output to obtain a different form. $\endgroup$ – Szabolcs Jun 11 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.