Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
3  
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. –  Kuba Mar 7 at 13:47
2  
If you'd like to see $b-a$, you might look into TraditionalForm. –  Mark McClure Mar 7 at 14:14
add comment

3 Answers 3

up vote 13 down vote accepted

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).

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

HornerForm[Simplify[a - b]]
share|improve this answer
    
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)]). –  bcp Mar 7 at 14:15
    
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]. –  Mark McClure Mar 7 at 14:16
    
@MarkMcClure - Hmm, you're right. Should have tested it a bit more and not relied on my (faulty) memory! –  Ymareth Mar 7 at 16:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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