12
$\begingroup$

Simplify[b - a] results in -a + b. I prefer b - a, which is a bit simpler (3 symbols instead of 4).

Can I make Mathematica to think the same way?

I believe one needs to redefine the ComplexityFunction.

$\endgroup$
2
  • 3
    $\begingroup$ It's because Mathemaica uses alphabetical order, try: Simplify[a - b]. Also try HoldForm[-a + b] // FullForm and HoldForm[b - a] // FullForm to see that it isn't very different. $\endgroup$
    – Kuba
    Mar 7, 2014 at 13:47
  • 2
    $\begingroup$ If you'd like to see $b-a$, you might look into TraditionalForm. $\endgroup$ Mar 7, 2014 at 14:14

3 Answers 3

17
$\begingroup$

It is not Simplify that changes b-a to -a+b. It happens automatically, and it cannot generally be prevented except by using Hold or HoldForm which will make it impossible to use the expression for calculations (until you remove the Hold wrapper again). But while you can't prevent changing b-a to -a+b internally, you can change how it will be displayed on screen, by using TraditionalForm.

Why does Mathematica not consider one form simpler than the other?

Let's look at the structure of these expressions:

In[1]:= Hold[b-a]//FullForm
Out[1]//FullForm= Hold[Plus[b,Times[-1,a]]]

In[2]:= Hold[-a+b]//FullForm
Out[2]//FullForm= Hold[Plus[Times[-1,a],b]]

The only difference is the ordering of the terms within Plus, but neither expression has fewer parts than the other. This is the consequence of the particular choice for their internal representation, which is shown above using FullForm.

Why does Mathematica reorder the terms of Plus?

Plus has the Orderless attribute. This attribute is used for functions that are commutative. The system will automatically bring any Orderless function to a canonical form by sorting its arguments the same way Sort would. See that documentation page for the sorting rules: symbols will generally be sorted alphabetically, so a comes before b.

It's not difficult to see why canonical forms are advantageous to use in computer algebra systems when it is at all possible to define and efficiently compute one. For example, it will make a comparison such as a+b==b+a trivial to carry out efficiently.

But I don't care what's simpler for a computer, $b-a$ is just more readable for humans!

You're right about that, that's why the function TraditionalForm will change the way expressions are displayed. It won't change their internal representation: it will still be Plus[Times[-1,a],b], i.e. something like (-1)*a + b. However it will change how they're displayed on screen and show $b-a$ for better readability.

I sometimes select the output cell and press Command-Shift-T to automatically convert the cell to TraditionalForm for better readability (e.g. it'll show matrices in 2D form and will order polynomials with higher order terms first).

$\endgroup$
1
  • 2
    $\begingroup$ What about Simplify[Tan[b - a]]? TraditionalForm doesn't help here... $\endgroup$
    – bcp
    Jul 25, 2014 at 8:21
3
$\begingroup$

You can also make StandardForm handle this case the way TraditionalForm does, and ensure all other expressions are formatted as StandardForm normally does them. You might like that if you don't have TraditionalForm notation for special functions memorized. Besides that this is an instructive example that you can read here.

$\endgroup$
0
$\begingroup$

I think HornerForm as a wrapper will do what you're after...

HornerForm[Simplify[a - b]]
$\endgroup$
3
  • $\begingroup$ You mean HornerForm[Simplify[b - a]]? I believe HornerForm can't help here. Besides, I am interested in simplyfying arbitrary expressions (not just polynomials) (Simplify[1/(b-a)]). $\endgroup$
    – bcp
    Mar 7, 2014 at 14:15
  • $\begingroup$ Hmm... I don't think that HornerForm is really a wrapper in the same sense that InputForm, StandardForm, and TraditionalForm are wrappers. In fact, HornerForm[Simplify[b - a]] returns the same thing as just Simplify[b - a]. $\endgroup$ Mar 7, 2014 at 14:16
  • $\begingroup$ @MarkMcClure - Hmm, you're right. Should have tested it a bit more and not relied on my (faulty) memory! $\endgroup$
    – Ymareth
    Mar 7, 2014 at 16:16

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.