Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The first three expressions evaluate as expected and the polynomial is displayed in what I would call "textbook" form. The last expression, however, switches the order of terms. Mathematica employs this change for two-term polynomials if it results in getting rid of the leading negative sign (at least that is the best I can deduce).

x^2 + x + 5 // TraditionalForm
(* x^2 + x + 5 *)

-x^2 + x + 5 // TraditionalForm
(* -x^2 + x + 5 *)

x^2 + x // TraditionalForm
(* x^2 + x *)

-x^2 + x // TraditionalForm
(* x - x^2 *)

These polynomials are the result of prior symbolic manipulation, so I cannot simply use HoldForm or the equivalent to maintain the desired order.

Is there a way to change this behavior in general so that the last expression displays as -x^2 + x? I can think of substitution rules to fix this particular example, but would like to find a robust solution that applies as transparently as possible across the board.

Edit

Additionally, PolynomialForm produces the same results:

PolynomialForm[-x^2 + x , TraditionalOrder -> True]
(* x - x^2 *)

PolynomialForm[-x^2 - x , TraditionalOrder -> True]
(* -x^2 - x *)

It seems that Mathematica will produce the traditional order for polynomial terms except when there are only two terms and reversing the order eliminates the leading negative sign.

share|improve this question
I was expecting to close this question with a reference to PolynomialForm[#, TraditionalOrder -> True] & but I see that you're going the other way. Let me think about that. – Mr.Wizard Mar 5 at 23:41
@Mr.Wizard: Yes, I already tried PolynomialForm and that produces the same results. I will add that information to the question because that will probably be a common thought pattern. – RandomBits Mar 5 at 23:45
Previous questions relating to this usually creates a new function to handle Plus. But would be nice with a way that lets you override the displayed order of Orderless arguments. – ssch Mar 6 at 0:21
@ssch I don't think those solve this one (which I assume is why you didn't post an answer.) RandomBits doesn't want to prevent ordering, he wants to control it. – Mr.Wizard Mar 6 at 0:46

1 Answer

I hope there is a better way but here is something to build upon:

TraditionalForm @ Row[MonomialList@#, "+"] & /@
  {x^2 + x + 5, -x^2 + x + 5, x^2 + x, -x^2 + x}

Mathematica graphics

share|improve this answer
2  
Similar Row[MonomialList[-x^2 + x, {x}, "DegreeLexicographic"], "+"] – ssch Mar 6 at 0:06
Is it the case that Plus@@MonomialList[expr] == expr for any expression? If so, then I can define a format: Format[xPlus[x___]] := Row[Riffle[{expr}, "+"]] and then xPlus := Plus and then use HoldForm[xPlus@@MonomialList[expr]] to the display ordering that I want while still having a valid expression when the hold is released (because the xPlus will be changed to Plus). – RandomBits Mar 6 at 3:47

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.